Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15194 was 13369, checked in by gkronber, 9 years ago

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