Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Advanced/RemotePluginInstaller.cs @ 3016

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

Worked on GUI for plugin management. #891 (Refactor GUI for plugin management)

File size: 7.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9
10namespace HeuristicLab.PluginInfrastructure.Advanced {
11  public partial class RemotePluginInstaller : UserControl {
12    private ListViewGroup newPluginsGroup;
13    private ListViewGroup productsGroup;
14    private ListViewGroup allPluginsGroup;
15
16    public event ItemCheckedEventHandler ItemChecked;
17
18    public RemotePluginInstaller() {
19      InitializeComponent();
20
21      imageListForRemoteItems.Images.Add(HeuristicLab.PluginInfrastructure.Resources.Resources.Assembly);
22      imageListForRemoteItems.Images.Add(HeuristicLab.PluginInfrastructure.Resources.Resources.Install);
23
24      newPluginsGroup = remotePluginsListView.Groups["newPluginsGroup"];
25      productsGroup = remotePluginsListView.Groups["productsGroup"];
26      allPluginsGroup = remotePluginsListView.Groups["allPluginsGroup"];
27    }
28
29    private IEnumerable<DeploymentService.ProductDescription> products;
30    public IEnumerable<DeploymentService.ProductDescription> Products {
31      get { return products ?? Enumerable.Empty<DeploymentService.ProductDescription>(); }
32      set {
33        if (value != this.products) {
34          this.products = value;
35          UpdateControl();
36        }
37      }
38    }
39
40    private IEnumerable<IPluginDescription> plugins;
41    public IEnumerable<IPluginDescription> AllPlugins {
42      get { return plugins ?? Enumerable.Empty<IPluginDescription>(); }
43      set {
44        if (value != this.plugins) {
45          this.plugins = value;
46          UpdateControl();
47        }
48      }
49    }
50
51    private IEnumerable<IPluginDescription> newPlugins;
52    public IEnumerable<IPluginDescription> NewPlugins {
53      get { return newPlugins ?? Enumerable.Empty<IPluginDescription>(); }
54      set {
55        if (value != this.newPlugins) {
56          this.newPlugins = value;
57          UpdateControl();
58        }
59      }
60    }
61
62    public IEnumerable<IPluginDescription> CheckedPlugins {
63      get {
64        return (from item in remotePluginsListView.Items.OfType<ListViewItem>()
65                where item.Checked
66                let plugin = item.Tag as IPluginDescription
67                where plugin != null
68                select plugin).ToList();
69      }
70    }
71
72    private void UpdateControl() {
73      ClearListView();
74
75      foreach (var newPlugin in NewPlugins) {
76        var item = CreateListViewItem(newPlugin);
77        item.Group = newPluginsGroup;
78        remotePluginsListView.Items.Add(item);
79      }
80
81      foreach (var product in Products) {
82        var item = CreateListViewItem(product);
83        item.Group = productsGroup;
84        remotePluginsListView.Items.Add(item);
85      }
86
87      foreach (var plugin in AllPlugins) {
88        var item = CreateListViewItem(plugin);
89        item.Group = allPluginsGroup;
90        remotePluginsListView.Items.Add(item);
91      }
92    }
93
94    private void ClearListView() {
95      List<ListViewItem> itemsToDelete = new List<ListViewItem>(remotePluginsListView.Items.OfType<ListViewItem>());
96      itemsToDelete.ForEach(item => remotePluginsListView.Items.Remove(item));
97    }
98
99    private ListViewItem CreateListViewItem(DeploymentService.ProductDescription product) {
100      ListViewItem item = new ListViewItem(new string[] { product.Name, product.Version.ToString(), string.Empty });
101      item.Tag = product;
102      return item;
103    }
104
105    private ListViewItem CreateListViewItem(IPluginDescription plugin) {
106      ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), plugin.Description });
107      item.Tag = plugin;
108      return item;
109    }
110
111    #region item checked event handler
112    private void remotePluginsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
113      // dispatch by check state and type of item (product/plugin)
114      IPluginDescription plugin = e.Item.Tag as IPluginDescription;
115      if (plugin != null)
116        if (e.Item.Checked)
117          HandlePluginChecked(plugin);
118        else
119          HandlePluginUnchecked(plugin);
120      else {
121        DeploymentService.ProductDescription product = e.Item.Tag as DeploymentService.ProductDescription;
122        if (product != null)
123          if (e.Item.Checked)
124            HandleProductChecked(product);
125          else
126            HandleProductUnchecked(product);
127      }
128      OnItemChecked(e);
129    }
130
131    private void HandleProductUnchecked(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription product) {
132      // also uncheck the plugins of the product
133      foreach (var plugin in product.Plugins) {
134        var item = FindItemForPlugin(plugin);
135        if (item != null && item.Checked)
136          item.Checked = false;
137      }
138    }
139
140    private void HandleProductChecked(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription product) {
141      // also check all plugins of the product
142      foreach (var plugin in product.Plugins) {
143        var item = FindItemForPlugin(plugin);
144        if (item != null && !item.Checked) {
145          item.Checked = true;
146        }
147      }
148    }
149
150    private void HandlePluginUnchecked(IPluginDescription plugin) {
151      // also uncheck all dependent plugins
152      var dependentPlugins = from otherPlugin in plugins
153                             where otherPlugin.Dependencies.Any(dep => dep.Name == plugin.Name && dep.Version == plugin.Version)
154                             select otherPlugin;
155      foreach (var dependentPlugin in dependentPlugins) {
156        var item = FindItemForPlugin(dependentPlugin);
157        if (item != null && item.Checked) {
158          item.Checked = false;
159        }
160      }
161      // also uncheck all products containing this plugin
162      var dependentProducts = from product in products
163                              where product.Plugins.Any(p => p.Name == plugin.Name && p.Version == plugin.Version)
164                              select product;
165      foreach (var dependentProduct in dependentProducts) {
166        var item = FindItemForProduct(dependentProduct);
167        if (item != null && item.Checked) {
168          item.Checked = false;
169        }
170      }
171    }
172
173    private void HandlePluginChecked(IPluginDescription plugin) {
174      // also check all dependencies
175      foreach (var dep in plugin.Dependencies) {
176        var item = FindItemForPlugin(dep);
177        if (item != null && !item.Checked) {
178          item.Checked = true;
179        }
180      }
181    }
182
183    private void OnItemChecked(ItemCheckedEventArgs e) {
184      if (ItemChecked != null) ItemChecked(this, e);
185    }
186    #endregion
187
188    #region helper methods
189    private ListViewItem FindItemForPlugin(IPluginDescription plugin) {
190      return (from item in remotePluginsListView.Items.OfType<ListViewItem>()
191              let otherPlugin = item.Tag as IPluginDescription
192              where otherPlugin != null && otherPlugin.Name == plugin.Name && otherPlugin.Version == plugin.Version
193              select item).SingleOrDefault();
194    }
195
196    private ListViewItem FindItemForProduct(DeploymentService.ProductDescription product) {
197      return (from item in remotePluginsListView.Items.OfType<ListViewItem>()
198              let otherProduct = item.Tag as DeploymentService.ProductDescription
199              where otherProduct != null && otherProduct.Name == product.Name && otherProduct.Version == product.Version
200              select item).SingleOrDefault();
201    }
202
203    #endregion
204  }
205}
Note: See TracBrowser for help on using the repository browser.