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.Data {
|
---|
32 | public partial class ItemListView<T> : ViewBase where T : IItem {
|
---|
33 | private ChooseItemDialog chooseItemDialog;
|
---|
34 |
|
---|
35 | public ItemList<T> ItemList {
|
---|
36 | get { return (ItemList<T>)Item; }
|
---|
37 | set { base.Item = value; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public ItemListView() {
|
---|
41 | InitializeComponent();
|
---|
42 | itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
|
---|
43 | }
|
---|
44 | public ItemListView(ItemList<T> itemList)
|
---|
45 | : this() {
|
---|
46 | ItemList = itemList;
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected override void RemoveItemEvents() {
|
---|
50 | ItemList.ItemAdded -= new EventHandler<ItemIndexEventArgs>(ItemList_ItemInserted);
|
---|
51 | ItemList.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(ItemList_ItemRemoved);
|
---|
52 | ItemList.Cleared -= new EventHandler(ItemList_Cleared);
|
---|
53 | base.RemoveItemEvents();
|
---|
54 | }
|
---|
55 | protected override void AddItemEvents() {
|
---|
56 | base.AddItemEvents();
|
---|
57 | ItemList.ItemAdded += new EventHandler<ItemIndexEventArgs>(ItemList_ItemInserted);
|
---|
58 | ItemList.ItemRemoved += new EventHandler<ItemIndexEventArgs>(ItemList_ItemRemoved);
|
---|
59 | ItemList.Cleared += new EventHandler(ItemList_Cleared);
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected override void UpdateControls() {
|
---|
63 | base.UpdateControls();
|
---|
64 | detailsGroupBox.Controls.Clear();
|
---|
65 | detailsGroupBox.Enabled = false;
|
---|
66 | removeButton.Enabled = false;
|
---|
67 | if(ItemList == null) {
|
---|
68 | typeTextBox.Text = "";
|
---|
69 | splitContainer.Enabled = false;
|
---|
70 | } else {
|
---|
71 | typeTextBox.Text = typeof(T).FullName;
|
---|
72 | splitContainer.Enabled = true;
|
---|
73 | foreach(ListViewItem item in itemsListView.Items) {
|
---|
74 | ((IItem)item.Tag).Changed -= new EventHandler(Item_Changed);
|
---|
75 | }
|
---|
76 | itemsListView.Items.Clear();
|
---|
77 | foreach(IItem data in ItemList) {
|
---|
78 | ListViewItem item = new ListViewItem();
|
---|
79 | item.Text = data.ToString();
|
---|
80 | item.Tag = data;
|
---|
81 | itemsListView.Items.Add(item);
|
---|
82 | data.Changed += new EventHandler(Item_Changed);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void elementsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
88 | if(detailsGroupBox.Controls.Count > 0)
|
---|
89 | detailsGroupBox.Controls[0].Dispose();
|
---|
90 | detailsGroupBox.Controls.Clear();
|
---|
91 | detailsGroupBox.Enabled = false;
|
---|
92 | removeButton.Enabled = false;
|
---|
93 | if(itemsListView.SelectedItems.Count > 0) {
|
---|
94 | removeButton.Enabled = true;
|
---|
95 | }
|
---|
96 | if(itemsListView.SelectedItems.Count == 1) {
|
---|
97 | IItem data = (IItem)itemsListView.SelectedItems[0].Tag;
|
---|
98 | Control view = (Control)data.CreateView();
|
---|
99 | detailsGroupBox.Controls.Add(view);
|
---|
100 | view.Dock = DockStyle.Fill;
|
---|
101 | detailsGroupBox.Enabled = true;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | #region Size Changed Events
|
---|
106 | private void elementsListView_SizeChanged(object sender, EventArgs e) {
|
---|
107 | if(itemsListView.Columns.Count > 0)
|
---|
108 | itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
|
---|
109 | }
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | #region Key Events
|
---|
113 | private void elementsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
114 | if(e.KeyCode == Keys.Delete) {
|
---|
115 | while(itemsListView.SelectedIndices.Count > 0)
|
---|
116 | ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
|
---|
117 | e.Handled = true;
|
---|
118 | } else if((e.KeyCode == Keys.C) && (e.Modifiers == Keys.Control)) {
|
---|
119 | // copy contents of itemlist to clipboard
|
---|
120 | StringBuilder buffer = new StringBuilder();
|
---|
121 | for(int i = 0; i < itemsListView.Items.Count; i++) {
|
---|
122 | buffer.Append(itemsListView.Items[i].Text);
|
---|
123 | buffer.Append("\n");
|
---|
124 | }
|
---|
125 | Clipboard.SetText(buffer.ToString());
|
---|
126 | e.Handled = true;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | #region Button Events
|
---|
132 | private void addButton_Click(object sender, EventArgs e) {
|
---|
133 | if(chooseItemDialog == null) {
|
---|
134 | chooseItemDialog = new ChooseItemDialog(typeof(T));
|
---|
135 | chooseItemDialog.Caption = "Add Item";
|
---|
136 | }
|
---|
137 | if(chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
138 | try {
|
---|
139 | ItemList.Add((T)chooseItemDialog.Item);
|
---|
140 | } catch(Exception ex) {
|
---|
141 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | private void removeButton_Click(object sender, EventArgs e) {
|
---|
146 | while(itemsListView.SelectedIndices.Count > 0)
|
---|
147 | ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
|
---|
148 | }
|
---|
149 | #endregion
|
---|
150 |
|
---|
151 | #region Drag and Drop Events
|
---|
152 | private void elementsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
153 | ListViewItem item = (ListViewItem)e.Item;
|
---|
154 | IItem data = (IItem)item.Tag;
|
---|
155 | DataObject dataObject = new DataObject();
|
---|
156 | dataObject.SetData("IItem", data);
|
---|
157 | dataObject.SetData("DragSource", itemsListView);
|
---|
158 | DoDragDrop(dataObject, DragDropEffects.Move);
|
---|
159 | }
|
---|
160 | private void elementsListView_DragEnter(object sender, DragEventArgs e) {
|
---|
161 | e.Effect = DragDropEffects.None;
|
---|
162 | if(e.Data.GetDataPresent("IItem")) {
|
---|
163 | Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
|
---|
164 | ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
|
---|
165 | if(item != null)
|
---|
166 | e.Effect = DragDropEffects.Move;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | private void elementsListView_DragOver(object sender, DragEventArgs e) {
|
---|
170 | e.Effect = DragDropEffects.None;
|
---|
171 | if(e.Data.GetDataPresent("IItem")) {
|
---|
172 | Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
|
---|
173 | ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
|
---|
174 | if(item != null)
|
---|
175 | e.Effect = DragDropEffects.Move;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | private void elementsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
179 | if(e.Effect != DragDropEffects.None) {
|
---|
180 | if(e.Data.GetDataPresent("IItem")) {
|
---|
181 | IItem data = (IItem)e.Data.GetData("IItem");
|
---|
182 | Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
|
---|
183 | ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
|
---|
184 | if(item != null) {
|
---|
185 | int index = item.Index;
|
---|
186 | ItemList.Remove((T)data);
|
---|
187 | ItemList.Insert(index, (T)data);
|
---|
188 | itemsListView.SelectedIndices.Clear();
|
---|
189 | itemsListView.SelectedIndices.Add(index);
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | #endregion
|
---|
195 |
|
---|
196 | #region Item and Item List Events
|
---|
197 | private void ItemList_ItemInserted(object sender, ItemIndexEventArgs e) {
|
---|
198 | if(InvokeRequired)
|
---|
199 | Invoke(new EventHandler<ItemIndexEventArgs>(ItemList_ItemInserted), sender, e);
|
---|
200 | else {
|
---|
201 | ListViewItem item = new ListViewItem();
|
---|
202 | item.Text = e.Item.ToString();
|
---|
203 | item.Tag = e.Item;
|
---|
204 | itemsListView.Items.Insert(e.Index, item);
|
---|
205 | e.Item.Changed += new EventHandler(Item_Changed);
|
---|
206 | }
|
---|
207 | }
|
---|
208 | private void ItemList_ItemRemoved(object sender, ItemIndexEventArgs e) {
|
---|
209 | if(InvokeRequired)
|
---|
210 | Invoke(new EventHandler<ItemIndexEventArgs>(ItemList_ItemRemoved), sender, e);
|
---|
211 | else {
|
---|
212 | itemsListView.Items.RemoveAt(e.Index);
|
---|
213 | e.Item.Changed -= new EventHandler(Item_Changed);
|
---|
214 | }
|
---|
215 | }
|
---|
216 | private void ItemList_Cleared(object sender, EventArgs e) {
|
---|
217 | Refresh();
|
---|
218 | }
|
---|
219 | private void Item_Changed(object sender, EventArgs e) {
|
---|
220 | if(InvokeRequired)
|
---|
221 | Invoke(new EventHandler(Item_Changed), sender, e);
|
---|
222 | else {
|
---|
223 | IItem data = (IItem)sender;
|
---|
224 | foreach(ListViewItem item in itemsListView.Items) {
|
---|
225 | if(item.Tag == data)
|
---|
226 | item.Text = data.ToString();
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|
230 | #endregion
|
---|
231 | }
|
---|
232 | }
|
---|