#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using System.Windows.Forms; namespace HeuristicLab.PluginInfrastructure.UI { /// /// Shows information on all discovered plugins. /// public partial class PluginInformationDialog : Form { public PluginInformationDialog(IEnumerable plugins) { InitializeComponent(); var entryAssembly = Assembly.GetEntryAssembly(); productTextBox.Text = GetProduct(entryAssembly); versionTextBox.Text = entryAssembly.GetFileVersion(); contactTextBox.Text = GetCopyright(entryAssembly); imageList.Images.Add(Resources.Plugin); textBox.Text = Resources.LicenseText; PopulatePluginList(plugins); ActiveControl = okButton; } private void PopulatePluginList(IEnumerable plugins) { pluginListView.Items.Clear(); foreach (var plugin in plugins) { ListViewItem pluginItem = CreateListViewItem(plugin); pluginListView.Items.Add(pluginItem); } } private ListViewItem CreateListViewItem(IPluginDescription plugin) { ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), plugin.Description }) { Name = plugin.Name, Tag = plugin, ImageIndex = 0 }; // set sub-item names to make sure they are shown in the corresponding columns of the ListView item.SubItems[0].Name = "Name"; item.SubItems[1].Name = "Version"; item.SubItems[2].Name = "Description"; return item; } private string GetCopyright(Assembly asm) { AssemblyCopyrightAttribute attribute = GetAttribute(asm); return attribute.Copyright; } private string GetProduct(Assembly asm) { AssemblyProductAttribute attribute = GetAttribute(asm); return attribute.Product; } private T GetAttribute(Assembly asm) { return (T)asm.GetCustomAttributes(typeof(T), false).Single(); } private void okButton_Click(object sender, EventArgs e) { Close(); } private void pluginListView_ItemActivate(object sender, EventArgs e) { } private void pluginListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) { if (e.IsSelected) { pluginListView.EnsureVisible(e.ItemIndex); ShowPluginInformation((IPluginDescription)e.Item.Tag); pluginListView.Select(); // focus plugin list view to highlight selected tile } } private void ShowPluginInformation(IPluginDescription pluginDescription) { productTextBox.Text = pluginDescription.Name; versionTextBox.Text = pluginDescription.Version.ToString(); contactTextBox.Text = pluginDescription.ContactName + " " + pluginDescription.ContactEmail; stateTextBox.Text = pluginDescription.PluginState.ToString(); textBox.Text = string.Empty; textBox.AppendText(pluginDescription.Description + Environment.NewLine + Environment.NewLine); if (!string.IsNullOrEmpty(pluginDescription.LoadingErrorInformation)) { var startSel = textBox.TextLength; textBox.AppendText("Error loading: " + pluginDescription.LoadingErrorInformation); textBox.SelectionStart = startSel; textBox.SelectionLength = textBox.TextLength; textBox.SelectionColor = Color.Red; textBox.AppendText(Environment.NewLine + Environment.NewLine); } textBox.AppendText("Files:" + Environment.NewLine); foreach (var f in pluginDescription.Files) { textBox.AppendText(f.Name); // might add icons for files here in the future textBox.AppendText(Environment.NewLine); } textBox.AppendText(Environment.NewLine); if (pluginDescription.Dependencies.Any()) textBox.AppendText("Dependencies:" + Environment.NewLine); // foreach dependency add a new line and set the formatting foreach (var d in pluginDescription.Dependencies) { // remember starting pos for selection var start = textBox.Text.Length; textBox.AppendText(d.Name); textBox.SelectionStart = start; textBox.SelectionLength = d.Name.Length; textBox.SelectionColor = Color.RoyalBlue; textBox.AppendText(Environment.NewLine); } textBox.AppendText(Environment.NewLine); textBox.AppendText(pluginDescription.LicenseText); } private void licenseTextBox_LinkClicked(object sender, LinkClickedEventArgs e) { System.Diagnostics.Process.Start(e.LinkText); } private void webLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start(webLinkLabel.Text); } private void mailLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("mailto:" + mailLinkLabel.Text); } // automatically adjust tile size to 100% width of listview private void pluginListView_ClientSizeChanged(object sender, EventArgs e) { pluginListView.TileSize = new Size(pluginListView.ClientSize.Width, pluginListView.TileSize.Height); } private void textBox_MouseClick(object sender, MouseEventArgs e) { var clickedWord = GetWordAtMousePos(e.Location); pluginListView.SelectedItems.Clear(); // find and select first matching item for (int pi = 0; pi < pluginListView.Items.Count; pi++) { if (pluginListView.Items[pi].Text == clickedWord) { pluginListView.Items[pi].Selected = true; break; } } } private void textBox_MouseMove(object sender, MouseEventArgs e) { var wordUnderMouse = GetWordAtMousePos(e.Location); // find first matching item bool foundMatchingItem = false; for (int pi = 0; pi < pluginListView.Items.Count; pi++) { if (pluginListView.Items[pi].Text == wordUnderMouse) { foundMatchingItem = true; break; } } // set to hand if a match is found Cursor = foundMatchingItem ? Cursors.Hand : Cursors.Arrow; } private void textBox_MouseLeave(object sender, EventArgs e) { // make sure the cursor is set to default when the control is left Cursor = Cursors.Default; } private string GetWordAtMousePos(Point pos) { int i = textBox.GetCharIndexFromPosition(pos); if (i < 0 || i > textBox.TextLength) return string.Empty; var text = textBox.Text; // extract clicked string (left and right are whitespace int startPos = i; while (startPos >= 0 && !char.IsWhiteSpace(text[startPos])) startPos--; int endPos = i; while (endPos < textBox.TextLength && !char.IsWhiteSpace(text[endPos])) endPos++; if (startPos == endPos) return string.Empty; return text.Substring(startPos + 1, endPos - startPos - 1); } } }