Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed relevant warnings in the plugin infrastructure (didn't fix warnings about XML comments of members that will be removed soon). #915 (Remove warnings from HL 3.3 solution)

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