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), true)]
|
---|
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 | Point coordinates = new Point(e.X, e.Y);
|
---|
223 | TreeNode node = symbolsTreeView.GetNodeAt(coordinates);
|
---|
224 | if (e.Button == System.Windows.Forms.MouseButtons.Left && node == 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 | GroupSymbol groupSymbol = null;
|
---|
259 |
|
---|
260 | TreeNode selectedNode = symbolsTreeView.SelectedNode;
|
---|
261 | if (selectedNode != null) {
|
---|
262 | groupSymbol = selectedNode.Tag as GroupSymbol;
|
---|
263 | if (groupSymbol == null && selectedNode.Parent != null) groupSymbol = selectedNode.Parent.Tag as GroupSymbol;
|
---|
264 | }
|
---|
265 | if (groupSymbol != null) groupSymbol.SymbolsCollection.Add(symbol);
|
---|
266 | else Content.AddSymbol(symbol);
|
---|
267 | }
|
---|
268 | catch (Exception ex) {
|
---|
269 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | private void copyButton_Click(object sender, EventArgs e) {
|
---|
275 | var symbol = symbolsTreeView.SelectedNode.Tag as ISymbol;
|
---|
276 | if (symbol != null && !(symbol is IReadOnlySymbol)) {
|
---|
277 |
|
---|
278 | Content.StartGrammarManipulation();
|
---|
279 | Cloner cloner = new Cloner();
|
---|
280 | var clonedSymbol = cloner.Clone(symbol);
|
---|
281 | ChangeDuplicateSymbolNames(clonedSymbol);
|
---|
282 |
|
---|
283 | GroupSymbol groupSymbol = null;
|
---|
284 | if (symbolsTreeView.SelectedNode.Parent != null) groupSymbol = symbolsTreeView.SelectedNode.Parent.Tag as GroupSymbol;
|
---|
285 |
|
---|
286 | if (groupSymbol != null) groupSymbol.SymbolsCollection.Add(clonedSymbol);
|
---|
287 | else Content.AddSymbol(clonedSymbol);
|
---|
288 |
|
---|
289 | UpdateGrammerConstraintsForClonedSymbol(symbol, cloner);
|
---|
290 | Content.FinishedGrammarManipulation();
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | private void removeButton_Click(object sender, EventArgs e) {
|
---|
295 | var symbol = symbolsTreeView.SelectedNode.Tag as ISymbol;
|
---|
296 | if (symbol != null && !(symbol is IReadOnlySymbol)) {
|
---|
297 | Content.RemoveSymbol(symbol);
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
302 | splitContainer1.Panel2Collapsed = !showDetailsCheckBox.Checked;
|
---|
303 | }
|
---|
304 |
|
---|
305 | private void showSampleTreeButton_Click(object sender, EventArgs e) {
|
---|
306 | SymbolicExpressionGrammarSampleExpressionTreeView view = new SymbolicExpressionGrammarSampleExpressionTreeView();
|
---|
307 | view.Content = Content;
|
---|
308 | view.Show();
|
---|
309 | }
|
---|
310 |
|
---|
311 | #endregion
|
---|
312 |
|
---|
313 | #region helpers
|
---|
314 | private void UpdateGrammerConstraintsForClonedSymbol(ISymbol symbol, Cloner cloner) {
|
---|
315 | foreach (var s in symbol.Flatten().Where(x => !(x is GroupSymbol))) {
|
---|
316 | if (!cloner.ClonedObjectRegistered(s)) throw new InvalidOperationException();
|
---|
317 | var clone = cloner.Clone(s);
|
---|
318 | Content.SetSubtreeCount(clone, Content.GetMinimumSubtreeCount(s), Content.GetMaximumSubtreeCount(s));
|
---|
319 | foreach (var childSymbol in Content.GetAllowedChildSymbols(s)) {
|
---|
320 | var newChildSymbol = childSymbol;
|
---|
321 | if (cloner.ClonedObjectRegistered(childSymbol)) newChildSymbol = cloner.Clone(childSymbol);
|
---|
322 | Content.AddAllowedChildSymbol(clone, newChildSymbol);
|
---|
323 | }
|
---|
324 | for (int i = 0; i < Content.GetMaximumSubtreeCount(s); i++) {
|
---|
325 | foreach (var childSymbol in Content.GetAllowedChildSymbols(s, i)) {
|
---|
326 | var newChildSymbol = childSymbol;
|
---|
327 | if (cloner.ClonedObjectRegistered(childSymbol)) newChildSymbol = cloner.Clone(childSymbol);
|
---|
328 | Content.AddAllowedChildSymbol(clone, newChildSymbol, i);
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | private void ChangeDuplicateSymbolNames(ISymbol symbol) {
|
---|
335 | foreach (var s in symbol.Flatten()) {
|
---|
336 | var originalSymbolName = s.Name;
|
---|
337 | int i = 1;
|
---|
338 | while (Content.ContainsSymbol(s)) {
|
---|
339 | s.Name = originalSymbolName + i;
|
---|
340 | i++;
|
---|
341 | }
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | private void UpdateSymbolDetailsViews() {
|
---|
346 | if (symbolsTreeView.SelectedNode != null) {
|
---|
347 | symbolDetailsViewHost.Content = (ISymbol)symbolsTreeView.SelectedNode.Tag;
|
---|
348 | allowedChildSymbolsControl.Symbol = (ISymbol)symbolsTreeView.SelectedNode.Tag;
|
---|
349 | } else {
|
---|
350 | symbolDetailsViewHost.Content = null;
|
---|
351 | allowedChildSymbolsControl.Symbol = null;
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | private IEnumerable<TreeNode> IterateTreeNodes(TreeNode node = null) {
|
---|
356 | TreeNodeCollection nodes;
|
---|
357 | if (node == null)
|
---|
358 | nodes = symbolsTreeView.Nodes;
|
---|
359 | else {
|
---|
360 | nodes = node.Nodes;
|
---|
361 | yield return node;
|
---|
362 | }
|
---|
363 |
|
---|
364 | foreach (var childNode in nodes.OfType<TreeNode>())
|
---|
365 | foreach (var n in IterateTreeNodes(childNode))
|
---|
366 | yield return n;
|
---|
367 | }
|
---|
368 |
|
---|
369 | protected virtual void RebuildImageList() {
|
---|
370 | symbolsTreeView.ImageList.Images.Clear();
|
---|
371 | foreach (TreeNode treeNode in IterateTreeNodes()) {
|
---|
372 | var symbol = (ISymbol)treeNode.Tag;
|
---|
373 | symbolsTreeView.ImageList.Images.Add(symbol == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : symbol.ItemImage);
|
---|
374 | treeNode.ImageIndex = symbolsTreeView.ImageList.Images.Count - 1;
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | //necessary code to handle dock correctly regarding the expanded nodes
|
---|
379 | bool[] expandendedState;
|
---|
380 | protected override void OnHandleCreated(EventArgs e) {
|
---|
381 | base.OnHandleCreated(e);
|
---|
382 | if (expandendedState == null) return;
|
---|
383 | var nodes = IterateTreeNodes().ToList();
|
---|
384 | for (int i = 0; i < nodes.Count; i++)
|
---|
385 | if (expandendedState[i]) nodes[i].Expand();
|
---|
386 | }
|
---|
387 | protected override void OnHandleDestroyed(EventArgs e) {
|
---|
388 | base.OnHandleDestroyed(e);
|
---|
389 | var nodes = IterateTreeNodes().ToList();
|
---|
390 | expandendedState = new bool[nodes.Count];
|
---|
391 | for (int i = 0; i < nodes.Count; i++)
|
---|
392 | expandendedState[i] = nodes[i].IsExpanded;
|
---|
393 | }
|
---|
394 | #endregion
|
---|
395 | }
|
---|
396 |
|
---|
397 | //this class is necessary to prevent double clicks which do not fire the checkbox checked event
|
---|
398 | internal class CheckBoxTreeView : TreeView {
|
---|
399 | protected override void WndProc(ref Message m) {
|
---|
400 | // Suppress WM_LBUTTONDBLCLK
|
---|
401 | if (m.Msg == 0x203) { m.Result = IntPtr.Zero; } else base.WndProc(ref m);
|
---|
402 | }
|
---|
403 | }
|
---|
404 | }
|
---|