Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Data/ItemListView_T.cs @ 2426

Last change on this file since 2426 was 40, checked in by swagner, 17 years ago

Worked on ticket #41

  • added generic ItemList<T>
File size: 8.0 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.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      }
118    }
119    #endregion
120
121    #region Button Events
122    private void addButton_Click(object sender, EventArgs e) {
123      if (chooseItemDialog == null) {
124        chooseItemDialog = new ChooseItemDialog(typeof(T));
125        chooseItemDialog.Caption = "Add Item";
126      }
127      if (chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
128        try {
129          ItemList.Add((T)chooseItemDialog.Item);
130        }
131        catch (Exception ex) {
132          Auxiliary.ShowErrorMessageBox(ex);
133        }
134      }
135    }
136    private void removeButton_Click(object sender, EventArgs e) {
137      while (itemsListView.SelectedIndices.Count > 0)
138        ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
139    }
140    #endregion
141
142    #region Drag and Drop Events
143    private void elementsListView_ItemDrag(object sender, ItemDragEventArgs e) {
144      ListViewItem item = (ListViewItem)e.Item;
145      IItem data = (IItem)item.Tag;
146      DataObject dataObject = new DataObject();
147      dataObject.SetData("IItem", data);
148      dataObject.SetData("DragSource", itemsListView);
149      DoDragDrop(dataObject, DragDropEffects.Move);
150    }
151    private void elementsListView_DragEnter(object sender, DragEventArgs e) {
152      e.Effect = DragDropEffects.None;
153      if (e.Data.GetDataPresent("IItem")) {
154        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
155        ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
156        if (item != null)
157          e.Effect = DragDropEffects.Move;
158      }
159    }
160    private void elementsListView_DragOver(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_DragDrop(object sender, DragEventArgs e) {
170      if (e.Effect != DragDropEffects.None) {
171        if (e.Data.GetDataPresent("IItem")) {
172          IItem data = (IItem)e.Data.GetData("IItem");
173          Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
174          ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
175          if (item != null) {
176            int index = item.Index;
177            ItemList.Remove((T)data);
178            ItemList.Insert(index, (T)data);
179            itemsListView.SelectedIndices.Clear();
180            itemsListView.SelectedIndices.Add(index);
181          }
182        }
183      }
184    }
185    #endregion
186
187    #region Item and Item List Events
188    private void ItemList_ItemInserted(object sender, ItemIndexEventArgs e) {
189      if (InvokeRequired)
190        Invoke(new EventHandler<ItemIndexEventArgs>(ItemList_ItemInserted), sender, e);
191      else {
192        ListViewItem item = new ListViewItem();
193        item.Text = e.Item.ToString();
194        item.Tag = e.Item;
195        itemsListView.Items.Insert(e.Index, item);
196        e.Item.Changed += new EventHandler(Item_Changed);
197      }
198    }
199    private void ItemList_ItemRemoved(object sender, ItemIndexEventArgs e) {
200      if (InvokeRequired)
201        Invoke(new EventHandler<ItemIndexEventArgs>(ItemList_ItemRemoved), sender, e);
202      else {
203        itemsListView.Items.RemoveAt(e.Index);
204        e.Item.Changed -= new EventHandler(Item_Changed);
205      }
206    }
207    private void ItemList_Cleared(object sender, EventArgs e) {
208      Refresh();
209    }
210    private void Item_Changed(object sender, EventArgs e) {
211      if (InvokeRequired)
212        Invoke(new EventHandler(Item_Changed), sender, e);
213      else {
214        IItem data = (IItem)sender;
215        foreach (ListViewItem item in itemsListView.Items) {
216          if (item.Tag == data)
217            item.Text = data.ToString();
218        }
219      }
220    }
221    #endregion
222  }
223}
Note: See TracBrowser for help on using the repository browser.