Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2876 was 2790, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 6.7 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  public partial class StarterForm : Form {
39
40    private ListViewItem pluginManagerListViewItem;
41    private bool abortRequested;
42    private PluginManager pluginManager;
43
44    public StarterForm()
45      : base() {
46      InitializeComponent();
47
48      string pluginPath = Path.GetFullPath(Application.StartupPath);
49      pluginManager = new PluginManager(pluginPath);
50      SplashScreen splashScreen = new SplashScreen(pluginManager, 1000, "Loading HeuristicLab...");
51      splashScreen.Show();
52
53      pluginManager.DiscoverAndCheckPlugins();
54
55      applicationsListView.Items.Clear();
56
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
65      foreach (ApplicationDescription info in pluginManager.Applications) {
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
76    public StarterForm(string appName)
77      : this() {
78      var appDesc = (from desc in pluginManager.Applications
79                     where desc.Name == appName
80                     select desc).SingleOrDefault();
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
91    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
92      if (applicationsListView.SelectedItems.Count > 0) {
93        ListViewItem selected = applicationsListView.SelectedItems[0];
94        if (selected == pluginManagerListViewItem) {
95          try {
96            Cursor = Cursors.AppStarting;
97            InstallationManagerForm form = new InstallationManagerForm();
98            this.Visible = false;
99            form.ShowDialog(this);
100            // RefreshApplicationsList();
101            this.Visible = true;
102          }
103          finally {
104            Cursor = Cursors.Arrow;
105          }
106        } else {
107          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
108          StartApplication(app);
109        }
110      }
111    }
112
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
135    private void applicationsListBox_SelectedIndexChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
136      if (e.IsSelected) {
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
155    private void ShowErrorMessageBox(Exception ex) {
156      MessageBoxOptions options = RightToLeft == RightToLeft.Yes ? MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading : MessageBoxOptions.DefaultDesktopOnly;
157      MessageBox.Show(null,
158         BuildErrorMessage(ex),
159         "Error - " + ex.GetType().Name,
160         MessageBoxButtons.OK,
161         MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, options);
162    }
163    private static string BuildErrorMessage(Exception ex) {
164      StringBuilder sb = new StringBuilder();
165      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
166
167      while (ex.InnerException != null) {
168        ex = ex.InnerException;
169        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
170      }
171      return sb.ToString();
172    }
173
174    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
175      abortRequested = true;
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.