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

Last change on this file since 9456 was 9456, checked in by swagner, 6 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
23using System.Diagnostics;
24using System.Linq;
25using System.Reflection;
26using System.Windows.Forms;
27using HeuristicLab.PluginInfrastructure.Manager;
28
29namespace HeuristicLab.PluginInfrastructure.Starter {
30  internal partial class SplashScreen : Form {
31    private const int FADE_INTERVAL = 50;
32    private Timer fadeTimer;
33    private int initialInterval;
34    private PluginManager pluginManager;
35
36    internal SplashScreen() {
37      InitializeComponent();
38    }
39
40    internal SplashScreen(PluginManager manager, int initialInterval)
41      : this() {
42      this.initialInterval = initialInterval;
43      this.pluginManager = manager;
44
45      RegisterPluginManagerEventHandlers();
46
47      FileVersionInfo pluginInfrastructureVersion = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location);
48      versionLabel.Text = "Version " + pluginInfrastructureVersion.FileVersion;
49      infoLabel.Text = "";
50
51      var attr = (AssemblyCopyrightAttribute)this.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false).Single();
52      copyrightLabel.Text = "Copyright " + attr.Copyright;
53
54      fadeTimer = new Timer();
55      fadeTimer.Tick += fadeTimer_Elapsed;
56      fadeTimer.Interval = initialInterval;
57    }
58
59    #region events
60    private void RegisterPluginManagerEventHandlers() {
61      pluginManager.ApplicationStarted += new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarted);
62      pluginManager.ApplicationStarting += new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarting);
63      pluginManager.Initializing += new EventHandler<PluginInfrastructureEventArgs>(manager_Initializing);
64      pluginManager.Initialized += new EventHandler<PluginInfrastructureEventArgs>(manager_Initialized);
65      pluginManager.PluginLoaded += new EventHandler<PluginInfrastructureEventArgs>(manager_PluginLoaded);
66      pluginManager.PluginUnloaded += new EventHandler<PluginInfrastructureEventArgs>(manager_PluginUnloaded);
67    }
68
69    private void DeregisterPluginManagerEventHandlers() {
70      pluginManager.ApplicationStarted -= new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarted);
71      pluginManager.ApplicationStarting -= new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarting);
72      pluginManager.Initializing -= new EventHandler<PluginInfrastructureEventArgs>(manager_Initializing);
73      pluginManager.Initialized -= new EventHandler<PluginInfrastructureEventArgs>(manager_Initialized);
74      pluginManager.PluginLoaded -= new EventHandler<PluginInfrastructureEventArgs>(manager_PluginLoaded);
75      pluginManager.PluginUnloaded -= new EventHandler<PluginInfrastructureEventArgs>(manager_PluginUnloaded);
76    }
77
78    private void manager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
79      SafeUpdateMessage("Unloaded " + e.Entity);
80    }
81
82    private void manager_PluginLoaded(object sender, PluginInfrastructureEventArgs e) {
83      SafeUpdateMessage("Loaded " + e.Entity);
84    }
85
86    private void manager_Initialized(object sender, PluginInfrastructureEventArgs e) {
87      SafeUpdateMessage("Initialized");
88    }
89
90    private void manager_Initializing(object sender, PluginInfrastructureEventArgs e) {
91      SafeUpdateMessage("Initializing");
92    }
93
94    private void manager_ApplicationStarting(object sender, PluginInfrastructureEventArgs e) {
95      SafeUpdateMessage("Starting " + e.Entity);
96    }
97
98    private void manager_ApplicationStarted(object sender, PluginInfrastructureEventArgs e) {
99      SafeUpdateMessage("Started " + e.Entity);
100    }
101    // called from event handlers
102    private void SafeUpdateMessage(string msg) {
103      try {
104        Invoke((Action<string>)UpdateMessage, msg);
105      }
106      catch (ObjectDisposedException) { }
107    }
108
109    // each tick of the timer reduce opacity and restart timer
110    private void fadeTimer_Elapsed(object sender, EventArgs e) {
111      // only called from local timer: no need to invoke here
112      FadeOut();
113    }
114    #endregion
115
116    public void Show(string initialText) {
117      if (InvokeRequired) Invoke((Action<string>)Show, initialText);
118      else {
119        Opacity = 1;
120        infoLabel.Text = initialText;
121        ResetFadeTimer();
122        Show();
123      }
124    }
125
126    public void Show(IWin32Window owner, string initialText) {
127      if (InvokeRequired) Invoke((Action<IWin32Window, string>)Show, owner, initialText);
128      else {
129        Opacity = 1;
130        infoLabel.Text = initialText;
131        ResetFadeTimer();
132        Show(owner);
133      }
134    }
135
136    private void ResetFadeTimer() {
137      // wait initialInterval again for the first tick
138      fadeTimer.Stop();
139      fadeTimer.Interval = initialInterval;
140      fadeTimer.Start();
141    }
142
143    private void UpdateMessage(string msg) {
144      ResetFadeTimer();
145      infoLabel.Text = msg;
146      Application.DoEvents(); // force immediate update of splash screen control
147    }
148
149    // reduces opacity of the splashscreen one step and restarts the fade-timer
150    private void FadeOut() {
151      fadeTimer.Stop();
152      fadeTimer.Interval = FADE_INTERVAL;
153      if (this.Opacity > 0) {
154        Opacity -= 0.1;
155        fadeTimer.Start();
156      } else {
157        Opacity = 0;
158        fadeTimer.Stop();
159        Hide();
160      }
161    }
162
163    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
164      // deregister events when form is closing
165      DeregisterPluginManagerEventHandlers();
166      base.OnClosing(e);
167    }
168  }
169}
Note: See TracBrowser for help on using the repository browser.