Free cookie consent management tool by TermsFeed Policy Generator

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

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

Copied projects related to the deployment service from the feature branch. #918 (Integrate deployment service into trunk and HL3.3 solution file)

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