Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed issues in plugin infrastructure as identified by abeham. #863 (Code review)

File size: 6.7 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
[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) {
95          try {
[2748]96            Cursor = Cursors.AppStarting;
97            InstallationManagerForm form = new InstallationManagerForm();
98            this.Visible = false;
99            form.ShowDialog(this);
100            // RefreshApplicationsList();
101            this.Visible = true;
[1394]102          }
103          finally {
104            Cursor = Cursors.Arrow;
105          }
[2]106        } else {
[2481]107          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
[2507]108          StartApplication(app);
[2]109        }
110      }
111    }
112
[2507]113    private void StartApplication(ApplicationDescription app) {
114      SplashScreen splashScreen = new SplashScreen(pluginManager, 2000, "Loading " + app.Name);
115      splashScreen.Show();
116      Thread t = new Thread(delegate() {
117        bool stopped = false;
118        do {
119          try {
120            if (!abortRequested)
121              pluginManager.Run(app);
122            stopped = true;
123          }
124          catch (Exception ex) {
125            stopped = false;
126            ThreadPool.QueueUserWorkItem(delegate(object exception) { ShowErrorMessageBox((Exception)exception); }, ex);
127            Thread.Sleep(5000); // sleep 5 seconds before autorestart
128          }
129        } while (!abortRequested && !stopped && app.AutoRestart);
130      });
131      t.SetApartmentState(ApartmentState.STA); // needed for the AdvancedOptimizationFrontent
132      t.Start();
133    }
134
[2]135    private void applicationsListBox_SelectedIndexChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
[1394]136      if (e.IsSelected) {
[2]137        startButton.Enabled = true;
138      } else {
139        startButton.Enabled = false;
140      }
141    }
142
143    private void largeIconsButton_Click(object sender, EventArgs e) {
144      applicationsListView.View = View.LargeIcon;
145    }
146
147    private void listButton_Click(object sender, EventArgs e) {
148      applicationsListView.View = View.List;
149    }
150
151    private void detailsButton_Click(object sender, EventArgs e) {
152      applicationsListView.View = View.Details;
153    }
154
[2504]155    private void ShowErrorMessageBox(Exception ex) {
156      MessageBoxOptions options = RightToLeft == RightToLeft.Yes ? MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading : MessageBoxOptions.DefaultDesktopOnly;
[2505]157      MessageBox.Show(null,
[2504]158         BuildErrorMessage(ex),
159         "Error - " + ex.GetType().Name,
160         MessageBoxButtons.OK,
161         MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, options);
[241]162    }
[2504]163    private static string BuildErrorMessage(Exception ex) {
[241]164      StringBuilder sb = new StringBuilder();
165      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
166
[1394]167      while (ex.InnerException != null) {
[241]168        ex = ex.InnerException;
169        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
170      }
171      return sb.ToString();
172    }
[1392]173
174    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
175      abortRequested = true;
176    }
[2]177  }
178}
Note: See TracBrowser for help on using the repository browser.