Free cookie consent management tool by TermsFeed Policy Generator

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

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

Changed starter form to instantiate only one splash screen and show and hide it as required. #183 (Splash screen sometimes causes ObjectDisposedException)

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