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, 14 years ago

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

File size: 7.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  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          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            }
110          }
111        } else {
112          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
113          StartApplication(app);
114        }
115      }
116    }
117
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 {
125            if (!abortRequested) {
126              SetCursor(Cursors.AppStarting);
127              pluginManager.Run(app);
128            }
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          }
136          finally {
137            SetCursor(Cursors.Default);
138          }
139        } while (!abortRequested && !stopped && app.AutoRestart);
140      });
141      t.SetApartmentState(ApartmentState.STA); // needed for the AdvancedOptimizationFrontent
142      t.Start();
143    }
144
145    private void SetCursor(Cursor cursor) {
146      if (InvokeRequired) Invoke((Action<Cursor>)SetCursor, cursor);
147      else {
148        Cursor = cursor;
149      }
150    }
151
152    private void applicationsListBox_SelectedIndexChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
153      if (e.IsSelected) {
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
172    private void ShowErrorMessageBox(Exception ex) {
173      MessageBoxOptions options = RightToLeft == RightToLeft.Yes ? MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading : MessageBoxOptions.DefaultDesktopOnly;
174      MessageBox.Show(null,
175         BuildErrorMessage(ex),
176         "Error - " + ex.GetType().Name,
177         MessageBoxButtons.OK,
178         MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, options);
179    }
180    private static string BuildErrorMessage(Exception ex) {
181      StringBuilder sb = new StringBuilder();
182      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
183
184      while (ex.InnerException != null) {
185        ex = ex.InnerException;
186        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
187      }
188      return sb.ToString();
189    }
190
191    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
192      abortRequested = true;
193    }
194  }
195}
Note: See TracBrowser for help on using the repository browser.