Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10577 was 10577, checked in by abeham, 11 years ago

#2136:

  • Fixed drag-drop behavior in VariableStoreView
  • Added additional information to Script and changed setting name and description
File size: 17.8 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          var 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);
227          } else {
228            var 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 = !Locked && !ReadOnly && e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) != null;
238    }
239    protected virtual void variableListView_DragOver(object sender, DragEventArgs e) {
240      e.Effect = DragDropEffects.None;
241      if (validDragOperation) {
242        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
243        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
244        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
245        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
246        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
247      }
248    }
249    protected virtual void variableListView_DragDrop(object sender, DragEventArgs e) {
250      if (e.Effect != DragDropEffects.None) {
251        object item = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
252        if (e.Effect.HasFlag(DragDropEffects.Copy)) {
253          var cloner = new Cloner();
254          var dc = item as IDeepCloneable;
255          if (dc != null) item = cloner.Clone(dc);
256        }
257        string name = GenerateNewVariableName();
258        Content.Add(name, item);
259        var listViewItem = variableListView.FindItemWithText(name);
260        listViewItem.BeginEdit();
261      }
262    }
263
264    private readonly Regex SafeVariableNameRegex = new Regex("^[@]?[_a-zA-Z][_a-zA-Z0-9]*$");
265    private void variableListView_AfterLabelEdit(object sender, LabelEditEventArgs e) {
266      string name = e.Label;
267      if (!string.IsNullOrEmpty(name) && SafeVariableNameRegex.IsMatch(name)) {
268        var variable = (KeyValuePair<string, object>)variableListView.Items[e.Item].Tag;
269        if (!Content.ContainsKey(name)) {
270          Content.Remove(variable.Key);
271          Content.Add(name, variable.Value);
272        }
273      }
274      e.CancelEdit = true;
275    }
276    #endregion
277
278    #region Button Events
279    protected virtual void addButton_Click(object sender, EventArgs e) {
280      object newVar = CreateItem();
281      if (newVar != null) {
282        string name = GenerateNewVariableName();
283        Content.Add(name, newVar);
284        var item = variableListView.FindItemWithText(name);
285        item.BeginEdit();
286      }
287    }
288    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
289      SortItemsListView(SortOrder.Ascending);
290    }
291    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
292      SortItemsListView(SortOrder.Descending);
293    }
294    protected virtual void removeButton_Click(object sender, EventArgs e) {
295      if (variableListView.SelectedItems.Count > 0) {
296        foreach (ListViewItem item in variableListView.SelectedItems)
297          Content.Remove(item.Text);
298        variableListView.SelectedItems.Clear();
299      }
300    }
301    #endregion
302
303    #region Content Events
304    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
305      if (InvokeRequired)
306        Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsAdded), sender, e);
307      else {
308        foreach (var item in e.Items)
309          AddVariable(item);
310        AdjustListViewColumnSizes();
311      }
312    }
313
314    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
315      if (InvokeRequired)
316        Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsReplaced), sender, e);
317      else {
318        foreach (var item in e.Items)
319          UpdateVariable(item);
320        AdjustListViewColumnSizes();
321      }
322    }
323
324    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
325      if (InvokeRequired)
326        Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_ItemsRemoved), sender, e);
327      else {
328        foreach (var item in e.Items)
329          RemoveVariable(item);
330        RebuildImageList();
331        AdjustListViewColumnSizes();
332      }
333    }
334    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, object>> e) {
335      if (InvokeRequired)
336        Invoke(new CollectionItemsChangedEventHandler<KeyValuePair<string, object>>(Content_CollectionReset), sender, e);
337      else {
338        foreach (var item in e.OldItems)
339          RemoveVariable(item);
340        RebuildImageList();
341        foreach (var item in e.Items)
342          AddVariable(item);
343        AdjustListViewColumnSizes();
344      }
345    }
346
347    private void item_ToStringChanged(object sender, EventArgs e) {
348      foreach (ListViewItem item in variableListView.Items) {
349        var variable = item.Tag as KeyValuePair<string, object>?;
350        if (variable != null && variable.Value.Value == sender) {
351          string value = (variable.Value.Value ?? "null").ToString();
352          item.SubItems[1].Text = value;
353          item.SubItems[2].Text = variable.Value.Value.GetType().ToString();
354          item.ToolTipText = GetToolTipText(variable.Value, item.ImageIndex == 0);
355          return;
356        }
357      }
358    }
359    #endregion
360
361    #region Helpers
362    protected virtual void SortItemsListView(SortOrder sortOrder) {
363      variableListView.Sorting = SortOrder.None;
364      variableListView.Sorting = sortOrder;
365      variableListView.Sorting = SortOrder.None;
366    }
367    protected virtual void AdjustListViewColumnSizes() {
368      foreach (ColumnHeader ch in variableListView.Columns)
369        ch.Width = -2;
370    }
371    protected virtual void RebuildImageList() {
372      variableListView.SmallImageList.Images.Clear();
373      variableListView.SmallImageList.Images.AddRange(new Image[] {
374        HeuristicLab.Common.Resources.VSImageLibrary.Error,
375        HeuristicLab.Common.Resources.VSImageLibrary.Object,
376        HeuristicLab.Common.Resources.VSImageLibrary.Nothing
377      });
378      foreach (ListViewItem listViewItem in variableListView.Items) {
379        var variable = (KeyValuePair<string, object>)listViewItem.Tag;
380        bool serializable = IsSerializable(variable);
381        if (serializable) {
382          listViewItem.ImageIndex = variable.Value == null ? 2 : 1;
383        } else listViewItem.ImageIndex = 0;
384      }
385    }
386
387    private string GetToolTipText(KeyValuePair<string, object> variable, bool serializable) {
388      if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
389      string value = (variable.Value ?? "null").ToString();
390      string type = variable.Value == null ? "null" : variable.Value.GetType().ToString();
391      string[] lines = {
392        "Name: " + variable.Key,
393        "Value: " + value,
394        "Type: " + type
395      };
396      string toolTipText = string.Join(Environment.NewLine, lines);
397      if (!serializable)
398        toolTipText += Environment.NewLine + "CAUTION: Type is not serializable!";
399      return toolTipText;
400    }
401
402    private string GenerateNewVariableName(string defaultName = "enter_name") {
403      if (Content.ContainsKey(defaultName)) {
404        int i = 1;
405        string newName;
406        do {
407          newName = defaultName + i++;
408        } while (Content.ContainsKey(newName));
409        return newName;
410      }
411      return defaultName;
412    }
413
414    private bool IsSerializable(KeyValuePair<string, object> variable) {
415      var ser = new Serializer(variable, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat()), "ROOT", true);
416      try {
417        return ser.Count() > 0;
418      } catch (PersistenceException) {
419        return false;
420      }
421    }
422    #endregion
423  }
424}
Note: See TracBrowser for help on using the repository browser.