Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.HLScript.Views/3.3/VariableStoreView.cs @ 10891

Last change on this file since 10891 was 10891, checked in by abeham, 10 years ago

#2136: merged r10359, r10391, r10401, r10506 to stable

File size: 19.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Text.RegularExpressions;
28using System.Windows.Forms;
29using HeuristicLab.Collections;
30using HeuristicLab.Common;
31using HeuristicLab.Core;
32using HeuristicLab.Core.Views;
33using HeuristicLab.MainForm;
34using HeuristicLab.MainForm.WindowsForms;
35using HeuristicLab.Persistence.Core;
36using HeuristicLab.Persistence.Default.Xml;
37using HeuristicLab.PluginInfrastructure;
38
39namespace HeuristicLab.Scripting.Views {
40  [View("ItemCollection View")]
41  [Content(typeof(VariableStore), true)]
42  public partial class VariableStoreView : AsynchronousContentView {
43    protected Dictionary<string, ListViewItem> itemListViewItemMapping;
44    protected TypeSelectorDialog typeSelectorDialog;
45    protected bool validDragOperation;
46
47    public new VariableStore Content {
48      get { return (VariableStore)base.Content; }
49      set { base.Content = value; }
50    }
51
52    public ListView ItemsListView {
53      get { return variableListView; }
54    }
55
56    public VariableStoreView() {
57      InitializeComponent();
58      itemListViewItemMapping = new Dictionary<string, ListViewItem>();
59      variableListView.SmallImageList.Images.AddRange(new Image[] {
60        HeuristicLab.Common.Resources.VSImageLibrary.Error,
61        HeuristicLab.Common.Resources.VSImageLibrary.Object,
62        HeuristicLab.Common.Resources.VSImageLibrary.Nothing
63      });
64    }
65
66    protected override void Dispose(bool disposing) {
67      if (disposing) {
68        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
69        if (components != null) components.Dispose();
70      }
71      base.Dispose(disposing);
72    }
73
74    protected override void DeregisterContentEvents() {
75      Content.ItemsAdded -= Content_ItemsAdded;
76      Content.ItemsReplaced -= Content_ItemsReplaced;
77      Content.ItemsRemoved -= Content_ItemsRemoved;
78      Content.CollectionReset -= Content_CollectionReset;
79      base.DeregisterContentEvents();
80    }
81    protected override void RegisterContentEvents() {
82      base.RegisterContentEvents();
83      Content.ItemsAdded += Content_ItemsAdded;
84      Content.ItemsReplaced += Content_ItemsReplaced;
85      Content.ItemsRemoved += Content_ItemsRemoved;
86      Content.CollectionReset += Content_CollectionReset;
87    }
88
89    protected override void OnContentChanged() {
90      base.OnContentChanged();
91      variableListView.Items.Clear();
92      itemListViewItemMapping.Clear();
93      RebuildImageList();
94      if (Content != null) {
95        Caption += " (" + Content.GetType().Name + ")";
96        foreach (var item in Content)
97          AddVariable(item);
98        AdjustListViewColumnSizes();
99        SortItemsListView(SortOrder.Ascending);
100      }
101    }
102
103    protected override void SetEnabledStateOfControls() {
104      base.SetEnabledStateOfControls();
105      if (Content == null) {
106        addButton.Enabled = false;
107        sortAscendingButton.Enabled = false;
108        sortDescendingButton.Enabled = false;
109        removeButton.Enabled = false;
110        variableListView.Enabled = false;
111      } else {
112        addButton.Enabled = !Locked && !ReadOnly;
113        sortAscendingButton.Enabled = variableListView.Items.Count > 1;
114        sortDescendingButton.Enabled = variableListView.Items.Count > 1;
115        removeButton.Enabled = !Locked && !ReadOnly && variableListView.SelectedItems.Count > 0;
116        variableListView.Enabled = true;
117        variableListView.LabelEdit = !Locked && !ReadOnly;
118      }
119    }
120
121    protected virtual object CreateItem() {
122      if (typeSelectorDialog == null) {
123        typeSelectorDialog = new TypeSelectorDialog { Caption = "Select Item" };
124        typeSelectorDialog.TypeSelector.Caption = "Available Items";
125        typeSelectorDialog.TypeSelector.Configure(typeof(IItem), false, true);
126      }
127
128      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
129        try {
130          return (object)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
131        } catch (Exception ex) {
132          ErrorHandling.ShowErrorDialog(this, ex);
133        }
134      }
135      return null;
136    }
137
138    protected virtual void AddVariable(KeyValuePair<string, object> variable) {
139      if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
140      string value = (variable.Value ?? "null").ToString();
141      string type = variable.Value == null ? "null" : variable.Value.GetType().ToString();
142      bool serializable = IsSerializable(variable);
143      var listViewItem = new ListViewItem(new[] { variable.Key, value, type }) { ToolTipText = GetToolTipText(variable, serializable), Tag = variable };
144      if (serializable) {
145        listViewItem.ImageIndex = variable.Value == null ? 2 : 1;
146      } else listViewItem.ImageIndex = 0;
147      variableListView.Items.Add(listViewItem);
148      itemListViewItemMapping[variable.Key] = listViewItem;
149      sortAscendingButton.Enabled = variableListView.Items.Count > 1;
150      sortDescendingButton.Enabled = variableListView.Items.Count > 1;
151      var item = variable.Value as IItem;
152      if (item != null) item.ToStringChanged += item_ToStringChanged;
153    }
154
155    protected virtual void RemoveVariable(KeyValuePair<string, object> variable) {
156      if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
157      ListViewItem listViewItem;
158      if (itemListViewItemMapping.TryGetValue(variable.Key, out listViewItem)) {
159        itemListViewItemMapping.Remove(variable.Key);
160        variableListView.Items.Remove(listViewItem);
161        sortAscendingButton.Enabled = variableListView.Items.Count > 1;
162        sortDescendingButton.Enabled = variableListView.Items.Count > 1;
163        var item = variable.Value as IItem;
164        if (item != null) item.ToStringChanged -= item_ToStringChanged;
165      }
166    }
167
168    protected virtual void UpdateVariable(KeyValuePair<string, object> variable) {
169      if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
170      ListViewItem listViewItem;
171      if (itemListViewItemMapping.TryGetValue(variable.Key, out listViewItem)) {
172        string value = (variable.Value ?? "null").ToString();
173        string type = variable.Value == null ? "null" : variable.Value.GetType().ToString();
174        bool serializable = IsSerializable(variable);
175        if (serializable) {
176          listViewItem.ImageIndex = variable.Value == null ? 2 : 1;
177        } else listViewItem.ImageIndex = 0;
178        listViewItem.SubItems[1].Text = value;
179        listViewItem.SubItems[2].Text = type;
180        listViewItem.ToolTipText = GetToolTipText(variable, serializable);
181        listViewItem.Tag = variable;
182      } else throw new ArgumentException("A variable with the specified name does not exist.", "variable");
183    }
184
185    #region ListView Events
186    protected virtual void variableListView_SelectedIndexChanged(object sender, EventArgs e) {
187      removeButton.Enabled = (Content != null) && !Locked && !ReadOnly && variableListView.SelectedItems.Count > 0;
188      AdjustListViewColumnSizes();
189    }
190    protected virtual void variableListView_KeyDown(object sender, KeyEventArgs e) {
191      if (e.KeyCode == Keys.Delete) {
192        if ((variableListView.SelectedItems.Count > 0) && !Locked && !ReadOnly) {
193          foreach (ListViewItem item in variableListView.SelectedItems)
194            Content.Remove(item.Text);
195        }
196      }
197    }
198    protected virtual void variableListView_DoubleClick(object sender, EventArgs e) {
199      if (variableListView.SelectedItems.Count == 1) {
200        var item = variableListView.SelectedItems[0].Tag as KeyValuePair<string, object>?;
201        if (item != null) {
202          var value = item.Value.Value as IContent;
203          if (value != null) {
204            IContentView view = MainFormManager.MainForm.ShowContent(value);
205            if (view != null) {
206              view.ReadOnly = ReadOnly;
207              view.Locked = Locked;
208            }
209          }
210        }
211      }
212    }
213    protected virtual void variableListView_ItemDrag(object sender, ItemDragEventArgs e) {
214      if (!Locked) {
215        var items = new List<object>();
216        foreach (ListViewItem listViewItem in variableListView.SelectedItems) {
217          var item = (KeyValuePair<string, object>)listViewItem.Tag as KeyValuePair<string, object>?;
218          if (item != null) items.Add(item.Value.Value);
219        }
220
221        if (items.Count > 0) {
222          DataObject data = new DataObject();
223          if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
224          else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
225          if (ReadOnly) {
226            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
227          } else {
228            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
229            if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
230              foreach (string item in items) Content.Remove(item);
231            }
232          }
233        }
234      }
235    }
236    protected virtual void variableListView_DragEnter(object sender, DragEventArgs e) {
237      validDragOperation = false;
238      if (!Locked && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is object)) {
239        validDragOperation = true;
240      } else if (!Locked && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
241        validDragOperation = true;
242        IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
243        foreach (object item in items)
244          validDragOperation = validDragOperation && (item is object);
245      }
246    }
247    protected virtual void variableListView_DragOver(object sender, DragEventArgs e) {
248      e.Effect = DragDropEffects.None;
249      if (validDragOperation) {
250        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
251        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
252        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
253        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
254        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
255      }
256    }
257    protected virtual void variableListView_DragDrop(object sender, DragEventArgs e) {
258      if (e.Effect != DragDropEffects.None) {
259        if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
260          IEnumerable<object> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<object>();
261          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
262            var cloner = new Cloner();
263            var clonedItems = new List<object>();
264            foreach (var item in items) {
265              var dc = item as IDeepCloneable;
266              clonedItems.Add(dc != null ? cloner.Clone(dc) : item);
267            }
268            items = clonedItems;
269          }
270          foreach (var item in items) {
271            string name = GenerateNewVariableName();
272            Content.Add(name, item);
273            var listViewItem = variableListView.FindItemWithText(name);
274            listViewItem.BeginEdit();
275          }
276        } else {
277          object item = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
278          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
279            var cloner = new Cloner();
280            var dc = item as IDeepCloneable;
281            if (dc != null) item = cloner.Clone(dc);
282          }
283          string name = GenerateNewVariableName();
284          Content.Add(name, item);
285          var listViewItem = variableListView.FindItemWithText(name);
286          listViewItem.BeginEdit();
287        }
288      }
289    }
290
291    private readonly Regex SafeVariableNameRegex = new Regex("^[@]?[_a-zA-Z][_a-zA-Z0-9]*$");
292    private void variableListView_AfterLabelEdit(object sender, LabelEditEventArgs e) {
293      string name = e.Label;
294      if (!string.IsNullOrEmpty(name) && SafeVariableNameRegex.IsMatch(name)) {
295        var variable = (KeyValuePair<string, object>)variableListView.Items[e.Item].Tag;
296        if (!Content.ContainsKey(name)) {
297          Content.Remove(variable.Key);
298          Content.Add(name, variable.Value);
299        }
300      }
301      e.CancelEdit = true;
302    }
303    #endregion
304
305    #region Button Events
306    protected virtual void addButton_Click(object sender, EventArgs e) {
307      object newVar = CreateItem();
308      if (newVar != null) {
309        string name = GenerateNewVariableName();
310        Content.Add(name, newVar);
311        var item = variableListView.FindItemWithText(name);
312        item.BeginEdit();
313      }
314    }
315    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
316      SortItemsListView(SortOrder.Ascending);
317    }
318    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
319      SortItemsListView(SortOrder.Descending);
320    }
321    protected virtual void removeButton_Click(object sender, EventArgs e) {
322      if (variableListView.SelectedItems.Count > 0) {
323        foreach (ListViewItem item in variableListView.SelectedItems)
324          Content.Remove(item.Text);
325        variableListView.SelectedItems.Clear();
326      }
327    }
328    #endregion
329
330    #region Content Events
331    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
332      if (InvokeRequired)
333        Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsAdded), sender, e);
334      else {
335        foreach (var item in e.Items)
336          AddVariable(item);
337        AdjustListViewColumnSizes();
338      }
339    }
340
341    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
342      if (InvokeRequired)
343        Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsReplaced), sender, e);
344      else {
345        foreach (var item in e.Items)
346          UpdateVariable(item);
347        AdjustListViewColumnSizes();
348      }
349    }
350
351    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
352      if (InvokeRequired)
353        Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsRemoved), sender, e);
354      else {
355        foreach (var item in e.Items)
356          RemoveVariable(item);
357        RebuildImageList();
358        AdjustListViewColumnSizes();
359      }
360    }
361    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
362      if (InvokeRequired)
363        Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_CollectionReset), sender, e);
364      else {
365        foreach (var item in e.OldItems)
366          RemoveVariable(item);
367        RebuildImageList();
368        foreach (var item in e.Items)
369          AddVariable(item);
370        AdjustListViewColumnSizes();
371      }
372    }
373
374    private void item_ToStringChanged(object sender, EventArgs e) {
375      foreach (ListViewItem item in variableListView.Items) {
376        var variable = item.Tag as KeyValuePair<string, object>?;
377        if (variable != null && variable.Value.Value == sender) {
378          string value = (variable.Value.Value ?? "null").ToString();
379          item.SubItems[1].Text = value;
380          item.SubItems[2].Text = variable.Value.Value.GetType().ToString();
381          item.ToolTipText = GetToolTipText(variable.Value, item.ImageIndex == 0);
382          return;
383        }
384      }
385    }
386    #endregion
387
388    #region Helpers
389    protected virtual void SortItemsListView(SortOrder sortOrder) {
390      variableListView.Sorting = SortOrder.None;
391      variableListView.Sorting = sortOrder;
392      variableListView.Sorting = SortOrder.None;
393    }
394    protected virtual void AdjustListViewColumnSizes() {
395      foreach (ColumnHeader ch in variableListView.Columns)
396        ch.Width = -2;
397    }
398    protected virtual void RebuildImageList() {
399      variableListView.SmallImageList.Images.Clear();
400      variableListView.SmallImageList.Images.AddRange(new Image[] {
401        HeuristicLab.Common.Resources.VSImageLibrary.Error,
402        HeuristicLab.Common.Resources.VSImageLibrary.Object,
403        HeuristicLab.Common.Resources.VSImageLibrary.Nothing
404      });
405      foreach (ListViewItem listViewItem in variableListView.Items) {
406        var variable = (KeyValuePair<string, object>)listViewItem.Tag;
407        bool serializable = IsSerializable(variable);
408        if (serializable) {
409          listViewItem.ImageIndex = variable.Value == null ? 2 : 1;
410        } else listViewItem.ImageIndex = 0;
411      }
412    }
413
414    private string GetToolTipText(KeyValuePair<string, object> variable, bool serializable) {
415      if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
416      string value = (variable.Value ?? "null").ToString();
417      string type = variable.Value == null ? "null" : variable.Value.GetType().ToString();
418      string[] lines = {
419        "Name: " + variable.Key,
420        "Value: " + value,
421        "Type: " + type
422      };
423      string toolTipText = string.Join(Environment.NewLine, lines);
424      if (!serializable)
425        toolTipText += Environment.NewLine + "CAUTION: Type is not serializable!";
426      return toolTipText;
427    }
428
429    private string GenerateNewVariableName(string defaultName = "enter_name") {
430      if (Content.ContainsKey(defaultName)) {
431        int i = 1;
432        string newName;
433        do {
434          newName = defaultName + i++;
435        } while (Content.ContainsKey(newName));
436        return newName;
437      }
438      return defaultName;
439    }
440
441    private bool IsSerializable(KeyValuePair<string, object> variable) {
442      var ser = new Serializer(variable, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat()), "ROOT", true);
443      try {
444        return ser.Count() > 0;
445      } catch (PersistenceException) {
446        return false;
447      }
448    }
449    #endregion
450  }
451}
Note: See TracBrowser for help on using the repository browser.