Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11479 for trunk/sources


Ignore:
Timestamp:
10/17/14 12:52:15 (10 years ago)
Author:
jkarder
Message:

#2262: worked on variable management of VariableStoreView

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/VariableStoreView.cs

    r11474 r11479  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Drawing;
    2524using System.Linq;
    2625using System.Text.RegularExpressions;
     
    4039  [Content(typeof(VariableStore), true)]
    4140  public partial class VariableStoreView : AsynchronousContentView {
     41    #region Image Names
     42    private const string ErrorImageName = "Error";
     43    private const string WarningImageName = "Warning";
     44    private const string HeuristicLabObjectImageName = "HeuristicLabObject";
     45    private const string ObjectImageName = "Object";
     46    private const string NothingImageName = "Nothing";
     47    #endregion
     48
    4249    protected readonly Dictionary<string, ListViewItem> itemListViewItemMapping;
    4350    protected readonly Dictionary<Type, bool> serializableLookup;
     
    5865      itemListViewItemMapping = new Dictionary<string, ListViewItem>();
    5966      serializableLookup = new Dictionary<Type, bool>();
    60       variableListView.SmallImageList.Images.AddRange(new Image[] {
    61         HeuristicLab.Common.Resources.VSImageLibrary.Error,
    62         HeuristicLab.Common.Resources.VSImageLibrary.Warning,
    63         HeuristicLab.Common.Resources.VSImageLibrary.Object,
    64         HeuristicLab.Common.Resources.VSImageLibrary.Nothing
    65       });
     67
     68      var images = variableListView.SmallImageList.Images;
     69      images.Add(ErrorImageName, Common.Resources.VSImageLibrary.Error);
     70      images.Add(WarningImageName, Common.Resources.VSImageLibrary.Warning);
     71      images.Add(HeuristicLabObjectImageName, Common.Resources.HeuristicLab.Icon.ToBitmap());
     72      images.Add(ObjectImageName, Common.Resources.VSImageLibrary.Object);
     73      images.Add(NothingImageName, Common.Resources.VSImageLibrary.Nothing);
    6674    }
    6775
     
    142150      string type = variable.Value == null ? "null" : variable.Value.GetType().ToString();
    143151      bool serializable = IsSerializable(variable);
    144       var listViewItem = new ListViewItem(new[] { variable.Key, value, type }) { ToolTipText = GetToolTipText(variable, serializable), Tag = variable };
    145       bool validIdentifier = SafeVariableNameRegex.IsMatch(variable.Key);
    146       if (!serializable) listViewItem.ImageIndex = 0;
    147       else if (!validIdentifier) listViewItem.ImageIndex = 1;
    148       else if (variable.Value != null) listViewItem.ImageIndex = 2;
    149       else listViewItem.ImageIndex = 3;
     152
     153      var listViewItem = new ListViewItem(new[] { variable.Key, value, type }) { Tag = variable };
     154      SetImageKey(listViewItem, serializable);
     155      SetToolTipText(listViewItem, serializable);
    150156      variableListView.Items.Add(listViewItem);
     157
    151158      itemListViewItemMapping[variable.Key] = listViewItem;
    152159      sortAscendingButton.Enabled = variableListView.Items.Count > 1;
     
    177184        bool serializable = IsSerializable(variable);
    178185        bool validIdentifier = SafeVariableNameRegex.IsMatch(variable.Key);
    179         if (!serializable) listViewItem.ImageIndex = 0;
    180         else if (!validIdentifier) listViewItem.ImageIndex = 1;
    181         else if (variable.Value != null) listViewItem.ImageIndex = 2;
    182         else listViewItem.ImageIndex = 3;
     186
     187        listViewItem.Tag = variable;
    183188        listViewItem.SubItems[1].Text = value;
    184189        listViewItem.SubItems[2].Text = type;
    185         listViewItem.ToolTipText = GetToolTipText(variable, serializable);
    186         listViewItem.Tag = variable;
     190        SetImageKey(listViewItem, serializable);
     191        SetToolTipText(listViewItem, serializable);
    187192      } else throw new ArgumentException("A variable with the specified name does not exist.", "variable");
    188193    }
     
    234239        var listViewItem = variableListView.SelectedItems[0];
    235240        var item = (KeyValuePair<string, object>)listViewItem.Tag;
    236         var data = new DataObject(HeuristicLab.Common.Constants.DragDropDataFormat, item.Value);
     241        if (!(item.Value is IDeepCloneable)) return;
     242        var data = new DataObject(HeuristicLab.Common.Constants.DragDropDataFormat, item);
    237243        DoDragDrop(data, DragDropEffects.Copy);
    238244      }
     
    250256      if (e.Effect == DragDropEffects.Copy) {
    251257        object item = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
    252         var dc = item as IDeepCloneable;
    253         if (dc != null) item = dc.Clone();
    254 
    255         string name;
     258
     259        string variableName;
     260        object variableValue;
    256261        bool editLabel;
    257         if (sender == variableListView) {
    258           name = GenerateNewVariableName(variableListView.FocusedItem.Text, false);
    259           editLabel = true;
     262        if (item is KeyValuePair<string, object>) {
     263          var variable = (KeyValuePair<string, object>)item;
     264          variableName = GenerateNewVariableName(out editLabel, variable.Key, false);
     265          variableValue = variable.Value;
    260266        } else {
     267          var cloneable = item as IDeepCloneable;
     268          variableValue = cloneable != null ? cloneable.Clone() : item;
     269
    261270          var namedItem = item as INamedItem;
    262           editLabel = namedItem == null || string.IsNullOrEmpty(namedItem.Name);
    263           name = editLabel ? GenerateNewVariableName() : GenerateNewVariableName(namedItem.Name, false);
     271          if (namedItem != null)
     272            variableName = GenerateNewVariableName(out editLabel, namedItem.Name, false);
     273          else
     274            variableName = GenerateNewVariableName(out editLabel);
    264275        }
    265276
    266         Content.Add(name, item);
    267         var listViewItem = variableListView.FindItemWithText(name);
     277        Content.Add(variableName, variableValue);
     278
     279        var listViewItem = variableListView.FindItemWithText(variableName);
    268280        variableListView.SelectedItems.Clear();
    269281        if (editLabel) listViewItem.BeginEdit();
     
    289301    #region Button Events
    290302    protected virtual void addButton_Click(object sender, EventArgs e) {
    291       object newVar = CreateItem();
    292       if (newVar == null) return;
    293 
    294       var namedItem = newVar as INamedItem;
    295       bool editLabel = namedItem == null || string.IsNullOrEmpty(namedItem.Name);
    296       string name = editLabel ? GenerateNewVariableName() : GenerateNewVariableName(namedItem.Name, false);
    297       Content.Add(name, newVar);
    298 
    299       var item = variableListView.FindItemWithText(name);
     303      object variableValue = CreateItem();
     304      if (variableValue == null) return;
     305
     306      string variableName;
     307      var namedItem = variableValue as INamedItem;
     308      if (namedItem != null)
     309        variableName = GenerateNewVariableName(namedItem.Name, false);
     310      else
     311        variableName = GenerateNewVariableName();
     312
     313      Content.Add(variableName, variableValue);
     314
     315      var item = variableListView.FindItemWithText(variableName);
     316      variableListView.SelectedItems.Clear();
    300317      item.BeginEdit();
    301318    }
     
    364381          item.SubItems[1].Text = value;
    365382          item.SubItems[2].Text = variable.Value.Value.GetType().ToString();
    366           item.ToolTipText = GetToolTipText(variable.Value, item.ImageIndex != 0);
     383          SetToolTipText(item, item.ImageIndex != 0);
    367384        }
    368385      }
     
    381398    }
    382399
    383     private string GetToolTipText(KeyValuePair<string, object> variable, bool serializable) {
     400    protected virtual void SetImageKey(ListViewItem listViewItem, bool serializable) {
     401      var variable = (KeyValuePair<string, object>)listViewItem.Tag;
     402      if (!serializable) listViewItem.ImageKey = ErrorImageName;
     403      else if (!SafeVariableNameRegex.IsMatch(variable.Key)) listViewItem.ImageKey = WarningImageName;
     404      else if (variable.Value is IItem) listViewItem.ImageKey = HeuristicLabObjectImageName;
     405      else if (variable.Value != null) listViewItem.ImageKey = ObjectImageName;
     406      else listViewItem.ImageKey = NothingImageName;
     407    }
     408
     409    protected virtual void SetToolTipText(ListViewItem listViewItem, bool serializable) {
     410      var variable = (KeyValuePair<string, object>)listViewItem.Tag;
    384411      if (string.IsNullOrEmpty(variable.Key)) throw new ArgumentException("The variable must have a name.", "variable");
    385412      string value = (variable.Value ?? "null").ToString();
     
    395422      if (!serializable)
    396423        toolTipText = "Caution: Type is not serializable!" + Environment.NewLine + toolTipText;
    397       return toolTipText;
     424      listViewItem.ToolTipText = toolTipText;
    398425    }
    399426
    400427    private string GenerateNewVariableName(string defaultName = DefaultVariableName, bool generateValidIdentifier = true) {
    401       if (generateValidIdentifier && !SafeVariableNameRegex.IsMatch(defaultName))
     428      bool editLabel;
     429      return GenerateNewVariableName(out editLabel, defaultName, generateValidIdentifier);
     430    }
     431
     432    private string GenerateNewVariableName(out bool defaultNameExists, string defaultName = DefaultVariableName, bool generateValidIdentifier = true) {
     433      if (string.IsNullOrEmpty(defaultName) || generateValidIdentifier && !SafeVariableNameRegex.IsMatch(defaultName))
    402434        defaultName = DefaultVariableName;
    403435      if (Content.ContainsKey(defaultName)) {
     
    408440          newName = string.Format(formatString, defaultName, i++);
    409441        } while (Content.ContainsKey(newName));
     442        defaultNameExists = true;
    410443        return newName;
    411444      }
     445      defaultNameExists = false;
    412446      return defaultName;
    413447    }
Note: See TracChangeset for help on using the changeset viewer.