Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Starter/SplashScreen.cs @ 12105

Last change on this file since 12105 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 6.2 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[4068]23using System.Linq;
24using System.Reflection;
[2]25using System.Windows.Forms;
[2481]26using HeuristicLab.PluginInfrastructure.Manager;
[2]27
[2527]28namespace HeuristicLab.PluginInfrastructure.Starter {
[2504]29  internal partial class SplashScreen : Form {
[595]30    private const int FADE_INTERVAL = 50;
[2505]31    private Timer fadeTimer;
[596]32    private int initialInterval;
[4515]33    private PluginManager pluginManager;
[3777]34
[2504]35    internal SplashScreen() {
[2]36      InitializeComponent();
[2481]37    }
[2]38
[3113]39    internal SplashScreen(PluginManager manager, int initialInterval)
[2481]40      : this() {
41      this.initialInterval = initialInterval;
[4515]42      this.pluginManager = manager;
[2505]43
[4515]44      RegisterPluginManagerEventHandlers();
[2]45
[11113]46      versionLabel.Text = "Version " + AssemblyHelpers.GetFileVersion(GetType().Assembly);
[2]47      infoLabel.Text = "";
48
[2505]49      var attr = (AssemblyCopyrightAttribute)this.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false).Single();
50      copyrightLabel.Text = "Copyright " + attr.Copyright;
[2]51
[2505]52      fadeTimer = new Timer();
53      fadeTimer.Tick += fadeTimer_Elapsed;
54      fadeTimer.Interval = initialInterval;
[2]55    }
56
[4515]57    #region events
58    private void RegisterPluginManagerEventHandlers() {
59      pluginManager.ApplicationStarted += new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarted);
60      pluginManager.ApplicationStarting += new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarting);
61      pluginManager.Initializing += new EventHandler<PluginInfrastructureEventArgs>(manager_Initializing);
62      pluginManager.Initialized += new EventHandler<PluginInfrastructureEventArgs>(manager_Initialized);
63      pluginManager.PluginLoaded += new EventHandler<PluginInfrastructureEventArgs>(manager_PluginLoaded);
64      pluginManager.PluginUnloaded += new EventHandler<PluginInfrastructureEventArgs>(manager_PluginUnloaded);
[2922]65    }
66
[4515]67    private void DeregisterPluginManagerEventHandlers() {
68      pluginManager.ApplicationStarted -= new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarted);
69      pluginManager.ApplicationStarting -= new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarting);
70      pluginManager.Initializing -= new EventHandler<PluginInfrastructureEventArgs>(manager_Initializing);
71      pluginManager.Initialized -= new EventHandler<PluginInfrastructureEventArgs>(manager_Initialized);
72      pluginManager.PluginLoaded -= new EventHandler<PluginInfrastructureEventArgs>(manager_PluginLoaded);
73      pluginManager.PluginUnloaded -= new EventHandler<PluginInfrastructureEventArgs>(manager_PluginUnloaded);
[2922]74    }
75
[4515]76    private void manager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
77      SafeUpdateMessage("Unloaded " + e.Entity);
[2922]78    }
79
[4515]80    private void manager_PluginLoaded(object sender, PluginInfrastructureEventArgs e) {
81      SafeUpdateMessage("Loaded " + e.Entity);
[2922]82    }
83
[4515]84    private void manager_Initialized(object sender, PluginInfrastructureEventArgs e) {
85      SafeUpdateMessage("Initialized");
[2922]86    }
87
[4515]88    private void manager_Initializing(object sender, PluginInfrastructureEventArgs e) {
89      SafeUpdateMessage("Initializing");
[2922]90    }
91
[4515]92    private void manager_ApplicationStarting(object sender, PluginInfrastructureEventArgs e) {
93      SafeUpdateMessage("Starting " + e.Entity);
94    }
95
96    private void manager_ApplicationStarted(object sender, PluginInfrastructureEventArgs e) {
97      SafeUpdateMessage("Started " + e.Entity);
98    }
99    // called from event handlers
100    private void SafeUpdateMessage(string msg) {
101      try {
102        Invoke((Action<string>)UpdateMessage, msg);
103      }
104      catch (ObjectDisposedException) { }
105    }
106
107    // each tick of the timer reduce opacity and restart timer
108    private void fadeTimer_Elapsed(object sender, EventArgs e) {
109      // only called from local timer: no need to invoke here
110      FadeOut();
111    }
112    #endregion
113
[3113]114    public void Show(string initialText) {
115      if (InvokeRequired) Invoke((Action<string>)Show, initialText);
116      else {
117        Opacity = 1;
118        infoLabel.Text = initialText;
119        ResetFadeTimer();
120        Show();
121      }
122    }
123
[3697]124    public void Show(IWin32Window owner, string initialText) {
[4515]125      if (InvokeRequired) Invoke((Action<IWin32Window, string>)Show, owner, initialText);
[3697]126      else {
127        Opacity = 1;
128        infoLabel.Text = initialText;
129        ResetFadeTimer();
130        Show(owner);
131      }
132    }
133
[3113]134    private void ResetFadeTimer() {
135      // wait initialInterval again for the first tick
136      fadeTimer.Stop();
137      fadeTimer.Interval = initialInterval;
138      fadeTimer.Start();
139    }
140
[2922]141    private void UpdateMessage(string msg) {
[4515]142      ResetFadeTimer();
143      infoLabel.Text = msg;
144      Application.DoEvents(); // force immediate update of splash screen control
[2]145    }
146
[3113]147    // reduces opacity of the splashscreen one step and restarts the fade-timer
148    private void FadeOut() {
[2505]149      fadeTimer.Stop();
150      fadeTimer.Interval = FADE_INTERVAL;
151      if (this.Opacity > 0) {
152        Opacity -= 0.1;
153        fadeTimer.Start();
154      } else {
155        Opacity = 0;
156        fadeTimer.Stop();
[3113]157        Hide();
[2439]158      }
159    }
[4515]160
161    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
162      // deregister events when form is closing
163      DeregisterPluginManagerEventHandlers();
164      base.OnClosing(e);
165    }
[2]166  }
167}
Note: See TracBrowser for help on using the repository browser.