Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.CEDMA.Core/DatabaseOperatorLibraryView.cs @ 12467

Last change on this file since 12467 was 418, checked in by gkronber, 16 years ago

worked on #211 (Full operator-graphs are stored multiple times in the database even though most of it is static anyway)

  • added class OperatorLink that allows to create CombinedOperators in the DatabaseOperatorLibrary which use other operators from the library (not via a copy but via a link)
File size: 9.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.ComponentModel;
26using System.Drawing;
27using System.Data;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Core;
32
33namespace HeuristicLab.CEDMA.Core {
34  public partial class DatabaseOperatorLibraryView : ViewBase {
35    private ChooseOperatorDialog chooseOperatorDialog;
36
37    public IOperatorLibrary OperatorLibrary {
38      get { return (IOperatorLibrary)Item; }
39      set { base.Item = value; }
40    }
41
42    public DatabaseOperatorLibraryView() {
43      InitializeComponent();
44      operatorsTreeView.TreeViewNodeSorter = new NodeSorter();
45      Caption = "Operator Library";
46    }
47    public DatabaseOperatorLibraryView(DatabaseOperatorLibrary operatorLibrary)
48      : this() {
49      OperatorLibrary = operatorLibrary;
50    }
51
52    private class NodeSorter : IComparer {
53      public int Compare(object x, object y) {
54        TreeNode tx = x as TreeNode;
55        TreeNode ty = y as TreeNode;
56
57        int result = string.Compare(tx.Text, ty.Text);
58        if(result == 0)
59          result = tx.Index.CompareTo(ty.Index);
60        return result;
61      }
62    }
63
64    protected override void UpdateControls() {
65      base.UpdateControls();
66      operatorsTreeView.Nodes.Clear();
67      addOperatorButton.Enabled = false;
68      removeButton.Enabled = false;
69      if(OperatorLibrary == null) {
70        Caption = "Operator Library";
71        operatorsGroupBox.Enabled = false;
72      } else {
73        Caption = OperatorLibrary.Group.Name + " (" + OperatorLibrary.GetType().Name + ")";
74        operatorsGroupBox.Enabled = true;
75        operatorsTreeView.Nodes.Add(CreateTreeNode(OperatorLibrary.Group));
76        operatorsTreeView.Sort();
77      }
78    }
79
80    private TreeNode CreateTreeNode(IOperatorGroup group) {
81      TreeNode node = new TreeNode();
82      node.Text = group.Name;
83      node.ForeColor = Color.LightGray;
84      node.Tag = group;
85
86      foreach(IOperator op in group.Operators)
87        node.Nodes.Add(CreateTreeNode(op));
88      foreach(IOperatorGroup subGroup in group.SubGroups)
89        node.Nodes.Add(CreateTreeNode(subGroup));
90      return node;
91    }
92    private TreeNode CreateTreeNode(IOperator op) {
93      TreeNode node = new TreeNode();
94      node.Text = op.Name;
95      node.ToolTipText = op.GetType().Name;
96      node.Tag = op;
97      return node;
98    }
99    private void RemoveTreeNode(TreeNode node) {
100      if(node.Tag is IOperatorGroup) {
101        IOperatorGroup group = node.Tag as IOperatorGroup;
102        IOperatorGroup parent = node.Parent.Tag as IOperatorGroup;
103        parent.RemoveSubGroup(group);
104      } else if(node.Tag is IOperator) {
105        IOperator op = node.Tag as IOperator;
106        IOperatorGroup group = node.Parent.Tag as IOperatorGroup;
107        group.RemoveOperator(op);
108      }
109      node.Remove();
110      EnableDisableButtons();
111    }
112
113    private void EnableDisableButtons() {
114      addOperatorButton.Enabled = false;
115      removeButton.Enabled = false;
116      operatorsTreeView.Focus();
117
118      if(operatorsTreeView.SelectedNode != null) {
119        if(operatorsTreeView.SelectedNode.Parent != null)
120          removeButton.Enabled = true;
121        if(operatorsTreeView.SelectedNode.Tag is IOperatorGroup) {
122          addOperatorButton.Enabled = true;
123        }
124      }
125    }
126
127    #region TreeView Events
128    private void operatorsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
129      EnableDisableButtons();
130    }
131    private void operatorsTreeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) {
132      e.CancelEdit = false;
133
134      string name = e.Label;
135      if(name != null) {
136        if(e.Node.Tag is IOperatorGroup)
137          ((IOperatorGroup)e.Node.Tag).Name = name;
138        else if(e.Node.Tag is IOperator)
139          ((IOperator)e.Node.Tag).Name = name;
140      }
141    }
142    private void operatorsTreeView_DoubleClick(object sender, EventArgs e) {
143      if(operatorsTreeView.SelectedNode != null) {
144        IOperator op = operatorsTreeView.SelectedNode.Tag as IOperator;
145        if(op != null) {
146          IView view = op.CreateView();
147          if(view != null)
148            PluginManager.ControlManager.ShowControl(view);
149        }
150      }
151    }
152    #endregion
153
154    #region Button Events
155    private void addOperatorButton_Click(object sender, EventArgs e) {
156      if(chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
157      if(chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
158        TreeNode node = operatorsTreeView.SelectedNode;
159        IOperatorGroup group = node.Tag as IOperatorGroup;
160        group.AddOperator(chooseOperatorDialog.Operator);
161        node.Nodes.Add(CreateTreeNode(chooseOperatorDialog.Operator));
162        operatorsTreeView.Sort();
163        EnableDisableButtons();
164      }
165    }
166    private void removeButton_Click(object sender, EventArgs e) {
167      TreeNode node = operatorsTreeView.SelectedNode;
168      RemoveTreeNode(node);
169    }
170    #endregion
171
172    #region Key Events
173    private void operatorsTreeView_KeyDown(object sender, KeyEventArgs e) {
174      TreeNode node = operatorsTreeView.SelectedNode;
175      if((e.KeyCode == Keys.Delete) && (node != null) && (node.Parent != null))
176        RemoveTreeNode(operatorsTreeView.SelectedNode);
177      if((e.KeyCode == Keys.F2) && (node != null))
178        node.BeginEdit();
179    }
180    #endregion
181
182
183    #region Mouse Events
184    private void operatorsTreeView_MouseDown(object sender, MouseEventArgs e) {
185      if(e.Button != MouseButtons.Right) return;
186      TreeNode clickedNode = operatorsTreeView.GetNodeAt(e.X, e.Y);
187      if(clickedNode != null) {
188        operatorsTreeView.SelectedNode = clickedNode;
189        operatorsTreeView.Refresh();
190      }
191    }
192    #endregion
193
194    #region Drag and Drop Events
195    private void operatorsTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
196      TreeNode node = (TreeNode)e.Item;
197      IOperator op = node.Tag as IOperator;
198      if(op != null) {
199        long opId = ((DatabaseOperatorLibrary)OperatorLibrary).GetId(op);
200        OperatorLink link = new OperatorLink(opId, op);
201        DataObject data = new DataObject();
202        data.SetData("IOperator", link);
203        data.SetData("DragSource", operatorsTreeView);
204        DoDragDrop(data, DragDropEffects.Copy);
205      }
206    }
207    private void operatorsTreeView_DragEnter(object sender, DragEventArgs e) {
208      e.Effect = DragDropEffects.None;
209      if(e.Data.GetDataPresent("IOperator")) {
210        Point p = operatorsTreeView.PointToClient(new Point(e.X, e.Y));
211        TreeNode node = operatorsTreeView.GetNodeAt(p);
212        if((node != null) && (node.Tag is IOperatorGroup))
213          e.Effect = DragDropEffects.Copy;
214      }
215    }
216    private void operatorsTreeView_DragOver(object sender, DragEventArgs e) {
217      e.Effect = DragDropEffects.None;
218      if(e.Data.GetDataPresent("IOperator")) {
219        Point p = operatorsTreeView.PointToClient(new Point(e.X, e.Y));
220        TreeNode node = operatorsTreeView.GetNodeAt(p);
221        if((node != null) && (node.Tag is IOperatorGroup))
222          e.Effect = DragDropEffects.Copy;
223      }
224    }
225    private void operatorsTreeView_DragDrop(object sender, DragEventArgs e) {
226      if(e.Effect != DragDropEffects.None) {
227        if(e.Data.GetDataPresent("IOperator")) {
228          IOperator op = (IOperator)e.Data.GetData("IOperator");
229          Point p = operatorsTreeView.PointToClient(new Point(e.X, e.Y));
230          TreeNode node = operatorsTreeView.GetNodeAt(p);
231          if(node != null) {
232            op = (IOperator)op.Clone();
233
234            while(op.SubOperators.Count > 0)
235              op.RemoveSubOperator(0);
236
237            IOperatorGroup group = (IOperatorGroup)node.Tag;
238            group.AddOperator(op);
239            node.Nodes.Add(CreateTreeNode(op));
240            operatorsTreeView.Sort();
241            EnableDisableButtons();
242          }
243        }
244      }
245    }
246    #endregion
247
248    private void saveButton_Click(object sender, EventArgs e) {
249      ((DatabaseOperatorLibrary)OperatorLibrary).Save();
250    }
251
252    private void refreshButton_Click(object sender, EventArgs e) {
253      ((DatabaseOperatorLibrary)OperatorLibrary).Restore();
254      UpdateControls();
255    }
256  }
257}
Note: See TracBrowser for help on using the repository browser.