Changeset 2668


Ignore:
Timestamp:
01/21/10 18:36:15 (10 years ago)
Author:
epitzer
Message:

Group assemblies by plugin and replace assembly and namespace list boxes with tree views (#842)

Location:
trunk/sources/HeuristicLab.Operators.Programmable/3.2
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Operators.Programmable/3.2/ProgrammableOperator.cs

    r2660 r2668  
    3535using System.Data.Linq;
    3636using System.Xml.XPath;
     37using HeuristicLab.PluginInfrastructure;
    3738
    3839namespace HeuristicLab.Operators.Programmable {
     
    6566    private object syncRoot = new object();
    6667
     68    public readonly Dictionary<string, List<Assembly>> Plugins;
     69
    6770    protected Dictionary<Assembly, bool> Assemblies;
    6871    public IEnumerable<Assembly> AvailableAssemblies {
     
    8487
    8588    public void SelectAssembly(Assembly a) {
    86       Assemblies[a] = true;
     89      if (a != null && Assemblies.ContainsKey(a))
     90        Assemblies[a] = true;
    8791    }
    8892
    8993    public void UnselectAssembly(Assembly a) {
    90       Assemblies[a] = false;
     94      if (a != null && Assemblies.ContainsKey(a))
     95        Assemblies[a] = false;
    9196    }
    9297
     
    132137      Assemblies = DiscoverAssemblies();
    133138      namespaces = new HashSet<string>(DiscoverNamespaces());
     139      Plugins = GroupAssemblies();
     140    }
     141
     142    private Dictionary<string, List<Assembly>> GroupAssemblies() {
     143      var plugins = new Dictionary<string, List<Assembly>>();
     144      var assemblyNames = Assemblies.ToDictionary(a => a.Key.Location, a => a.Key);
     145      foreach (var plugin in ApplicationManager.Manager.Plugins) {
     146        var aList = new List<Assembly>();
     147        foreach (var aName in plugin.Assemblies) {
     148          Assembly a;
     149          assemblyNames.TryGetValue(aName, out a);
     150          if (a != null) {
     151            aList.Add(a);
     152            assemblyNames.Remove(aName);
     153          }
     154        }
     155        plugins[plugin.Name] = aList;
     156      }
     157      plugins["other"] = assemblyNames.Values.ToList();
     158      return plugins;
    134159    }
    135160
     
    248273      foreach (var ns in Namespaces)
    249274        if (possibleNamespaces.Contains(ns))
    250           yield return ns;     
     275          yield return ns;
    251276    }
    252277
  • trunk/sources/HeuristicLab.Operators.Programmable/3.2/ProgrammableOperatorView.Designer.cs

    r2660 r2668  
    4949      System.Windows.Forms.GroupBox groupBox1;
    5050      System.Windows.Forms.GroupBox groupBox2;
    51       this.assembliesListBox = new System.Windows.Forms.CheckedListBox();
    52       this.namespacesListBox = new System.Windows.Forms.CheckedListBox();
     51      this.assembliesTreeView = new System.Windows.Forms.TreeView();
    5352      this.groupBox3 = new System.Windows.Forms.GroupBox();
    5453      this.showCodeButton = new System.Windows.Forms.Button();
     54      this.codeEditor = new HeuristicLab.CodeEditor.CodeEditor();
    5555      this.compileButton = new System.Windows.Forms.Button();
    5656      this.tabControl = new System.Windows.Forms.TabControl();
     
    6666      this.descriptionTabPage = new System.Windows.Forms.TabPage();
    6767      this.descriptionTextBox = new System.Windows.Forms.TextBox();
    68       this.codeEditor = new HeuristicLab.CodeEditor.CodeEditor();
     68      this.namespacesTreeView = new System.Windows.Forms.TreeView();
    6969      splitContainer1 = new System.Windows.Forms.SplitContainer();
    7070      splitContainer2 = new System.Windows.Forms.SplitContainer();
     
    125125      // groupBox1
    126126      //
    127       groupBox1.Controls.Add(this.assembliesListBox);
     127      groupBox1.Controls.Add(this.assembliesTreeView);
    128128      groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
    129129      groupBox1.Location = new System.Drawing.Point(0, 0);
     
    134134      groupBox1.Text = "Assemblies";
    135135      //
    136       // assembliesListBox
    137       //
    138       this.assembliesListBox.CheckOnClick = true;
    139       this.assembliesListBox.Dock = System.Windows.Forms.DockStyle.Fill;
    140       this.assembliesListBox.FormattingEnabled = true;
    141       this.assembliesListBox.Location = new System.Drawing.Point(3, 16);
    142       this.assembliesListBox.Name = "assembliesListBox";
    143       this.assembliesListBox.Size = new System.Drawing.Size(334, 64);
    144       this.assembliesListBox.Sorted = true;
    145       this.assembliesListBox.TabIndex = 0;
    146       this.assembliesListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.assembliesListBox_ItemCheck);
     136      // assembliesTreeView
     137      //
     138      this.assembliesTreeView.CheckBoxes = true;
     139      this.assembliesTreeView.Dock = System.Windows.Forms.DockStyle.Fill;
     140      this.assembliesTreeView.Location = new System.Drawing.Point(3, 16);
     141      this.assembliesTreeView.Name = "assembliesTreeView";
     142      this.assembliesTreeView.Size = new System.Drawing.Size(334, 66);
     143      this.assembliesTreeView.TabIndex = 1;
     144      this.assembliesTreeView.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.assembliesTreeView_AfterCheck);
    147145      //
    148146      // groupBox2
    149147      //
    150       groupBox2.Controls.Add(this.namespacesListBox);
     148      groupBox2.Controls.Add(this.namespacesTreeView);
    151149      groupBox2.Dock = System.Windows.Forms.DockStyle.Fill;
    152150      groupBox2.Location = new System.Drawing.Point(0, 0);
     
    157155      groupBox2.Text = "Namespaces";
    158156      //
    159       // namespacesListBox
    160       //
    161       this.namespacesListBox.CheckOnClick = true;
    162       this.namespacesListBox.Dock = System.Windows.Forms.DockStyle.Fill;
    163       this.namespacesListBox.FormattingEnabled = true;
    164       this.namespacesListBox.Location = new System.Drawing.Point(3, 16);
    165       this.namespacesListBox.Name = "namespacesListBox";
    166       this.namespacesListBox.Size = new System.Drawing.Size(343, 64);
    167       this.namespacesListBox.Sorted = true;
    168       this.namespacesListBox.TabIndex = 1;
    169       this.namespacesListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.namespacesListBox_ItemCheck);
     157      // namespacesTreeView
     158      //
     159      this.namespacesTreeView.CheckBoxes = true;
     160      this.namespacesTreeView.Dock = System.Windows.Forms.DockStyle.Fill;
     161      this.namespacesTreeView.Location = new System.Drawing.Point(3, 16);
     162      this.namespacesTreeView.Name = "namespacesTreeView";
     163      this.namespacesTreeView.PathSeparator = ".";
     164      this.namespacesTreeView.Size = new System.Drawing.Size(343, 66);
     165      this.namespacesTreeView.TabIndex = 2;
     166      this.namespacesTreeView.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.namespacesTreeView_AfterCheck);
    170167      //
    171168      // groupBox3
     
    194191      this.showCodeButton.Name = "showCodeButton";
    195192      this.showCodeButton.Size = new System.Drawing.Size(133, 23);
    196       this.showCodeButton.TabIndex = 7;
     193      this.showCodeButton.TabIndex = 4;
    197194      this.showCodeButton.Text = "Show Generated Code";
    198195      this.showCodeButton.UseVisualStyleBackColor = true;
     
    360357      this.codeEditor.Size = new System.Drawing.Size(675, 309);
    361358      this.codeEditor.Suffix = "\n    return null;\n  }\n}";
    362       this.codeEditor.TabIndex = 6;
     359      this.codeEditor.TabIndex = 0;
    363360      this.codeEditor.UserCode = "\n\n\n";
    364361      this.codeEditor.TextEditorValidated += new System.EventHandler(this.codeEditor_Validated);
     
    405402    private System.Windows.Forms.Button removeVariableInfoButton;
    406403    private System.Windows.Forms.Button addVariableInfoButton;
    407     private System.Windows.Forms.CheckedListBox assembliesListBox;
    408     private System.Windows.Forms.CheckedListBox namespacesListBox;
     404    private System.Windows.Forms.TreeView assembliesTreeView;
     405    private System.Windows.Forms.TreeView namespacesTreeView;
    409406    private System.Windows.Forms.GroupBox groupBox3;
    410407    private HeuristicLab.CodeEditor.CodeEditor codeEditor;
     
    412409    private System.Windows.Forms.Button showCodeButton;
    413410
     411
    414412  }
    415413}
  • trunk/sources/HeuristicLab.Operators.Programmable/3.2/ProgrammableOperatorView.cs

    r2660 r2668  
    5151      ProgrammableOperator = programmableOperator;
    5252    }
    53    
     53
    5454    protected override void RemoveItemEvents() {
    5555      operatorBaseVariableInfosView.Operator = null;
     
    8282
    8383public class Operator {
    84   public static IOperation Execute(IOperator op, IScope scope, parameters ...) {";
     84  public static IOperation Execute(IOperator op, IScope scope, parameters ...) {
     85";
    8586        codeEditor.Suffix = @"
    8687    return null;
    8788  }
    88 }";   
    89         assembliesListBox.DataSource = null;
    90       } else {       
     89}";
     90        assembliesTreeView.Nodes.Clear();
     91      } else {
    9192        codeEditor.Enabled = true;
    9293        addVariableInfoButton.Enabled = true;
     
    108109        }
    109110        codeEditor.ScrollAfterPrefix();
    110       }     
    111     }
    112 
    113    
     111      }
     112    }
     113
     114
    114115    private string GetGeneratedPrefix() {
    115116      StringBuilder prefix = new StringBuilder();
     
    119120      prefix.AppendLine();
    120121      prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {");
    121       prefix.Append("  ").Append(ProgrammableOperator.Signature).Append(" {");
     122      prefix.Append("  ").Append(ProgrammableOperator.Signature).AppendLine(" {");
    122123      return prefix.ToString();
    123124    }
     
    140141        } else {
    141142          ProgrammableOperator.AddVariableInfo(dialog.VariableInfo);
    142           Recompile();         
     143          Recompile();
    143144        }
    144145      }
     
    151152      for (int i = 0; i < selected.Length; i++)
    152153        ProgrammableOperator.RemoveVariableInfo(selected[i].FormalName);
     154      Recompile();
    153155    }
    154156
    155157    private void Recompile() {
    156158      try {
    157         ProgrammableOperator.Compile();       
     159        ProgrammableOperator.Compile();
    158160        MessageBox.Show("Compilation successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
    159161      } catch (Exception ex) {
     
    161163      }
    162164      UpdateControls();
    163       codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator");     
     165      codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator");
    164166    }
    165167
    166168    private void compileButton_Click(object sender, EventArgs e) {
    167169      Recompile();
    168     }   
     170    }
    169171
    170172    #region ProgrammableOperator Events
     
    177179    #endregion
    178180
    179     public static Assembly GetAssembly(CheckedListBox box, int index) {
    180       return (Assembly)(((CheckedListBoxItem)box.Items[index]).Tag);
    181     }
    182 
    183     private void assembliesListBox_ItemCheck(object sender, ItemCheckEventArgs e) {
     181    private void assembliesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
    184182      if (initializing)
    185183        return;
    186       Assembly a = GetAssembly(assembliesListBox, e.Index);
    187       if (e.NewValue == CheckState.Checked) {               
    188         ProgrammableOperator.SelectAssembly(a);
    189         codeEditor.AddAssembly(a);
    190       } else if (e.NewValue == CheckState.Unchecked) {
    191         ProgrammableOperator.UnselectAssembly(a);
    192         codeEditor.RemoveAssembly(a);
    193       } else {
     184      Assembly a = e.Node.Tag as Assembly;
     185      if (a == null && e.Node.Nodes.Count > 0) {
     186        foreach (TreeNode n in e.Node.Nodes)
     187          n.Checked = e.Node.Checked;
    194188        return;
    195       }
    196       InitializeNamespacesList();     
     189      } else {
     190        if (e.Node.Checked) {
     191          ProgrammableOperator.SelectAssembly(a);
     192          codeEditor.AddAssembly(a);
     193        } else {
     194          ProgrammableOperator.UnselectAssembly(a);
     195          codeEditor.RemoveAssembly(a);
     196        }
     197      }
     198      InitializeNamespacesList();
    197199      codeEditor.Prefix = GetGeneratedPrefix();
    198200    }
     
    200202    private bool initializing = false;
    201203    private void InitializeAssemblyList() {
    202       assembliesListBox.Items.Clear();
     204      initializing = true;
     205      assembliesTreeView.BeginUpdate();
     206      assembliesTreeView.Nodes.Clear();
    203207      var selectedAssemblies = new HashSet<Assembly>(ProgrammableOperator.SelectedAssemblies);
    204       initializing = true;
    205       foreach (var a in ProgrammableOperator.AvailableAssemblies.ToList()) {
    206         assembliesListBox.Items.Add(
    207           new CheckedListBoxItem(a.GetName().Name, a),
    208           selectedAssemblies.Contains(a));
    209       }
     208      foreach (var p in ProgrammableOperator.Plugins) {
     209        var node = assembliesTreeView.Nodes.Add(p.Key);
     210        node.Tag = p;
     211        foreach (var a in p.Value) {
     212          var aNode = node.Nodes.Add(a.GetName().Name);
     213          aNode.Tag = a;
     214          if (selectedAssemblies.Contains(a))
     215            aNode.Checked = true;
     216        }
     217        if (node.Nodes.Count == 1 && node.Nodes[0].Name == node.Nodes[0].Name) {
     218          node.Tag = node.Nodes[0].Tag;
     219          node.Nodes.Clear();
     220        } else if (node.Nodes.Count > 0 && node.Nodes.Cast<TreeNode>().All(n => n.Checked)) {
     221          node.Checked = true;
     222        }
     223      }
     224      assembliesTreeView.EndUpdate();
    210225      initializing = false;
    211226    }
     
    213228    private void InitializeNamespacesList() {
    214229      initializing = true;
    215       namespacesListBox.Items.Clear();
     230      namespacesTreeView.Nodes.Clear();
    216231      var selectedNamespaces = new HashSet<string>(ProgrammableOperator.Namespaces);
    217       foreach (var ns in ProgrammableOperator.GetAllNamespaces(true)) {
    218         namespacesListBox.Items.Add(ns, selectedNamespaces.Contains(ns));
    219       }
     232      foreach (var ns in ProgrammableOperator.GetAllNamespaces(true))
     233        AddNamespace(namespacesTreeView.Nodes, ns, selectedNamespaces.Contains(ns));
    220234      codeEditor.Prefix = GetGeneratedPrefix();
    221235      initializing = false;
    222236    }
    223237
    224     private void namespacesListBox_ItemCheck(object sender, ItemCheckEventArgs e) {
     238    private void AddNamespace(TreeNodeCollection parentNodes, string ns, bool isSelected) {
     239      int dotIndex = ns.IndexOf('.');
     240      string prefix = ns;
     241      if (dotIndex != -1)
     242        prefix = ns.Substring(0, dotIndex);
     243      TreeNode node = null;
     244      if (parentNodes.ContainsKey(prefix)) {
     245        node = parentNodes[prefix];
     246      } else {
     247        node = parentNodes.Add(prefix, prefix);
     248      }
     249      if (dotIndex != -1 && dotIndex + 1 < ns.Length) {
     250        AddNamespace(node.Nodes, ns.Substring(dotIndex + 1, ns.Length - (dotIndex + 1)), isSelected);
     251        if (isSelected)
     252          node.Expand();
     253      }  else
     254        node.Checked = isSelected;
     255    }
     256
     257    private void namespacesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
    225258      if (initializing)
    226259        return;
    227       if (e.NewValue == CheckState.Checked) {
    228         ProgrammableOperator.SelectNamespace((string)namespacesListBox.Items[e.Index]);
    229       } else if (e.NewValue == CheckState.Unchecked) {
    230         ProgrammableOperator.UnselectNamespace((string)namespacesListBox.Items[e.Index]);
     260      if (e.Node.Checked) {
     261        ProgrammableOperator.SelectNamespace(e.Node.FullPath);
     262      } else {
     263        ProgrammableOperator.UnselectNamespace(e.Node.FullPath);
    231264      }
    232265      codeEditor.Prefix = GetGeneratedPrefix();
     
    237270    }
    238271
    239   }
    240 
    241   public class CheckedListBoxItem : IComparable {
    242 
    243     public object Tag { get; private set; }
    244     public string Text { get; private set; }
    245 
    246     public CheckedListBoxItem(string text, object tag) {
    247       Text = text;
    248       Tag = tag;
    249     }
    250 
    251     public override string ToString() {
    252       return Text;
    253     }
    254 
    255     public int CompareTo(object obj) {
    256       if (obj == null)
    257         throw new ArgumentException("cannot compare to null");
    258       if (!(obj is CheckedListBoxItem))
    259         throw new ArgumentException(string.Format(
    260           "cannot compare CheckedListBoxItem to {0}",
    261           obj.GetType().Name));
    262       return Text.CompareTo(((CheckedListBoxItem)obj).Text);
    263     }
     272
    264273  }
    265274}
Note: See TracChangeset for help on using the changeset viewer.