Changeset 2660 for trunk/sources/HeuristicLab.Operators.Programmable/3.2/ProgrammableOperatorView.cs
- Timestamp:
- 01/20/10 16:16:02 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Operators.Programmable/3.2/ProgrammableOperatorView.cs
r1530 r2660 26 26 using System.Data; 27 27 using System.Text; 28 using System.Linq; 28 29 using System.Windows.Forms; 29 30 using HeuristicLab.Core; 30 31 using HeuristicLab.Operators; 32 using System.CodeDom.Compiler; 33 using System.Reflection; 34 using HeuristicLab.CodeEditor; 31 35 32 36 namespace HeuristicLab.Operators.Programmable { 37 33 38 public partial class ProgrammableOperatorView : ViewBase { 39 34 40 public ProgrammableOperator ProgrammableOperator { 35 41 get { return (ProgrammableOperator)Item; } … … 40 46 InitializeComponent(); 41 47 } 48 42 49 public ProgrammableOperatorView(ProgrammableOperator programmableOperator) 43 50 : this() { 44 51 ProgrammableOperator = programmableOperator; 45 52 } 46 53 47 54 protected override void RemoveItemEvents() { 48 55 operatorBaseVariableInfosView.Operator = null; … … 66 73 base.UpdateControls(); 67 74 if (ProgrammableOperator == null) { 68 code TextBox.Text = "";69 code TextBox.Enabled = false;75 codeEditor.Text = ""; 76 codeEditor.Enabled = false; 70 77 addVariableInfoButton.Enabled = false; 71 78 removeVariableInfoButton.Enabled = false; 72 79 descriptionTextBox.Text = ""; 73 80 descriptionTextBox.Enabled = false; 74 } else { 75 codeTextBox.Text = ProgrammableOperator.Code; 76 codeTextBox.Enabled = true; 81 codeEditor.Prefix = @"using System 82 83 public class Operator { 84 public static IOperation Execute(IOperator op, IScope scope, parameters ...) {"; 85 codeEditor.Suffix = @" 86 return null; 87 } 88 }"; 89 assembliesListBox.DataSource = null; 90 } else { 91 codeEditor.Enabled = true; 77 92 addVariableInfoButton.Enabled = true; 78 93 removeVariableInfoButton.Enabled = operatorBaseVariableInfosView.SelectedVariableInfos.Count > 0; 79 94 descriptionTextBox.Text = ProgrammableOperator.Description; 80 95 descriptionTextBox.Enabled = true; 81 } 96 codeEditor.Prefix = GetGeneratedPrefix(); 97 codeEditor.Suffix = @" 98 return null; 99 } 100 }"; 101 codeEditor.UserCode = ProgrammableOperator.Code; 102 if (codeEditor.UserCode == "") 103 codeEditor.UserCode = "\n\n\n"; 104 InitializeAssemblyList(); 105 InitializeNamespacesList(); 106 foreach (var a in ProgrammableOperator.SelectedAssemblies) { 107 codeEditor.AddAssembly(a); 108 } 109 codeEditor.ScrollAfterPrefix(); 110 } 111 } 112 113 114 private string GetGeneratedPrefix() { 115 StringBuilder prefix = new StringBuilder(); 116 foreach (var ns in ProgrammableOperator.GetSelectedAndValidNamespaces()) { 117 prefix.Append("using ").Append(ns).AppendLine(";"); 118 } 119 prefix.AppendLine(); 120 prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {"); 121 prefix.Append(" ").Append(ProgrammableOperator.Signature).Append(" {"); 122 return prefix.ToString(); 82 123 } 83 124 … … 85 126 removeVariableInfoButton.Enabled = operatorBaseVariableInfosView.SelectedVariableInfos.Count > 0; 86 127 } 87 88 #region Validated Events 89 private void codeTextBox_Validated(object sender, EventArgs e) { 90 ProgrammableOperator.Code = codeTextBox.Text; 128 private void codeEditor_Validated(object sender, EventArgs e) { 129 ProgrammableOperator.Code = codeEditor.UserCode; 91 130 } 92 131 private void descriptionTextBox_Validated(object sender, EventArgs e) { 93 132 ProgrammableOperator.SetDescription(descriptionTextBox.Text); 94 133 } 95 #endregion 96 97 #region Click Events 98 private void compileButton_Click(object sender, EventArgs e) { 99 try { 100 ProgrammableOperator.Compile(); 101 MessageBox.Show("Compilation successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); 102 } 103 catch (Exception ex) { 104 Auxiliary.ShowErrorMessageBox(ex); 105 } 106 } 134 107 135 private void addVariableInfoButton_Click(object sender, EventArgs e) { 108 136 AddVariableInfoDialog dialog = new AddVariableInfoDialog(); 109 137 if (dialog.ShowDialog(this) == DialogResult.OK) { 110 if (ProgrammableOperator.GetVariableInfo(dialog.VariableInfo.FormalName) != null) 138 if (ProgrammableOperator.GetVariableInfo(dialog.VariableInfo.FormalName) != null) { 111 139 Auxiliary.ShowErrorMessageBox("A variable info with the same formal name already exists."); 112 else140 } else { 113 141 ProgrammableOperator.AddVariableInfo(dialog.VariableInfo); 142 Recompile(); 143 } 114 144 } 115 145 dialog.Dispose(); 116 146 } 147 117 148 private void removeVariableInfoButton_Click(object sender, EventArgs e) { 118 149 IVariableInfo[] selected = new IVariableInfo[operatorBaseVariableInfosView.SelectedVariableInfos.Count]; … … 121 152 ProgrammableOperator.RemoveVariableInfo(selected[i].FormalName); 122 153 } 123 #endregion 154 155 private void Recompile() { 156 try { 157 ProgrammableOperator.Compile(); 158 MessageBox.Show("Compilation successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); 159 } catch (Exception ex) { 160 Auxiliary.ShowErrorMessageBox(ex); 161 } 162 UpdateControls(); 163 codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator"); 164 } 165 166 private void compileButton_Click(object sender, EventArgs e) { 167 Recompile(); 168 } 124 169 125 170 #region ProgrammableOperator Events 126 171 private void ProgrammableOperator_CodeChanged(object sender, EventArgs e) { 127 code TextBox.Text = ProgrammableOperator.Code;172 codeEditor.Text = ProgrammableOperator.Code; 128 173 } 129 174 private void ProgrammableOperator_DescriptionChanged(object sender, EventArgs e) { … … 131 176 } 132 177 #endregion 178 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) { 184 if (initializing) 185 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 { 194 return; 195 } 196 InitializeNamespacesList(); 197 codeEditor.Prefix = GetGeneratedPrefix(); 198 } 199 200 private bool initializing = false; 201 private void InitializeAssemblyList() { 202 assembliesListBox.Items.Clear(); 203 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 } 210 initializing = false; 211 } 212 213 private void InitializeNamespacesList() { 214 initializing = true; 215 namespacesListBox.Items.Clear(); 216 var selectedNamespaces = new HashSet<string>(ProgrammableOperator.Namespaces); 217 foreach (var ns in ProgrammableOperator.GetAllNamespaces(true)) { 218 namespacesListBox.Items.Add(ns, selectedNamespaces.Contains(ns)); 219 } 220 codeEditor.Prefix = GetGeneratedPrefix(); 221 initializing = false; 222 } 223 224 private void namespacesListBox_ItemCheck(object sender, ItemCheckEventArgs e) { 225 if (initializing) 226 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]); 231 } 232 codeEditor.Prefix = GetGeneratedPrefix(); 233 } 234 235 private void showCodeButton_Click(object sender, EventArgs e) { 236 new CodeViewer(ProgrammableOperator.CompilationUnitCode).ShowDialog(this); 237 } 238 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 } 133 264 } 134 265 }
Note: See TracChangeset
for help on using the changeset viewer.