Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3454 was 3454, checked in by swagner, 14 years ago

Adapted views according the new read-only property (#973)

File size: 2.3 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    public SymbolicExpressionView(SymbolicExpressionTree content)
27      : this() {
28      Content = content;
29    }
30
31
32    protected override void OnContentChanged() {
33      base.OnContentChanged();
34      if (Content == null) {
35        Caption = "SymbolicExpression View";
36        textBox.Text = string.Empty;
37      } else {
38        textBox.Text = SymbolicExpression(Content.Root, 0);
39      }
40      SetEnabledStateOfControls();
41    }
42
43    protected override void OnReadOnlyChanged() {
44      base.OnReadOnlyChanged();
45      SetEnabledStateOfControls();
46    }
47
48    private void SetEnabledStateOfControls() {
49      textBox.Enabled = Content != null;
50      textBox.ReadOnly = ReadOnly;
51    }
52
53    private static string SymbolicExpression(SymbolicExpressionTreeNode node, int indentLength) {
54      StringBuilder strBuilder = new StringBuilder();
55      strBuilder.Append(' ', indentLength); strBuilder.Append("(");
56      // internal nodes or leaf nodes?
57      if (node.SubTrees.Count > 0) {
58        // symbol on same line as '('
59        strBuilder.AppendLine(node.ToString());
60        // each subtree expression on a new line
61        // and closing ')' also on new line
62        foreach (var subtree in node.SubTrees) {
63          strBuilder.AppendLine(SymbolicExpression(subtree, indentLength + 2));
64        }
65        strBuilder.Append(' ', indentLength); strBuilder.Append(")");
66      } else {
67        // symbol in the same line with as '(' and ')'
68        strBuilder.Append(node.ToString());
69        strBuilder.Append(")");
70      }
71      return strBuilder.ToString();
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.