Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab/MainForm.cs @ 1673

Last change on this file since 1673 was 1394, checked in by gkronber, 16 years ago

Fixed #526 (UI feedback when starting plugin-manager). Mouse cursor is changed before loading the plugin manager form and set back to normal once all plugins are loaded.

File size: 6.1 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.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29using System.Diagnostics;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.PluginInfrastructure.GUI;
32using System.Threading;
33
34namespace HeuristicLab {
35  public partial class MainForm : Form {
36
37    private ListViewItem pluginManagerListViewItem;
38    private bool abortRequested;
39
40    public MainForm() {
41      abortRequested = false;
42      SplashScreen splashScreen = new SplashScreen(1000, "Loading HeuristicLab...");
43      splashScreen.Owner = this;
44      splashScreen.Show();
45
46      Application.DoEvents();
47      this.Enabled = false;
48
49      PluginManager.Manager.Action += new PluginManagerActionEventHandler(splashScreen.Manager_Action);
50      PluginManager.Manager.Initialize();
51
52      InitializeComponent();
53
54      RefreshApplicationsList();
55
56      this.Enabled = true;
57      this.Visible = true;
58    }
59
60    private void RefreshApplicationsList() {
61      applicationsListView.Items.Clear();
62
63      pluginManagerListViewItem = new ListViewItem("Plugin Manager", 0);
64      pluginManagerListViewItem.Group = applicationsListView.Groups["Plugin Management"];
65      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "-"));
66      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "Install, upgrade or delete plugins"));
67      pluginManagerListViewItem.ToolTipText = "Install, upgrade or delete plugins";
68
69      applicationsListView.Items.Add(pluginManagerListViewItem);
70
71      foreach (ApplicationInfo info in PluginManager.Manager.InstalledApplications) {
72        ListViewItem item = new ListViewItem(info.Name, 0);
73        item.Tag = info;
74        item.Group = applicationsListView.Groups["Applications"];
75        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Version.ToString()));
76        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Description));
77        item.ToolTipText = info.Description;
78        applicationsListView.Items.Add(item);
79      }
80    }
81
82    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
83      if (applicationsListView.SelectedItems.Count > 0) {
84        ListViewItem selected = applicationsListView.SelectedItems[0];
85        if (selected == pluginManagerListViewItem) {
86          try {
87            Cursor = Cursors.AppStarting;
88            ManagerForm form = new ManagerForm();
89            this.Visible = false;
90            form.ShowDialog(this);
91            RefreshApplicationsList();
92            this.Visible = true;
93          }
94          finally {
95            Cursor = Cursors.Arrow;
96          }
97        } else {
98          ApplicationInfo app = (ApplicationInfo)applicationsListView.SelectedItems[0].Tag;
99          SplashScreen splashScreen = new SplashScreen(2000, "Loading " + app.Name);
100          splashScreen.Owner = this;
101          splashScreen.Show();
102          PluginManager.Manager.Action += new PluginManagerActionEventHandler(splashScreen.Manager_Action);
103          Thread t = new Thread(delegate() {
104            bool stopped = false;
105            do {
106              try {
107                if (!abortRequested)
108                  PluginManager.Manager.Run(app);
109                stopped = true;
110              }
111              catch (Exception ex) {
112                stopped = false;
113                ThreadPool.QueueUserWorkItem(delegate(object exception) { ShowErrorMessageBox((Exception)exception); }, ex);
114                Thread.Sleep(5000); // sleep 5 seconds before autorestart
115              }
116            } while (!abortRequested && !stopped && app.AutoRestart);
117          });
118          t.SetApartmentState(ApartmentState.STA); // needed for the AdvancedOptimizationFrontent
119          t.Start();
120        }
121      }
122    }
123
124    private void applicationsListBox_SelectedIndexChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
125      if (e.IsSelected) {
126        startButton.Enabled = true;
127      } else {
128        startButton.Enabled = false;
129      }
130    }
131
132    private void largeIconsButton_Click(object sender, EventArgs e) {
133      applicationsListView.View = View.LargeIcon;
134    }
135
136    private void listButton_Click(object sender, EventArgs e) {
137      applicationsListView.View = View.List;
138    }
139
140    private void detailsButton_Click(object sender, EventArgs e) {
141      applicationsListView.View = View.Details;
142    }
143
144    public void ShowErrorMessageBox(Exception ex) {
145      MessageBox.Show(BuildErrorMessage(ex),
146                      "Error - " + ex.GetType().Name,
147                      MessageBoxButtons.OK,
148                      MessageBoxIcon.Error);
149    }
150    private string BuildErrorMessage(Exception ex) {
151      StringBuilder sb = new StringBuilder();
152      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
153
154      while (ex.InnerException != null) {
155        ex = ex.InnerException;
156        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
157      }
158      return sb.ToString();
159    }
160
161    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
162      abortRequested = true;
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.