Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/RunCreation/Views/OKBExperimentUploadView.cs @ 7839

Last change on this file since 7839 was 7839, checked in by ascheibe, 12 years ago

#1174 added drag and drop support for the experiment upload view

File size: 11.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
25using System.Linq;
26using System.Threading.Tasks;
27using System.Windows.Forms;
28using HeuristicLab.Clients.Access;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33using HeuristicLab.Optimization;
34
35namespace HeuristicLab.Clients.OKB.RunCreation {
36  [View("OKBExperimentUpload View")]
37  [Content(typeof(RunCollection), false)]
38  [Content(typeof(IOptimizer), false)]
39  public partial class OKBExperimentUploadView : ItemView {
40    public new IOptimizer Content {
41      get { return (IOptimizer)base.Content; }
42      set {
43        base.Content = value;
44      }
45    }
46
47    private const string algorithmTypeParameterName = "Algorithm Type";
48    private const string problemTypeParameterName = "Problem Type";
49    private const string algorithmNameParameterName = "Algorithm Name";
50    private const string problemNameParameterName = "Problem Name";
51    private const int algorithmColumnIndex = 3;
52    private const int problemColumnIndex = 6;
53
54    private List<IRun> runs = new List<IRun>();
55    private List<Problem> problems = new List<Problem>();
56    private List<Algorithm> algorithms = new List<Algorithm>();
57    Algorithm selectedAlgorithm = null;
58    Problem selectedProblem = null;
59    private ProgressView progressView;
60
61    public OKBExperimentUploadView() {
62      InitializeComponent();
63    }
64
65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67      if (Content == null) {
68        ClearRuns();
69      } else {
70        AddRuns(Content);
71        DisplayRuns();
72      }
73    }
74
75    private void AddRuns(IItem item) {
76      if (item is Experiment) {
77        runs.AddRange((item as Experiment).Runs);
78      } else if (item is RunCollection) {
79        runs.AddRange((item as RunCollection));
80      } else if (item is IOptimizer) {
81        runs.AddRange((item as IOptimizer).Runs);
82      } else if (item is IRun) {
83        runs.Add(item as IRun);
84      }
85    }
86
87    protected override void RegisterContentEvents() {
88      base.RegisterContentEvents();
89      RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
90      RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
91    }
92
93    protected override void DeregisterContentEvents() {
94      RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
95      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
96      base.DeregisterContentEvents();
97    }
98
99    private void DisplayError(Exception ex) {
100      PluginInfrastructure.ErrorHandling.ShowErrorDialog("An error occured while retrieving algorithm and problem information from the OKB.", ex);
101    }
102
103    private void DisplayRuns() {
104      if (RunCreationClient.Instance.Algorithms == null || RunCreationClient.Instance.Algorithms.Count() == 0) {
105        RunCreationClient.Instance.RefreshAsync(DisplayError);
106      } else {
107        CreateUI();
108      }
109    }
110
111    private void CreateUI() {
112      if (problems.Count == 0)
113        problems.AddRange(RunCreationClient.Instance.Problems);
114      if (algorithms.Count == 0)
115        algorithms.AddRange(RunCreationClient.Instance.Algorithms);
116
117      IItem algorithmType;
118      IItem problemType;
119      IItem algorithmName;
120      IItem problemName;
121
122      DataGridViewComboBoxColumn cmbAlgorithm = dataGridView.Columns[algorithmColumnIndex] as DataGridViewComboBoxColumn;
123      cmbAlgorithm.DataSource = algorithms;
124      cmbAlgorithm.DisplayMember = "Name";
125
126      DataGridViewComboBoxColumn cmbProblem = dataGridView.Columns[problemColumnIndex] as DataGridViewComboBoxColumn;
127      cmbProblem.DataSource = problems;
128      cmbProblem.DisplayMember = "Name";
129
130      dataGridView.Rows.Clear();
131      foreach (IRun run in runs) {
132        int idx = dataGridView.Rows.Add(run.Name);
133        DataGridViewRow curRow = dataGridView.Rows[idx];
134        curRow.Tag = run;
135
136        if (run.Parameters.TryGetValue(algorithmTypeParameterName, out algorithmType)) {
137          HeuristicLab.Data.StringValue algStr = algorithmType as HeuristicLab.Data.StringValue;
138          if (algStr != null) {
139            curRow.Cells[1].Value = algStr;
140          }
141        }
142
143        if (run.Parameters.TryGetValue(algorithmNameParameterName, out algorithmName)) {
144          HeuristicLab.Data.StringValue algStr = algorithmName as HeuristicLab.Data.StringValue;
145          if (algStr != null) {
146            curRow.Cells[2].Value = algStr;
147          }
148        }
149
150        if (run.Parameters.TryGetValue(problemTypeParameterName, out problemType)) {
151          HeuristicLab.Data.StringValue prbStr = problemType as HeuristicLab.Data.StringValue;
152          if (prbStr != null) {
153            curRow.Cells[4].Value = prbStr;
154          }
155        }
156
157        if (run.Parameters.TryGetValue(problemNameParameterName, out problemName)) {
158          HeuristicLab.Data.StringValue prbStr = problemName as HeuristicLab.Data.StringValue;
159          if (prbStr != null) {
160            curRow.Cells[5].Value = prbStr;
161          }
162        }
163      }
164    }
165
166    private void ClearRuns() {
167      dataGridView.Rows.Clear();
168      runs.Clear();
169    }
170
171    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
172      if (InvokeRequired) {
173        Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
174      } else {
175        IProgress prog = new Progress();
176        prog.Status = "Refreshing algorithms and problems...";
177        SetProgressView(prog);
178      }
179    }
180
181    private void RunCreationClient_Refreshed(object sender, EventArgs e) {
182      if (InvokeRequired) {
183        Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
184      } else {
185        FinishProgressView();
186
187        CreateUI();
188        SetEnabledStateOfControls();
189      }
190    }
191
192    private void btnUpload_Click(object sender, EventArgs e) {
193      var task = System.Threading.Tasks.Task.Factory.StartNew(UploadAsync);
194      task.ContinueWith((t) => {
195        FinishProgressView();
196        PluginInfrastructure.ErrorHandling.ShowErrorDialog("An exception occured while uploading the runs to the OKB.", t.Exception);
197      }, TaskContinuationOptions.OnlyOnFaulted);
198    }
199
200    private void UploadAsync() {
201      IProgress prog = new Progress();
202      prog.Status = "Uploading runs to OKB...";
203      prog.ProgressValue = 0;
204      double count = dataGridView.Rows.Count;
205      int i = 0;
206
207      SetProgressView(prog);
208      foreach (DataGridViewRow row in dataGridView.Rows) {
209        selectedAlgorithm = algorithms.Where(x => x.Name == row.Cells[algorithmColumnIndex].Value.ToString()).FirstOrDefault();
210        selectedProblem = problems.Where(x => x.Name == row.Cells[problemColumnIndex].Value.ToString()).FirstOrDefault();
211        if (selectedAlgorithm == null || selectedProblem == null) {
212          throw new ArgumentException("Can't retrieve the algorithm/problem to upload");
213        }
214
215        OKBRun run = new OKBRun(selectedAlgorithm.Id, selectedProblem.Id, row.Tag as IRun, UserInformation.Instance.User.Id);
216        run.Store();
217        i++;
218        prog.ProgressValue = ((double)i) / count;
219      }
220      FinishProgressView();
221    }
222
223    private void SetProgressView(IProgress progress) {
224      if (InvokeRequired) {
225        Invoke(new Action<IProgress>(SetProgressView), progress);
226      } else {
227        if (progressView == null) {
228          progressView = new ProgressView(this, progress);
229        } else {
230          progressView.Progress = progress;
231        }
232      }
233    }
234
235    private void FinishProgressView() {
236      if (InvokeRequired) {
237        Invoke(new Action(FinishProgressView));
238      } else {
239        if (progressView != null) {
240          progressView.Finish();
241          progressView = null;
242          SetEnabledStateOfControls();
243        }
244      }
245    }
246
247    private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
248      if (e.Button == System.Windows.Forms.MouseButtons.Right && dataGridView[e.ColumnIndex, e.RowIndex].Value != null) {
249        string curVal = dataGridView[e.ColumnIndex, e.RowIndex].Value.ToString();
250        selectedAlgorithm = algorithms.Where(x => x.Name == curVal).FirstOrDefault();
251        selectedProblem = problems.Where(x => x.Name == curVal).FirstOrDefault();
252
253        if (selectedAlgorithm != null || selectedProblem != null) {
254          Point pos = this.PointToClient(Cursor.Position);
255          contextMenu.Show(this, pos);
256        }
257      }
258    }
259
260    private void setColumnToThisValueToolStripMenuItem_Click(object sender, EventArgs e) {
261      if (selectedAlgorithm != null) {
262        for (int i = 0; i < dataGridView.Rows.Count; i++) {
263          var row = dataGridView.Rows[i];
264          row.Cells[algorithmColumnIndex].Value = selectedAlgorithm.Name;
265        }
266      } else if (selectedProblem != null) {
267        for (int i = 0; i < dataGridView.Rows.Count; i++) {
268          var row = dataGridView.Rows[i];
269          row.Cells[problemColumnIndex].Value = selectedProblem.Name;
270        }
271      }
272      selectedAlgorithm = null;
273      selectedProblem = null;
274    }
275
276    private void dataGridView_DragEnter(object sender, DragEventArgs e) {
277      e.Effect = DragDropEffects.None;
278      if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IRun
279        || e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IOptimizer) {
280        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
281        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
282        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
283        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
284        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
285      }
286    }
287
288    private void dataGridView_DragDrop(object sender, DragEventArgs e) {
289      if (e.Effect != DragDropEffects.None) {
290        IItem optimizer = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IItem;
291        if (e.Effect.HasFlag(DragDropEffects.Copy)) optimizer = (IItem)optimizer.Clone();
292        AddRuns(optimizer);
293        DisplayRuns();
294      }
295    }
296  }
297}
Note: See TracBrowser for help on using the repository browser.