Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RefactorPluginInfrastructure-2522/HeuristicLab.PluginInfrastructure.UI/StarterForm.cs @ 13360

Last change on this file since 13360 was 13360, checked in by gkronber, 8 years ago

#2522: added new PluginInformationDialog

File size: 8.9 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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
22using System;
23using System.Collections.Generic;
[4068]24using System.IO;
25using System.Linq;
[2]26using System.Threading;
[8563]27using System.Threading.Tasks;
[4068]28using System.Windows.Forms;
[2481]29using HeuristicLab.PluginInfrastructure.Manager;
[2]30
[13338]31namespace HeuristicLab.PluginInfrastructure.UI {
[3092]32  /// <summary>
33  /// The starter form is responsible for initializing the plugin infrastructure
34  /// and shows a list of installed applications.
35  /// </summary>
[2507]36  public partial class StarterForm : Form {
[13360]37    private const string pluginManagerItemName = "Plugins";
[13333]38    // private const string updatePluginsItemName = "Updates Available";
39    private const string optimizerItemName = "Optimizer"; // TODO: encoding a specific name of an application here is problematic (applications are discovered dynamically)
[2]40
[8748]41    private readonly ICommandLineArgument[] arguments;
[6413]42
[2]43    private ListViewItem pluginManagerListViewItem;
[13338]44    private bool abortRequested; // TODO: necessary?
[2488]45    private PluginManager pluginManager;
[3113]46    private SplashScreen splashScreen;
[8563]47
[3092]48    /// <summary>
49    /// Initializes an instance of the starter form.
50    /// The starter form shows a splashscreen and initializes the plugin infrastructure.
51    /// </summary>
[2507]52    public StarterForm()
[2481]53      : base() {
54      InitializeComponent();
[13338]55      largeImageList.Images.Add(Resources.HeuristicLab.ToBitmap());
56      smallImageList.Images.Add(Resources.HeuristicLab.ToBitmap());
57      Text = "HeuristicLab " + AssemblyExtensions.GetFileVersion(GetType().Assembly);
[2481]58
[2748]59      string pluginPath = Path.GetFullPath(Application.StartupPath);
[2495]60      pluginManager = new PluginManager(pluginPath);
[3113]61      splashScreen = new SplashScreen(pluginManager, 1000);
[8748]62      splashScreen.VisibleChanged += new EventHandler(splashScreen_VisibleChanged);
[3736]63      splashScreen.Show(this, "Loading HeuristicLab...");
[2]64
[2497]65      pluginManager.DiscoverAndCheckPlugins();
[3474]66      UpdateApplicationsList();
[2]67    }
68
[3092]69    /// <summary>
[8563]70    /// Creates a new StarterForm and passes the arguments in <paramref name="args"/>.
71    /// </summary>
72    /// <param name="args">The arguments that should be processed</param>
73    public StarterForm(string[] args)
74      : this() {
[8748]75      arguments = CommandLineArgumentHandling.GetArguments(args);
[8563]76    }
77
[8748]78    protected override void SetVisibleCore(bool value) {
[8818]79      value &= !(arguments.OfType<HideStarterArgument>().Any() || arguments.OfType<OpenArgument>().Any());
[8748]80      if (!value) HandleArguments();
81      base.SetVisibleCore(value);
[8563]82    }
83
[8748]84    #region Events
85    private void StarterForm_Load(object sender, EventArgs e) {
86      HandleArguments();
87    }
88
89    private void StarterForm_FormClosing(object sender, FormClosingEventArgs e) {
90      splashScreen.Close();
91      abortRequested = true;
92    }
93
94    private void applicationsListView_SelectedIndexChanged(object sender, EventArgs e) {
95      startButton.Enabled = applicationsListView.SelectedItems.Count > 0;
96    }
97
[2]98    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
[1394]99      if (applicationsListView.SelectedItems.Count > 0) {
[2]100        ListViewItem selected = applicationsListView.SelectedItems[0];
[6413]101        if (selected.Text == pluginManagerItemName) {
[13360]102          try {
103            Cursor = Cursors.AppStarting;
104            using (var form = new PluginInformationDialog(pluginManager.Plugins)) {
105              form.ShowDialog(this);
[2922]106            }
[13360]107          } finally {
108            Cursor = Cursors.Arrow;
[1394]109          }
[2]110        } else {
[2481]111          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
[8818]112          StartApplication(app, arguments);
[2]113        }
114      }
115    }
116
[8748]117    private void largeIconsButton_Click(object sender, EventArgs e) {
118      applicationsListView.View = View.LargeIcon;
119    }
120
121    private void detailsButton_Click(object sender, EventArgs e) {
122      applicationsListView.View = View.Details;
123      foreach (ColumnHeader column in applicationsListView.Columns) {
124        if (applicationsListView.Items.Count > 0)
125          column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
126        else column.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
127      }
128    }
129
130    private void aboutButton_Click(object sender, EventArgs e) {
131      List<IPluginDescription> plugins = new List<IPluginDescription>(pluginManager.Plugins.OfType<IPluginDescription>());
132      using (var dialog = new AboutDialog(plugins)) {
133        dialog.ShowDialog();
134      }
135    }
136
137    private void splashScreen_VisibleChanged(object sender, EventArgs e) {
138      // close hidden starter form
[8818]139      if (!splashScreen.Visible && arguments != null &&
140           (arguments.OfType<HideStarterArgument>().Any() || arguments.OfType<OpenArgument>().Any()))
[8748]141        Close();
142    }
143    #endregion
144
145    #region Helpers
[3474]146    private void UpdateApplicationsList() {
[6413]147      if (InvokeRequired) Invoke((Action)UpdateApplicationsList);
148      else {
149        applicationsListView.Items.Clear();
150        AddPluginManagerItem();
[3474]151
[6413]152        foreach (ApplicationDescription info in pluginManager.Applications) {
153          ListViewItem item = new ListViewItem(info.Name, 0);
154          item.Tag = info;
155          item.Group = applicationsListView.Groups["Applications"];
156          item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Version.ToString()));
157          item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Description));
158          item.ToolTipText = info.Description;
159          applicationsListView.Items.Add(item);
160        }
161        foreach (ColumnHeader column in applicationsListView.Columns) {
162          if (applicationsListView.Items.Count > 0)
163            column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
164          else column.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
165        }
166      }
167    }
[3474]168
[6413]169    private void AddPluginManagerItem() {
[6518]170      pluginManagerListViewItem = new ListViewItem(pluginManagerItemName, 0);
[13360]171      pluginManagerListViewItem.Group = applicationsListView.Groups["Plugin Information"];
[13338]172      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, AssemblyExtensions.GetFileVersion(GetType().Assembly)));
[13360]173      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "Show detailed plugin information"));
174      pluginManagerListViewItem.ToolTipText = "Show detailed plugin information";
[6413]175
[6518]176      applicationsListView.Items.Add(pluginManagerListViewItem);
[6413]177    }
178
[8748]179    private void HandleArguments() {
180      try {
[8818]181        if (arguments.OfType<OpenArgument>().Any() && !arguments.OfType<StartArgument>().Any()) {
182          InitiateApplicationStart(optimizerItemName);
183        }
[8748]184        foreach (var argument in arguments) {
185          if (argument is StartArgument) {
[8818]186            var arg = (StartArgument)argument;
187            InitiateApplicationStart(arg.Value);
[8748]188          }
189        }
[13338]190      } catch (AggregateException ex) {
191        ShowErrorDialog(ex);
[8748]192      }
193    }
194
[8818]195    private void InitiateApplicationStart(string appName) {
196      var appDesc = (from desc in pluginManager.Applications
197                     where desc.Name.Equals(appName)
198                     select desc).SingleOrDefault();
199      if (appDesc != null) {
200        StartApplication(appDesc, arguments);
201      } else {
202        MessageBox.Show("Cannot start application " + appName + ".",
203                        "HeuristicLab",
204                        MessageBoxButtons.OK,
205                        MessageBoxIcon.Warning);
206      }
207    }
208
209    private void StartApplication(ApplicationDescription app, ICommandLineArgument[] args) {
[3113]210      splashScreen.Show("Loading " + app.Name);
[13338]211      // STAThread is necessary for a UI component we are using in the application
212      var t = new Thread(() => pluginManager.Run(app, args));
213      t.SetApartmentState(ApartmentState.STA);
[2507]214      t.Start();
215    }
[8748]216    #endregion
[13338]217
218    private void ShowErrorDialog(Exception exception) {
219      MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
220    }
[2]221  }
222}
Note: See TracBrowser for help on using the repository browser.