Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PluginInfrastructure Refactoring/HeuristicLab/SplashScreen.cs @ 2481

Last change on this file since 2481 was 2481, checked in by gkronber, 14 years ago

Refactored class Loader in plugin infrastructure. #799

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