Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.PluginInfrastructure/Starter/SplashScreen.cs @ 4483

Last change on this file since 4483 was 3113, checked in by gkronber, 15 years ago

Changed starter form to instantiate only one splash screen and show and hide it as required. #183 (Splash screen sometimes causes ObjectDisposedException)

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
24using System.Data;
25using System.Drawing;
26using System.Windows.Forms;
27using System.Reflection;
28using System.Linq;
29using HeuristicLab.PluginInfrastructure;
30using HeuristicLab.PluginInfrastructure.Manager;
31
32namespace HeuristicLab.PluginInfrastructure.Starter {
33  internal partial class SplashScreen : Form {
34    private const int FADE_INTERVAL = 50;
35    private Timer fadeTimer;
36    private int initialInterval;
37    private PluginManager manager;
38    private bool fadeOutForced;
39    internal SplashScreen() {
40      InitializeComponent();
41    }
42
43    internal SplashScreen(PluginManager manager, int initialInterval)
44      : this() {
45      this.initialInterval = initialInterval;
46      this.manager = manager;
47
48      manager.ApplicationStarted += new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarted);
49      manager.ApplicationStarting += new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarting);
50      manager.Initializing += new EventHandler<PluginInfrastructureEventArgs>(manager_Initializing);
51      manager.Initialized += new EventHandler<PluginInfrastructureEventArgs>(manager_Initialized);
52      manager.PluginLoaded += new EventHandler<PluginInfrastructureEventArgs>(manager_PluginLoaded);
53      manager.PluginUnloaded += new EventHandler<PluginInfrastructureEventArgs>(manager_PluginUnloaded);
54
55      titleLabel.Text = Application.ProductName;
56      versionLabel.Text = "Version " + Application.ProductVersion;
57      infoLabel.Text = "";
58
59      var attr = (AssemblyCopyrightAttribute)this.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false).Single();
60      copyrightLabel.Text = "Copyright " + attr.Copyright;
61
62      string user = HeuristicLab.PluginInfrastructure.Properties.Settings.Default.User;
63      string company = HeuristicLab.PluginInfrastructure.Properties.Settings.Default.Organization;
64
65      userNameLabel.Text = string.IsNullOrEmpty(user) ? "-" : user;
66      companyLabel.Text = string.IsNullOrEmpty(company) ? "-" : company;
67
68      fadeTimer = new Timer();
69      fadeTimer.Tick += fadeTimer_Elapsed;
70      fadeTimer.Interval = initialInterval;
71    }
72
73    void manager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
74      UpdateMessage("Unloaded " + e.Entity);
75    }
76
77    void manager_PluginLoaded(object sender, PluginInfrastructureEventArgs e) {
78      UpdateMessage("Loaded " + e.Entity);
79    }
80
81    void manager_Initialized(object sender, PluginInfrastructureEventArgs e) {
82      UpdateMessage("Initialized");
83    }
84
85    void manager_Initializing(object sender, PluginInfrastructureEventArgs e) {
86      UpdateMessage("Initializing");
87    }
88
89    void manager_ApplicationStarting(object sender, PluginInfrastructureEventArgs e) {
90      UpdateMessage("Starting " + e.Entity);
91    }
92
93    void manager_ApplicationStarted(object sender, PluginInfrastructureEventArgs e) {
94      UpdateMessage("Started " + e.Entity);
95    }
96
97    public void Show(string initialText) {
98      if (InvokeRequired) Invoke((Action<string>)Show, initialText);
99      else {
100        Opacity = 1;
101        infoLabel.Text = initialText;
102        fadeOutForced = false;
103        ResetFadeTimer();
104        Show();
105      }
106    }
107
108    private void ResetFadeTimer() {
109      // wait initialInterval again for the first tick
110      fadeTimer.Stop();
111      fadeTimer.Interval = initialInterval;
112      fadeTimer.Start();
113    }
114
115
116    private void SetInfoText(string text) {
117      if (InvokeRequired) Invoke((Action<string>)SetInfoText, text);
118      else {
119        infoLabel.Text = text;
120      }
121    }
122
123    private void UpdateMessage(string msg) {
124      if (InvokeRequired) {
125        Invoke((Action<string>)UpdateMessage, msg);
126      } else {
127        // when the user forced a fade-out (by closing the splashscreen)
128        // don't reset the fadeTimer
129        if (!fadeOutForced) {
130          ResetFadeTimer();
131        }
132        SetInfoText(msg);
133        Application.DoEvents(); // force immediate update of splash screen control
134      }
135    }
136
137    // each tick of the timer reduce opacity and restart timer
138    private void fadeTimer_Elapsed(object sender, EventArgs e) {
139      FadeOut();
140    }
141
142    // reduces opacity of the splashscreen one step and restarts the fade-timer
143    private void FadeOut() {
144      fadeTimer.Stop();
145      fadeTimer.Interval = FADE_INTERVAL;
146      if (this.Opacity > 0) {
147        Opacity -= 0.1;
148        fadeTimer.Start();
149      } else {
150        Opacity = 0;
151        fadeTimer.Stop();
152        Hide();
153      }
154    }
155
156    // force fade out
157    private void closeButton_Click(object sender, EventArgs e) {
158      fadeOutForced = true;
159      FadeOut();
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.