Free cookie consent management tool by TermsFeed Policy Generator

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

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

adapted view captions (ticket #893)

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32
33namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
34  [View("SymbolicExpression View")]
35  [Content(typeof(SymbolicExpressionTree), false)]
36  public partial class SymbolicExpressionView : AsynchronousContentView {
37    public new SymbolicExpressionTree Content {
38      get { return (SymbolicExpressionTree)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public SymbolicExpressionView() {
43      InitializeComponent();
44    }
45
46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48      if (Content == null)
49        textBox.Text = string.Empty;
50      else
51        textBox.Text = SymbolicExpression(Content.Root, 0);
52
53      SetEnabledStateOfControls();
54    }
55
56    protected override void OnReadOnlyChanged() {
57      base.OnReadOnlyChanged();
58      SetEnabledStateOfControls();
59    }
60
61    private void SetEnabledStateOfControls() {
62      textBox.Enabled = Content != null;
63      textBox.ReadOnly = ReadOnly;
64    }
65
66    private static string SymbolicExpression(SymbolicExpressionTreeNode node, int indentLength) {
67      StringBuilder strBuilder = new StringBuilder();
68      strBuilder.Append(' ', indentLength); strBuilder.Append("(");
69      // internal nodes or leaf nodes?
70      if (node.SubTrees.Count > 0) {
71        // symbol on same line as '('
72        strBuilder.AppendLine(node.ToString());
73        // each subtree expression on a new line
74        // and closing ')' also on new line
75        foreach (var subtree in node.SubTrees) {
76          strBuilder.AppendLine(SymbolicExpression(subtree, indentLength + 2));
77        }
78        strBuilder.Append(' ', indentLength); strBuilder.Append(")");
79      } else {
80        // symbol in the same line with as '(' and ')'
81        strBuilder.Append(node.ToString());
82        strBuilder.Append(")");
83      }
84      return strBuilder.ToString();
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.