Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.PluginInfrastructure/3.3/Starter/StarterForm.cs @ 4995

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

Added work-around for race-condition in SplashScreen:

  • deregister events correctly
  • catch ObjectDisposedException in UpdateMessage in splash-screen

#795

File size: 8.0 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.Diagnostics;
25using System.IO;
26using System.Linq;
27using System.Threading;
28using System.Windows.Forms;
29using HeuristicLab.PluginInfrastructure.Advanced;
30using HeuristicLab.PluginInfrastructure.Manager;
31
32namespace HeuristicLab.PluginInfrastructure.Starter {
33  /// <summary>
34  /// The starter form is responsible for initializing the plugin infrastructure
35  /// and shows a list of installed applications.
36  /// </summary>
37  public partial class StarterForm : Form {
38
39    private ListViewItem pluginManagerListViewItem;
40    private bool abortRequested;
41    private PluginManager pluginManager;
42    private SplashScreen splashScreen;
43
44    /// <summary>
45    /// Initializes an instance of the starter form.
46    /// The starter form shows a splashscreen and initializes the plugin infrastructure.
47    /// </summary>
48    public StarterForm()
49      : base() {
50      InitializeComponent();
51      largeImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.HeuristicLab.ToBitmap());
52      smallImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.HeuristicLab.ToBitmap());
53      FileVersionInfo pluginInfrastructureVersion = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location);
54      Text = "HeuristicLab " + pluginInfrastructureVersion.FileVersion;
55
56      string pluginPath = Path.GetFullPath(Application.StartupPath);
57      pluginManager = new PluginManager(pluginPath);
58      splashScreen = new SplashScreen(pluginManager, 1000);
59      splashScreen.Show(this, "Loading HeuristicLab...");
60
61      pluginManager.DiscoverAndCheckPlugins();
62      UpdateApplicationsList();
63    }
64
65    /// <summary>
66    /// Creates a new StarterForm and tries to start application with <paramref name="appName"/> immediately.
67    /// </summary>
68    /// <param name="appName">Name of the application</param>
69    public StarterForm(string appName)
70      : this() {
71      var appDesc = (from desc in pluginManager.Applications
72                     where desc.Name == appName
73                     select desc).SingleOrDefault();
74      if (appDesc != null) {
75        StartApplication(appDesc);
76      } else {
77        MessageBox.Show("Cannot start application " + appName + ".",
78                        "HeuristicLab",
79                        MessageBoxButtons.OK,
80                        MessageBoxIcon.Warning);
81      }
82    }
83
84    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
85      if (applicationsListView.SelectedItems.Count > 0) {
86        ListViewItem selected = applicationsListView.SelectedItems[0];
87        if (selected == pluginManagerListViewItem) {
88          if (pluginManager.Plugins.Any(x => x.PluginState == PluginState.Loaded)) {
89            MessageBox.Show("Installation Manager cannot be started while another HeuristicLab application is active." + Environment.NewLine +
90              "Please stop all active HeuristicLab applications and try again.", "Plugin Manager",
91              MessageBoxButtons.OK, MessageBoxIcon.Information);
92          } else {
93            try {
94              Cursor = Cursors.AppStarting;
95              using (InstallationManagerForm form = new InstallationManagerForm(pluginManager)) {
96                form.ShowDialog(this);
97              }
98              UpdateApplicationsList();
99            }
100            finally {
101              Cursor = Cursors.Arrow;
102            }
103          }
104        } else {
105          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
106          StartApplication(app);
107        }
108      }
109    }
110
111    private void UpdateApplicationsList() {
112      applicationsListView.Items.Clear();
113      FileVersionInfo pluginInfrastructureVersion = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location);
114      pluginManagerListViewItem = new ListViewItem("Plugin Manager", 0);
115      pluginManagerListViewItem.Group = applicationsListView.Groups["Plugin Management"];
116      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, pluginInfrastructureVersion.FileVersion));
117      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "Install, upgrade or delete plugins"));
118      pluginManagerListViewItem.ToolTipText = "Install, upgrade or delete plugins";
119
120      applicationsListView.Items.Add(pluginManagerListViewItem);
121
122      foreach (ApplicationDescription info in pluginManager.Applications) {
123        ListViewItem item = new ListViewItem(info.Name, 0);
124        item.Tag = info;
125        item.Group = applicationsListView.Groups["Applications"];
126        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Version.ToString()));
127        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Description));
128        item.ToolTipText = info.Description;
129        applicationsListView.Items.Add(item);
130      }
131      foreach (ColumnHeader column in applicationsListView.Columns) {
132        if (applicationsListView.Items.Count > 0)
133          column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
134        else column.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
135      }
136    }
137
138    private void StartApplication(ApplicationDescription app) {
139      splashScreen.Show("Loading " + app.Name);
140      Thread t = new Thread(delegate() {
141        bool stopped = false;
142        do {
143          try {
144            if (!abortRequested) {
145              pluginManager.Run(app);
146            }
147            stopped = true;
148          }
149          catch (Exception ex) {
150            stopped = false;
151            ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
152            Thread.Sleep(5000); // sleep 5 seconds before autorestart
153          }
154        } while (!abortRequested && !stopped && app.AutoRestart);
155      });
156      t.SetApartmentState(ApartmentState.STA); // needed for the AdvancedOptimizationFrontent
157      t.Start();
158    }
159
160    private void applicationsListView_SelectedIndexChanged(object sender, EventArgs e) {
161      startButton.Enabled = applicationsListView.SelectedItems.Count > 0;
162    }
163
164    private void largeIconsButton_Click(object sender, EventArgs e) {
165      applicationsListView.View = View.LargeIcon;
166    }
167
168    private void detailsButton_Click(object sender, EventArgs e) {
169      applicationsListView.View = View.Details;
170      foreach (ColumnHeader column in applicationsListView.Columns) {
171        if (applicationsListView.Items.Count > 0)
172          column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
173        else column.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
174      }
175    }
176
177    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
178      splashScreen.Close();
179      abortRequested = true;
180    }
181
182    private void aboutButton_Click(object sender, EventArgs e) {
183      List<IPluginDescription> plugins = new List<IPluginDescription>(pluginManager.Plugins.OfType<IPluginDescription>());
184      using (var dialog = new AboutDialog(plugins)) {
185        dialog.ShowDialog();
186      }
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.