Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Advanced/ProductEditor.cs @ 3509

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

Incorporated review comments by swagner into plugin infrastructure. #989 (Implement review comments in plugin infrastructure)

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