source: branches/RefactorPluginInfrastructure-2522/HeuristicLab.PluginInfrastructure.UI/PluginInformationDialog.cs @ 13363

Last change on this file since 13363 was 13363, checked in by gkronber, 7 years ago

#2522: improvements to new PluginInformationDialog and removed obsolete classes

File size: 7.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.Drawing;
25using System.IO;
26using System.Linq;
27using System.Reflection;
28using System.Text.RegularExpressions;
29using System.Windows.Forms;
30
31namespace HeuristicLab.PluginInfrastructure.UI {
32  /// <summary>
33  /// Shows information on all discovered plugins.
34  /// </summary>
35  public partial class PluginInformationDialog : Form {
36    public PluginInformationDialog(IEnumerable<IPluginDescription> plugins) {
37      InitializeComponent();
38      var entryAssembly = Assembly.GetEntryAssembly();
39      productTextBox.Text = GetProduct(entryAssembly);
40      versionTextBox.Text = entryAssembly.GetFileVersion();
41      contactTextBox.Text = GetCopyright(entryAssembly);
42      imageList.Images.Add(Resources.Plugin);
43      textBox.Text = Resources.LicenseText;
44      PopulatePluginList(plugins);
45      ActiveControl = okButton;
46    }
47
48    private void PopulatePluginList(IEnumerable<IPluginDescription> plugins) {
49      pluginListView.Items.Clear();
50      foreach (var plugin in plugins) {
51        ListViewItem pluginItem = CreateListViewItem(plugin);
52        pluginListView.Items.Add(pluginItem);
53      }
54    }
55
56    private ListViewItem CreateListViewItem(IPluginDescription plugin) {
57      ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), plugin.Description }) {
58        Name = plugin.Name,
59        Tag = plugin,
60        ImageIndex = 0
61      };
62
63      // set sub-item names to make sure they are shown in the corresponding columns of the ListView
64      item.SubItems[0].Name = "Name";
65      item.SubItems[1].Name = "Version";
66      item.SubItems[2].Name = "Description";
67      return item;
68    }
69
70    private string GetCopyright(Assembly asm) {
71      AssemblyCopyrightAttribute attribute = GetAttribute<AssemblyCopyrightAttribute>(asm);
72      return attribute.Copyright;
73    }
74
75    private string GetProduct(Assembly asm) {
76      AssemblyProductAttribute attribute = GetAttribute<AssemblyProductAttribute>(asm);
77      return attribute.Product;
78    }
79
80    private T GetAttribute<T>(Assembly asm) {
81      return (T)asm.GetCustomAttributes(typeof(T), false).Single();
82    }
83
84    private void okButton_Click(object sender, EventArgs e) {
85      Close();
86    }
87
88    private void pluginListView_ItemActivate(object sender, EventArgs e) {
89
90    }
91    private void pluginListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
92      if (e.IsSelected) {
93        pluginListView.EnsureVisible(e.ItemIndex);
94        ShowPluginInformation((IPluginDescription)e.Item.Tag);
95        pluginListView.Select(); // focus plugin list view to highlight selected tile
96      }
97    }
98
99    private void ShowPluginInformation(IPluginDescription pluginDescription) {
100      productTextBox.Text = pluginDescription.Name;
101      versionTextBox.Text = pluginDescription.Version.ToString();
102      contactTextBox.Text = pluginDescription.ContactName + " " + pluginDescription.ContactEmail;
103      stateTextBox.Text = pluginDescription.PluginState.ToString();
104
105      textBox.Text = string.Empty;
106      textBox.AppendText(pluginDescription.Description + Environment.NewLine + Environment.NewLine);
107
108      if (!string.IsNullOrEmpty(pluginDescription.LoadingErrorInformation)) {
109        var startSel = textBox.TextLength;
110        textBox.AppendText("Error loading: " + pluginDescription.LoadingErrorInformation);
111        textBox.SelectionStart = startSel;
112        textBox.SelectionLength = textBox.TextLength;
113        textBox.SelectionColor = Color.Red;
114        textBox.AppendText(Environment.NewLine + Environment.NewLine);
115      }
116
117      textBox.AppendText("Files:" + Environment.NewLine);
118      foreach (var f in pluginDescription.Files) {
119        textBox.AppendText(f.Name); // might add icons for files here in the future
120        textBox.AppendText(Environment.NewLine);
121      }
122
123      textBox.AppendText(Environment.NewLine);
124      if (pluginDescription.Dependencies.Any())
125        textBox.AppendText("Dependencies:" + Environment.NewLine);
126
127      // foreach dependency add a new line and set the formatting
128
129      foreach (var d in pluginDescription.Dependencies) {
130        // remember starting pos for selection
131        var start = textBox.Text.Length;
132        textBox.AppendText(d.Name);
133        textBox.SelectionStart = start;
134        textBox.SelectionLength = d.Name.Length;
135        textBox.SelectionColor = Color.RoyalBlue;
136        textBox.AppendText(Environment.NewLine);
137      }
138      textBox.AppendText(Environment.NewLine);
139      textBox.AppendText(pluginDescription.LicenseText);
140    }
141
142    private void licenseTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
143      System.Diagnostics.Process.Start(e.LinkText);
144    }
145
146    private void webLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
147      System.Diagnostics.Process.Start(webLinkLabel.Text);
148    }
149
150    private void mailLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
151      System.Diagnostics.Process.Start("mailto:" + mailLinkLabel.Text);
152    }
153
154    // automatically adjust tile size to 100% width of listview
155    private void pluginListView_ClientSizeChanged(object sender, EventArgs e) {
156      pluginListView.TileSize = new Size(pluginListView.ClientSize.Width, pluginListView.TileSize.Height);
157    }
158
159    private void textBox_MouseClick(object sender, MouseEventArgs e) {
160      var clickedWord = GetWordAtMousePos(e.Location);
161
162      pluginListView.SelectedItems.Clear();
163
164      // find and select first matching item
165      for (int pi = 0; pi < pluginListView.Items.Count; pi++) {
166        if (pluginListView.Items[pi].Text == clickedWord) {
167          pluginListView.Items[pi].Selected = true;
168          break;
169        }
170      }
171    }
172
173    private void textBox_MouseMove(object sender, MouseEventArgs e) {
174      var wordUnderMouse = GetWordAtMousePos(e.Location);
175
176      // find first matching item
177      bool foundMatchingItem = false;
178      for (int pi = 0; pi < pluginListView.Items.Count; pi++) {
179        if (pluginListView.Items[pi].Text == wordUnderMouse) {
180          foundMatchingItem = true;
181          break;
182        }
183      }
184      // set to hand if a match is found
185      Cursor = foundMatchingItem ? Cursors.Hand : Cursors.Arrow;
186    }
187    private void textBox_MouseLeave(object sender, EventArgs e) {
188      // make sure the cursor is set to default when the control is left
189      Cursor = Cursors.Default;
190    }
191
192    private string GetWordAtMousePos(Point pos) {
193      int i = textBox.GetCharIndexFromPosition(pos);
194      if (i < 0 || i > textBox.TextLength) return string.Empty;
195      var text = textBox.Text;
196      // extract clicked string (left and right are whitespace
197      int startPos = i; while (startPos >= 0 && !char.IsWhiteSpace(text[startPos])) startPos--;
198      int endPos = i; while (endPos < textBox.TextLength && !char.IsWhiteSpace(text[endPos])) endPos++;
199      if (startPos == endPos) return string.Empty;
200      return text.Substring(startPos + 1, endPos - startPos - 1);
201    }
202
203  }
204}
Note: See TracBrowser for help on using the repository browser.