[417] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.ComponentModel;
|
---|
| 26 | using System.Drawing;
|
---|
| 27 | using System.Data;
|
---|
| 28 | using System.Text;
|
---|
| 29 | using System.Windows.Forms;
|
---|
| 30 | using HeuristicLab.PluginInfrastructure;
|
---|
| 31 | using HeuristicLab.Core;
|
---|
| 32 |
|
---|
| 33 | namespace 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);
|
---|
[418] | 58 | if(result == 0)
|
---|
[417] | 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;
|
---|
[418] | 69 | if(OperatorLibrary == null) {
|
---|
[417] | 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) {
|
---|
[418] | 100 | if(node.Tag is IOperatorGroup) {
|
---|
[417] | 101 | IOperatorGroup group = node.Tag as IOperatorGroup;
|
---|
| 102 | IOperatorGroup parent = node.Parent.Tag as IOperatorGroup;
|
---|
| 103 | parent.RemoveSubGroup(group);
|
---|
[418] | 104 | } else if(node.Tag is IOperator) {
|
---|
[417] | 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 |
|
---|
[418] | 118 | if(operatorsTreeView.SelectedNode != null) {
|
---|
| 119 | if(operatorsTreeView.SelectedNode.Parent != null)
|
---|
[417] | 120 | removeButton.Enabled = true;
|
---|
[418] | 121 | if(operatorsTreeView.SelectedNode.Tag is IOperatorGroup) {
|
---|
[417] | 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;
|
---|
[418] | 135 | if(name != null) {
|
---|
| 136 | if(e.Node.Tag is IOperatorGroup)
|
---|
[417] | 137 | ((IOperatorGroup)e.Node.Tag).Name = name;
|
---|
[418] | 138 | else if(e.Node.Tag is IOperator)
|
---|
[417] | 139 | ((IOperator)e.Node.Tag).Name = name;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | private void operatorsTreeView_DoubleClick(object sender, EventArgs e) {
|
---|
[418] | 143 | if(operatorsTreeView.SelectedNode != null) {
|
---|
[417] | 144 | IOperator op = operatorsTreeView.SelectedNode.Tag as IOperator;
|
---|
[418] | 145 | if(op != null) {
|
---|
[417] | 146 | IView view = op.CreateView();
|
---|
[418] | 147 | if(view != null)
|
---|
[417] | 148 | PluginManager.ControlManager.ShowControl(view);
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | #endregion
|
---|
| 153 |
|
---|
| 154 | #region Button Events
|
---|
| 155 | private void addOperatorButton_Click(object sender, EventArgs e) {
|
---|
[418] | 156 | if(chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
|
---|
| 157 | if(chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[417] | 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;
|
---|
[418] | 175 | if((e.KeyCode == Keys.Delete) && (node != null) && (node.Parent != null))
|
---|
[417] | 176 | RemoveTreeNode(operatorsTreeView.SelectedNode);
|
---|
[418] | 177 | if((e.KeyCode == Keys.F2) && (node != null))
|
---|
[417] | 178 | node.BeginEdit();
|
---|
| 179 | }
|
---|
| 180 | #endregion
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 | #region Mouse Events
|
---|
| 184 | private void operatorsTreeView_MouseDown(object sender, MouseEventArgs e) {
|
---|
[418] | 185 | if(e.Button != MouseButtons.Right) return;
|
---|
[417] | 186 | TreeNode clickedNode = operatorsTreeView.GetNodeAt(e.X, e.Y);
|
---|
[418] | 187 | if(clickedNode != null) {
|
---|
[417] | 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;
|
---|
[418] | 198 | if(op != null) {
|
---|
| 199 | long opId = ((DatabaseOperatorLibrary)OperatorLibrary).GetId(op);
|
---|
| 200 | OperatorLink link = new OperatorLink(opId, op);
|
---|
[417] | 201 | DataObject data = new DataObject();
|
---|
[418] | 202 | data.SetData("IOperator", link);
|
---|
[417] | 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;
|
---|
[418] | 209 | if(e.Data.GetDataPresent("IOperator")) {
|
---|
[417] | 210 | Point p = operatorsTreeView.PointToClient(new Point(e.X, e.Y));
|
---|
| 211 | TreeNode node = operatorsTreeView.GetNodeAt(p);
|
---|
[418] | 212 | if((node != null) && (node.Tag is IOperatorGroup))
|
---|
[417] | 213 | e.Effect = DragDropEffects.Copy;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 | private void operatorsTreeView_DragOver(object sender, DragEventArgs e) {
|
---|
| 217 | e.Effect = DragDropEffects.None;
|
---|
[418] | 218 | if(e.Data.GetDataPresent("IOperator")) {
|
---|
[417] | 219 | Point p = operatorsTreeView.PointToClient(new Point(e.X, e.Y));
|
---|
| 220 | TreeNode node = operatorsTreeView.GetNodeAt(p);
|
---|
[418] | 221 | if((node != null) && (node.Tag is IOperatorGroup))
|
---|
[417] | 222 | e.Effect = DragDropEffects.Copy;
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 | private void operatorsTreeView_DragDrop(object sender, DragEventArgs e) {
|
---|
[418] | 226 | if(e.Effect != DragDropEffects.None) {
|
---|
| 227 | if(e.Data.GetDataPresent("IOperator")) {
|
---|
[417] | 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);
|
---|
[418] | 231 | if(node != null) {
|
---|
[417] | 232 | op = (IOperator)op.Clone();
|
---|
| 233 |
|
---|
[418] | 234 | while(op.SubOperators.Count > 0)
|
---|
[417] | 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 | }
|
---|