Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11436


Ignore:
Timestamp:
10/09/14 16:59:34 (10 years ago)
Author:
jkarder
Message:

#2262:

  • IEnumerable<T> extension methods are supported by the variables collection
  • INamedItem variables get the item's name per default
  • variables can be renamed by pressing F2
  • variables are removed faster
  • multiple variables can be selected
  • the code editor supports scrolling while scripts are executed
Location:
trunk/sources
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.CodeEditor/3.3/CodeEditor.cs

    r10359 r11436  
    133133    }
    134134
     135    public bool ReadOnly {
     136      get { return Doc.ReadOnly; }
     137      set { Doc.ReadOnly = value; }
     138    }
     139
    135140    #endregion
    136141
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/ScriptView.cs

    r11171 r11436  
    4545    }
    4646
     47    public override bool ReadOnly {
     48      get { return codeEditor.ReadOnly; }
     49      set { codeEditor.ReadOnly = value; }
     50    }
     51
     52    public override bool Locked {
     53      get { return codeEditor.ReadOnly; }
     54      set { codeEditor.ReadOnly = value; }
     55    }
     56
    4757    protected override void RegisterContentEvents() {
    4858      base.RegisterContentEvents();
     
    8393      base.SetEnabledStateOfControls();
    8494      compileButton.Enabled = Content != null && !Locked && !ReadOnly;
    85       codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
     95      codeEditor.Enabled = Content != null;
    8696    }
    8797
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/VariableStoreView.Designer.cs

    r10506 r11436  
    6666      this.variableListView.LabelEdit = true;
    6767      this.variableListView.Location = new System.Drawing.Point(6, 49);
    68       this.variableListView.MultiSelect = false;
    6968      this.variableListView.Name = "variableListView";
    7069      this.variableListView.ShowItemToolTips = true;
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/VariableStoreView.cs

    r11014 r11436  
    5858      variableListView.SmallImageList.Images.AddRange(new Image[] {
    5959        HeuristicLab.Common.Resources.VSImageLibrary.Error,
     60        HeuristicLab.Common.Resources.VSImageLibrary.Warning,
    6061        HeuristicLab.Common.Resources.VSImageLibrary.Object,
    6162        HeuristicLab.Common.Resources.VSImageLibrary.Nothing
     
    9091      variableListView.Items.Clear();
    9192      itemListViewItemMapping.Clear();
    92       RebuildImageList();
    9393      if (Content != null) {
    9494        Caption += " (" + Content.GetType().Name + ")";
     
    141141      bool serializable = IsSerializable(variable);
    142142      var listViewItem = new ListViewItem(new[] { variable.Key, value, type }) { ToolTipText = GetToolTipText(variable, serializable), Tag = variable };
    143       if (serializable) {
    144         listViewItem.ImageIndex = variable.Value == null ? 2 : 1;
    145       } else listViewItem.ImageIndex = 0;
     143      bool validIdentifier = SafeVariableNameRegex.IsMatch(variable.Key);
     144      if (!serializable) listViewItem.ImageIndex = 0;
     145      else if (!validIdentifier) listViewItem.ImageIndex = 1;
     146      else if (variable.Value != null) listViewItem.ImageIndex = 2;
     147      else listViewItem.ImageIndex = 3;
    146148      variableListView.Items.Add(listViewItem);
    147149      itemListViewItemMapping[variable.Key] = listViewItem;
     
    172174        string type = variable.Value == null ? "null" : variable.Value.GetType().ToString();
    173175        bool serializable = IsSerializable(variable);
    174         if (serializable) {
    175           listViewItem.ImageIndex = variable.Value == null ? 2 : 1;
    176         } else listViewItem.ImageIndex = 0;
     176        if (!serializable) listViewItem.ImageIndex = 0;
     177        else if (variable.Value != null) listViewItem.ImageIndex = 2;
     178        else listViewItem.ImageIndex = 3;
    177179        listViewItem.SubItems[1].Text = value;
    178180        listViewItem.SubItems[2].Text = type;
     
    188190    }
    189191    protected virtual void variableListView_KeyDown(object sender, KeyEventArgs e) {
    190       if (e.KeyCode == Keys.Delete) {
    191         if ((variableListView.SelectedItems.Count > 0) && !Locked && !ReadOnly) {
    192           foreach (ListViewItem item in variableListView.SelectedItems)
    193             Content.Remove(item.Text);
    194         }
     192      switch (e.KeyCode) {
     193        case Keys.Delete:
     194          if ((variableListView.SelectedItems.Count > 0) && !Locked && !ReadOnly) {
     195            foreach (ListViewItem item in variableListView.SelectedItems)
     196              Content.Remove(item.Text);
     197          }
     198          break;
     199        case Keys.F2:
     200          var focusedItem = variableListView.FocusedItem;
     201          if (variableListView.LabelEdit && focusedItem.Selected)
     202            focusedItem.BeginEdit();
     203          break;
     204        case Keys.A:
     205          if (e.Modifiers.HasFlag(Keys.Control)) {
     206            foreach (ListViewItem item in variableListView.Items)
     207              item.Selected = true;
     208          }
     209          break;
    195210      }
    196211    }
     
    211226    }
    212227    protected virtual void variableListView_ItemDrag(object sender, ItemDragEventArgs e) {
    213       if (!Locked) {
    214         var items = new List<object>();
    215         foreach (ListViewItem listViewItem in variableListView.SelectedItems) {
    216           var item = (KeyValuePair<string, object>)listViewItem.Tag as KeyValuePair<string, object>?;
    217           if (item != null) items.Add(item.Value.Value);
    218         }
    219 
    220         if (items.Count > 0) {
    221           var data = new DataObject();
    222           if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
    223           else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
    224           if (ReadOnly) {
    225             DoDragDrop(data, DragDropEffects.Copy);
    226           } else {
    227             var result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
    228             if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
    229               foreach (string item in items) Content.Remove(item);
    230             }
    231           }
    232         }
     228      if (!Locked && variableListView.SelectedItems.Count == 1) {
     229        var listViewItem = variableListView.SelectedItems[0];
     230        var item = (KeyValuePair<string, object>)listViewItem.Tag;
     231        var data = new DataObject(HeuristicLab.Common.Constants.DragDropDataFormat, item.Value);
     232        DoDragDrop(data, DragDropEffects.Copy);
    233233      }
    234234    }
     
    239239      e.Effect = DragDropEffects.None;
    240240      if (validDragOperation) {
    241         if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
    242         else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
    243         else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
    244         else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
    245         else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
     241        if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
    246242      }
    247243    }
    248244    protected virtual void variableListView_DragDrop(object sender, DragEventArgs e) {
    249       if (e.Effect != DragDropEffects.None) {
     245      if (e.Effect == DragDropEffects.Copy) {
    250246        object item = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
    251         if (e.Effect.HasFlag(DragDropEffects.Copy)) {
    252           var cloner = new Cloner();
    253           var dc = item as IDeepCloneable;
    254           if (dc != null) item = cloner.Clone(dc);
    255         }
    256         string name = GenerateNewVariableName();
     247        var cloner = new Cloner();
     248        var dc = item as IDeepCloneable;
     249        if (dc != null) item = cloner.Clone(dc);
     250        var namedItem = item as INamedItem;
     251        bool nameValid = namedItem != null && !string.IsNullOrEmpty(namedItem.Name);
     252        string name = nameValid ? GenerateNewVariableName(namedItem.Name, false) : GenerateNewVariableName();
    257253        Content.Add(name, item);
    258254        var listViewItem = variableListView.FindItemWithText(name);
    259         listViewItem.BeginEdit();
     255        if (!nameValid) listViewItem.BeginEdit();
    260256      }
    261257    }
    262258
    263259    private readonly Regex SafeVariableNameRegex = new Regex("^[@]?[_a-zA-Z][_a-zA-Z0-9]*$");
     260    private const string DefaultVariableName = "enter_name";
     261
    264262    private void variableListView_AfterLabelEdit(object sender, LabelEditEventArgs e) {
    265263      string name = e.Label;
     
    327325        foreach (var item in e.Items)
    328326          RemoveVariable(item);
    329         RebuildImageList();
    330327        AdjustListViewColumnSizes();
    331328      }
     
    337334        foreach (var item in e.OldItems)
    338335          RemoveVariable(item);
    339         RebuildImageList();
    340336        foreach (var item in e.Items)
    341337          AddVariable(item);
     
    352348          item.SubItems[2].Text = variable.Value.Value.GetType().ToString();
    353349          item.ToolTipText = GetToolTipText(variable.Value, item.ImageIndex != 0);
    354           return;
    355350        }
    356351      }
     
    367362      foreach (ColumnHeader ch in variableListView.Columns)
    368363        ch.Width = -2;
    369     }
    370     protected virtual void RebuildImageList() {
    371       variableListView.SmallImageList.Images.Clear();
    372       variableListView.SmallImageList.Images.AddRange(new Image[] {
    373         HeuristicLab.Common.Resources.VSImageLibrary.Error,
    374         HeuristicLab.Common.Resources.VSImageLibrary.Object,
    375         HeuristicLab.Common.Resources.VSImageLibrary.Nothing
    376       });
    377       foreach (ListViewItem listViewItem in variableListView.Items) {
    378         var variable = (KeyValuePair<string, object>)listViewItem.Tag;
    379         bool serializable = IsSerializable(variable);
    380         if (serializable) {
    381           listViewItem.ImageIndex = variable.Value == null ? 2 : 1;
    382         } else listViewItem.ImageIndex = 0;
    383       }
    384364    }
    385365
     
    394374      };
    395375      string toolTipText = string.Join(Environment.NewLine, lines);
     376      if (!SafeVariableNameRegex.IsMatch(variable.Key))
     377        toolTipText = "Caution: Identifier is no valid C# identifier!" + Environment.NewLine + toolTipText;
    396378      if (!serializable)
    397379        toolTipText = "Caution: Type is not serializable!" + Environment.NewLine + toolTipText;
     
    399381    }
    400382
    401     private string GenerateNewVariableName(string defaultName = "enter_name") {
     383    private string GenerateNewVariableName(string defaultName = DefaultVariableName, bool generateValidIdentifier = true) {
     384      if (generateValidIdentifier && !SafeVariableNameRegex.IsMatch(defaultName))
     385        defaultName = DefaultVariableName;
    402386      if (Content.ContainsKey(defaultName)) {
    403387        int i = 1;
     388        string formatString = generateValidIdentifier ? "{0}{1}" : "{0} ({1})";
    404389        string newName;
    405390        do {
    406           newName = defaultName + i++;
     391          newName = string.Format(formatString, defaultName, i++);
    407392        } while (Content.ContainsKey(newName));
    408393        return newName;
  • trunk/sources/HeuristicLab.Scripting/3.3/CSharpScript.cs

    r11135 r11436  
    4242// use 'vars.Clear()' to remove all variables
    4343// use 'foreach (KeyValuePair<string, object> v in vars) { ... }' to iterate over all variables
     44// use 'variables' to work with IEnumerable<T> extension methods on the script's variable store
    4445
    4546using System;
  • trunk/sources/HeuristicLab.Scripting/3.3/CSharpScriptBase.cs

    r10857 r11436  
    2727namespace HeuristicLab.Scripting {
    2828  public abstract class CSharpScriptBase {
     29    protected Variables variables;
    2930    protected dynamic vars;
    3031
     
    4142
    4243    internal void Execute(VariableStore variableStore) {
    43       vars = new Variables(variableStore);
     44      variables = vars = new Variables(variableStore);
    4445      Main();
    4546    }
Note: See TracChangeset for help on using the changeset viewer.