Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Advanced/EditProductsView.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 17.9 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
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Linq;
26using System.ServiceModel;
27using System.Windows.Forms;
28
29namespace HeuristicLab.PluginInfrastructure.Advanced {
30  internal partial class EditProductsView : InstallationManagerControl {
31    private const string RefreshMessage = "Downloading product and plugin information...";
32    private const string UploadMessage = "Uploading product and plugin information...";
33    private const string DeleteProductMessage = "Deleting product...";
34
35    private BackgroundWorker refreshProductsWorker;
36    private BackgroundWorker uploadChangedProductsWorker;
37    private BackgroundWorker deleteProductWorker;
38
39    private List<DeploymentService.ProductDescription> products;
40    private List<DeploymentService.PluginDescription> plugins;
41    private HashSet<DeploymentService.ProductDescription> dirtyProducts;
42
43    public EditProductsView() {
44      InitializeComponent();
45
46      productImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.Setup_Install);
47      productImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.ArrowUp);
48      pluginImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.Plugin);
49
50      dirtyProducts = new HashSet<DeploymentService.ProductDescription>();
51      refreshProductsWorker = new BackgroundWorker();
52      refreshProductsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(refreshProductsWorker_RunWorkerCompleted);
53      refreshProductsWorker.DoWork += new DoWorkEventHandler(refreshProductsWorker_DoWork);
54
55      uploadChangedProductsWorker = new BackgroundWorker();
56      uploadChangedProductsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(uploadChangedProductsWorker_RunWorkerCompleted);
57      uploadChangedProductsWorker.DoWork += new DoWorkEventHandler(uploadChangedProductsWorker_DoWork);
58
59      deleteProductWorker = new BackgroundWorker();
60      deleteProductWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(deleteProductWorker_RunWorkerCompleted);
61      deleteProductWorker.DoWork += new DoWorkEventHandler(deleteProductWorker_DoWork);
62    }
63
64    #region event handlers for delete product background worker
65    void deleteProductWorker_DoWork(object sender, DoWorkEventArgs e) {
66      var products = (IEnumerable<DeploymentService.ProductDescription>)e.Argument;
67      var adminClient = DeploymentService.AdminClientFactory.CreateClient();
68      // upload
69      try {
70        foreach (var product in products) {
71          adminClient.DeleteProduct(product);
72        }
73        adminClient.Close();
74      }
75      catch (TimeoutException) {
76        adminClient.Abort();
77        throw;
78      }
79      catch (FaultException) {
80        adminClient.Abort();
81        throw;
82      }
83      catch (CommunicationException) {
84        adminClient.Abort();
85        throw;
86      }
87      // refresh     
88      var updateClient = DeploymentService.UpdateClientFactory.CreateClient();
89      try {
90        e.Result = new object[] { updateClient.GetProducts(), updateClient.GetPlugins() };
91        updateClient.Close();
92      }
93      catch (TimeoutException) {
94        updateClient.Abort();
95        throw;
96      }
97      catch (FaultException) {
98        updateClient.Abort();
99        throw;
100      }
101      catch (CommunicationException) {
102        updateClient.Abort();
103        throw;
104      }
105    }
106
107    void deleteProductWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
108      if (e.Error != null) {
109        StatusView.ShowError("Connection Error",
110        "There was an error while connecting to the server." + Environment.NewLine +
111           "Please check your connection settings and user credentials.");
112        this.products.Clear();
113        this.plugins.Clear();
114      } else {
115        this.products = new List<DeploymentService.ProductDescription>(
116  (DeploymentService.ProductDescription[])((object[])e.Result)[0]);
117        this.plugins = new List<DeploymentService.PluginDescription>(
118          (DeploymentService.PluginDescription[])((object[])e.Result)[1]);
119
120        EnableControls();
121      }
122      UpdateProductsList();
123      dirtyProducts.Clear();
124      StatusView.HideProgressIndicator();
125      StatusView.RemoveMessage(DeleteProductMessage);
126      StatusView.UnlockUI();
127    }
128    #endregion
129
130    #region event handlers for upload products background worker
131    private void uploadChangedProductsWorker_DoWork(object sender, DoWorkEventArgs e) {
132      var products = (IEnumerable<DeploymentService.ProductDescription>)e.Argument;
133      var adminClient = DeploymentService.AdminClientFactory.CreateClient();
134      // upload
135      try {
136        foreach (var product in products) {
137          adminClient.DeployProduct(product);
138        }
139        adminClient.Close();
140      }
141      catch (TimeoutException) {
142        adminClient.Abort();
143        throw;
144      }
145      catch (FaultException) {
146        adminClient.Abort();
147        throw;
148      }
149      catch (CommunicationException) {
150        adminClient.Abort();
151        throw;
152      }
153      // refresh     
154      var updateClient = DeploymentService.UpdateClientFactory.CreateClient();
155      try {
156        e.Result = new object[] { updateClient.GetProducts(), updateClient.GetPlugins() };
157        updateClient.Close();
158      }
159      catch (TimeoutException) {
160        updateClient.Abort();
161        throw;
162      }
163      catch (FaultException) {
164        updateClient.Abort();
165        throw;
166      }
167      catch (CommunicationException) {
168        updateClient.Abort();
169        throw;
170      }
171    }
172
173    private void uploadChangedProductsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
174      if (e.Error != null) {
175        StatusView.ShowError("Connection Error",
176        "There was an error while connecting to the server." + Environment.NewLine +
177           "Please check your connection settings and user credentials.");
178        this.products.Clear();
179        this.plugins.Clear();
180      } else {
181        this.products = new List<DeploymentService.ProductDescription>(
182  (DeploymentService.ProductDescription[])((object[])e.Result)[0]);
183        this.plugins = new List<DeploymentService.PluginDescription>(
184          (DeploymentService.PluginDescription[])((object[])e.Result)[1]);
185
186      }
187      UpdateProductsList();
188      dirtyProducts.Clear();
189      EnableControls();
190      StatusView.HideProgressIndicator();
191      StatusView.RemoveMessage(UploadMessage);
192      StatusView.UnlockUI();
193    }
194    #endregion
195
196    #region event handlers for refresh products background worker
197    private void refreshProductsWorker_DoWork(object sender, DoWorkEventArgs e) {
198      var updateClient = DeploymentService.UpdateClientFactory.CreateClient();
199      try {
200        e.Result = new object[] { updateClient.GetProducts(), updateClient.GetPlugins() };
201        updateClient.Close();
202      }
203      catch (TimeoutException) {
204        updateClient.Abort();
205        throw;
206      }
207      catch (FaultException) {
208        updateClient.Abort();
209        throw;
210      }
211      catch (CommunicationException) {
212        updateClient.Abort();
213        throw;
214      }
215    }
216
217    private void refreshProductsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
218      if (e.Error != null) {
219        StatusView.ShowError("Connection Error",
220          "There was an error while connecting to the server." + Environment.NewLine +
221                   "Please check your connection settings and user credentials.");
222        this.products.Clear();
223        this.plugins.Clear();
224
225      } else {
226        this.products = new List<DeploymentService.ProductDescription>(
227          (DeploymentService.ProductDescription[])((object[])e.Result)[0]);
228        this.plugins = new List<DeploymentService.PluginDescription>(
229          (DeploymentService.PluginDescription[])((object[])e.Result)[1]);
230      }
231      UpdateProductsList();
232      dirtyProducts.Clear();
233      EnableControls();
234      StatusView.HideProgressIndicator();
235      StatusView.RemoveMessage(RefreshMessage);
236      StatusView.UnlockUI();
237    }
238    #endregion
239
240    private void UpdateProductsList() {
241      productsListView.SelectedItems.Clear();
242      productsListView.Items.Clear();
243      foreach (var prodDesc in products) {
244        productsListView.Items.Add(CreateListViewItem(prodDesc));
245      }
246      Util.ResizeColumns(productsListView.Columns.OfType<ColumnHeader>());
247    }
248
249    private void productsListBox_SelectedIndexChanged(object sender, EventArgs e) {
250      bool productSelected = productsListView.SelectedItems.Count > 0;
251      detailsGroupBox.Enabled = productSelected;
252      UpdateProductButtons();
253      if (productSelected) {
254        DeploymentService.ProductDescription activeProduct = (DeploymentService.ProductDescription)((ListViewItem)productsListView.SelectedItems[0]).Tag;
255        nameTextBox.Text = activeProduct.Name;
256        versionTextBox.Text = activeProduct.Version.ToString();
257
258        // populate plugins list view
259        ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
260        pluginListView.SuppressItemCheckedEvents = true;
261        foreach (var plugin in plugins.OfType<IPluginDescription>()) {
262          pluginListView.Items.Add(CreateListViewItem(plugin));
263        }
264        pluginListView.SuppressItemCheckedEvents = false;
265        foreach (var plugin in activeProduct.Plugins) {
266          pluginListView.CheckItems(FindItemsForPlugin(plugin));
267        }
268      } else {
269        nameTextBox.Text = string.Empty;
270        versionTextBox.Text = string.Empty;
271        pluginListView.Items.Clear();
272      }
273      Util.ResizeColumns(pluginListView.Columns.OfType<ColumnHeader>());
274    }
275
276    private void UpdateProductButtons() {
277      uploadButton.Enabled = dirtyProducts.Count > 0;
278      if (productsListView.SelectedItems.Count > 0) {
279        var selectedProduct = (DeploymentService.ProductDescription)productsListView.SelectedItems[0].Tag;
280        deleteProductButton.Enabled = !dirtyProducts.Contains(selectedProduct);
281      } else {
282        deleteProductButton.Enabled = false;
283      }
284    }
285
286
287    #region button event handlers
288    private void newProductButton_Click(object sender, EventArgs e) {
289      var newProduct = new DeploymentService.ProductDescription("New product", new Version("0.0.0.0"));
290      products.Add(newProduct);
291      UpdateProductsList();
292      MarkProductDirty(newProduct);
293    }
294
295    private void saveButton_Click(object sender, EventArgs e) {
296      StatusView.LockUI();
297      StatusView.ShowProgressIndicator();
298      StatusView.ShowMessage(UploadMessage);
299      uploadChangedProductsWorker.RunWorkerAsync(dirtyProducts);
300    }
301    private void refreshButton_Click(object sender, EventArgs e) {
302      StatusView.LockUI();
303      StatusView.ShowProgressIndicator();
304      StatusView.ShowMessage(RefreshMessage);
305      refreshProductsWorker.RunWorkerAsync();
306    }
307    private void deleteProductButton_Click(object sender, EventArgs e) {
308      StatusView.LockUI();
309      StatusView.ShowProgressIndicator();
310      StatusView.ShowMessage(DeleteProductMessage);
311      var selectedProducts = from item in productsListView.SelectedItems.OfType<ListViewItem>()
312                             select (DeploymentService.ProductDescription)item.Tag;
313      deleteProductWorker.RunWorkerAsync(selectedProducts.ToList());
314    }
315
316    #endregion
317
318    #region textbox changed event handlers
319    private void nameTextBox_TextChanged(object sender, EventArgs e) {
320      if (productsListView.SelectedItems.Count > 0) {
321        ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
322        DeploymentService.ProductDescription activeProduct = (DeploymentService.ProductDescription)activeItem.Tag;
323        if (string.IsNullOrEmpty(nameTextBox.Name)) {
324          errorProvider.SetError(nameTextBox, "Invalid value");
325        } else {
326          if (activeProduct.Name != nameTextBox.Text) {
327            activeProduct.Name = nameTextBox.Text;
328            activeItem.SubItems[0].Text = activeProduct.Name;
329            errorProvider.SetError(nameTextBox, string.Empty);
330            MarkProductDirty(activeProduct);
331          }
332        }
333      }
334    }
335
336
337    private void versionTextBox_TextChanged(object sender, EventArgs e) {
338      if (productsListView.SelectedItems.Count > 0) {
339        ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
340        DeploymentService.ProductDescription activeProduct = (DeploymentService.ProductDescription)activeItem.Tag;
341        try {
342          var newVersion = new Version(versionTextBox.Text);
343          if (activeProduct.Version != newVersion) {
344            activeProduct.Version = newVersion;
345            activeItem.SubItems[1].Text = versionTextBox.Text;
346            errorProvider.SetError(versionTextBox, string.Empty);
347            MarkProductDirty(activeProduct);
348          }
349        }
350        catch (OverflowException) {
351          errorProvider.SetError(versionTextBox, "Invalid value");
352        }
353
354        catch (ArgumentException) {
355          errorProvider.SetError(versionTextBox, "Invalid value");
356        }
357        catch (FormatException) {
358          errorProvider.SetError(versionTextBox, "Invalid value");
359        }
360      }
361    }
362    #endregion
363
364
365    #region plugin list view
366    private void OnItemChecked(ItemCheckedEventArgs e) {
367      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
368      DeploymentService.ProductDescription activeProduct = (DeploymentService.ProductDescription)activeItem.Tag;
369      activeProduct.Plugins = (from item in pluginListView.CheckedItems.OfType<ListViewItem>()
370                               select (DeploymentService.PluginDescription)item.Tag).ToArray();
371      MarkProductDirty(activeProduct);
372    }
373
374    private void listView_ItemChecked(object sender, ItemCheckedEventArgs e) {
375      List<IPluginDescription> modifiedPlugins = new List<IPluginDescription>();
376      if (e.Item.Checked) {
377        foreach (ListViewItem item in pluginListView.SelectedItems) {
378          var plugin = (IPluginDescription)item.Tag;
379          // also check all dependencies
380          if (!modifiedPlugins.Contains(plugin))
381            modifiedPlugins.Add(plugin);
382          foreach (var dep in Util.GetAllDependencies(plugin)) {
383            if (!modifiedPlugins.Contains(dep))
384              modifiedPlugins.Add(dep);
385          }
386        }
387        pluginListView.CheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
388        OnItemChecked(e);
389      } else {
390        foreach (ListViewItem item in pluginListView.SelectedItems) {
391          var plugin = (IPluginDescription)item.Tag;
392          // also uncheck all dependent plugins
393          if (!modifiedPlugins.Contains(plugin))
394            modifiedPlugins.Add(plugin);
395          foreach (var dep in Util.GetAllDependents(plugin, plugins.Cast<IPluginDescription>())) {
396            if (!modifiedPlugins.Contains(dep))
397              modifiedPlugins.Add(dep);
398          }
399
400        }
401        pluginListView.UncheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
402        OnItemChecked(e);
403      }
404    }
405
406
407    #endregion
408
409    #region helper
410    private void MarkProductDirty(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription activeProduct) {
411      if (!dirtyProducts.Contains(activeProduct)) {
412        dirtyProducts.Add(activeProduct);
413        var item = FindItemForProduct(activeProduct);
414        item.ImageIndex = 1;
415        UpdateProductButtons();
416      }
417    }
418    private ListViewItem CreateListViewItem(DeploymentService.ProductDescription productDescription) {
419      ListViewItem item = new ListViewItem(new string[] { productDescription.Name, productDescription.Version.ToString() });
420      item.Tag = productDescription;
421      item.ImageIndex = 0;
422      return item;
423    }
424
425    private ListViewItem CreateListViewItem(IPluginDescription plugin) {
426      ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(),
427          string.Empty, plugin.Description });
428      item.Tag = plugin;
429      item.ImageIndex = 0;
430      item.Checked = false;
431      return item;
432    }
433
434    private ListViewItem FindItemForProduct(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription activeProduct) {
435      return (from item in productsListView.Items.OfType<ListViewItem>()
436              let product = item.Tag as DeploymentService.ProductDescription
437              where product != null
438              where product == activeProduct
439              select item).Single();
440    }
441    private IEnumerable<ListViewItem> FindItemsForPlugin(IPluginDescription plugin) {
442      return from item in pluginListView.Items.OfType<ListViewItem>()
443             let p = item.Tag as IPluginDescription
444             where p.Name == plugin.Name
445             where p.Version == plugin.Version
446             select item;
447    }
448
449    private void EnableControls() {
450      newProductButton.Enabled = true;
451      productsListView.Enabled = true;
452    }
453    #endregion
454
455  }
456}
Note: See TracBrowser for help on using the repository browser.