Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.PluginInfrastructure/Advanced/ProductEditor.cs @ 9335

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

Implemented review comments in plugin manager. #989 (Implement review comments in plugin infrastructure)

File size: 10.6 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      foreach (var product in products) {
63        adminClient.DeployProduct(product);
64      }
65    }
66
67    private void uploadChangedProductsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
68      if (e.Error != null) {
69        MessageBox.Show("There was an error while connecting to the server." + Environment.NewLine +
70           "Please check your connection settings and user credentials.");
71      } else {
72        this.Enabled = true;
73        refreshProductsWorker.RunWorkerAsync();
74      }
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      e.Result = new object[] { updateClient.GetProducts(), updateClient.GetPlugins() };
82    }
83
84    private void refreshProductsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
85      if (e.Error != null) {
86        MessageBox.Show("There was an error while connecting to the server." + Environment.NewLine +
87                   "Please check your connection settings and user credentials.");
88      } else {
89        this.products = new List<DeploymentService.ProductDescription>(
90          (DeploymentService.ProductDescription[])((object[])e.Result)[0]);
91        this.plugins = new List<DeploymentService.PluginDescription>(
92          (DeploymentService.PluginDescription[])((object[])e.Result)[1]);
93
94        UpdateProductsList();
95        dirtyProducts.Clear();
96
97        Cursor = Cursors.Default;
98        SetControlsEnabled(true);
99      }
100    }
101    #endregion
102
103    private void UpdateProductsList() {
104      productsListView.Items.Clear();
105      foreach (var prodDesc in products) {
106        productsListView.Items.Add(CreateListViewItem(prodDesc));
107      }
108      foreach (ColumnHeader column in productsListView.Columns)
109        column.Width = -1;
110    }
111
112    private void productsListBox_SelectedIndexChanged(object sender, EventArgs e) {
113      bool productSelected = productsListView.SelectedItems.Count > 0;
114      detailsGroupBox.Enabled = productSelected;
115      uploadButton.Enabled = productSelected;
116      if (productsListView.SelectedItems.Count == 0) {
117        ClearProductDetails();
118        return;
119      }
120      DeploymentService.ProductDescription activeProduct = (DeploymentService.ProductDescription)((ListViewItem)productsListView.SelectedItems[0]).Tag;
121      UpdateProductDetails(activeProduct);
122    }
123
124    private void ClearProductDetails() {
125      nameTextBox.Text = string.Empty;
126      versionTextBox.Text = string.Empty;
127      pluginListView.Plugins = new IPluginDescription[0];
128    }
129
130    private void UpdateProductDetails(DeploymentService.ProductDescription activeProduct) {
131      nameTextBox.Text = activeProduct.Name;
132      versionTextBox.Text = activeProduct.Version.ToString();
133
134      UpdatePluginsListView();
135    }
136
137    private ListViewItem CreateListViewItem(DeploymentService.ProductDescription productDescription) {
138      ListViewItem item = new ListViewItem(new string[] { productDescription.Name, productDescription.Version.ToString() });
139      item.Tag = productDescription;
140      item.ImageIndex = 0;
141      return item;
142    }
143
144    private void SetControlsEnabled(bool enabled) {
145      uploadButton.Enabled = enabled;
146      refreshButton.Enabled = enabled;
147      newProductButton.Enabled = enabled;
148      splitContainer.Enabled = enabled;
149      productsListView.Enabled = enabled;
150      nameLabel.Enabled = enabled;
151      nameTextBox.Enabled = enabled;
152      versionLabel.Enabled = enabled;
153      versionTextBox.Enabled = enabled;
154      pluginListView.Enabled = enabled;
155    }
156
157    #region button event handlers
158    private void newProductButton_Click(object sender, EventArgs e) {
159      var newProduct = new DeploymentService.ProductDescription("New product", new Version("0.0.0.0"));
160      ListViewItem item = CreateListViewItem(newProduct);
161      productsListView.Items.Add(item);
162      MarkProductDirty(newProduct);
163    }
164
165    private void saveButton_Click(object sender, EventArgs e) {
166      this.Enabled = false;
167      uploadChangedProductsWorker.RunWorkerAsync(dirtyProducts);
168    }
169    private void refreshButton_Click(object sender, EventArgs e) {
170      SetControlsEnabled(false);
171      Cursor = Cursors.AppStarting;
172      refreshProductsWorker.RunWorkerAsync();
173    }
174
175    #endregion
176
177    #region textbox changed event handlers
178    private void nameTextBox_TextChanged(object sender, EventArgs e) {
179      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
180      DeploymentService.ProductDescription activeProduct = (DeploymentService.ProductDescription)activeItem.Tag;
181      if (string.IsNullOrEmpty(nameTextBox.Name)) {
182        errorProvider.SetError(nameTextBox, "Invalid value");
183      } else {
184        if (activeProduct.Name != nameTextBox.Text) {
185          activeProduct.Name = nameTextBox.Text;
186          activeItem.SubItems[0].Text = activeProduct.Name;
187          errorProvider.SetError(nameTextBox, string.Empty);
188          MarkProductDirty(activeProduct);
189        }
190      }
191    }
192
193
194    private void versionTextBox_TextChanged(object sender, EventArgs e) {
195      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
196      DeploymentService.ProductDescription activeProduct = (DeploymentService.ProductDescription)activeItem.Tag;
197      try {
198        var newVersion = new Version(versionTextBox.Text);
199        if (activeProduct.Version != newVersion) {
200          activeProduct.Version = newVersion;
201          activeItem.SubItems[1].Text = versionTextBox.Text;
202          errorProvider.SetError(versionTextBox, string.Empty);
203          MarkProductDirty(activeProduct);
204        }
205      }
206      catch (OverflowException) {
207        errorProvider.SetError(versionTextBox, "Invalid value");
208      }
209
210      catch (ArgumentException) {
211        errorProvider.SetError(versionTextBox, "Invalid value");
212      }
213      catch (FormatException) {
214        errorProvider.SetError(versionTextBox, "Invalid value");
215      }
216    }
217    #endregion
218
219
220    #region plugin list view
221    private void UpdatePluginsListView() {
222      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
223      DeploymentService.ProductDescription activeProduct = (DeploymentService.ProductDescription)activeItem.Tag;
224      pluginListView.Plugins = plugins.OfType<IPluginDescription>();
225      foreach (var plugin in activeProduct.Plugins) pluginListView.CheckPlugin(plugin);
226    }
227
228    private void pluginListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
229      ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
230      DeploymentService.ProductDescription activeProduct = (DeploymentService.ProductDescription)activeItem.Tag;
231      activeProduct.Plugins = pluginListView.CheckedPlugins.Cast<DeploymentService.PluginDescription>().ToArray();
232      MarkProductDirty(activeProduct);
233    }
234    #endregion
235
236    private void MarkProductDirty(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription activeProduct) {
237      if (!dirtyProducts.Contains(activeProduct)) {
238        dirtyProducts.Add(activeProduct);
239        var item = FindItemForProduct(activeProduct);
240        item.ImageIndex = 1;
241      }
242    }
243    private ListViewItem FindItemForProduct(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription activeProduct) {
244      return (from item in productsListView.Items.OfType<ListViewItem>()
245              let product = item.Tag as DeploymentService.ProductDescription
246              where product != null
247              where product == activeProduct
248              select item).Single();
249    }
250  }
251}
Note: See TracBrowser for help on using the repository browser.