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.Core;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Constraints {
|
---|
32 | public partial class AllSubOperatorsTypeConstraintView : ViewBase {
|
---|
33 | private SubOperatorTypeConstraint constraint;
|
---|
34 |
|
---|
35 | public SubOperatorTypeConstraint Constraint {
|
---|
36 | get { return constraint; }
|
---|
37 | set {
|
---|
38 | constraint = value;
|
---|
39 | UpdateAllowedOperatorsList();
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public AllSubOperatorsTypeConstraintView() {
|
---|
44 | InitializeComponent();
|
---|
45 | }
|
---|
46 |
|
---|
47 | public AllSubOperatorsTypeConstraintView(SubOperatorTypeConstraint constraint) {
|
---|
48 | InitializeComponent();
|
---|
49 | this.constraint = constraint;
|
---|
50 | UpdateAllowedOperatorsList();
|
---|
51 | }
|
---|
52 |
|
---|
53 | private void UpdateAllowedOperatorsList() {
|
---|
54 | listView.Clear();
|
---|
55 | foreach(IOperator op in constraint.AllowedSubOperators) {
|
---|
56 | ListViewItem item = new ListViewItem();
|
---|
57 | item.Name = op.Name;
|
---|
58 | item.Text = op.Name;
|
---|
59 | item.Tag = op;
|
---|
60 | listView.Items.Add(item);
|
---|
61 | }
|
---|
62 | Refresh();
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void op_NameChanged(object sender, EventArgs e) {
|
---|
66 | IOperator srcOp = (IOperator)sender;
|
---|
67 |
|
---|
68 | foreach(ListViewItem item in listView.Items) {
|
---|
69 | if(item.Tag == srcOp) {
|
---|
70 | item.Text = srcOp.Name;
|
---|
71 | item.Name = srcOp.Name;
|
---|
72 | break;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | Refresh();
|
---|
76 | }
|
---|
77 |
|
---|
78 | #region Key Events
|
---|
79 | private void listView_KeyDown(object sender, KeyEventArgs e) {
|
---|
80 | if(e.KeyCode == Keys.Delete) {
|
---|
81 | while(listView.SelectedIndices.Count > 0) {
|
---|
82 | constraint.RemoveOperator((IOperator)listView.SelectedItems[0].Tag);
|
---|
83 | listView.Items.RemoveAt(listView.SelectedIndices[0]);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | #endregion
|
---|
88 |
|
---|
89 | #region Drag Events
|
---|
90 |
|
---|
91 | private void listView_DragDrop(object sender, DragEventArgs e) {
|
---|
92 | if(e.Effect != DragDropEffects.None) {
|
---|
93 | if(e.Data.GetDataPresent("IOperator")) {
|
---|
94 | IOperator op = (IOperator)e.Data.GetData("IOperator");
|
---|
95 | ListViewItem item = new ListViewItem();
|
---|
96 | item.Tag = op;
|
---|
97 | item.Name = op.Name;
|
---|
98 | item.Text = op.Name;
|
---|
99 | listView.Items.Add(item);
|
---|
100 | constraint.AddOperator(op);
|
---|
101 | op.NameChanged += new EventHandler(op_NameChanged);
|
---|
102 | listView.SelectedIndices.Clear();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | private void listView_DragEnter(object sender, DragEventArgs e) {
|
---|
109 | this.FindForm().BringToFront();
|
---|
110 | this.Focus();
|
---|
111 | e.Effect = DragDropEffects.None;
|
---|
112 | if(e.Data.GetDataPresent("IOperator")) {
|
---|
113 | IOperator op = (IOperator)e.Data.GetData("IOperator");
|
---|
114 | if(!ListContains(op)) {
|
---|
115 | e.Effect = DragDropEffects.Copy;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | private void listView_DragOver(object sender, DragEventArgs e) {
|
---|
122 | e.Effect = DragDropEffects.None;
|
---|
123 | if(e.Data.GetDataPresent("IOperator")) {
|
---|
124 | IOperator op = (IOperator)e.Data.GetData("IOperator");
|
---|
125 | if(!ListContains(op)) {
|
---|
126 | e.Effect = DragDropEffects.Copy;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | #endregion
|
---|
131 |
|
---|
132 | #region button events
|
---|
133 | private void removeButton_Click(object sender, EventArgs e) {
|
---|
134 | while(listView.SelectedIndices.Count > 0) {
|
---|
135 | constraint.RemoveOperator((IOperator)listView.SelectedItems[0].Tag);
|
---|
136 | listView.Items.RemoveAt(listView.SelectedIndices[0]);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | #endregion
|
---|
140 |
|
---|
141 | private bool ListContains(IOperator op) {
|
---|
142 | foreach(ListViewItem item in listView.Items) {
|
---|
143 | if(item.Tag == op) return true;
|
---|
144 | }
|
---|
145 | return false;
|
---|
146 | }
|
---|
147 |
|
---|
148 | private void listView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
149 | if(listView.SelectedItems.Count > 0) {
|
---|
150 | removeButton.Enabled = true;
|
---|
151 | } else {
|
---|
152 | removeButton.Enabled = false;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|