Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.3/SymbolicExpressionView.cs @ 3566

Last change on this file since 3566 was 3566, checked in by mkommend, 14 years ago

removed ctors with contents in all views (ticket #972)

File size: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using HeuristicLab.MainForm;
10using HeuristicLab.MainForm.WindowsForms;
11
12namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
13  [View("SymbolicExpression View")]
14  [Content(typeof(SymbolicExpressionTree), false)]
15  public partial class SymbolicExpressionView : AsynchronousContentView {
16    public new SymbolicExpressionTree Content {
17      get { return (SymbolicExpressionTree)base.Content; }
18      set { base.Content = value; }
19    }
20
21    public SymbolicExpressionView() {
22      InitializeComponent();
23      Caption = "SymbolicExpression View";
24    }
25
26    protected override void OnContentChanged() {
27      base.OnContentChanged();
28      if (Content == null) {
29        Caption = "SymbolicExpression View";
30        textBox.Text = string.Empty;
31      } else {
32        textBox.Text = SymbolicExpression(Content.Root, 0);
33      }
34      SetEnabledStateOfControls();
35    }
36
37    protected override void OnReadOnlyChanged() {
38      base.OnReadOnlyChanged();
39      SetEnabledStateOfControls();
40    }
41
42    private void SetEnabledStateOfControls() {
43      textBox.Enabled = Content != null;
44      textBox.ReadOnly = ReadOnly;
45    }
46
47    private static string SymbolicExpression(SymbolicExpressionTreeNode node, int indentLength) {
48      StringBuilder strBuilder = new StringBuilder();
49      strBuilder.Append(' ', indentLength); strBuilder.Append("(");
50      // internal nodes or leaf nodes?
51      if (node.SubTrees.Count > 0) {
52        // symbol on same line as '('
53        strBuilder.AppendLine(node.ToString());
54        // each subtree expression on a new line
55        // and closing ')' also on new line
56        foreach (var subtree in node.SubTrees) {
57          strBuilder.AppendLine(SymbolicExpression(subtree, indentLength + 2));
58        }
59        strBuilder.Append(' ', indentLength); strBuilder.Append(")");
60      } else {
61        // symbol in the same line with as '(' and ')'
62        strBuilder.Append(node.ToString());
63        strBuilder.Append(")");
64      }
65      return strBuilder.ToString();
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.