Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure.UI/PluginInformationDialog.cs @ 16371

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

#2522: improvements to AboutDialog and PluginInformationDialog

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