Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9015 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 6.3 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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.Diagnostics;
24using System.Linq;
25using System.Reflection;
[2]26using System.Windows.Forms;
[2481]27using HeuristicLab.PluginInfrastructure.Manager;
[2]28
[2527]29namespace HeuristicLab.PluginInfrastructure.Starter {
[2504]30  internal partial class SplashScreen : Form {
[595]31    private const int FADE_INTERVAL = 50;
[2505]32    private Timer fadeTimer;
[596]33    private int initialInterval;
[4515]34    private PluginManager pluginManager;
[3777]35
[2504]36    internal SplashScreen() {
[2]37      InitializeComponent();
[2481]38    }
[2]39
[3113]40    internal SplashScreen(PluginManager manager, int initialInterval)
[2481]41      : this() {
42      this.initialInterval = initialInterval;
[4515]43      this.pluginManager = manager;
[2505]44
[4515]45      RegisterPluginManagerEventHandlers();
[2]46
[3777]47      FileVersionInfo pluginInfrastructureVersion = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location);
48      versionLabel.Text = "Version " + pluginInfrastructureVersion.FileVersion;
[2]49      infoLabel.Text = "";
50
[2505]51      var attr = (AssemblyCopyrightAttribute)this.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false).Single();
52      copyrightLabel.Text = "Copyright " + attr.Copyright;
[2]53
[2505]54      fadeTimer = new Timer();
55      fadeTimer.Tick += fadeTimer_Elapsed;
56      fadeTimer.Interval = initialInterval;
[2]57    }
58
[4515]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);
[2922]67    }
68
[4515]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);
[2922]76    }
77
[4515]78    private void manager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
79      SafeUpdateMessage("Unloaded " + e.Entity);
[2922]80    }
81
[4515]82    private void manager_PluginLoaded(object sender, PluginInfrastructureEventArgs e) {
83      SafeUpdateMessage("Loaded " + e.Entity);
[2922]84    }
85
[4515]86    private void manager_Initialized(object sender, PluginInfrastructureEventArgs e) {
87      SafeUpdateMessage("Initialized");
[2922]88    }
89
[4515]90    private void manager_Initializing(object sender, PluginInfrastructureEventArgs e) {
91      SafeUpdateMessage("Initializing");
[2922]92    }
93
[4515]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
[3113]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
[3697]126    public void Show(IWin32Window owner, string initialText) {
[4515]127      if (InvokeRequired) Invoke((Action<IWin32Window, string>)Show, owner, initialText);
[3697]128      else {
129        Opacity = 1;
130        infoLabel.Text = initialText;
131        ResetFadeTimer();
132        Show(owner);
133      }
134    }
135
[3113]136    private void ResetFadeTimer() {
137      // wait initialInterval again for the first tick
138      fadeTimer.Stop();
139      fadeTimer.Interval = initialInterval;
140      fadeTimer.Start();
141    }
142
[2922]143    private void UpdateMessage(string msg) {
[4515]144      ResetFadeTimer();
145      infoLabel.Text = msg;
146      Application.DoEvents(); // force immediate update of splash screen control
[2]147    }
148
[3113]149    // reduces opacity of the splashscreen one step and restarts the fade-timer
150    private void FadeOut() {
[2505]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();
[3113]159        Hide();
[2439]160      }
161    }
[4515]162
163    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
164      // deregister events when form is closing
165      DeregisterPluginManagerEventHandlers();
166      base.OnClosing(e);
167    }
[2]168  }
169}
Note: See TracBrowser for help on using the repository browser.