Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Advanced/LocalPluginManager.cs @ 3474

Last change on this file since 3474 was 3474, checked in by gkronber, 14 years ago

Incorporated review comments by swagner into plugin infrastructure. #989 (Implement review comments in plugin infrastructure)

File size: 5.2 KB
Line 
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
21using System;
22using System.Collections.Generic;
23using System.ComponentModel;
24using System.Drawing;
25using System.Data;
26using System.Linq;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.PluginInfrastructure.Manager;
30
31namespace HeuristicLab.PluginInfrastructure.Advanced {
32  internal partial class LocalPluginManagerView : UserControl {
33
34    public event ItemCheckedEventHandler ItemChecked;
35
36    private BackgroundWorker refreshPluginListBackgroundWorker = new BackgroundWorker();
37
38    private ListViewGroup enabledPluginsGroup;
39    private ListViewGroup disabledPluginsGroup;
40
41    public LocalPluginManagerView() {
42      InitializeComponent();
43
44      enabledPluginsGroup = localPluginsListView.Groups["activePluginsGroup"];
45      disabledPluginsGroup = localPluginsListView.Groups["disabledPluginsGroup"];
46    }
47
48    private IEnumerable<PluginDescription> plugins;
49    public IEnumerable<PluginDescription> Plugins {
50      get { return plugins; }
51      set {
52        if (value != plugins) {
53          this.plugins = value;
54          UpdateControl();
55        }
56      }
57    }
58
59    public IEnumerable<IPluginDescription> CheckedPlugins {
60      get {
61        return (from item in localPluginsListView.Items.OfType<ListViewItem>()
62                where item.Checked
63                let plugin = item.Tag as IPluginDescription
64                where plugin != null
65                select plugin).ToList();
66      }
67    }
68
69    private void UpdateControl() {
70      ClearPluginList();
71      localPluginsListView.SuppressItemCheckedEvents = true;
72      foreach (var plugin in plugins) {
73        var item = CreateListViewItem(plugin);
74        if (plugin.PluginState == PluginState.Enabled) {
75          item.Group = enabledPluginsGroup;
76        } else if (plugin.PluginState == PluginState.Disabled) {
77          item.Group = disabledPluginsGroup;
78        }
79        localPluginsListView.Items.Add(item);
80      }
81      localPluginsListView.SuppressItemCheckedEvents = false;
82    }
83
84    private void ClearPluginList() {
85      List<ListViewItem> itemsToRemove = new List<ListViewItem>(from item in localPluginsListView.Items.OfType<ListViewItem>()
86                                                                select item);
87      itemsToRemove.ForEach(item => localPluginsListView.Items.Remove(item));
88    }
89
90    private ListViewItem CreateListViewItem(PluginDescription plugin) {
91      ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), plugin.Description });
92      item.Tag = plugin;
93      return item;
94    }
95
96    private void pluginsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
97      // checked items are marked for removal
98      if (e.Item.Checked) {
99        List<ListViewItem> modifiedItems = new List<ListViewItem>();
100        foreach (ListViewItem item in localPluginsListView.SelectedItems) {
101          var plugin = (IPluginDescription)item.Tag;
102          modifiedItems.Add(item);
103          // also uncheck all dependent plugins
104          foreach (ListViewItem dependentItem in localPluginsListView.Items) {
105            var dependent = (IPluginDescription)dependentItem.Tag;
106            if (!dependentItem.Checked && (from dep in dependent.Dependencies
107                                           where dep.Name == plugin.Name
108                                           where dep.Version == plugin.Version
109                                           select dep).Any()) {
110              modifiedItems.Add(dependentItem);
111            }
112          }
113        }
114        localPluginsListView.CheckItems(modifiedItems);
115      } else {
116        List<ListViewItem> modifiedItems = new List<ListViewItem>();
117        foreach (ListViewItem item in localPluginsListView.SelectedItems) {
118          var plugin = (IPluginDescription)item.Tag;
119          modifiedItems.Add(item);
120        }
121        localPluginsListView.UncheckItems(modifiedItems);
122      }
123      OnItemChecked(e);
124    }
125
126    private void OnItemChecked(ItemCheckedEventArgs e) {
127      if (ItemChecked != null) ItemChecked(this, e);
128    }
129
130    private void localPluginsListView_ItemActivate(object sender, EventArgs e) {
131      if (localPluginsListView.SelectedItems.Count > 0) {
132        var plugin = (PluginDescription)localPluginsListView.SelectedItems[0].Tag;
133        PluginView pluginView = new PluginView(plugin);
134        pluginView.Show();
135      }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.