Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2522: added new PluginInformationDialog

File size: 6.4 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      Util.ResizeColumns(pluginListView.Columns.OfType<ColumnHeader>());
55    }
56
57    private ListViewItem CreateListViewItem(IPluginDescription plugin) {
58      ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), plugin.Description }) {
59        Name = plugin.Name,
60        Tag = plugin,
61        ImageIndex = 0
62      };
63
64      // set sub-item names to make sure they are shown in the corresponding columns of the ListView
65      item.SubItems[0].Name = "Name";
66      item.SubItems[1].Name = "Version";
67      item.SubItems[2].Name = "Description";
68      return item;
69    }
70
71    private string GetCopyright(Assembly asm) {
72      AssemblyCopyrightAttribute attribute = GetAttribute<AssemblyCopyrightAttribute>(asm);
73      return attribute.Copyright;
74    }
75
76    private string GetProduct(Assembly asm) {
77      AssemblyProductAttribute attribute = GetAttribute<AssemblyProductAttribute>(asm);
78      return attribute.Product;
79    }
80
81    private T GetAttribute<T>(Assembly asm) {
82      return (T)asm.GetCustomAttributes(typeof(T), false).Single();
83    }
84
85    private void okButton_Click(object sender, EventArgs e) {
86      Close();
87    }
88
89    private void pluginListView_ItemActivate(object sender, EventArgs e) {
90
91    }
92    private void pluginListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
93      if (e.IsSelected) {
94        pluginListView.EnsureVisible(e.ItemIndex);
95        ShowPluginInformation((IPluginDescription)e.Item.Tag);
96        pluginListView.Select(); // focus plugin list view to highlight selected tile
97      }
98    }
99
100    private void ShowPluginInformation(IPluginDescription pluginDescription) {
101      productTextBox.Text = pluginDescription.Name;
102      versionTextBox.Text = pluginDescription.Version.ToString();
103      contactTextBox.Text =
104        pluginDescription.ContactName + " " + pluginDescription.ContactEmail;
105
106      // build text and later replace plugindependency names with RTF links (too lazy to create RTF document)
107      textBox.Text =
108        pluginDescription.Description +
109        Environment.NewLine + Environment.NewLine +
110        "Files:" + Environment.NewLine +
111        string.Join(Environment.NewLine, pluginDescription.Files) +
112        Environment.NewLine + Environment.NewLine +
113        "Dependencies:" + Environment.NewLine;
114
115      // foreach dependency add a new line and set the formatting
116
117      foreach (var d in pluginDescription.Dependencies) {
118        // remember starting pos for selection
119        var start = textBox.Text.Length;
120        textBox.AppendText(d.Name);
121        textBox.SelectionStart = start;
122        textBox.SelectionLength = d.Name.Length;
123        textBox.SelectionColor = Color.RoyalBlue;
124        textBox.AppendText(Environment.NewLine);
125      }
126      textBox.AppendText(Environment.NewLine);
127      textBox.AppendText(pluginDescription.LicenseText);
128    }
129
130    private void licenseTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
131      System.Diagnostics.Process.Start(e.LinkText);
132    }
133
134    private void webLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
135      System.Diagnostics.Process.Start(webLinkLabel.Text);
136    }
137
138    private void mailLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
139      System.Diagnostics.Process.Start("mailto:" + mailLinkLabel.Text);
140    }
141
142    // automatically adjust tile size to 100% width of listview
143    private void pluginListView_ClientSizeChanged(object sender, EventArgs e) {
144      pluginListView.TileSize = new Size(pluginListView.ClientSize.Width, pluginListView.TileSize.Height);
145    }
146
147    private void textBox_MouseClick(object sender, MouseEventArgs e) {
148      int i = textBox.GetCharIndexFromPosition(e.Location);
149      var text = textBox.Text;
150      // extract clicked string (left and right are whitespace
151      int startPos = i; while (startPos >= 0 && !char.IsWhiteSpace(text[startPos])) startPos--;
152      int endPos = i; while (endPos < textBox.TextLength && !char.IsWhiteSpace(text[endPos])) endPos++;
153      string clickedWord = text.Substring(startPos + 1, endPos - startPos - 1);
154
155      pluginListView.SelectedItems.Clear();
156
157      // find and select first matching item
158      for (int pi = 0; pi < pluginListView.Items.Count; pi++) {
159        if (pluginListView.Items[pi].Text == clickedWord) {
160          pluginListView.Items[pi].Selected = true;
161          break;
162        }
163      }
164    }
165
166  }
167}
Note: See TracBrowser for help on using the repository browser.