Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3620 was 3600, checked in by gkronber, 15 years ago

Implemented reviewer comments in plugin infrastructure. #989 (Implement review comments in plugin infrastructure)

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