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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 | using PluginDeploymentService = HeuristicLab.PluginInfrastructure.Advanced.DeploymentService;
|
---|
32 | using System.ServiceModel;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 |
|
---|
35 | namespace 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 | }
|
---|
114 |
|
---|
115 | private void productsListBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
116 | if (productsListView.SelectedItems.Count == 0) return;
|
---|
117 | PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)((ListViewItem)productsListView.SelectedItems[0]).Tag;
|
---|
118 | UpdateProductDetails(activeProduct);
|
---|
119 | }
|
---|
120 |
|
---|
121 | private void UpdateProductDetails(PluginDeploymentService.ProductDescription activeProduct) {
|
---|
122 | nameTextBox.Text = activeProduct.Name;
|
---|
123 | versionTextBox.Text = activeProduct.Version.ToString();
|
---|
124 |
|
---|
125 | UpdatePluginsListView();
|
---|
126 | }
|
---|
127 |
|
---|
128 | private ListViewItem CreateListViewItem(PluginDeploymentService.ProductDescription productDescription) {
|
---|
129 | ListViewItem item = new ListViewItem(new string[] { productDescription.Name, productDescription.Version.ToString() });
|
---|
130 | item.Tag = productDescription;
|
---|
131 | item.ImageIndex = 0;
|
---|
132 | return item;
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void SetControlsEnabled(bool enabled) {
|
---|
136 | saveButton.Enabled = enabled;
|
---|
137 | refreshButton.Enabled = enabled;
|
---|
138 | newProductButton.Enabled = enabled;
|
---|
139 | splitContainer.Enabled = enabled;
|
---|
140 | productsListView.Enabled = enabled;
|
---|
141 | nameLabel.Enabled = enabled;
|
---|
142 | nameTextBox.Enabled = enabled;
|
---|
143 | versionLabel.Enabled = enabled;
|
---|
144 | versionTextBox.Enabled = enabled;
|
---|
145 | pluginListView.Enabled = enabled;
|
---|
146 | }
|
---|
147 |
|
---|
148 | #region button event handlers
|
---|
149 | private void newProductButton_Click(object sender, EventArgs e) {
|
---|
150 | var newProduct = new PluginDeploymentService.ProductDescription("New product", new Version("0.0.0.0"));
|
---|
151 | ListViewItem item = CreateListViewItem(newProduct);
|
---|
152 | productsListView.Items.Add(item);
|
---|
153 | MarkProductDirty(newProduct);
|
---|
154 | }
|
---|
155 |
|
---|
156 | private void saveButton_Click(object sender, EventArgs e) {
|
---|
157 | this.Enabled = false;
|
---|
158 | uploadChangedProductsWorker.RunWorkerAsync(dirtyProducts);
|
---|
159 | }
|
---|
160 | private void refreshButton_Click(object sender, EventArgs e) {
|
---|
161 | SetControlsEnabled(false);
|
---|
162 | Cursor = Cursors.AppStarting;
|
---|
163 | refreshProductsWorker.RunWorkerAsync();
|
---|
164 | }
|
---|
165 |
|
---|
166 | #endregion
|
---|
167 |
|
---|
168 | #region textbox changed event handlers
|
---|
169 | private void nameTextBox_TextChanged(object sender, EventArgs e) {
|
---|
170 | ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
|
---|
171 | PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)activeItem.Tag;
|
---|
172 | if (string.IsNullOrEmpty(nameTextBox.Name)) {
|
---|
173 | errorProvider.SetError(nameTextBox, "Invalid value");
|
---|
174 | } else {
|
---|
175 | if (activeProduct.Name != nameTextBox.Text) {
|
---|
176 | activeProduct.Name = nameTextBox.Text;
|
---|
177 | activeItem.SubItems[0].Text = activeProduct.Name;
|
---|
178 | errorProvider.SetError(nameTextBox, string.Empty);
|
---|
179 | MarkProductDirty(activeProduct);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | private void versionTextBox_TextChanged(object sender, EventArgs e) {
|
---|
186 | ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
|
---|
187 | PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)activeItem.Tag;
|
---|
188 | try {
|
---|
189 | var newVersion = new Version(versionTextBox.Text);
|
---|
190 | if (activeProduct.Version != newVersion) {
|
---|
191 | activeProduct.Version = newVersion;
|
---|
192 | activeItem.SubItems[1].Text = versionTextBox.Text;
|
---|
193 | errorProvider.SetError(versionTextBox, string.Empty);
|
---|
194 | MarkProductDirty(activeProduct);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | catch (OverflowException) {
|
---|
198 | errorProvider.SetError(versionTextBox, "Invalid value");
|
---|
199 | }
|
---|
200 |
|
---|
201 | catch (ArgumentException) {
|
---|
202 | errorProvider.SetError(versionTextBox, "Invalid value");
|
---|
203 | }
|
---|
204 | catch (FormatException) {
|
---|
205 | errorProvider.SetError(versionTextBox, "Invalid value");
|
---|
206 | }
|
---|
207 | }
|
---|
208 | #endregion
|
---|
209 |
|
---|
210 |
|
---|
211 | #region plugin list view
|
---|
212 | private void UpdatePluginsListView() {
|
---|
213 | ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
|
---|
214 | PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)activeItem.Tag;
|
---|
215 | pluginListView.Plugins = plugins.OfType<IPluginDescription>();
|
---|
216 | foreach (var plugin in activeProduct.Plugins) pluginListView.CheckPlugin(plugin);
|
---|
217 | }
|
---|
218 |
|
---|
219 | private void pluginListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
|
---|
220 | ListViewItem activeItem = (ListViewItem)productsListView.SelectedItems[0];
|
---|
221 | PluginDeploymentService.ProductDescription activeProduct = (PluginDeploymentService.ProductDescription)activeItem.Tag;
|
---|
222 | activeProduct.Plugins = pluginListView.CheckedPlugins.Cast<PluginDeploymentService.PluginDescription>().ToArray();
|
---|
223 | MarkProductDirty(activeProduct);
|
---|
224 | }
|
---|
225 | #endregion
|
---|
226 |
|
---|
227 | private void MarkProductDirty(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription activeProduct) {
|
---|
228 | if (!dirtyProducts.Contains(activeProduct)) {
|
---|
229 | dirtyProducts.Add(activeProduct);
|
---|
230 | var item = FindItemForProduct(activeProduct);
|
---|
231 | item.ImageIndex = 1;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | private ListViewItem FindItemForProduct(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription activeProduct) {
|
---|
235 | return (from item in productsListView.Items.OfType<ListViewItem>()
|
---|
236 | let product = item.Tag as PluginDeploymentService.ProductDescription
|
---|
237 | where product != null
|
---|
238 | where product == activeProduct
|
---|
239 | select item).Single();
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|