Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented changes as requested by swagner. #799

File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
35
36namespace HeuristicLab.PluginInfrastructure.Starter {
37  public partial class StarterForm : Form {
38
39    private ListViewItem pluginManagerListViewItem;
40    private bool abortRequested;
41    private PluginManager pluginManager;
42
43    public StarterForm()
44      : base() {
45      InitializeComponent();
46
47      string pluginPath = Path.GetFullPath(Application.StartupPath);     
48      pluginManager = new PluginManager(pluginPath);
49      SplashScreen splashScreen = new SplashScreen(pluginManager, 1000, "Loading HeuristicLab...");
50      splashScreen.Show();
51
52      pluginManager.DiscoverAndCheckPlugins();
53
54      applicationsListView.Items.Clear();
55
56      pluginManagerListViewItem = new ListViewItem("Plugin Manager", 0);
57      pluginManagerListViewItem.Group = applicationsListView.Groups["Plugin Management"];
58      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "-"));
59      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "Install, upgrade or delete plugins"));
60      pluginManagerListViewItem.ToolTipText = "Install, upgrade or delete plugins";
61
62      applicationsListView.Items.Add(pluginManagerListViewItem);
63
64      foreach (ApplicationDescription info in pluginManager.Applications) {
65        ListViewItem item = new ListViewItem(info.Name, 0);
66        item.Tag = info;
67        item.Group = applicationsListView.Groups["Applications"];
68        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Version.ToString()));
69        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Description));
70        item.ToolTipText = info.Description;
71        applicationsListView.Items.Add(item);
72      }
73    }
74
75    public StarterForm(string appName)
76      : this() {
77      var appDesc = (from desc in pluginManager.Applications
78                     where desc.Name == appName
79                     select desc).Single();
80      if (appDesc != null) {
81        StartApplication(appDesc);
82      } else {
83        MessageBox.Show("Cannot start application " + appName + ".",
84                        "HeuristicLab",
85                        MessageBoxButtons.OK,
86                        MessageBoxIcon.Warning);
87      }
88    }
89
90    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
91      if (applicationsListView.SelectedItems.Count > 0) {
92        ListViewItem selected = applicationsListView.SelectedItems[0];
93        if (selected == pluginManagerListViewItem) {
94          try {
95            //Cursor = Cursors.AppStarting;
96            //ManagerForm form = new ManagerForm();
97            //this.Visible = false;
98            //form.ShowDialog(this);
99            //// RefreshApplicationsList();
100            //this.Visible = true;
101          }
102          finally {
103            Cursor = Cursors.Arrow;
104          }
105        } else {
106          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
107          StartApplication(app);
108        }
109      }
110    }
111
112    private void StartApplication(ApplicationDescription app) {
113      SplashScreen splashScreen = new SplashScreen(pluginManager, 2000, "Loading " + app.Name);
114      splashScreen.Show();
115      Thread t = new Thread(delegate() {
116        bool stopped = false;
117        do {
118          try {
119            if (!abortRequested)
120              pluginManager.Run(app);
121            stopped = true;
122          }
123          catch (Exception ex) {
124            stopped = false;
125            ThreadPool.QueueUserWorkItem(delegate(object exception) { ShowErrorMessageBox((Exception)exception); }, ex);
126            Thread.Sleep(5000); // sleep 5 seconds before autorestart
127          }
128        } while (!abortRequested && !stopped && app.AutoRestart);
129      });
130      t.SetApartmentState(ApartmentState.STA); // needed for the AdvancedOptimizationFrontent
131      t.Start();
132    }
133
134    private void applicationsListBox_SelectedIndexChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
135      if (e.IsSelected) {
136        startButton.Enabled = true;
137      } else {
138        startButton.Enabled = false;
139      }
140    }
141
142    private void largeIconsButton_Click(object sender, EventArgs e) {
143      applicationsListView.View = View.LargeIcon;
144    }
145
146    private void listButton_Click(object sender, EventArgs e) {
147      applicationsListView.View = View.List;
148    }
149
150    private void detailsButton_Click(object sender, EventArgs e) {
151      applicationsListView.View = View.Details;
152    }
153
154    private void ShowErrorMessageBox(Exception ex) {
155      MessageBoxOptions options = RightToLeft == RightToLeft.Yes ? MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading : MessageBoxOptions.DefaultDesktopOnly;
156      MessageBox.Show(null,
157         BuildErrorMessage(ex),
158         "Error - " + ex.GetType().Name,
159         MessageBoxButtons.OK,
160         MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, options);
161    }
162    private static string BuildErrorMessage(Exception ex) {
163      StringBuilder sb = new StringBuilder();
164      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
165
166      while (ex.InnerException != null) {
167        ex = ex.InnerException;
168        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
169      }
170      return sb.ToString();
171    }
172
173    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
174      abortRequested = true;
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.