1 | using System.Windows.Forms;
|
---|
2 | using HeuristicLab.Core.Views;
|
---|
3 | using HeuristicLab.MainForm;
|
---|
4 |
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Views {
|
---|
7 | using System.Collections.Generic;
|
---|
8 | using System.Linq;
|
---|
9 | using Expressions;
|
---|
10 |
|
---|
11 | using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
|
---|
12 | using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
|
---|
13 |
|
---|
14 | using Stack;
|
---|
15 |
|
---|
16 | [View("Push Expression Selection Editor")]
|
---|
17 | [Content(typeof(IEnabledExpressionsConfiguration), true)]
|
---|
18 | public partial class ExpressionSelectionView : ItemView {
|
---|
19 | public ExpressionSelectionView() {
|
---|
20 | InitializeComponent();
|
---|
21 |
|
---|
22 | InitEvents();
|
---|
23 | Name = "Push Expression Selection Editor";
|
---|
24 | }
|
---|
25 |
|
---|
26 | public new IEnabledExpressionsConfiguration Content
|
---|
27 | {
|
---|
28 | get { return (IEnabledExpressionsConfiguration)base.Content; }
|
---|
29 | set
|
---|
30 | {
|
---|
31 | if (Content != null) Content.EnabledExpressionsChanged -= EnabledExpressionsChanged;
|
---|
32 | base.Content = value;
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | protected override void OnReadOnlyChanged() {
|
---|
37 | base.OnReadOnlyChanged();
|
---|
38 |
|
---|
39 | expressionsTreeView.Enabled = !ReadOnly;
|
---|
40 | }
|
---|
41 |
|
---|
42 | private void InitEvents() {
|
---|
43 | expressionsTreeView.AfterCheck += ExpressionsTreeViewAfterCheck;
|
---|
44 | }
|
---|
45 |
|
---|
46 | private void ExpressionsTreeViewAfterCheck(object sender, TreeViewEventArgs e) {
|
---|
47 | if (e.Action == TreeViewAction.Unknown)
|
---|
48 | return;
|
---|
49 |
|
---|
50 | Content.EnabledExpressionsChanged -= EnabledExpressionsChanged;
|
---|
51 | var stackType = (StackTypes)e.Node.Tag;
|
---|
52 |
|
---|
53 | if (e.Node.Parent == null) {
|
---|
54 | Content.SetStack(stackType, e.Node.Checked);
|
---|
55 | SetStackNodeText(e.Node);
|
---|
56 | foreach (var subNode in e.Node.Nodes.OfType<TreeNode>().Where(n => n.Checked != e.Node.Checked))
|
---|
57 | subNode.Checked = e.Node.Checked;
|
---|
58 | } else {
|
---|
59 | Content.SetExpression(e.Node.Text, e.Node.Checked);
|
---|
60 | SetStackNodeText(e.Node.Parent);
|
---|
61 |
|
---|
62 | // enable/disable stack if no or at least one expression is enabled
|
---|
63 | if ((e.Node.Checked && !e.Node.Parent.Checked) ||
|
---|
64 | (!e.Node.Checked && e.Node.Parent.Nodes.OfType<TreeNode>().All(n => !n.Checked))) {
|
---|
65 | e.Node.Parent.Checked = e.Node.Checked;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | Content.EnabledExpressionsChanged += EnabledExpressionsChanged;
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected override void OnContentChanged() {
|
---|
73 | expressionsTreeView.Nodes.Clear();
|
---|
74 |
|
---|
75 | if (Content == null) {
|
---|
76 | return;
|
---|
77 | }
|
---|
78 |
|
---|
79 | Content.EnabledExpressionsChanged += EnabledExpressionsChanged;
|
---|
80 | UpdateNodes();
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void EnabledExpressionsChanged(object sender, EnabledExpressionsChangedEventArgs e) {
|
---|
84 | if (InvokeRequired) Invoke(new MethodInvoker(UpdateNodes));
|
---|
85 | else UpdateNodes();
|
---|
86 | }
|
---|
87 |
|
---|
88 | private void UpdateNodes() {
|
---|
89 | expressionsTreeView.Nodes.Clear();
|
---|
90 |
|
---|
91 | var treeNodes = GetTreeNodes(Content.EnabledExpressions)
|
---|
92 | .OrderBy(x => x.Text)
|
---|
93 | .ToArray();
|
---|
94 |
|
---|
95 | expressionsTreeView.Nodes.AddRange(treeNodes);
|
---|
96 | }
|
---|
97 |
|
---|
98 | private IEnumerable<TreeNode> GetTreeNodes(IReadOnlyList<string> enabledExpressions) {
|
---|
99 | foreach (var stackType in ExpressionTable.StackTypeToNamesTable.Keys) {
|
---|
100 | var stackTreeNode = new TreeNode {
|
---|
101 | Tag = stackType
|
---|
102 | };
|
---|
103 |
|
---|
104 | var stackExpressions = ExpressionTable.StackTypeToNamesTable[stackType];
|
---|
105 | var enabledStackExpressions = enabledExpressions.Intersect(stackExpressions).ToList();
|
---|
106 |
|
---|
107 | SetStackNodeText(stackTreeNode);
|
---|
108 | stackTreeNode.Checked = enabledStackExpressions.Any();
|
---|
109 |
|
---|
110 | var expressionNodes = stackExpressions
|
---|
111 | .Where(name => {
|
---|
112 | var type = ExpressionTable.NameToTypeTable[name];
|
---|
113 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
114 |
|
---|
115 | return !attribute.IsHidden;
|
---|
116 | })
|
---|
117 | .OrderBy(name => name)
|
---|
118 | .Select(name => new TreeNode {
|
---|
119 | Text = name,
|
---|
120 | Tag = stackType,
|
---|
121 | Checked = enabledStackExpressions.Contains(name),
|
---|
122 | }).ToArray();
|
---|
123 |
|
---|
124 | foreach (var node in expressionNodes) {
|
---|
125 | var type = ExpressionTable.NameToTypeTable[node.Text];
|
---|
126 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
127 |
|
---|
128 | node.ToolTipText = attribute.Description;
|
---|
129 | }
|
---|
130 |
|
---|
131 | stackTreeNode.Nodes.AddRange(expressionNodes);
|
---|
132 |
|
---|
133 | yield return stackTreeNode;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | private void SetStackNodeText(TreeNode node) {
|
---|
138 | var stackTypes = (StackTypes)node.Tag;
|
---|
139 | var stackExpressions = ExpressionTable.StackTypeToNamesTable[stackTypes];
|
---|
140 | var enabledStackExpressions = Content.EnabledExpressions.Intersect(stackExpressions).ToList();
|
---|
141 | var stackNames = string.Join(", ", stackTypes.ToValues().Select(type => type.ToString()).OrderBy(x => x));
|
---|
142 |
|
---|
143 | node.Text = string.Format(
|
---|
144 | "{0} ({1}/{2})",
|
---|
145 | stackNames,
|
---|
146 | enabledStackExpressions.Count,
|
---|
147 | stackExpressions.Count);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|