Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab/SplashScreen.cs @ 2439

Last change on this file since 2439 was 2439, checked in by abeham, 15 years ago

wrapping close method to possibly solve #183

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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 HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab {
31  public partial class SplashScreen : Form {
32    private const int FADE_INTERVAL = 50;
33    private System.Timers.Timer fadeTimer;
34    private int initialInterval;
35    private object bigLock = new object();
36    private bool closing = false;
37
38    public SplashScreen() {
39      InitializeComponent();
40
41      Assembly assembly = this.GetType().Assembly;
42      object[] attributes = assembly.GetCustomAttributes(false);
43      string user, company;
44
45      titleLabel.Text = Application.ProductName;
46      versionLabel.Text = "Version " + Application.ProductVersion;
47      infoLabel.Text = "";
48
49      foreach(object obj in attributes) {
50        if(obj is AssemblyCopyrightAttribute) {
51          copyrightLabel.Text = "Copyright " + ((AssemblyCopyrightAttribute)obj).Copyright;
52        }
53      }
54
55      try {
56        user = HeuristicLab.Properties.Settings.Default.User;
57        company = HeuristicLab.Properties.Settings.Default.Organization;
58
59        if((user == null) || (user.Equals(""))) {
60          userNameLabel.Text = "-";
61        } else {
62          userNameLabel.Text = user;
63        }
64
65        if((company == null) || (company.Equals(""))) {
66          companyLabel.Text = "-";
67        } else {
68          companyLabel.Text = company;
69        }
70      } catch(Exception) {
71        userNameLabel.Text = "-";
72        companyLabel.Text = "-";
73      }
74    }
75
76    public SplashScreen(int initialInterval, string initialText)
77      : this() {
78      this.initialInterval = initialInterval;
79      infoLabel.Text = initialText;
80    }
81
82    private void SetInfoText(string text) {
83      this.Invoke((MethodInvoker)delegate() { infoLabel.Text = text; });
84    }
85
86    public void Manager_Action(object sender, PluginManagerActionEventArgs e) {
87      string info;
88      if(e.Action == PluginManagerAction.Initializing) info = "Initializing ...";
89      else if(e.Action == PluginManagerAction.InitializingPlugin) info = "Initializing Plugin " + e.Id + " ...";
90      else if(e.Action == PluginManagerAction.InitializedPlugin) info = "Initializing Plugin " + e.Id + " ... Initialized";
91      else if(e.Action == PluginManagerAction.Initialized) {
92        info = "Initialization Completed";
93        fadeTimer = new System.Timers.Timer();
94        fadeTimer.SynchronizingObject = this;
95        fadeTimer.Elapsed += new System.Timers.ElapsedEventHandler(fadeTimer_Elapsed);
96        fadeTimer.Interval = initialInterval;
97        fadeTimer.AutoReset = true;
98        fadeTimer.Start();
99      } else {
100        if(e.Id != null) info = e.Action.ToString() + "   (" + e.Id + ")";
101        else info = e.Action.ToString();
102      }
103      SetInfoText(info);
104    }
105
106    private void fadeTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
107      fadeTimer.Stop();
108      if(InvokeRequired) {
109        Invoke((MethodInvoker)UpdateOpacity);
110      } else {
111        UpdateOpacity();
112      }
113    }
114
115    private void UpdateOpacity() {
116      lock(bigLock) {
117        if(closing) return;
118        if(Opacity > 0.9) {
119          Opacity = 0.9;
120          fadeTimer.Interval = FADE_INTERVAL;
121          fadeTimer.Start();
122        } else if(this.Opacity > 0) {
123          Opacity -= 0.1;
124          fadeTimer.Start();
125        } else {
126          Opacity = 0;
127          CloseSplashScreen();
128        }
129      }
130    }
131
132    private void closeButton_Click(object sender, EventArgs e) {
133      lock(bigLock) {
134        if(fadeTimer != null) fadeTimer.Stop();
135        CloseSplashScreen();
136      }
137    }
138
139    private void CloseSplashScreen() {
140      if (!closing) { // just close once
141        closing = true;
142        PluginManager.Manager.Action -= new PluginManagerActionEventHandler(this.Manager_Action); // remove event before calling close
143        Application.DoEvents(); // work up all existing events
144        Close(); // close
145      }
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.