Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3068 was 3068, checked in by gkronber, 15 years ago

Worked on multi-select behavior of list views in deployment server interfaces. #891 (Refactor GUI for plugin management)

File size: 8.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      remotePluginsListView.SuppressItemCheckedEvents = true;
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      remotePluginsListView.SuppressItemCheckedEvents = false;
93    }
94
95    private void ClearListView() {
96      List<ListViewItem> itemsToDelete = new List<ListViewItem>(remotePluginsListView.Items.OfType<ListViewItem>());
97      itemsToDelete.ForEach(item => remotePluginsListView.Items.Remove(item));
98    }
99
100    private ListViewItem CreateListViewItem(DeploymentService.ProductDescription product) {
101      ListViewItem item = new ListViewItem(new string[] { product.Name, product.Version.ToString(), string.Empty });
102      item.Tag = product;
103      return item;
104    }
105
106    private ListViewItem CreateListViewItem(IPluginDescription plugin) {
107      ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), plugin.Description });
108      item.Tag = plugin;
109      return item;
110    }
111
112    #region item checked event handler
113    private void remotePluginsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
114      foreach (ListViewItem item in remotePluginsListView.SelectedItems) {
115        // dispatch by check state and type of item (product/plugin)
116        IPluginDescription plugin = item.Tag as IPluginDescription;
117        if (plugin != null)
118          if (e.Item.Checked)
119            HandlePluginChecked(plugin);
120          else
121            HandlePluginUnchecked(plugin);
122        else {
123          DeploymentService.ProductDescription product = item.Tag as DeploymentService.ProductDescription;
124          if (product != null)
125            if (e.Item.Checked)
126              HandleProductChecked(product);
127            else
128              HandleProductUnchecked(product);
129        }
130      }
131      OnItemChecked(e);
132    }
133
134    private void HandleProductUnchecked(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription product) {
135      // also uncheck the plugins of the product
136      List<ListViewItem> modifiedItems = new List<ListViewItem>();
137      modifiedItems.Add(FindItemForProduct(product));
138      foreach (var plugin in product.Plugins) {
139        var item = FindItemForPlugin(plugin);
140        if (item != null && item.Checked)
141          modifiedItems.Add(item);
142      }
143      remotePluginsListView.UncheckItems(modifiedItems);
144    }
145
146    private void HandleProductChecked(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription product) {
147      // also check all plugins of the product
148      List<ListViewItem> modifiedItems = new List<ListViewItem>();
149      modifiedItems.Add(FindItemForProduct(product));
150      foreach (var plugin in product.Plugins) {
151        var item = FindItemForPlugin(plugin);
152        if (item != null && !item.Checked) {
153          modifiedItems.Add(item);
154        }
155      }
156      remotePluginsListView.CheckItems(modifiedItems);
157    }
158
159    private void HandlePluginUnchecked(IPluginDescription plugin) {
160      // also uncheck all dependent plugins
161      List<ListViewItem> modifiedItems = new List<ListViewItem>();
162      modifiedItems.Add(FindItemForPlugin(plugin));
163      var dependentPlugins = from otherPlugin in plugins
164                             where otherPlugin.Dependencies.Any(dep => dep.Name == plugin.Name && dep.Version == plugin.Version)
165                             select otherPlugin;
166      foreach (var dependentPlugin in dependentPlugins) {
167        var item = FindItemForPlugin(dependentPlugin);
168        if (item != null && item.Checked) {
169          modifiedItems.Add(item);
170        }
171      }
172      // also uncheck all products containing this plugin
173      var dependentProducts = from product in products
174                              where product.Plugins.Any(p => p.Name == plugin.Name && p.Version == plugin.Version)
175                              select product;
176      foreach (var dependentProduct in dependentProducts) {
177        var item = FindItemForProduct(dependentProduct);
178        if (item != null && item.Checked) {
179          modifiedItems.Add(item);
180        }
181      }
182      remotePluginsListView.UncheckItems(modifiedItems);
183    }
184
185    private void HandlePluginChecked(IPluginDescription plugin) {
186      // also check all dependencies
187      List<ListViewItem> modifiedItems = new List<ListViewItem>();
188      modifiedItems.Add(FindItemForPlugin(plugin));
189      foreach (var dep in plugin.Dependencies) {
190        var item = FindItemForPlugin(dep);
191        if (item != null && !item.Checked) {
192          modifiedItems.Add(item);
193        }
194      }
195      remotePluginsListView.CheckItems(modifiedItems);
196    }
197
198    private void OnItemChecked(ItemCheckedEventArgs e) {
199      if (ItemChecked != null) ItemChecked(this, e);
200    }
201    #endregion
202
203    #region helper methods
204    private ListViewItem FindItemForPlugin(IPluginDescription plugin) {
205      return (from item in remotePluginsListView.Items.OfType<ListViewItem>()
206              let otherPlugin = item.Tag as IPluginDescription
207              where otherPlugin != null && otherPlugin.Name == plugin.Name && otherPlugin.Version == plugin.Version
208              select item).SingleOrDefault();
209    }
210
211    private ListViewItem FindItemForProduct(DeploymentService.ProductDescription product) {
212      return (from item in remotePluginsListView.Items.OfType<ListViewItem>()
213              let otherProduct = item.Tag as DeploymentService.ProductDescription
214              where otherProduct != null && otherProduct.Name == product.Name && otherProduct.Version == product.Version
215              select item).SingleOrDefault();
216    }
217
218    #endregion
219  }
220}
Note: See TracBrowser for help on using the repository browser.