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