Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on GUI for plugin management. #891 (Refactor GUI for plugin management)

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