Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/AllSubOperatorsTypeConstraintView.cs @ 1176

Last change on this file since 1176 was 1176, checked in by vdorfer, 15 years ago

Created API documentation for HeuristicLab.BitVector and HeuristicLab.Constraints namespace and changed a comment in HeuristicLab.IntVector namespace(#331)

File size: 5.3 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.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core;
30
31namespace HeuristicLab.Constraints {
32  /// <summary>
33  /// The visual representation of the <see cref="AllSubOperatorsTypeConstraint"/>.
34  /// </summary>
35  public partial class AllSubOperatorsTypeConstraintView : ViewBase {
36    private SubOperatorTypeConstraint constraint = new SubOperatorTypeConstraint();
37
38    /// <summary>
39    /// Gets or sets the SubOperatorTypeConstraint to display.
40    /// </summary>
41    public SubOperatorTypeConstraint Constraint {
42      get { return constraint; }
43      set {
44        constraint = value;
45        UpdateAllowedOperatorsList();
46      }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="AllSubOperatorsTypeConstraintView"/>.
51    /// </summary>
52    public AllSubOperatorsTypeConstraintView() {
53      InitializeComponent();
54    }
55
56    /// <summary>
57    /// Initializes a new instance of <see cref="AllSubOperatorsTypeConstraintView"/> with
58    /// the given <paramref name="constraint"/> to display.
59    /// </summary>
60    /// <param name="constraint">The constraint that should be displayed.</param>
61    public AllSubOperatorsTypeConstraintView(SubOperatorTypeConstraint constraint) {
62      this.constraint = constraint;
63      InitializeComponent();
64    }
65
66    private void UpdateAllowedOperatorsList() {
67      listView.Clear();
68      foreach(IOperator op in constraint.AllowedSubOperators) {
69        ListViewItem item = new ListViewItem();
70        item.Name = op.Name;
71        item.Text = op.Name;
72        item.Tag = op;
73        listView.Items.Add(item);
74      }
75      Refresh();
76    }
77
78    private void op_NameChanged(object sender, EventArgs e) {
79      IOperator srcOp = (IOperator)sender;
80
81      foreach(ListViewItem item in listView.Items) {
82        if(item.Tag == srcOp) {
83          item.Text = srcOp.Name;
84          item.Name = srcOp.Name;
85          break;
86        }
87      }
88      Refresh();
89    }
90
91    #region Key Events
92    private void listView_KeyDown(object sender, KeyEventArgs e) {
93      if(e.KeyCode == Keys.Delete) {
94        while(listView.SelectedIndices.Count > 0) {
95          constraint.RemoveOperator((IOperator)listView.SelectedItems[0].Tag);
96          listView.Items.RemoveAt(listView.SelectedIndices[0]);
97        }
98      }
99    }
100    #endregion
101
102    #region Drag Events
103    private void listView_DragDrop(object sender, DragEventArgs e) {
104      if(e.Effect != DragDropEffects.None) {
105        if(e.Data.GetDataPresent("IOperator")) {
106          IOperator op = (IOperator)e.Data.GetData("IOperator");
107          ListViewItem item = new ListViewItem();
108          item.Tag = op;
109          item.Name = op.Name;
110          item.Text = op.Name;
111          listView.Items.Add(item);
112          constraint.AddOperator(op);
113          op.NameChanged += new EventHandler(op_NameChanged);
114          listView.SelectedIndices.Clear();
115        }
116      }
117    }
118
119    private void listView_DragEnter(object sender, DragEventArgs e) {
120      this.FindForm().BringToFront();
121      this.Focus();
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
131    private void listView_DragOver(object sender, DragEventArgs e) {
132      e.Effect = DragDropEffects.None;
133      if(e.Data.GetDataPresent("IOperator")) {
134        IOperator op = (IOperator)e.Data.GetData("IOperator");
135        if(!ListContains(op)) {
136          e.Effect = DragDropEffects.Copy;
137        }
138      }
139    }
140    #endregion
141
142    #region button events
143    private void removeButton_Click(object sender, EventArgs e) {
144      while(listView.SelectedIndices.Count > 0) {
145        constraint.RemoveOperator((IOperator)listView.SelectedItems[0].Tag);
146        listView.Items.RemoveAt(listView.SelectedIndices[0]);
147      }
148    }
149    #endregion
150
151    private bool ListContains(IOperator op) {
152      foreach(ListViewItem item in listView.Items) {
153        if(item.Tag == op) return true;
154      }
155      return false;
156    }
157
158    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
159      if(listView.SelectedItems.Count > 0) {
160        removeButton.Enabled = true;
161      } else {
162        removeButton.Enabled = false;
163      }
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.