Free cookie consent management tool by TermsFeed Policy Generator

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

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

Incorporated review comments by swagner into plugin infrastructure. #989 (Implement review comments in plugin infrastructure)

File size: 7.9 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[2507]23using System.Linq;
[2]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;
[2481]33using HeuristicLab.PluginInfrastructure.Manager;
[2495]34using System.IO;
[2748]35using HeuristicLab.PluginInfrastructure.Advanced;
[2]36
[2527]37namespace HeuristicLab.PluginInfrastructure.Starter {
[3092]38  /// <summary>
39  /// The starter form is responsible for initializing the plugin infrastructure
40  /// and shows a list of installed applications.
41  /// </summary>
[2507]42  public partial class StarterForm : Form {
[2]43
44    private ListViewItem pluginManagerListViewItem;
[1392]45    private bool abortRequested;
[2488]46    private PluginManager pluginManager;
[3113]47    private SplashScreen splashScreen;
[2]48
[3092]49    /// <summary>
50    /// Initializes an instance of the starter form.
51    /// The starter form shows a splashscreen and initializes the plugin infrastructure.
52    /// </summary>
[2507]53    public StarterForm()
[2481]54      : base() {
55      InitializeComponent();
56
[2748]57      string pluginPath = Path.GetFullPath(Application.StartupPath);
[2495]58      pluginManager = new PluginManager(pluginPath);
[3113]59      splashScreen = new SplashScreen(pluginManager, 1000);
60      splashScreen.Show("Loading HeuristicLab...");
[2]61
[2497]62      pluginManager.DiscoverAndCheckPlugins();
[2]63
[3474]64      UpdateApplicationsList();
[2]65    }
66
[3092]67    /// <summary>
68    /// Creates a new StarterForm and tries to start application with <paramref name="appName"/> immediately.
69    /// </summary>
70    /// <param name="appName">Name of the application</param>
[2507]71    public StarterForm(string appName)
72      : this() {
73      var appDesc = (from desc in pluginManager.Applications
74                     where desc.Name == appName
[2748]75                     select desc).SingleOrDefault();
[2507]76      if (appDesc != null) {
77        StartApplication(appDesc);
78      } else {
79        MessageBox.Show("Cannot start application " + appName + ".",
80                        "HeuristicLab",
81                        MessageBoxButtons.OK,
82                        MessageBoxIcon.Warning);
83      }
84    }
85
[2]86    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
[1394]87      if (applicationsListView.SelectedItems.Count > 0) {
[2]88        ListViewItem selected = applicationsListView.SelectedItems[0];
[1394]89        if (selected == pluginManagerListViewItem) {
[2922]90          if (pluginManager.Plugins.Any(x => x.PluginState == PluginState.Loaded)) {
91            MessageBox.Show("Installation Manager cannot be started while another HeuristicLab application is active." + Environment.NewLine +
92              "Please stop all HeuristicLab applications and try again.");
93          } else {
94            try {
95              Cursor = Cursors.AppStarting;
[3474]96              InstallationManagerForm form = new InstallationManagerForm(pluginManager);
[2922]97              this.Visible = false;
98              form.ShowDialog(this);
[3474]99              UpdateApplicationsList();
[2922]100              this.Visible = true;
101            }
102            finally {
103              Cursor = Cursors.Arrow;
104            }
[1394]105          }
[2]106        } else {
[2481]107          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
[2507]108          StartApplication(app);
[2]109        }
110      }
111    }
112
[3474]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, "-"));
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
[2507]135    private void StartApplication(ApplicationDescription app) {
[3113]136      splashScreen.Show("Loading " + app.Name);
[2507]137      Thread t = new Thread(delegate() {
138        bool stopped = false;
139        do {
140          try {
[2922]141            if (!abortRequested) {
142              SetCursor(Cursors.AppStarting);
[2507]143              pluginManager.Run(app);
[2922]144            }
[2507]145            stopped = true;
146          }
147          catch (Exception ex) {
148            stopped = false;
149            ThreadPool.QueueUserWorkItem(delegate(object exception) { ShowErrorMessageBox((Exception)exception); }, ex);
150            Thread.Sleep(5000); // sleep 5 seconds before autorestart
151          }
[2922]152          finally {
153            SetCursor(Cursors.Default);
154          }
[2507]155        } while (!abortRequested && !stopped && app.AutoRestart);
156      });
157      t.SetApartmentState(ApartmentState.STA); // needed for the AdvancedOptimizationFrontent
158      t.Start();
159    }
160
[2922]161    private void SetCursor(Cursor cursor) {
162      if (InvokeRequired) Invoke((Action<Cursor>)SetCursor, cursor);
163      else {
164        Cursor = cursor;
165      }
166    }
167
[2]168    private void applicationsListBox_SelectedIndexChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
[1394]169      if (e.IsSelected) {
[2]170        startButton.Enabled = true;
171      } else {
172        startButton.Enabled = false;
173      }
174    }
175
176    private void largeIconsButton_Click(object sender, EventArgs e) {
177      applicationsListView.View = View.LargeIcon;
178    }
179
180    private void listButton_Click(object sender, EventArgs e) {
181      applicationsListView.View = View.List;
182    }
183
184    private void detailsButton_Click(object sender, EventArgs e) {
185      applicationsListView.View = View.Details;
186    }
187
[2504]188    private void ShowErrorMessageBox(Exception ex) {
189      MessageBoxOptions options = RightToLeft == RightToLeft.Yes ? MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading : MessageBoxOptions.DefaultDesktopOnly;
[2505]190      MessageBox.Show(null,
[2504]191         BuildErrorMessage(ex),
192         "Error - " + ex.GetType().Name,
193         MessageBoxButtons.OK,
194         MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, options);
[241]195    }
[2504]196    private static string BuildErrorMessage(Exception ex) {
[3289]197      string nl = Environment.NewLine;
[241]198      StringBuilder sb = new StringBuilder();
[3289]199      sb.Append(ex.Message + nl + ex.StackTrace);
[241]200
[1394]201      while (ex.InnerException != null) {
[241]202        ex = ex.InnerException;
[3289]203        sb.Append(nl + "-----" + nl + ex.Message + nl + ex.StackTrace);
[241]204      }
205      return sb.ToString();
206    }
[1392]207
208    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
209      abortRequested = true;
210    }
[2]211  }
212}
Note: See TracBrowser for help on using the repository browser.