Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2522_RefactorPluginInfrastructure/HeuristicLab.PluginInfrastructure.UI/StarterForm.cs @ 18066

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

#2522: improvements to AboutDialog and PluginInformationDialog

File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.IO;
25using System.Linq;
26using System.Reflection;
27using System.Threading;
28using System.Threading.Tasks;
29using System.Windows.Forms;
30using HeuristicLab.PluginInfrastructure.Manager;
31
32namespace HeuristicLab.PluginInfrastructure.UI {
33  /// <summary>
34  /// The starter form is responsible for initializing the plugin infrastructure
35  /// and shows a list of installed applications.
36  /// </summary>
37  public partial class StarterForm : Form {
38    private const string pluginManagerItemName = "Plugins";
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)
41
42    private readonly ICommandLineArgument[] arguments;
43
44    private ListViewItem pluginManagerListViewItem;
45    private bool abortRequested; // TODO: necessary?
46    private PluginManager pluginManager;
47    private SplashScreen splashScreen;
48
49    /// <summary>
50    /// Initializes an instance of the starter form.
51    /// The starter form shows a splashscreen and initializes the plugin infrastructure.
52    /// </summary>
53    public StarterForm()
54      : base() {
55      InitializeComponent();
56      largeImageList.Images.Add(Resources.HeuristicLab.ToBitmap());
57      smallImageList.Images.Add(Resources.HeuristicLab.ToBitmap());
58      Text = "HeuristicLab " + AssemblyExtensions.GetFileVersion(GetType().Assembly);
59
60      string pluginPath = Path.GetFullPath(Application.StartupPath);
61      pluginManager = new PluginManager(pluginPath);
62      splashScreen = new SplashScreen(pluginManager, 1000);
63      splashScreen.VisibleChanged += new EventHandler(splashScreen_VisibleChanged);
64      splashScreen.Show(this, "Loading HeuristicLab...");
65
66      pluginManager.DiscoverAndCheckPlugins();
67      UpdateApplicationsList();
68    }
69
70    /// <summary>
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() {
76      arguments = CommandLineArgumentHandling.GetArguments(args);
77    }
78
79    protected override void SetVisibleCore(bool value) {
80      value &= !(arguments.OfType<HideStarterArgument>().Any() || arguments.OfType<OpenArgument>().Any());
81      if (!value) HandleArguments();
82      base.SetVisibleCore(value);
83    }
84
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
99    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
100      if (applicationsListView.SelectedItems.Count > 0) {
101        ListViewItem selected = applicationsListView.SelectedItems[0];
102        if (selected.Text == pluginManagerItemName) {
103          try {
104            Cursor = Cursors.AppStarting;
105            using (var form = new PluginInformationDialog(pluginManager.Plugins)) {
106              form.ShowDialog(this);
107            }
108          } finally {
109            Cursor = Cursors.Arrow;
110          }
111        } else {
112          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
113          StartApplication(app, arguments);
114        }
115      }
116    }
117
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>());
133      using (var dialog = new AboutDialog(plugins, Assembly.GetEntryAssembly())) {
134        dialog.ShowDialog();
135      }
136    }
137
138    private void splashScreen_VisibleChanged(object sender, EventArgs e) {
139      // close hidden starter form
140      if (!splashScreen.Visible && arguments != null &&
141           (arguments.OfType<HideStarterArgument>().Any() || arguments.OfType<OpenArgument>().Any()))
142        Close();
143    }
144    #endregion
145
146    #region Helpers
147    private void UpdateApplicationsList() {
148      if (InvokeRequired) Invoke((Action)UpdateApplicationsList);
149      else {
150        applicationsListView.Items.Clear();
151        AddPluginManagerItem();
152
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    }
169
170    private void AddPluginManagerItem() {
171      pluginManagerListViewItem = new ListViewItem(pluginManagerItemName, 0);
172      pluginManagerListViewItem.Group = applicationsListView.Groups["Plugin Information"];
173      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, AssemblyExtensions.GetFileVersion(GetType().Assembly)));
174      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "Show detailed plugin information"));
175      pluginManagerListViewItem.ToolTipText = "Show detailed plugin information";
176
177      applicationsListView.Items.Add(pluginManagerListViewItem);
178    }
179
180    private void HandleArguments() {
181      try {
182        if (arguments.OfType<OpenArgument>().Any() && !arguments.OfType<StartArgument>().Any()) {
183          InitiateApplicationStart(optimizerItemName);
184        }
185        foreach (var argument in arguments) {
186          if (argument is StartArgument) {
187            var arg = (StartArgument)argument;
188            InitiateApplicationStart(arg.Value);
189          }
190        }
191      } catch (AggregateException ex) {
192        ShowErrorDialog(ex);
193      }
194    }
195
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) {
211      splashScreen.Show("Loading " + app.Name);
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);
215      t.Start();
216    }
217    #endregion
218
219    private void ShowErrorDialog(Exception exception) {
220      MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
221    }
222  }
223}
Note: See TracBrowser for help on using the repository browser.