Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DeploymentServer Prototype/HeuristicLab.Services/HeuristicLab.DeploymentService.AdminClient/ProductEditor.cs @ 2802

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

Worked on administration front-end for deployment service. #860 (Deployment server for plugin installation from web locations)

File size: 6.4 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 HeuristicLab.PluginInfrastructure;
11using UpdateService = HeuristicLab.PluginInfrastructure.PluginUpdateService;
12
13namespace HeuristicLab.DeploymentService.AdminClient {
14  public partial class ProductEditor : HeuristicLab.MainForm.WindowsForms.View {
15    private BackgroundWorker refreshProductsWorker;
16    private BackgroundWorker uploadChangedProductsWorker;
17    private List<UpdateService.ProductDescription> products;
18    private List<UpdateService.PluginDescription> plugins;
19    private HashSet<UpdateService.ProductDescription> dirtyProducts;
20
21    public ProductEditor() {
22      InitializeComponent();
23      Caption = "Products";
24
25      dirtyProducts = new HashSet<UpdateService.ProductDescription>();
26      refreshProductsWorker = new BackgroundWorker();
27      refreshProductsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(refreshProductsWorker_RunWorkerCompleted);
28      refreshProductsWorker.DoWork += new DoWorkEventHandler(refreshProductsWorker_DoWork);
29
30      uploadChangedProductsWorker = new BackgroundWorker();
31      uploadChangedProductsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(uploadChangedProductsWorker_RunWorkerCompleted);
32      uploadChangedProductsWorker.DoWork += new DoWorkEventHandler(uploadChangedProductsWorker_DoWork);
33    }
34
35    void uploadChangedProductsWorker_DoWork(object sender, DoWorkEventArgs e) {
36      var products = (IEnumerable<UpdateService.ProductDescription>)e.Argument;
37      using (var adminClient = new AdminService.AdminClient()) {
38        foreach (var product in products) {
39          adminClient.DeployProduct(product);
40        }
41      }
42      e.Cancel = false;
43    }
44
45    void uploadChangedProductsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
46      this.Enabled = true;
47    }
48
49    void refreshProductsWorker_DoWork(object sender, DoWorkEventArgs e) {
50      var updateClient = new UpdateService.UpdateClient();
51      e.Result = new object[] { updateClient.GetProducts(), updateClient.GetPlugins() };
52    }
53
54    void refreshProductsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
55      this.products = new List<UpdateService.ProductDescription>(
56        (UpdateService.ProductDescription[])((object[])e.Result)[0]);
57      this.plugins = new List<UpdateService.PluginDescription>(
58        (UpdateService.PluginDescription[])((object[])e.Result)[1]);
59
60      UpdateProductsList();
61      dirtyProducts.Clear();
62
63      Cursor = Cursors.Default;
64      SetControlsEnabled(true);
65    }
66
67    private void UpdateProductsList() {
68      foreach (var prodDesc in products) {
69        productsListBox.Items.Add(CreateListViewItem(prodDesc));
70      }
71    }
72
73    private void productsListBox_SelectedIndexChanged(object sender, EventArgs e) {
74      UpdateService.ProductDescription activeProduct = (UpdateService.ProductDescription)((ListViewItem)productsListBox.SelectedItem).Tag;
75      UpdateProductDetails(activeProduct);
76    }
77
78    private void UpdateProductDetails(UpdateService.ProductDescription activeProduct) {
79      nameTextBox.Text = activeProduct.Name;
80      versionTextBox.Text = activeProduct.Version.ToString();
81
82      pluginsList.Items.Clear();
83      foreach (UpdateService.PluginDescription pluginDesc in plugins) {
84        var matching = from p in activeProduct.Plugins
85                       where p.Name == pluginDesc.Name
86                       where p.Version == pluginDesc.Version
87                       select p;
88
89        if (matching.Any()) pluginsList.Items.Add(pluginDesc, true);
90        else pluginsList.Items.Add(pluginDesc, false);
91      }
92    }
93
94    private void refreshButton_Click(object sender, EventArgs e) {
95      SetControlsEnabled(false);
96      Cursor = Cursors.AppStarting;
97      refreshProductsWorker.RunWorkerAsync();
98    }
99
100    private void SetControlsEnabled(bool enabled) {
101      saveButton.Enabled = enabled;
102      refreshButton.Enabled = enabled;
103      splitContainer.Enabled = enabled;
104    }
105
106    private void newProductButton_Click(object sender, EventArgs e) {
107      var newProduct = new UpdateService.ProductDescription();
108      newProduct.Name = "New product";
109      newProduct.Version = new Version("0.0.0.0");
110      ListViewItem item = CreateListViewItem(newProduct);
111      productsListBox.Items.Add(item);
112      dirtyProducts.Add(newProduct);
113    }
114
115    private ListViewItem CreateListViewItem(UpdateService.ProductDescription productDescription) {
116      ListViewItem item = new ListViewItem();
117      item.Text = productDescription.Name + " " + productDescription.Version;
118      item.Tag = productDescription;
119      return item;
120    }
121
122    private void saveButton_Click(object sender, EventArgs e) {
123      this.Enabled = false;
124      uploadChangedProductsWorker.RunWorkerAsync(dirtyProducts);
125    }
126
127    private void nameTextBox_TextChanged(object sender, EventArgs e) {
128      ListViewItem activeItem = (ListViewItem)productsListBox.SelectedItem;
129      UpdateService.ProductDescription activeProduct = (UpdateService.ProductDescription)activeItem.Tag;
130      if (string.IsNullOrEmpty(nameTextBox.Name)) {
131        errorProvider.SetError(nameTextBox, "Invalid value");
132      } else {
133        activeProduct.Name = nameTextBox.Text;
134        activeItem.Text = activeProduct.Name + " " + activeProduct.Version;
135        errorProvider.SetError(nameTextBox, string.Empty);
136      }
137    }
138
139    private void versionTextBox_TextChanged(object sender, EventArgs e) {
140      ListViewItem activeItem = (ListViewItem)productsListBox.SelectedItem;
141      UpdateService.ProductDescription activeProduct = (UpdateService.ProductDescription)activeItem.Tag;
142      try {
143        activeProduct.Version = new Version(versionTextBox.Text);
144        activeItem.Text = activeProduct.Name + " " + activeProduct.Version;
145        errorProvider.SetError(versionTextBox, string.Empty);
146      }
147      catch (OverflowException ex) {
148        errorProvider.SetError(versionTextBox, "Invalid value");
149      }
150
151      catch (ArgumentException ex) {
152        errorProvider.SetError(versionTextBox, "Invalid value");
153      }
154      catch (FormatException ex) {
155        errorProvider.SetError(versionTextBox, "Invalid value");
156      }
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.