[14727] | 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;
|
---|
[15032] | 12 | using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
|
---|
[14727] | 13 |
|
---|
| 14 | using Stack;
|
---|
| 15 |
|
---|
| 16 | [View("Push Expression Selection Editor")]
|
---|
| 17 | [Content(typeof(IEnabledExpressionsConfiguration), true)]
|
---|
[15017] | 18 | public partial class ExpressionSelectionView : ItemView {
|
---|
[14727] | 19 | public ExpressionSelectionView() {
|
---|
| 20 | InitializeComponent();
|
---|
[14834] | 21 |
|
---|
[14727] | 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 | {
|
---|
[14834] | 31 | if (Content != null) Content.EnabledExpressionsChanged -= EnabledExpressionsChanged;
|
---|
[14727] | 32 | base.Content = value;
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | protected override void OnReadOnlyChanged() {
|
---|
| 37 | base.OnReadOnlyChanged();
|
---|
| 38 |
|
---|
[14834] | 39 | expressionsTreeView.Enabled = !ReadOnly;
|
---|
[14727] | 40 | }
|
---|
| 41 |
|
---|
| 42 | private void InitEvents() {
|
---|
[14834] | 43 | expressionsTreeView.AfterCheck += ExpressionsTreeViewAfterCheck;
|
---|
[14727] | 44 | }
|
---|
| 45 |
|
---|
| 46 | private void ExpressionsTreeViewAfterCheck(object sender, TreeViewEventArgs e) {
|
---|
[14834] | 47 | if (e.Action == TreeViewAction.Unknown)
|
---|
| 48 | return;
|
---|
[14727] | 49 |
|
---|
[14834] | 50 | Content.EnabledExpressionsChanged -= EnabledExpressionsChanged;
|
---|
| 51 | var stackType = (StackTypes)e.Node.Tag;
|
---|
| 52 |
|
---|
| 53 | if (e.Node.Parent == null) {
|
---|
[15032] | 54 | Content.SetStack(stackType, e.Node.Checked);
|
---|
[14834] | 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;
|
---|
[14727] | 58 | } else {
|
---|
[15017] | 59 | Content.SetExpression(e.Node.Text, e.Node.Checked);
|
---|
[14834] | 60 | SetStackNodeText(e.Node.Parent);
|
---|
| 61 |
|
---|
[15017] | 62 | // enable/disable stack if no or at least one expression is enabled
|
---|
[14834] | 63 | if ((e.Node.Checked && !e.Node.Parent.Checked) ||
|
---|
[15032] | 64 | (!e.Node.Checked && e.Node.Parent.Nodes.OfType<TreeNode>().All(n => !n.Checked))) {
|
---|
[14834] | 65 | e.Node.Parent.Checked = e.Node.Checked;
|
---|
[15017] | 66 | }
|
---|
[14727] | 67 | }
|
---|
[14834] | 68 |
|
---|
| 69 | Content.EnabledExpressionsChanged += EnabledExpressionsChanged;
|
---|
[14727] | 70 | }
|
---|
| 71 |
|
---|
| 72 | protected override void OnContentChanged() {
|
---|
| 73 | expressionsTreeView.Nodes.Clear();
|
---|
| 74 |
|
---|
| 75 | if (Content == null) {
|
---|
| 76 | return;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[14834] | 79 | Content.EnabledExpressionsChanged += EnabledExpressionsChanged;
|
---|
[15017] | 80 | UpdateNodes();
|
---|
[14834] | 81 | }
|
---|
[14777] | 82 |
|
---|
[14834] | 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 |
|
---|
[15032] | 91 | var treeNodes = GetTreeNodes(Content.EnabledExpressions)
|
---|
| 92 | .OrderBy(x => x.Text)
|
---|
| 93 | .ToArray();
|
---|
| 94 |
|
---|
[14727] | 95 | expressionsTreeView.Nodes.AddRange(treeNodes);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[15032] | 98 | private IEnumerable<TreeNode> GetTreeNodes(IReadOnlyList<string> enabledExpressions) {
|
---|
[14727] | 99 | foreach (var stackType in ExpressionTable.StackTypeToNamesTable.Keys) {
|
---|
| 100 | var stackTreeNode = new TreeNode {
|
---|
[14834] | 101 | Tag = stackType
|
---|
[14727] | 102 | };
|
---|
| 103 |
|
---|
| 104 | var stackExpressions = ExpressionTable.StackTypeToNamesTable[stackType];
|
---|
[14834] | 105 | var enabledStackExpressions = enabledExpressions.Intersect(stackExpressions).ToList();
|
---|
[14727] | 106 |
|
---|
[14834] | 107 | SetStackNodeText(stackTreeNode);
|
---|
[14727] | 108 | stackTreeNode.Checked = enabledStackExpressions.Any();
|
---|
| 109 |
|
---|
| 110 | var expressionNodes = stackExpressions
|
---|
[15032] | 111 | .Where(name => {
|
---|
| 112 | var type = ExpressionTable.NameToTypeTable[name];
|
---|
| 113 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
| 114 |
|
---|
| 115 | return !attribute.IsHidden;
|
---|
| 116 | })
|
---|
[14727] | 117 | .OrderBy(name => name)
|
---|
| 118 | .Select(name => new TreeNode {
|
---|
| 119 | Text = name,
|
---|
[14834] | 120 | Tag = stackType,
|
---|
[14727] | 121 | Checked = enabledStackExpressions.Contains(name),
|
---|
| 122 | }).ToArray();
|
---|
| 123 |
|
---|
[15032] | 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 |
|
---|
[14727] | 131 | stackTreeNode.Nodes.AddRange(expressionNodes);
|
---|
| 132 |
|
---|
| 133 | yield return stackTreeNode;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[14834] | 136 |
|
---|
| 137 | private void SetStackNodeText(TreeNode node) {
|
---|
[15032] | 138 | var stackTypes = (StackTypes)node.Tag;
|
---|
| 139 | var stackExpressions = ExpressionTable.StackTypeToNamesTable[stackTypes];
|
---|
[14834] | 140 | var enabledStackExpressions = Content.EnabledExpressions.Intersect(stackExpressions).ToList();
|
---|
[15032] | 141 | var stackNames = string.Join(", ", stackTypes.ToValues().Select(type => type.ToString()).OrderBy(x => x));
|
---|
[14834] | 142 |
|
---|
| 143 | node.Text = string.Format(
|
---|
| 144 | "{0} ({1}/{2})",
|
---|
[15032] | 145 | stackNames,
|
---|
[14834] | 146 | enabledStackExpressions.Count,
|
---|
[15032] | 147 | stackExpressions.Count);
|
---|
[14834] | 148 | }
|
---|
[14727] | 149 | }
|
---|
| 150 | }
|
---|