[3081] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System;
|
---|
[2802] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 | using System.Data;
|
---|
| 27 | using System.Linq;
|
---|
| 28 | using System.Text;
|
---|
| 29 | using System.Windows.Forms;
|
---|
| 30 | using HeuristicLab.PluginInfrastructure;
|
---|
| 31 |
|
---|
[3474] | 32 | namespace HeuristicLab.PluginInfrastructure.Advanced {
|
---|
[3068] | 33 | /// <summary>
|
---|
| 34 | /// Wraps a ListView and adds functionality to automatically check and uncheck dependencies of plugins.
|
---|
| 35 | /// </summary>
|
---|
[3081] | 36 | internal partial class PluginListView : UserControl {
|
---|
[3045] | 37 | public event ItemCheckedEventHandler ItemChecked;
|
---|
[2802] | 38 |
|
---|
[3045] | 39 | private IEnumerable<IPluginDescription> plugins;
|
---|
| 40 | public IEnumerable<IPluginDescription> Plugins {
|
---|
| 41 | get { return plugins; }
|
---|
| 42 | set {
|
---|
| 43 | plugins = value;
|
---|
[3068] | 44 | checkedPlugins.Clear();
|
---|
[3045] | 45 | UpdateControls();
|
---|
| 46 | }
|
---|
[2802] | 47 | }
|
---|
| 48 |
|
---|
[3179] | 49 | private Dictionary<IPluginDescription, bool> checkedPlugins = new Dictionary<IPluginDescription, bool>();
|
---|
[3045] | 50 | public IEnumerable<IPluginDescription> CheckedPlugins {
|
---|
| 51 | get {
|
---|
[3179] | 52 | return from pair in checkedPlugins
|
---|
| 53 | where pair.Value
|
---|
| 54 | select pair.Key;
|
---|
[2816] | 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[3045] | 58 | public PluginListView() {
|
---|
| 59 | InitializeComponent();
|
---|
[2816] | 60 | }
|
---|
| 61 |
|
---|
[3045] | 62 | public void CheckPlugin(IPluginDescription plugin) {
|
---|
[3068] | 63 | MarkPluginChecked(plugin);
|
---|
| 64 | listView.CheckItems(new IPluginDescription[] { plugin }.Select(x => FindItemsForPlugin(x).Single()));
|
---|
[2802] | 65 | }
|
---|
| 66 |
|
---|
[3068] | 67 |
|
---|
[3045] | 68 | private void UpdateControls() {
|
---|
| 69 | if (plugins != null) {
|
---|
| 70 | listView.Items.Clear();
|
---|
[3068] | 71 | listView.SuppressItemCheckedEvents = true;
|
---|
[3045] | 72 | foreach (var plugin in plugins) {
|
---|
| 73 | listView.Items.Add(CreateListViewItem(plugin));
|
---|
[2816] | 74 | }
|
---|
[3208] | 75 | foreach (ColumnHeader column in listView.Columns)
|
---|
[3600] | 76 | column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
| 77 |
|
---|
[3068] | 78 | listView.SuppressItemCheckedEvents = false;
|
---|
[2802] | 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[3045] | 82 | private ListViewItem CreateListViewItem(IPluginDescription plugin) {
|
---|
| 83 | var item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString() });
|
---|
[3179] | 84 | item.Checked = checkedPlugins.ContainsKey(plugin) ? checkedPlugins[plugin] : false;
|
---|
[3045] | 85 | item.Tag = plugin;
|
---|
| 86 | return item;
|
---|
[2860] | 87 | }
|
---|
| 88 |
|
---|
| 89 | private void listView_ItemChecked(object sender, ItemCheckedEventArgs e) {
|
---|
[3068] | 90 | List<IPluginDescription> modifiedPlugins = new List<IPluginDescription>();
|
---|
[2860] | 91 | if (e.Item.Checked) {
|
---|
[3068] | 92 | foreach (ListViewItem item in listView.SelectedItems) {
|
---|
| 93 | var plugin = (IPluginDescription)item.Tag;
|
---|
| 94 | // also check all dependencies
|
---|
| 95 | MarkPluginChecked(plugin);
|
---|
[3179] | 96 | if (!modifiedPlugins.Contains(plugin))
|
---|
| 97 | modifiedPlugins.Add(plugin);
|
---|
[3068] | 98 | foreach (var dep in GetAllDependencies(plugin)) {
|
---|
| 99 | MarkPluginChecked(dep);
|
---|
[3179] | 100 | if (!modifiedPlugins.Contains(dep))
|
---|
| 101 | modifiedPlugins.Add(dep);
|
---|
[3068] | 102 | }
|
---|
[2860] | 103 | }
|
---|
[3068] | 104 | listView.CheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
|
---|
| 105 | OnItemChecked(e);
|
---|
[2860] | 106 | } else {
|
---|
[3068] | 107 | foreach (ListViewItem item in listView.SelectedItems) {
|
---|
| 108 | var plugin = (IPluginDescription)item.Tag;
|
---|
| 109 | // also uncheck all dependent plugins
|
---|
| 110 | MarkPluginUnchecked(plugin);
|
---|
[3179] | 111 | if (!modifiedPlugins.Contains(plugin))
|
---|
| 112 | modifiedPlugins.Add(plugin);
|
---|
[3068] | 113 | foreach (var dep in GetAllDependents(plugin)) {
|
---|
| 114 | MarkPluginUnchecked(dep);
|
---|
[3179] | 115 | if (!modifiedPlugins.Contains(dep))
|
---|
| 116 | modifiedPlugins.Add(dep);
|
---|
[3068] | 117 | }
|
---|
| 118 |
|
---|
[2860] | 119 | }
|
---|
[3068] | 120 | listView.UncheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
|
---|
| 121 | OnItemChecked(e);
|
---|
[2860] | 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[3068] | 125 | private void MarkPluginChecked(IPluginDescription plugin) {
|
---|
[3179] | 126 | checkedPlugins[plugin] = true;
|
---|
[3068] | 127 | }
|
---|
| 128 |
|
---|
| 129 | private void MarkPluginUnchecked(IPluginDescription plugin) {
|
---|
[3179] | 130 | checkedPlugins[plugin] = false;
|
---|
[3068] | 131 | }
|
---|
| 132 |
|
---|
[3045] | 133 | private IEnumerable<ListViewItem> FindItemsForPlugin(IPluginDescription plugin) {
|
---|
| 134 | return from item in listView.Items.OfType<ListViewItem>()
|
---|
| 135 | let p = item.Tag as IPluginDescription
|
---|
| 136 | where p.Name == plugin.Name
|
---|
| 137 | where p.Version == plugin.Version
|
---|
| 138 | select item;
|
---|
[2816] | 139 | }
|
---|
| 140 |
|
---|
[3045] | 141 | private IEnumerable<IPluginDescription> GetAllDependents(IPluginDescription plugin) {
|
---|
| 142 | return from p in plugins
|
---|
| 143 | let matchingEntries = from dep in GetAllDependencies(p)
|
---|
| 144 | where dep.Name == plugin.Name
|
---|
| 145 | where dep.Version == plugin.Version
|
---|
| 146 | select dep
|
---|
| 147 | where matchingEntries.Any()
|
---|
| 148 | select p;
|
---|
[2802] | 149 | }
|
---|
| 150 |
|
---|
[3045] | 151 | private IEnumerable<IPluginDescription> GetAllDependencies(IPluginDescription plugin) {
|
---|
[3179] | 152 | HashSet<IPluginDescription> yieldedPlugins = new HashSet<IPluginDescription>();
|
---|
[3045] | 153 | foreach (var dep in plugin.Dependencies) {
|
---|
| 154 | foreach (var recDep in GetAllDependencies(dep)) {
|
---|
[3179] | 155 | if (!yieldedPlugins.Contains(recDep)) {
|
---|
| 156 | yieldedPlugins.Add(recDep);
|
---|
| 157 | yield return recDep;
|
---|
| 158 | }
|
---|
[2860] | 159 | }
|
---|
[3179] | 160 | if (!yieldedPlugins.Contains(dep)) {
|
---|
| 161 | yieldedPlugins.Add(dep);
|
---|
| 162 | yield return dep;
|
---|
| 163 | }
|
---|
[2802] | 164 | }
|
---|
| 165 | }
|
---|
[2816] | 166 |
|
---|
[3045] | 167 | private void OnItemChecked(ItemCheckedEventArgs e) {
|
---|
[3068] | 168 | if (ItemChecked != null) ItemChecked(this, e);
|
---|
[2816] | 169 | }
|
---|
[2802] | 170 | }
|
---|
| 171 | }
|
---|