Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginAdministrator/3.3/ProductEditor.cs @ 3352

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

Implemented changes in plugin administrator UI as requested by swagner. #949 (Overhaul look and feel of plugin administrator)

File size: 10.7 KB
RevLine 
[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
22using System;
[2802]23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.MainForm;
[2816]31using PluginDeploymentService = HeuristicLab.PluginInfrastructure.Advanced.DeploymentService;
[3015]32using System.ServiceModel;
33using HeuristicLab.PluginInfrastructure;
[2802]34
[3081]35namespace HeuristicLab.PluginAdministrator {
36  internal partial class ProductEditor : HeuristicLab.MainForm.WindowsForms.View {
[2802]37    private BackgroundWorker refreshProductsWorker;
38    private BackgroundWorker uploadChangedProductsWorker;
[2816]39    private List<PluginDeploymentService.ProductDescription> products;
40    private List<PluginDeploymentService.PluginDescription> plugins;
41    private HashSet<PluginDeploymentService.ProductDescription> dirtyProducts;
[2802]42
43    public ProductEditor() {
44      InitializeComponent();
45      Caption = "Products";
46
[3015]47      productImageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Assembly);
48      productImageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.ArrowUp);
49      pluginImageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Assembly);
50
[2816]51      dirtyProducts = new HashSet<PluginDeploymentService.ProductDescription>();
[2802]52      refreshProductsWorker = new BackgroundWorker();
53      refreshProductsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(refreshProductsWorker_RunWorkerCompleted);
54      refreshProductsWorker.DoWork += new DoWorkEventHandler(refreshProductsWorker_DoWork);
55
56      uploadChangedProductsWorker = new BackgroundWorker();
57      uploadChangedProductsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(uploadChangedProductsWorker_RunWorkerCompleted);
58      uploadChangedProductsWorker.DoWork += new DoWorkEventHandler(uploadChangedProductsWorker_DoWork);
59    }
60
[3015]61    #region event handlers for upload products background worker
62    private void uploadChangedProductsWorker_DoWork(object sender, DoWorkEventArgs e) {
[2816]63      var products = (IEnumerable<PluginDeploymentService.ProductDescription>)e.Argument;
[3015]64      var adminClient = PluginDeploymentService.AdminClientFactory.CreateClient();
65      try {
[2802]66        foreach (var product in products) {
67          adminClient.DeployProduct(product);
68        }
[3015]69        e.Cancel = false;
[2802]70      }
[3015]71      catch (FaultException) {
72      }
[2802]73    }
74
[3015]75    private void uploadChangedProductsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
[2802]76      this.Enabled = true;
[3015]77      refreshProductsWorker.RunWorkerAsync();
[2802]78    }
[3015]79    #endregion
[2802]80
[3015]81    #region event handlers for refresh products background worker
82    private void refreshProductsWorker_DoWork(object sender, DoWorkEventArgs e) {
83      var updateClient = PluginDeploymentService.UpdateClientFactory.CreateClient();
84      try {
85        e.Result = new object[] { updateClient.GetProducts(), updateClient.GetPlugins() };
86      }
87      catch (FaultException) {
88        e.Cancel = true;
89      }
[2802]90    }
91
[3015]92    private void refreshProductsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
[3179]93      if (!e.Cancelled && e.Result != null) {
94        this.products = new List<PluginDeploymentService.ProductDescription>(
95          (PluginDeploymentService.ProductDescription[])((object[])e.Result)[0]);
96        this.plugins = new List<PluginDeploymentService.PluginDescription>(
97          (PluginDeploymentService.PluginDescription[])((object[])e.Result)[1]);
[2802]98
[3179]99        UpdateProductsList();
100        dirtyProducts.Clear();
[2802]101
[3179]102        Cursor = Cursors.Default;
103        SetControlsEnabled(true);
104      }
[2802]105    }
[3015]106    #endregion
[2802]107
108    private void UpdateProductsList() {
[3015]109      productsListView.Items.Clear();
[2802]110      foreach (var prodDesc in products) {
[3015]111        productsListView.Items.Add(CreateListViewItem(prodDesc));
[2802]112      }
[3208]113      foreach (ColumnHeader column in productsListView.Columns)
114        column.Width = -1;
[2802]115    }
116
117    private void productsListBox_SelectedIndexChanged(object sender, EventArgs e) {
[3208]118      bool productSelected = productsListView.SelectedItems.Count > 0;
119      detailsGroupBox.Enabled = productSelected;
120      uploadButton.Enabled = productSelected;
121      if (productsListView.SelectedItems.Count == 0) {
122        ClearProductDetails();
123        return;
124      }
[3015]125      PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)((ListViewItem)productsListView.SelectedItems[0]).Tag;
[2802]126      UpdateProductDetails(activeProduct);
127    }
128
[3208]129    private void ClearProductDetails() {
130      nameTextBox.Text = string.Empty;
131      versionTextBox.Text = string.Empty;
132      pluginListView.Plugins = new IPluginDescription[0];
133    }
134
[2816]135    private void UpdateProductDetails(PluginDeploymentService.ProductDescription activeProduct) {
[2802]136      nameTextBox.Text = activeProduct.Name;
137      versionTextBox.Text = activeProduct.Version.ToString();
138
[3045]139      UpdatePluginsListView();
[2802]140    }
141
[3015]142    private ListViewItem CreateListViewItem(PluginDeploymentService.ProductDescription productDescription) {
143      ListViewItem item = new ListViewItem(new string[] { productDescription.Name, productDescription.Version.ToString() });
144      item.Tag = productDescription;
145      item.ImageIndex = 0;
146      return item;
147    }
148
[2802]149    private void SetControlsEnabled(bool enabled) {
[3208]150      uploadButton.Enabled = enabled;
[2802]151      refreshButton.Enabled = enabled;
[3045]152      newProductButton.Enabled = enabled;
[2802]153      splitContainer.Enabled = enabled;
[3179]154      productsListView.Enabled = enabled;
155      nameLabel.Enabled = enabled;
156      nameTextBox.Enabled = enabled;
157      versionLabel.Enabled = enabled;
158      versionTextBox.Enabled = enabled;
159      pluginListView.Enabled = enabled;
[2802]160    }
161
[3045]162    #region button event handlers
[2802]163    private void newProductButton_Click(object sender, EventArgs e) {
[2816]164      var newProduct = new PluginDeploymentService.ProductDescription("New product", new Version("0.0.0.0"));
[2802]165      ListViewItem item = CreateListViewItem(newProduct);
[3015]166      productsListView.Items.Add(item);
167      MarkProductDirty(newProduct);
[2802]168    }
169
170    private void saveButton_Click(object sender, EventArgs e) {
171      this.Enabled = false;
172      uploadChangedProductsWorker.RunWorkerAsync(dirtyProducts);
173    }
[3045]174    private void refreshButton_Click(object sender, EventArgs e) {
175      SetControlsEnabled(false);
176      Cursor = Cursors.AppStarting;
177      refreshProductsWorker.RunWorkerAsync();
178    }
[2802]179
[3045]180    #endregion
181
182    #region textbox changed event handlers
[2802]183    private void nameTextBox_TextChanged(object sender, EventArgs e) {
[3015]184      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
[2816]185      PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)activeItem.Tag;
[2802]186      if (string.IsNullOrEmpty(nameTextBox.Name)) {
187        errorProvider.SetError(nameTextBox, "Invalid value");
188      } else {
[3015]189        if (activeProduct.Name != nameTextBox.Text) {
190          activeProduct.Name = nameTextBox.Text;
191          activeItem.SubItems[0].Text = activeProduct.Name;
192          errorProvider.SetError(nameTextBox, string.Empty);
193          MarkProductDirty(activeProduct);
194        }
[2802]195      }
196    }
197
[3015]198
[2802]199    private void versionTextBox_TextChanged(object sender, EventArgs e) {
[3015]200      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
[2816]201      PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)activeItem.Tag;
[2802]202      try {
[3015]203        var newVersion = new Version(versionTextBox.Text);
204        if (activeProduct.Version != newVersion) {
205          activeProduct.Version = newVersion;
206          activeItem.SubItems[1].Text = versionTextBox.Text;
207          errorProvider.SetError(versionTextBox, string.Empty);
208          MarkProductDirty(activeProduct);
209        }
[2802]210      }
[3081]211      catch (OverflowException) {
[2802]212        errorProvider.SetError(versionTextBox, "Invalid value");
213      }
214
[3081]215      catch (ArgumentException) {
[2802]216        errorProvider.SetError(versionTextBox, "Invalid value");
217      }
[3081]218      catch (FormatException) {
[2802]219        errorProvider.SetError(versionTextBox, "Invalid value");
220      }
221    }
[3045]222    #endregion
[3015]223
[3045]224
225    #region plugin list view
226    private void UpdatePluginsListView() {
[3015]227      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
228      PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)activeItem.Tag;
[3045]229      pluginListView.Plugins = plugins.OfType<IPluginDescription>();
230      foreach (var plugin in activeProduct.Plugins) pluginListView.CheckPlugin(plugin);
[3015]231    }
232
[3045]233    private void pluginListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
234      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
235      PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)activeItem.Tag;
236      activeProduct.Plugins = pluginListView.CheckedPlugins.Cast<PluginDeploymentService.PluginDescription>().ToArray();
237      MarkProductDirty(activeProduct);
238    }
239    #endregion
240
[3015]241    private void MarkProductDirty(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription activeProduct) {
242      if (!dirtyProducts.Contains(activeProduct)) {
243        dirtyProducts.Add(activeProduct);
244        var item = FindItemForProduct(activeProduct);
245        item.ImageIndex = 1;
246      }
247    }
248    private ListViewItem FindItemForProduct(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription activeProduct) {
249      return (from item in productsListView.Items.OfType<ListViewItem>()
250              let product = item.Tag as PluginDeploymentService.ProductDescription
251              where product != null
252              where product == activeProduct
253              select item).Single();
254    }
[2802]255  }
256}
Note: See TracBrowser for help on using the repository browser.