Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4070 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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.Collections.Generic;
24using System.Diagnostics;
25using System.IO;
26using System.Linq;
27using System.Threading;
28using System.Windows.Forms;
29using HeuristicLab.PluginInfrastructure.Advanced;
30using HeuristicLab.PluginInfrastructure.Manager;
31
32namespace HeuristicLab.PluginInfrastructure.Starter {
33  /// <summary>
34  /// The starter form is responsible for initializing the plugin infrastructure
35  /// and shows a list of installed applications.
36  /// </summary>
37  public partial class StarterForm : Form {
38
39    private ListViewItem pluginManagerListViewItem;
40    private bool abortRequested;
41    private PluginManager pluginManager;
42    private SplashScreen splashScreen;
43
44    /// <summary>
45    /// Initializes an instance of the starter form.
46    /// The starter form shows a splashscreen and initializes the plugin infrastructure.
47    /// </summary>
48    public StarterForm()
49      : base() {
50      InitializeComponent();
51      largeImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.HeuristicLab.ToBitmap());
52      smallImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.HeuristicLab.ToBitmap());
53      FileVersionInfo pluginInfrastructureVersion = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location);
54      Text = "HeuristicLab " + pluginInfrastructureVersion.FileVersion;
55
56      string pluginPath = Path.GetFullPath(Application.StartupPath);
57      pluginManager = new PluginManager(pluginPath);
58      splashScreen = new SplashScreen(pluginManager, 1000);
59      splashScreen.Show(this, "Loading HeuristicLab...");
60
61      pluginManager.DiscoverAndCheckPlugins();
62      UpdateApplicationsList();
63    }
64
65    /// <summary>
66    /// Creates a new StarterForm and tries to start application with <paramref name="appName"/> immediately.
67    /// </summary>
68    /// <param name="appName">Name of the application</param>
69    public StarterForm(string appName)
70      : this() {
71      var appDesc = (from desc in pluginManager.Applications
72                     where desc.Name == appName
73                     select desc).SingleOrDefault();
74      if (appDesc != null) {
75        StartApplication(appDesc);
76      } else {
77        MessageBox.Show("Cannot start application " + appName + ".",
78                        "HeuristicLab",
79                        MessageBoxButtons.OK,
80                        MessageBoxIcon.Warning);
81      }
82    }
83
84    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
85      if (applicationsListView.SelectedItems.Count > 0) {
86        ListViewItem selected = applicationsListView.SelectedItems[0];
87        if (selected == pluginManagerListViewItem) {
88          if (pluginManager.Plugins.Any(x => x.PluginState == PluginState.Loaded)) {
89            MessageBox.Show("Installation Manager cannot be started while another HeuristicLab application is active." + Environment.NewLine +
90              "Please stop all active HeuristicLab applications and try again.", "Plugin Manager",
91              MessageBoxButtons.OK, MessageBoxIcon.Information);
92          } else {
93            try {
94              Cursor = Cursors.AppStarting;
95              InstallationManagerForm form = new InstallationManagerForm(pluginManager);
96              form.ShowDialog(this);
97              UpdateApplicationsList();
98            }
99            finally {
100              Cursor = Cursors.Arrow;
101            }
102          }
103        } else {
104          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
105          StartApplication(app);
106        }
107      }
108    }
109
110    private void UpdateApplicationsList() {
111      applicationsListView.Items.Clear();
112      FileVersionInfo pluginInfrastructureVersion = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location);
113      pluginManagerListViewItem = new ListViewItem("Plugin Manager", 0);
114      pluginManagerListViewItem.Group = applicationsListView.Groups["Plugin Management"];
115      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, pluginInfrastructureVersion.FileVersion));
116      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "Install, upgrade or delete plugins"));
117      pluginManagerListViewItem.ToolTipText = "Install, upgrade or delete plugins";
118
119      applicationsListView.Items.Add(pluginManagerListViewItem);
120
121      foreach (ApplicationDescription info in pluginManager.Applications) {
122        ListViewItem item = new ListViewItem(info.Name, 0);
123        item.Tag = info;
124        item.Group = applicationsListView.Groups["Applications"];
125        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Version.ToString()));
126        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Description));
127        item.ToolTipText = info.Description;
128        applicationsListView.Items.Add(item);
129      }
130      foreach (ColumnHeader column in applicationsListView.Columns) {
131        if (applicationsListView.Items.Count > 0)
132          column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
133        else column.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
134      }
135    }
136
137    private void StartApplication(ApplicationDescription app) {
138      splashScreen.Show("Loading " + app.Name);
139      Thread t = new Thread(delegate() {
140        bool stopped = false;
141        do {
142          try {
143            if (!abortRequested) {
144              pluginManager.Run(app);
145            }
146            stopped = true;
147          }
148          catch (Exception ex) {
149            stopped = false;
150            ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
151            Thread.Sleep(5000); // sleep 5 seconds before autorestart
152          }
153        } while (!abortRequested && !stopped && app.AutoRestart);
154      });
155      t.SetApartmentState(ApartmentState.STA); // needed for the AdvancedOptimizationFrontent
156      t.Start();
157    }
158
159    private void applicationsListView_SelectedIndexChanged(object sender, EventArgs e) {
160      startButton.Enabled = applicationsListView.SelectedItems.Count > 0;
161    }
162
163    private void largeIconsButton_Click(object sender, EventArgs e) {
164      applicationsListView.View = View.LargeIcon;
165    }
166
167    private void detailsButton_Click(object sender, EventArgs e) {
168      applicationsListView.View = View.Details;
169      foreach (ColumnHeader column in applicationsListView.Columns) {
170        if (applicationsListView.Items.Count > 0)
171          column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
172        else column.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
173      }
174    }
175
176    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
177      abortRequested = true;
178    }
179
180    private void aboutButton_Click(object sender, EventArgs e) {
181      List<IPluginDescription> plugins = new List<IPluginDescription>(pluginManager.Plugins.OfType<IPluginDescription>());
182      var dialog = new AboutDialog(plugins);
183      dialog.ShowDialog();
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.