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

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

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