Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Starter/StarterForm.cs @ 3831

Last change on this file since 3831 was 3758, checked in by swagner, 14 years ago

Implemented ErrorDialog and OperatorExecutionException (#1007)

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