Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.PluginInfrastructure/Starter/StarterForm.cs @ 12448

Last change on this file since 12448 was 3573, checked in by gkronber, 15 years ago

Implemented a list of review comments by swagner. #989 (Implement review comments in plugin infrastructure)

File size: 7.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.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      Text = "HeuristicLab " + this.GetType().Assembly.GetName().Version;
57
58      string pluginPath = Path.GetFullPath(Application.StartupPath);
59      pluginManager = new PluginManager(pluginPath);
60      splashScreen = new SplashScreen(pluginManager, 1000);
61      splashScreen.Show("Loading HeuristicLab...");
62
63      pluginManager.DiscoverAndCheckPlugins();
64
65      UpdateApplicationsList();
66    }
67
68    /// <summary>
69    /// Creates a new StarterForm and tries to start application with <paramref name="appName"/> immediately.
70    /// </summary>
71    /// <param name="appName">Name of the application</param>
72    public StarterForm(string appName)
73      : this() {
74      var appDesc = (from desc in pluginManager.Applications
75                     where desc.Name == appName
76                     select desc).SingleOrDefault();
77      if (appDesc != null) {
78        StartApplication(appDesc);
79      } else {
80        MessageBox.Show("Cannot start application " + appName + ".",
81                        "HeuristicLab",
82                        MessageBoxButtons.OK,
83                        MessageBoxIcon.Warning);
84      }
85    }
86
87    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
88      if (applicationsListView.SelectedItems.Count > 0) {
89        ListViewItem selected = applicationsListView.SelectedItems[0];
90        if (selected == pluginManagerListViewItem) {
91          if (pluginManager.Plugins.Any(x => x.PluginState == PluginState.Loaded)) {
92            MessageBox.Show("Installation Manager cannot be started while another HeuristicLab application is active." + Environment.NewLine +
93              "Please stop all active HeuristicLab applications and try again.", "Plugin Manager",
94              MessageBoxButtons.OK, MessageBoxIcon.Information);
95          } else {
96            try {
97              Cursor = Cursors.AppStarting;
98              InstallationManagerForm form = new InstallationManagerForm(pluginManager);
99              form.ShowDialog(this);
100              UpdateApplicationsList();
101            }
102            finally {
103              Cursor = Cursors.Arrow;
104            }
105          }
106        } else {
107          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
108          StartApplication(app);
109        }
110      }
111    }
112
113    private void UpdateApplicationsList() {
114      applicationsListView.Items.Clear();
115
116      pluginManagerListViewItem = new ListViewItem("Plugin Manager", 0);
117      pluginManagerListViewItem.Group = applicationsListView.Groups["Plugin Management"];
118      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, GetType().Assembly.GetName().Version.ToString()));
119      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "Install, upgrade or delete plugins"));
120      pluginManagerListViewItem.ToolTipText = "Install, upgrade or delete plugins";
121
122      applicationsListView.Items.Add(pluginManagerListViewItem);
123
124      foreach (ApplicationDescription info in pluginManager.Applications) {
125        ListViewItem item = new ListViewItem(info.Name, 0);
126        item.Tag = info;
127        item.Group = applicationsListView.Groups["Applications"];
128        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Version.ToString()));
129        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Description));
130        item.ToolTipText = info.Description;
131        applicationsListView.Items.Add(item);
132      }
133    }
134
135    private void StartApplication(ApplicationDescription app) {
136      splashScreen.Show("Loading " + app.Name);
137      Thread t = new Thread(delegate() {
138        bool stopped = false;
139        do {
140          try {
141            if (!abortRequested) {
142              pluginManager.Run(app);
143            }
144            stopped = true;
145          }
146          catch (Exception ex) {
147            stopped = false;
148            ThreadPool.QueueUserWorkItem(delegate(object exception) { ShowErrorMessageBox((Exception)exception); }, ex);
149            Thread.Sleep(5000); // sleep 5 seconds before autorestart
150          }
151        } while (!abortRequested && !stopped && app.AutoRestart);
152      });
153      t.SetApartmentState(ApartmentState.STA); // needed for the AdvancedOptimizationFrontent
154      t.Start();
155    }
156
157    private void applicationsListView_SelectedIndexChanged(object sender, EventArgs e) {
158      startButton.Enabled = applicationsListView.SelectedItems.Count > 0;
159    }
160
161    private void largeIconsButton_Click(object sender, EventArgs e) {
162      applicationsListView.View = View.LargeIcon;
163    }
164
165    private void detailsButton_Click(object sender, EventArgs e) {
166      applicationsListView.View = View.Details;
167    }
168
169    private void ShowErrorMessageBox(Exception ex) {
170      MessageBoxOptions options = RightToLeft == RightToLeft.Yes ? MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading : MessageBoxOptions.DefaultDesktopOnly;
171      MessageBox.Show(null,
172         BuildErrorMessage(ex),
173         "Error - " + ex.GetType().Name,
174         MessageBoxButtons.OK,
175         MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, options);
176    }
177    private static string BuildErrorMessage(Exception ex) {
178      string nl = Environment.NewLine;
179      StringBuilder sb = new StringBuilder();
180      sb.Append(ex.Message + nl + ex.StackTrace);
181
182      while (ex.InnerException != null) {
183        ex = ex.InnerException;
184        sb.Append(nl + "-----" + nl + ex.Message + nl + ex.StackTrace);
185      }
186      return sb.ToString();
187    }
188
189    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
190      abortRequested = true;
191    }
192
193  }
194}
Note: See TracBrowser for help on using the repository browser.