Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PluginInfrastructure Refactoring/HeuristicLab/MainForm.cs @ 2496

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

Fixed loading of plugins in correct order as defined by plugin dependencies in AppDomains for applications. (ApplicationManager) #799

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 System.Threading;
32using HeuristicLab.PluginInfrastructure.Manager;
33using System.IO;
34
35namespace HeuristicLab {
36  public partial class MainForm : Form {
37
38    private ListViewItem pluginManagerListViewItem;
39    private bool abortRequested;
40    private PluginManager pluginManager;
41
42    public MainForm()
43      : base() {
44      InitializeComponent();
45
46      abortRequested = false;
47      string pluginPath = Path.GetFullPath(HeuristicLab.PluginInfrastructure.Properties.Settings.Default.PluginDir);
48      pluginManager = new PluginManager(pluginPath);
49      SplashScreen splashScreen = new SplashScreen(pluginManager, 1000, "Loading HeuristicLab...");
50      splashScreen.Owner = this;
51      splashScreen.Show();
52
53      Application.DoEvents();
54      this.Enabled = false;
55
56      pluginManager.Initialize();
57
58      applicationsListView.Items.Clear();
59
60      pluginManagerListViewItem = new ListViewItem("Plugin Manager", 0);
61      pluginManagerListViewItem.Group = applicationsListView.Groups["Plugin Management"];
62      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "-"));
63      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "Install, upgrade or delete plugins"));
64      pluginManagerListViewItem.ToolTipText = "Install, upgrade or delete plugins";
65
66      applicationsListView.Items.Add(pluginManagerListViewItem);
67
68      foreach (ApplicationDescription info in pluginManager.Applications) {
69        ListViewItem item = new ListViewItem(info.Name, 0);
70        item.Tag = info;
71        item.Group = applicationsListView.Groups["Applications"];
72        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Version.ToString()));
73        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Description));
74        item.ToolTipText = info.Description;
75        applicationsListView.Items.Add(item);
76      }
77
78
79      this.Enabled = true;
80      this.Visible = true;
81    }
82
83    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
84      if (applicationsListView.SelectedItems.Count > 0) {
85        ListViewItem selected = applicationsListView.SelectedItems[0];
86        if (selected == pluginManagerListViewItem) {
87          try {
88            //Cursor = Cursors.AppStarting;
89            //ManagerForm form = new ManagerForm();
90            //this.Visible = false;
91            //form.ShowDialog(this);
92            //// RefreshApplicationsList();
93            //this.Visible = true;
94          }
95          finally {
96            Cursor = Cursors.Arrow;
97          }
98        } else {
99          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
100          SplashScreen splashScreen = new SplashScreen(pluginManager, 2000, "Loading " + app.Name);
101          splashScreen.Owner = this;
102          splashScreen.Show();
103          Thread t = new Thread(delegate() {
104            bool stopped = false;
105            do {
106              try {
107                if (!abortRequested)
108                  pluginManager.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.