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.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Text;
|
---|
28 | using System.Windows.Forms;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Core.Views {
|
---|
32 | /// <summary>
|
---|
33 | /// The visual representation of a <see cref="Variable"/>.
|
---|
34 | /// </summary>
|
---|
35 | [Content(typeof(Variable), true)]
|
---|
36 | public partial class VariableView : NamedItemView {
|
---|
37 | protected TypeSelectorDialog typeSelectorDialog;
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Gets or sets the variable to represent visually.
|
---|
41 | /// </summary>
|
---|
42 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
|
---|
43 | /// No own data storage present.</remarks>
|
---|
44 | public Variable Variable {
|
---|
45 | get { return (Variable)Item; }
|
---|
46 | set { base.Item = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
|
---|
51 | /// </summary>
|
---|
52 | public VariableView() {
|
---|
53 | InitializeComponent();
|
---|
54 | Caption = "Variable";
|
---|
55 | }
|
---|
56 | /// <summary>
|
---|
57 | /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
|
---|
58 | /// </summary>
|
---|
59 | /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
|
---|
60 | /// <param name="variable">The variable to represent visually.</param>
|
---|
61 | public VariableView(Variable variable)
|
---|
62 | : this() {
|
---|
63 | Variable = variable;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /// <summary>
|
---|
67 | /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
|
---|
68 | /// </summary>
|
---|
69 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
70 | protected override void DeregisterObjectEvents() {
|
---|
71 | Variable.ValueChanged -= new EventHandler(Variable_ValueChanged);
|
---|
72 | base.DeregisterObjectEvents();
|
---|
73 | }
|
---|
74 |
|
---|
75 | /// <summary>
|
---|
76 | /// Adds eventhandlers to the underlying <see cref="Variable"/>.
|
---|
77 | /// </summary>
|
---|
78 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
79 | protected override void RegisterObjectEvents() {
|
---|
80 | base.RegisterObjectEvents();
|
---|
81 | Variable.ValueChanged += new EventHandler(Variable_ValueChanged);
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override void OnObjectChanged() {
|
---|
85 | base.OnObjectChanged();
|
---|
86 | if (Variable == null) {
|
---|
87 | Caption = "Variable";
|
---|
88 | dataTypeTextBox.Text = "-";
|
---|
89 | dataTypeTextBox.Enabled = false;
|
---|
90 | setValueButton.Enabled = false;
|
---|
91 | clearValueButton.Enabled = false;
|
---|
92 | valueGroupBox.Enabled = false;
|
---|
93 | viewHost.Object = null;
|
---|
94 | } else {
|
---|
95 | Caption = Variable.Name + " (" + Variable.GetType().Name + ")";
|
---|
96 | dataTypeTextBox.Text = Variable.Value == null ? "-" : Variable.Value.GetType().FullName;
|
---|
97 | dataTypeTextBox.Enabled = Variable.Value != null;
|
---|
98 | setValueButton.Enabled = Variable.Value == null;
|
---|
99 | clearValueButton.Enabled = Variable.Value != null;
|
---|
100 | valueGroupBox.Enabled = true;
|
---|
101 | viewHost.Object = Variable.Value;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected virtual void Variable_ValueChanged(object sender, EventArgs e) {
|
---|
106 | if (InvokeRequired)
|
---|
107 | Invoke(new EventHandler(Variable_ValueChanged), sender, e);
|
---|
108 | else {
|
---|
109 | dataTypeTextBox.Text = Variable.Value == null ? "-" : Variable.Value.GetType().FullName;
|
---|
110 | dataTypeTextBox.Enabled = Variable.Value != null;
|
---|
111 | setValueButton.Enabled = Variable.Value == null;
|
---|
112 | clearValueButton.Enabled = Variable.Value != null;
|
---|
113 | viewHost.Object = Variable.Value;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected virtual void setValueButton_Click(object sender, EventArgs e) {
|
---|
118 | if (typeSelectorDialog == null) {
|
---|
119 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
120 | typeSelectorDialog.Caption = "Select Value Type";
|
---|
121 | typeSelectorDialog.TypeSelector.Configure(typeof(IItem), false, false);
|
---|
122 | }
|
---|
123 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
124 | Variable.Value = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
125 | }
|
---|
126 | }
|
---|
127 | protected virtual void clearValueButton_Click(object sender, EventArgs e) {
|
---|
128 | Variable.Value = null;
|
---|
129 | }
|
---|
130 | protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
|
---|
131 | e.Effect = DragDropEffects.None;
|
---|
132 | Type type = e.Data.GetData("Type") as Type;
|
---|
133 | if ((type != null) && (typeof(IItem).IsAssignableFrom(type))) {
|
---|
134 | if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy; // CTRL key
|
---|
135 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
136 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
137 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
138 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
|
---|
142 | if (e.Effect != DragDropEffects.None) {
|
---|
143 | IItem item = e.Data.GetData("Value") as IItem;
|
---|
144 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IItem)item.Clone();
|
---|
145 | Variable.Value = item;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|