Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/20/10 16:16:02 (14 years ago)
Author:
epitzer
Message:

New ProgrammableOperator with syntax highlighting, code completion, configurable assemblies and namespaces (#842)

File:
1 edited

Legend:

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

    r1530 r2660  
    2626using System.Data;
    2727using System.Text;
     28using System.Linq;
    2829using System.Windows.Forms;
    2930using HeuristicLab.Core;
    3031using HeuristicLab.Operators;
     32using System.CodeDom.Compiler;
     33using System.Reflection;
     34using HeuristicLab.CodeEditor;
    3135
    3236namespace HeuristicLab.Operators.Programmable {
     37
    3338  public partial class ProgrammableOperatorView : ViewBase {
     39
    3440    public ProgrammableOperator ProgrammableOperator {
    3541      get { return (ProgrammableOperator)Item; }
     
    4046      InitializeComponent();
    4147    }
     48
    4249    public ProgrammableOperatorView(ProgrammableOperator programmableOperator)
    4350      : this() {
    4451      ProgrammableOperator = programmableOperator;
    4552    }
    46 
     53   
    4754    protected override void RemoveItemEvents() {
    4855      operatorBaseVariableInfosView.Operator = null;
     
    6673      base.UpdateControls();
    6774      if (ProgrammableOperator == null) {
    68         codeTextBox.Text = "";
    69         codeTextBox.Enabled = false;
     75        codeEditor.Text = "";
     76        codeEditor.Enabled = false;
    7077        addVariableInfoButton.Enabled = false;
    7178        removeVariableInfoButton.Enabled = false;
    7279        descriptionTextBox.Text = "";
    7380        descriptionTextBox.Enabled = false;
    74       } else {
    75         codeTextBox.Text = ProgrammableOperator.Code;
    76         codeTextBox.Enabled = true;
     81        codeEditor.Prefix = @"using System
     82
     83public 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;
    7792        addVariableInfoButton.Enabled = true;
    7893        removeVariableInfoButton.Enabled = operatorBaseVariableInfosView.SelectedVariableInfos.Count > 0;
    7994        descriptionTextBox.Text = ProgrammableOperator.Description;
    8095        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();
    82123    }
    83124
     
    85126      removeVariableInfoButton.Enabled = operatorBaseVariableInfosView.SelectedVariableInfos.Count > 0;
    86127    }
    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;
    91130    }
    92131    private void descriptionTextBox_Validated(object sender, EventArgs e) {
    93132      ProgrammableOperator.SetDescription(descriptionTextBox.Text);
    94133    }
    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
    107135    private void addVariableInfoButton_Click(object sender, EventArgs e) {
    108136      AddVariableInfoDialog dialog = new AddVariableInfoDialog();
    109137      if (dialog.ShowDialog(this) == DialogResult.OK) {
    110         if (ProgrammableOperator.GetVariableInfo(dialog.VariableInfo.FormalName) != null)
     138        if (ProgrammableOperator.GetVariableInfo(dialog.VariableInfo.FormalName) != null) {
    111139          Auxiliary.ShowErrorMessageBox("A variable info with the same formal name already exists.");
    112         else
     140        } else {
    113141          ProgrammableOperator.AddVariableInfo(dialog.VariableInfo);
     142          Recompile();         
     143        }
    114144      }
    115145      dialog.Dispose();
    116146    }
     147
    117148    private void removeVariableInfoButton_Click(object sender, EventArgs e) {
    118149      IVariableInfo[] selected = new IVariableInfo[operatorBaseVariableInfosView.SelectedVariableInfos.Count];
     
    121152        ProgrammableOperator.RemoveVariableInfo(selected[i].FormalName);
    122153    }
    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    }   
    124169
    125170    #region ProgrammableOperator Events
    126171    private void ProgrammableOperator_CodeChanged(object sender, EventArgs e) {
    127       codeTextBox.Text = ProgrammableOperator.Code;
     172      codeEditor.Text = ProgrammableOperator.Code;
    128173    }
    129174    private void ProgrammableOperator_DescriptionChanged(object sender, EventArgs e) {
     
    131176    }
    132177    #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    }
    133264  }
    134265}
Note: See TracChangeset for help on using the changeset viewer.