Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Scripting.Views/3.3/VariableStoreView.cs @ 13042

Last change on this file since 13042 was 12634, checked in by jkarder, 9 years ago

#2416: refactored invoke call

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