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