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