1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core.Views;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 | using HeuristicLab.PluginInfrastructure;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
33 | [View("Symbolic Expression Grammar Editor")]
|
---|
34 | [Content(typeof(ISymbolicExpressionGrammar), false)]
|
---|
35 | public partial class SymbolicExpressionGrammarEditorView : NamedItemView {
|
---|
36 | public SymbolicExpressionGrammarEditorView() {
|
---|
37 | InitializeComponent();
|
---|
38 | }
|
---|
39 |
|
---|
40 | public override bool ReadOnly {
|
---|
41 | get {
|
---|
42 | if ((Content != null) && Content.ReadOnly) return true;
|
---|
43 | return base.ReadOnly;
|
---|
44 | }
|
---|
45 | set {
|
---|
46 | if ((Content != null) && Content.ReadOnly) base.ReadOnly = true;
|
---|
47 | else base.ReadOnly = value;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public new ISymbolicExpressionGrammar Content {
|
---|
52 | get { return (ISymbolicExpressionGrammar)base.Content; }
|
---|
53 | set { base.Content = value; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected override void SetEnabledStateOfControls() {
|
---|
57 | base.SetEnabledStateOfControls();
|
---|
58 | addButton.Enabled = Content != null;
|
---|
59 | removeButton.Enabled = symbolsTreeView.SelectedNode != null && !(symbolsTreeView.SelectedNode.Tag is IReadOnlySymbol);
|
---|
60 | copyButton.Enabled = symbolsTreeView.SelectedNode != null && !(symbolsTreeView.SelectedNode.Tag is IReadOnlySymbol);
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected override void OnContentChanged() {
|
---|
64 | base.OnContentChanged();
|
---|
65 | if (Content != null) {
|
---|
66 | symbolsTreeView.Nodes.Clear();
|
---|
67 | UpdateSymbolsTreeView();
|
---|
68 | symbolsTreeView.ExpandAll();
|
---|
69 | allowedChildSymbolsControl.Grammar = Content;
|
---|
70 | allowedChildSymbolsControl.Symbol = null;
|
---|
71 | symbolDetailsViewHost.Content = null;
|
---|
72 | } else {
|
---|
73 | symbolsTreeView.Nodes.Clear();
|
---|
74 | allowedChildSymbolsControl.Grammar = null;
|
---|
75 | symbolDetailsViewHost.Content = null;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | #region events
|
---|
80 | protected override void RegisterContentEvents() {
|
---|
81 | base.RegisterContentEvents();
|
---|
82 | Content.ReadOnlyChanged += new System.EventHandler(Content_ReadOnlyChanged);
|
---|
83 | Content.Changed += new System.EventHandler(Content_Changed);
|
---|
84 | }
|
---|
85 | protected override void DeregisterContentEvents() {
|
---|
86 | Content.ReadOnlyChanged -= new System.EventHandler(Content_ReadOnlyChanged);
|
---|
87 | Content.Changed -= new System.EventHandler(Content_Changed);
|
---|
88 | base.DeregisterContentEvents();
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void Content_ReadOnlyChanged(object sender, EventArgs e) {
|
---|
92 | ReadOnly = Content.ReadOnly;
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void Content_Changed(object sender, EventArgs e) {
|
---|
96 | ISymbol symbol = null;
|
---|
97 | if (symbolsTreeView.SelectedNode != null)
|
---|
98 | symbol = (ISymbol)symbolsTreeView.SelectedNode.Tag;
|
---|
99 |
|
---|
100 | allowedChildSymbolsControl.Grammar = Content;
|
---|
101 |
|
---|
102 | UpdateSymbolsTreeView();
|
---|
103 | if (symbol != null && Content.ContainsSymbol(symbol)) {
|
---|
104 | symbolsTreeView.SelectedNode = IterateTreeNodes().Where(n => n.Tag == symbol).ToList().FirstOrDefault();
|
---|
105 | UpdateSymbolDetailsViews();
|
---|
106 | }
|
---|
107 | }
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | private void UpdateSymbolsTreeView() {
|
---|
111 | var symbols = Content.Symbols.ToList();
|
---|
112 | foreach (var treeNode in IterateTreeNodes().ToList()) {
|
---|
113 | var symbol = treeNode.Tag as ISymbol;
|
---|
114 | if (!symbols.Contains(symbol))
|
---|
115 | treeNode.Remove();
|
---|
116 | }
|
---|
117 |
|
---|
118 | var groupSymbols = symbols.OfType<GroupSymbol>().ToList();
|
---|
119 | var topLevelSymbols = Content.Symbols.Where(s => !groupSymbols.Any(g => g.Symbols.Contains(s)));
|
---|
120 | UpdateChildTreeNodes(symbolsTreeView.Nodes, topLevelSymbols);
|
---|
121 |
|
---|
122 | RebuildImageList();
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void UpdateChildTreeNodes(TreeNodeCollection collection, IEnumerable<ISymbol> symbols) {
|
---|
126 | foreach (ISymbol symbol in symbols) {
|
---|
127 | TreeNode node = collection.Cast<TreeNode>().Where(n => n.Tag == symbol).FirstOrDefault();
|
---|
128 | if (node == null) {
|
---|
129 | node = new TreeNode();
|
---|
130 | node.Tag = symbol;
|
---|
131 | collection.Add(node);
|
---|
132 | }
|
---|
133 | node.Checked = symbol.Enabled;
|
---|
134 | node.Text = symbol.Name;
|
---|
135 |
|
---|
136 | var groupSymbol = symbol as GroupSymbol;
|
---|
137 | if (groupSymbol != null) UpdateChildTreeNodes(node.Nodes, groupSymbol.Symbols);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | private void symbolsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
142 | if (e.Action != TreeViewAction.Unknown) UpdateSymbolDetailsViews();
|
---|
143 |
|
---|
144 | removeButton.Enabled = symbolsTreeView.SelectedNode != null && !(symbolsTreeView.SelectedNode.Tag is IReadOnlySymbol);
|
---|
145 | copyButton.Enabled = symbolsTreeView.SelectedNode != null && !(symbolsTreeView.SelectedNode.Tag is IReadOnlySymbol);
|
---|
146 | }
|
---|
147 |
|
---|
148 | private void symbolsTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
149 | if (e.Action != TreeViewAction.Unknown) {
|
---|
150 | Content.StartGrammarManipulation();
|
---|
151 | allowedChildSymbolsControl.Symbol = null;
|
---|
152 | var symbol = (ISymbol)e.Node.Tag;
|
---|
153 | symbol.Enabled = e.Node.Checked;
|
---|
154 | foreach (var node in IterateTreeNodes())
|
---|
155 | node.Checked = ((ISymbol)node.Tag).Enabled;
|
---|
156 |
|
---|
157 | Content.FinishedGrammarManipulation();
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | #region drag & drop operations
|
---|
162 | private GroupSymbol parentOfDraggedSymbol;
|
---|
163 | private void symbolsTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
164 | if (!Locked) {
|
---|
165 | var treeNode = e.Item as TreeNode;
|
---|
166 | if (treeNode.Parent != null) parentOfDraggedSymbol = treeNode.Parent.Tag as GroupSymbol;
|
---|
167 | var data = new DataObject();
|
---|
168 | data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, treeNode.Tag);
|
---|
169 | validDragOperation = true;
|
---|
170 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Move);
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | private bool validDragOperation;
|
---|
176 | private void symbolsTreeView_DragEnter(object sender, DragEventArgs e) {
|
---|
177 | validDragOperation = false;
|
---|
178 | if (Content == null) return;
|
---|
179 |
|
---|
180 | var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
181 | var symbol = data as ISymbol;
|
---|
182 | if (symbol != null && !(symbol is IReadOnlySymbol)) validDragOperation = true;
|
---|
183 | }
|
---|
184 | private void symbolsTreeView_DragOver(object sender, DragEventArgs e) {
|
---|
185 | e.Effect = DragDropEffects.None;
|
---|
186 | if (validDragOperation) {
|
---|
187 | GroupSymbol groupSymbol = null;
|
---|
188 | Point mouse = symbolsTreeView.PointToClient(new Point(e.X, e.Y));
|
---|
189 | TreeNode node = symbolsTreeView.GetNodeAt(mouse);
|
---|
190 | if (node != null) groupSymbol = node.Tag as GroupSymbol;
|
---|
191 | if (node != null && groupSymbol == null) groupSymbol = node.Parent.Tag as GroupSymbol;
|
---|
192 | var symbol = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
193 | if (symbol == groupSymbol) return;
|
---|
194 |
|
---|
195 | if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
196 | }
|
---|
197 | }
|
---|
198 | private void symbolsTreeView_DragDrop(object sender, DragEventArgs e) {
|
---|
199 | var symbol = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ISymbol;
|
---|
200 |
|
---|
201 | GroupSymbol groupSymbol = null;
|
---|
202 | Point mouse = symbolsTreeView.PointToClient(new Point(e.X, e.Y));
|
---|
203 | TreeNode node = symbolsTreeView.GetNodeAt(mouse);
|
---|
204 | if (node != null) groupSymbol = node.Tag as GroupSymbol;
|
---|
205 | if (node != null && groupSymbol == null) groupSymbol = node.Parent.Tag as GroupSymbol;
|
---|
206 |
|
---|
207 | Content.StartGrammarManipulation();
|
---|
208 | Cloner cloner = new Cloner();
|
---|
209 | var clonedSymbol = cloner.Clone(symbol);
|
---|
210 | ChangeDuplicateSymbolNames(clonedSymbol);
|
---|
211 |
|
---|
212 | if (groupSymbol != null) groupSymbol.SymbolsCollection.Add(clonedSymbol);
|
---|
213 | else Content.AddSymbol(clonedSymbol);
|
---|
214 |
|
---|
215 | UpdateGrammerConstraintsForClonedSymbol(symbol, cloner);
|
---|
216 | Content.FinishedGrammarManipulation();
|
---|
217 | }
|
---|
218 | #endregion
|
---|
219 |
|
---|
220 | private void symbolsTreeView_MouseDown(object sender, MouseEventArgs e) {
|
---|
221 | // enables deselection of treeNodes
|
---|
222 | if (symbolsTreeView.SelectedNode == null) return;
|
---|
223 | Point coordinates = new Point(e.X, e.Y);
|
---|
224 | if (e.Button == System.Windows.Forms.MouseButtons.Left && symbolsTreeView.GetNodeAt(coordinates) == null) {
|
---|
225 | symbolsTreeView.SelectedNode = null;
|
---|
226 | symbolDetailsViewHost.Content = null;
|
---|
227 | SetEnabledStateOfControls();
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | private void symbolsTreeView_KeyDown(object sender, KeyEventArgs e) {
|
---|
232 | if (ReadOnly) return;
|
---|
233 | if (symbolsTreeView.SelectedNode == null) return;
|
---|
234 | if (e.KeyCode != Keys.Delete) return;
|
---|
235 |
|
---|
236 | var symbol = (ISymbol)symbolsTreeView.SelectedNode.Tag;
|
---|
237 | if (!(symbol is IReadOnlySymbol))
|
---|
238 | Content.RemoveSymbol(symbol);
|
---|
239 |
|
---|
240 | SetEnabledStateOfControls();
|
---|
241 | UpdateSymbolDetailsViews();
|
---|
242 | RebuildImageList();
|
---|
243 | }
|
---|
244 |
|
---|
245 | #region button events
|
---|
246 | private TypeSelectorDialog typeSelectorDialog;
|
---|
247 | private void addButton_Click(object sender, EventArgs e) {
|
---|
248 | if (typeSelectorDialog == null) {
|
---|
249 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
250 | typeSelectorDialog.Caption = "Select Symbol";
|
---|
251 | typeSelectorDialog.TypeSelector.Caption = "Available Symbols";
|
---|
252 | typeSelectorDialog.TypeSelector.Configure(typeof(ISymbol), false, false, (t) => { return !typeof(IReadOnlySymbol).IsAssignableFrom(t); });
|
---|
253 | }
|
---|
254 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
255 | try {
|
---|
256 | ISymbol symbol = (ISymbol)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
257 | ChangeDuplicateSymbolNames(symbol);
|
---|
258 |
|
---|
259 | var selectedNode = symbolsTreeView.SelectedNode;
|
---|
260 | GroupSymbol groupSymbol = selectedNode.Tag as GroupSymbol;
|
---|
261 | if (groupSymbol == null && selectedNode.Parent != null) groupSymbol = selectedNode.Parent.Tag as GroupSymbol;
|
---|
262 |
|
---|
263 | if (groupSymbol != null) groupSymbol.SymbolsCollection.Add(symbol);
|
---|
264 | else Content.AddSymbol(symbol);
|
---|
265 | }
|
---|
266 | catch (Exception ex) {
|
---|
267 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | private void copyButton_Click(object sender, EventArgs e) {
|
---|
273 | var symbol = symbolsTreeView.SelectedNode.Tag as ISymbol;
|
---|
274 | if (symbol != null && !(symbol is IReadOnlySymbol)) {
|
---|
275 |
|
---|
276 | Content.StartGrammarManipulation();
|
---|
277 | Cloner cloner = new Cloner();
|
---|
278 | var clonedSymbol = cloner.Clone(symbol);
|
---|
279 | ChangeDuplicateSymbolNames(clonedSymbol);
|
---|
280 |
|
---|
281 | GroupSymbol groupSymbol = null;
|
---|
282 | if (symbolsTreeView.SelectedNode.Parent != null) groupSymbol = symbolsTreeView.SelectedNode.Parent.Tag as GroupSymbol;
|
---|
283 |
|
---|
284 | if (groupSymbol != null) groupSymbol.SymbolsCollection.Add(clonedSymbol);
|
---|
285 | else Content.AddSymbol(clonedSymbol);
|
---|
286 |
|
---|
287 | UpdateGrammerConstraintsForClonedSymbol(symbol, cloner);
|
---|
288 | Content.FinishedGrammarManipulation();
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | private void removeButton_Click(object sender, EventArgs e) {
|
---|
293 | var symbol = symbolsTreeView.SelectedNode.Tag as ISymbol;
|
---|
294 | if (symbol != null && !(symbol is IReadOnlySymbol)) {
|
---|
295 | Content.RemoveSymbol(symbol);
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
300 | splitContainer1.Panel2Collapsed = !showDetailsCheckBox.Checked;
|
---|
301 | }
|
---|
302 | #endregion
|
---|
303 |
|
---|
304 | #region helpers
|
---|
305 | private void UpdateGrammerConstraintsForClonedSymbol(ISymbol symbol, Cloner cloner) {
|
---|
306 | foreach (var s in symbol.Flatten().Where(x => !(x is GroupSymbol))) {
|
---|
307 | if (!cloner.ClonedObjectRegistered(s)) throw new InvalidOperationException();
|
---|
308 | var clone = cloner.Clone(s);
|
---|
309 | Content.SetSubtreeCount(clone, Content.GetMinimumSubtreeCount(s), Content.GetMaximumSubtreeCount(s));
|
---|
310 | foreach (var childSymbol in Content.GetAllowedChildSymbols(s)) {
|
---|
311 | var newChildSymbol = childSymbol;
|
---|
312 | if (cloner.ClonedObjectRegistered(childSymbol)) newChildSymbol = cloner.Clone(childSymbol);
|
---|
313 | Content.AddAllowedChildSymbol(clone, newChildSymbol);
|
---|
314 | }
|
---|
315 | for (int i = 0; i < Content.GetMaximumSubtreeCount(s); i++) {
|
---|
316 | foreach (var childSymbol in Content.GetAllowedChildSymbols(s, i)) {
|
---|
317 | var newChildSymbol = childSymbol;
|
---|
318 | if (cloner.ClonedObjectRegistered(childSymbol)) newChildSymbol = cloner.Clone(childSymbol);
|
---|
319 | Content.AddAllowedChildSymbol(clone, newChildSymbol, i);
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | private void ChangeDuplicateSymbolNames(ISymbol symbol) {
|
---|
326 | foreach (var s in symbol.Flatten()) {
|
---|
327 | var originalSymbolName = s.Name;
|
---|
328 | int i = 1;
|
---|
329 | while (Content.ContainsSymbol(s)) {
|
---|
330 | s.Name = originalSymbolName + i;
|
---|
331 | i++;
|
---|
332 | }
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | private void UpdateSymbolDetailsViews() {
|
---|
337 | if (symbolsTreeView.SelectedNode != null) {
|
---|
338 | symbolDetailsViewHost.Content = (ISymbol)symbolsTreeView.SelectedNode.Tag;
|
---|
339 | allowedChildSymbolsControl.Symbol = (ISymbol)symbolsTreeView.SelectedNode.Tag;
|
---|
340 | } else {
|
---|
341 | symbolDetailsViewHost.Content = null;
|
---|
342 | allowedChildSymbolsControl.Symbol = null;
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | private IEnumerable<TreeNode> IterateTreeNodes(TreeNode node = null) {
|
---|
347 | TreeNodeCollection nodes;
|
---|
348 | if (node == null)
|
---|
349 | nodes = symbolsTreeView.Nodes;
|
---|
350 | else {
|
---|
351 | nodes = node.Nodes;
|
---|
352 | yield return node;
|
---|
353 | }
|
---|
354 |
|
---|
355 | foreach (var childNode in nodes.OfType<TreeNode>())
|
---|
356 | foreach (var n in IterateTreeNodes(childNode))
|
---|
357 | yield return n;
|
---|
358 | }
|
---|
359 |
|
---|
360 | protected virtual void RebuildImageList() {
|
---|
361 | symbolsTreeView.ImageList.Images.Clear();
|
---|
362 | foreach (TreeNode treeNode in IterateTreeNodes()) {
|
---|
363 | var symbol = (ISymbol)treeNode.Tag;
|
---|
364 | symbolsTreeView.ImageList.Images.Add(symbol == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : symbol.ItemImage);
|
---|
365 | treeNode.ImageIndex = symbolsTreeView.ImageList.Images.Count - 1;
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | //necessary code to handle dock correctly regarding the expanded nodes
|
---|
370 | bool[] expandendedState;
|
---|
371 | protected override void OnHandleCreated(EventArgs e) {
|
---|
372 | base.OnHandleCreated(e);
|
---|
373 | if (expandendedState == null) return;
|
---|
374 | var nodes = IterateTreeNodes().ToList();
|
---|
375 | for (int i = 0; i < nodes.Count; i++)
|
---|
376 | if (expandendedState[i]) nodes[i].Expand();
|
---|
377 | }
|
---|
378 | protected override void OnHandleDestroyed(EventArgs e) {
|
---|
379 | base.OnHandleDestroyed(e);
|
---|
380 | var nodes = IterateTreeNodes().ToList();
|
---|
381 | expandendedState = new bool[nodes.Count];
|
---|
382 | for (int i = 0; i < nodes.Count; i++)
|
---|
383 | expandendedState[i] = nodes[i].IsExpanded;
|
---|
384 | }
|
---|
385 | #endregion
|
---|
386 | }
|
---|
387 |
|
---|
388 | //this class is necessary to prevent double clicks which do not fire the checkbox checked event
|
---|
389 | internal class CheckBoxTreeView : TreeView {
|
---|
390 | protected override void WndProc(ref Message m) {
|
---|
391 | // Suppress WM_LBUTTONDBLCLK
|
---|
392 | if (m.Msg == 0x203) { m.Result = IntPtr.Zero; } else base.WndProc(ref m);
|
---|
393 | }
|
---|
394 | }
|
---|
395 | }
|
---|