Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.PluginInfrastructure/3.3/Starter/SplashScreen.cs @ 16003

Last change on this file since 16003 was 14927, checked in by gkronber, 8 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

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