Changeset 2668
- Timestamp:
- 01/21/10 18:36:15 (15 years ago)
- 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 35 35 using System.Data.Linq; 36 36 using System.Xml.XPath; 37 using HeuristicLab.PluginInfrastructure; 37 38 38 39 namespace HeuristicLab.Operators.Programmable { … … 65 66 private object syncRoot = new object(); 66 67 68 public readonly Dictionary<string, List<Assembly>> Plugins; 69 67 70 protected Dictionary<Assembly, bool> Assemblies; 68 71 public IEnumerable<Assembly> AvailableAssemblies { … … 84 87 85 88 public void SelectAssembly(Assembly a) { 86 Assemblies[a] = true; 89 if (a != null && Assemblies.ContainsKey(a)) 90 Assemblies[a] = true; 87 91 } 88 92 89 93 public void UnselectAssembly(Assembly a) { 90 Assemblies[a] = false; 94 if (a != null && Assemblies.ContainsKey(a)) 95 Assemblies[a] = false; 91 96 } 92 97 … … 132 137 Assemblies = DiscoverAssemblies(); 133 138 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; 134 159 } 135 160 … … 248 273 foreach (var ns in Namespaces) 249 274 if (possibleNamespaces.Contains(ns)) 250 yield return ns; 275 yield return ns; 251 276 } 252 277 -
trunk/sources/HeuristicLab.Operators.Programmable/3.2/ProgrammableOperatorView.Designer.cs
r2660 r2668 49 49 System.Windows.Forms.GroupBox groupBox1; 50 50 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(); 53 52 this.groupBox3 = new System.Windows.Forms.GroupBox(); 54 53 this.showCodeButton = new System.Windows.Forms.Button(); 54 this.codeEditor = new HeuristicLab.CodeEditor.CodeEditor(); 55 55 this.compileButton = new System.Windows.Forms.Button(); 56 56 this.tabControl = new System.Windows.Forms.TabControl(); … … 66 66 this.descriptionTabPage = new System.Windows.Forms.TabPage(); 67 67 this.descriptionTextBox = new System.Windows.Forms.TextBox(); 68 this. codeEditor = new HeuristicLab.CodeEditor.CodeEditor();68 this.namespacesTreeView = new System.Windows.Forms.TreeView(); 69 69 splitContainer1 = new System.Windows.Forms.SplitContainer(); 70 70 splitContainer2 = new System.Windows.Forms.SplitContainer(); … … 125 125 // groupBox1 126 126 // 127 groupBox1.Controls.Add(this.assemblies ListBox);127 groupBox1.Controls.Add(this.assembliesTreeView); 128 128 groupBox1.Dock = System.Windows.Forms.DockStyle.Fill; 129 129 groupBox1.Location = new System.Drawing.Point(0, 0); … … 134 134 groupBox1.Text = "Assemblies"; 135 135 // 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); 147 145 // 148 146 // groupBox2 149 147 // 150 groupBox2.Controls.Add(this.namespaces ListBox);148 groupBox2.Controls.Add(this.namespacesTreeView); 151 149 groupBox2.Dock = System.Windows.Forms.DockStyle.Fill; 152 150 groupBox2.Location = new System.Drawing.Point(0, 0); … … 157 155 groupBox2.Text = "Namespaces"; 158 156 // 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); 170 167 // 171 168 // groupBox3 … … 194 191 this.showCodeButton.Name = "showCodeButton"; 195 192 this.showCodeButton.Size = new System.Drawing.Size(133, 23); 196 this.showCodeButton.TabIndex = 7;193 this.showCodeButton.TabIndex = 4; 197 194 this.showCodeButton.Text = "Show Generated Code"; 198 195 this.showCodeButton.UseVisualStyleBackColor = true; … … 360 357 this.codeEditor.Size = new System.Drawing.Size(675, 309); 361 358 this.codeEditor.Suffix = "\n return null;\n }\n}"; 362 this.codeEditor.TabIndex = 6;359 this.codeEditor.TabIndex = 0; 363 360 this.codeEditor.UserCode = "\n\n\n"; 364 361 this.codeEditor.TextEditorValidated += new System.EventHandler(this.codeEditor_Validated); … … 405 402 private System.Windows.Forms.Button removeVariableInfoButton; 406 403 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; 409 406 private System.Windows.Forms.GroupBox groupBox3; 410 407 private HeuristicLab.CodeEditor.CodeEditor codeEditor; … … 412 409 private System.Windows.Forms.Button showCodeButton; 413 410 411 414 412 } 415 413 } -
trunk/sources/HeuristicLab.Operators.Programmable/3.2/ProgrammableOperatorView.cs
r2660 r2668 51 51 ProgrammableOperator = programmableOperator; 52 52 } 53 53 54 54 protected override void RemoveItemEvents() { 55 55 operatorBaseVariableInfosView.Operator = null; … … 82 82 83 83 public class Operator { 84 public static IOperation Execute(IOperator op, IScope scope, parameters ...) {"; 84 public static IOperation Execute(IOperator op, IScope scope, parameters ...) { 85 "; 85 86 codeEditor.Suffix = @" 86 87 return null; 87 88 } 88 }"; 89 assemblies ListBox.DataSource = null;90 } else { 89 }"; 90 assembliesTreeView.Nodes.Clear(); 91 } else { 91 92 codeEditor.Enabled = true; 92 93 addVariableInfoButton.Enabled = true; … … 108 109 } 109 110 codeEditor.ScrollAfterPrefix(); 110 } 111 } 112 113 111 } 112 } 113 114 114 115 private string GetGeneratedPrefix() { 115 116 StringBuilder prefix = new StringBuilder(); … … 119 120 prefix.AppendLine(); 120 121 prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {"); 121 prefix.Append(" ").Append(ProgrammableOperator.Signature).Append (" {");122 prefix.Append(" ").Append(ProgrammableOperator.Signature).AppendLine(" {"); 122 123 return prefix.ToString(); 123 124 } … … 140 141 } else { 141 142 ProgrammableOperator.AddVariableInfo(dialog.VariableInfo); 142 Recompile(); 143 Recompile(); 143 144 } 144 145 } … … 151 152 for (int i = 0; i < selected.Length; i++) 152 153 ProgrammableOperator.RemoveVariableInfo(selected[i].FormalName); 154 Recompile(); 153 155 } 154 156 155 157 private void Recompile() { 156 158 try { 157 ProgrammableOperator.Compile(); 159 ProgrammableOperator.Compile(); 158 160 MessageBox.Show("Compilation successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); 159 161 } catch (Exception ex) { … … 161 163 } 162 164 UpdateControls(); 163 codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator"); 165 codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator"); 164 166 } 165 167 166 168 private void compileButton_Click(object sender, EventArgs e) { 167 169 Recompile(); 168 } 170 } 169 171 170 172 #region ProgrammableOperator Events … … 177 179 #endregion 178 180 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) { 184 182 if (initializing) 185 183 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; 194 188 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(); 197 199 codeEditor.Prefix = GetGeneratedPrefix(); 198 200 } … … 200 202 private bool initializing = false; 201 203 private void InitializeAssemblyList() { 202 assembliesListBox.Items.Clear(); 204 initializing = true; 205 assembliesTreeView.BeginUpdate(); 206 assembliesTreeView.Nodes.Clear(); 203 207 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(); 210 225 initializing = false; 211 226 } … … 213 228 private void InitializeNamespacesList() { 214 229 initializing = true; 215 namespaces ListBox.Items.Clear();230 namespacesTreeView.Nodes.Clear(); 216 231 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)); 220 234 codeEditor.Prefix = GetGeneratedPrefix(); 221 235 initializing = false; 222 236 } 223 237 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) { 225 258 if (initializing) 226 259 return; 227 if (e.N ewValue == 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); 231 264 } 232 265 codeEditor.Prefix = GetGeneratedPrefix(); … … 237 270 } 238 271 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 264 273 } 265 274 }
Note: See TracChangeset
for help on using the changeset viewer.