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
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.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.MainForm;
31using PluginDeploymentService = HeuristicLab.PluginInfrastructure.Advanced.DeploymentService;
32using System.ServiceModel;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.PluginAdministrator {
36  internal partial class ProductEditor : HeuristicLab.MainForm.WindowsForms.View {
37    private BackgroundWorker refreshProductsWorker;
38    private BackgroundWorker uploadChangedProductsWorker;
39    private List<PluginDeploymentService.ProductDescription> products;
40    private List<PluginDeploymentService.PluginDescription> plugins;
41    private HashSet<PluginDeploymentService.ProductDescription> dirtyProducts;
42
43    public ProductEditor() {
44      InitializeComponent();
45      Caption = "Products";
46
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
51      dirtyProducts = new HashSet<PluginDeploymentService.ProductDescription>();
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
61    #region event handlers for upload products background worker
62    private void uploadChangedProductsWorker_DoWork(object sender, DoWorkEventArgs e) {
63      var products = (IEnumerable<PluginDeploymentService.ProductDescription>)e.Argument;
64      var adminClient = PluginDeploymentService.AdminClientFactory.CreateClient();
65      try {
66        foreach (var product in products) {
67          adminClient.DeployProduct(product);
68        }
69        e.Cancel = false;
70      }
71      catch (FaultException) {
72      }
73    }
74
75    private void uploadChangedProductsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
76      this.Enabled = true;
77      refreshProductsWorker.RunWorkerAsync();
78    }
79    #endregion
80
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      }
90    }
91
92    private void refreshProductsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
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]);
98
99        UpdateProductsList();
100        dirtyProducts.Clear();
101
102        Cursor = Cursors.Default;
103        SetControlsEnabled(true);
104      }
105    }
106    #endregion
107
108    private void UpdateProductsList() {
109      productsListView.Items.Clear();
110      foreach (var prodDesc in products) {
111        productsListView.Items.Add(CreateListViewItem(prodDesc));
112      }
113      foreach (ColumnHeader column in productsListView.Columns)
114        column.Width = -1;
115    }
116
117    private void productsListBox_SelectedIndexChanged(object sender, EventArgs e) {
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      }
125      PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)((ListViewItem)productsListView.SelectedItems[0]).Tag;
126      UpdateProductDetails(activeProduct);
127    }
128
129    private void ClearProductDetails() {
130      nameTextBox.Text = string.Empty;
131      versionTextBox.Text = string.Empty;
132      pluginListView.Plugins = new IPluginDescription[0];
133    }
134
135    private void UpdateProductDetails(PluginDeploymentService.ProductDescription activeProduct) {
136      nameTextBox.Text = activeProduct.Name;
137      versionTextBox.Text = activeProduct.Version.ToString();
138
139      UpdatePluginsListView();
140    }
141
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
149    private void SetControlsEnabled(bool enabled) {
150      uploadButton.Enabled = enabled;
151      refreshButton.Enabled = enabled;
152      newProductButton.Enabled = enabled;
153      splitContainer.Enabled = enabled;
154      productsListView.Enabled = enabled;
155      nameLabel.Enabled = enabled;
156      nameTextBox.Enabled = enabled;
157      versionLabel.Enabled = enabled;
158      versionTextBox.Enabled = enabled;
159      pluginListView.Enabled = enabled;
160    }
161
162    #region button event handlers
163    private void newProductButton_Click(object sender, EventArgs e) {
164      var newProduct = new PluginDeploymentService.ProductDescription("New product", new Version("0.0.0.0"));
165      ListViewItem item = CreateListViewItem(newProduct);
166      productsListView.Items.Add(item);
167      MarkProductDirty(newProduct);
168    }
169
170    private void saveButton_Click(object sender, EventArgs e) {
171      this.Enabled = false;
172      uploadChangedProductsWorker.RunWorkerAsync(dirtyProducts);
173    }
174    private void refreshButton_Click(object sender, EventArgs e) {
175      SetControlsEnabled(false);
176      Cursor = Cursors.AppStarting;
177      refreshProductsWorker.RunWorkerAsync();
178    }
179
180    #endregion
181
182    #region textbox changed event handlers
183    private void nameTextBox_TextChanged(object sender, EventArgs e) {
184      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
185      PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)activeItem.Tag;
186      if (string.IsNullOrEmpty(nameTextBox.Name)) {
187        errorProvider.SetError(nameTextBox, "Invalid value");
188      } else {
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        }
195      }
196    }
197
198
199    private void versionTextBox_TextChanged(object sender, EventArgs e) {
200      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
201      PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)activeItem.Tag;
202      try {
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        }
210      }
211      catch (OverflowException) {
212        errorProvider.SetError(versionTextBox, "Invalid value");
213      }
214
215      catch (ArgumentException) {
216        errorProvider.SetError(versionTextBox, "Invalid value");
217      }
218      catch (FormatException) {
219        errorProvider.SetError(versionTextBox, "Invalid value");
220      }
221    }
222    #endregion
223
224
225    #region plugin list view
226    private void UpdatePluginsListView() {
227      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
228      PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)activeItem.Tag;
229      pluginListView.Plugins = plugins.OfType<IPluginDescription>();
230      foreach (var plugin in activeProduct.Plugins) pluginListView.CheckPlugin(plugin);
231    }
232
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
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    }
255  }
256}
Note: See TracBrowser for help on using the repository browser.