Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Data/3.2/ItemListView_T.cs @ 3708

Last change on this file since 3708 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

File size: 10.1 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.Common;
30using HeuristicLab.Core;
31
32namespace HeuristicLab.Data {
33  /// <summary>
34  /// The visual representation of the class <see cref="HeuristicLab.Data.ItemList&lt;T&gt;"/>.
35  /// </summary>
36  /// <typeparam name="T">The type of the items in the list.</typeparam>
37  public partial class ItemListView<T> : ViewBase where T : IItem {
38    private ChooseItemDialog chooseItemDialog;
39
40    /// <summary>
41    /// Gets or sets the item list to represent visually.
42    /// </summary>
43    /// <remarks>Uses property <see cref="HeuristicLab.Core.ViewBase.Item"/> of base class <see cref="ViewBase"/>.
44    /// No own data storage present.</remarks>
45    public ItemList<T> ItemList {
46      get { return (ItemList<T>)Item; }
47      set { base.Item = value; }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of the class <see cref="HeuristicLab.Data.ItemListView&lt;T&gt;"/>.
52    /// </summary>
53    public ItemListView() {
54      InitializeComponent();
55      itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
56    }
57    /// <summary>
58    /// Initializes a new instance of the class <see cref="HeuristicLab.Data.ItemListView&lt;T&gt;"/> with the given
59    /// <paramref name="itemList"/>.
60    /// <note type="caution"> No CopyConstructor! <paramref name="itemList"/> is not copied!</note>
61    /// </summary>
62    /// <param name="itemList">The list of items to represent visually.</param>
63    public ItemListView(ItemList<T> itemList)
64      : this() {
65      ItemList = itemList;
66    }
67
68    /// <summary>
69    /// Removes the eventhandlers from the underlying <see cref="HeuristicLab.Data.ItemList&lt;T&gt;"/>.
70    /// </summary>
71    /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.
72    /// </remarks>
73    protected override void RemoveItemEvents() {
74      ItemList.ItemAdded -= new EventHandler<EventArgs<IItem, int>>(ItemList_ItemInserted);
75      ItemList.ItemRemoved -= new EventHandler<EventArgs<IItem, int>>(ItemList_ItemRemoved);
76      ItemList.Cleared -= new EventHandler(ItemList_Cleared);
77      base.RemoveItemEvents();
78    }
79    /// <summary>
80    /// Adds eventhandlers to the underlying <see cref="HeuristicLab.Data.ItemList&lt;T&gt;"/>.
81    /// </summary>
82    /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.
83    /// </remarks>
84    protected override void AddItemEvents() {
85      base.AddItemEvents();
86      ItemList.ItemAdded += new EventHandler<EventArgs<IItem, int>>(ItemList_ItemInserted);
87      ItemList.ItemRemoved += new EventHandler<EventArgs<IItem, int>>(ItemList_ItemRemoved);
88      ItemList.Cleared += new EventHandler(ItemList_Cleared);
89    }
90
91    /// <summary>
92    /// Update all controls with the lastest elements in the list.
93    /// </summary>
94    protected override void UpdateControls() {
95      base.UpdateControls();
96      detailsGroupBox.Controls.Clear();
97      detailsGroupBox.Enabled = false;
98      removeButton.Enabled = false;
99      if(ItemList == null) {
100        typeTextBox.Text = "";
101        splitContainer.Enabled = false;
102      } else {
103        typeTextBox.Text = typeof(T).FullName;
104        splitContainer.Enabled = true;
105        foreach(ListViewItem item in itemsListView.Items) {
106          ((IItem)item.Tag).Changed -= new EventHandler(Item_Changed);
107        }
108        itemsListView.Items.Clear();
109        foreach(IItem data in ItemList) {
110          ListViewItem item = new ListViewItem();
111          item.Text = data.ToString();
112          item.Tag = data;
113          itemsListView.Items.Add(item);
114          data.Changed += new EventHandler(Item_Changed);
115        }
116      }
117    }
118
119    private void elementsListView_SelectedIndexChanged(object sender, EventArgs e) {
120      if(detailsGroupBox.Controls.Count > 0)
121        detailsGroupBox.Controls[0].Dispose();
122      detailsGroupBox.Controls.Clear();
123      detailsGroupBox.Enabled = false;
124      removeButton.Enabled = false;
125      if(itemsListView.SelectedItems.Count > 0) {
126        removeButton.Enabled = true;
127      }
128      if(itemsListView.SelectedItems.Count == 1) {
129        IItem data = (IItem)itemsListView.SelectedItems[0].Tag;
130        Control view = (Control)data.CreateView();
131        detailsGroupBox.Controls.Add(view);
132        view.Dock = DockStyle.Fill;
133        detailsGroupBox.Enabled = true;
134      }
135    }
136
137    #region Size Changed Events
138    private void elementsListView_SizeChanged(object sender, EventArgs e) {
139      if(itemsListView.Columns.Count > 0)
140        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
141    }
142    #endregion
143
144    #region Key Events
145    private void elementsListView_KeyDown(object sender, KeyEventArgs e) {
146      if(e.KeyCode == Keys.Delete) {
147        while(itemsListView.SelectedIndices.Count > 0)
148          ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
149        e.Handled = true;
150      } else if((e.KeyCode == Keys.C) && (e.Modifiers == Keys.Control)) {
151        // copy contents of itemlist to clipboard
152        StringBuilder buffer = new StringBuilder();
153        for(int i = 0; i < itemsListView.Items.Count; i++) {
154          buffer.Append(itemsListView.Items[i].Text.Replace(';','\t'));
155          buffer.Append("\n");
156        }
157        Clipboard.SetText(buffer.ToString());
158        e.Handled = true;
159      }
160    }
161    #endregion
162
163    #region Button Events
164    private void addButton_Click(object sender, EventArgs e) {
165      if(chooseItemDialog == null) {
166        chooseItemDialog = new ChooseItemDialog(typeof(T));
167        chooseItemDialog.Caption = "Add Item";
168      }
169      if(chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
170        try {
171          ItemList.Add((T)chooseItemDialog.Item);
172        } catch(Exception ex) {
173          Auxiliary.ShowErrorMessageBox(ex);
174        }
175      }
176    }
177    private void removeButton_Click(object sender, EventArgs e) {
178      while(itemsListView.SelectedIndices.Count > 0)
179        ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
180    }
181    #endregion
182
183    #region Drag and Drop Events
184    private void elementsListView_ItemDrag(object sender, ItemDragEventArgs e) {
185      ListViewItem item = (ListViewItem)e.Item;
186      IItem data = (IItem)item.Tag;
187      DataObject dataObject = new DataObject();
188      dataObject.SetData("IItem", data);
189      dataObject.SetData("DragSource", itemsListView);
190      DoDragDrop(dataObject, DragDropEffects.Move);
191    }
192    private void elementsListView_DragEnter(object sender, DragEventArgs e) {
193      e.Effect = DragDropEffects.None;
194      if(e.Data.GetDataPresent("IItem")) {
195        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
196        ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
197        if(item != null)
198          e.Effect = DragDropEffects.Move;
199      }
200    }
201    private void elementsListView_DragOver(object sender, DragEventArgs e) {
202      e.Effect = DragDropEffects.None;
203      if(e.Data.GetDataPresent("IItem")) {
204        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
205        ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
206        if(item != null)
207          e.Effect = DragDropEffects.Move;
208      }
209    }
210    private void elementsListView_DragDrop(object sender, DragEventArgs e) {
211      if(e.Effect != DragDropEffects.None) {
212        if(e.Data.GetDataPresent("IItem")) {
213          IItem data = (IItem)e.Data.GetData("IItem");
214          Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
215          ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
216          if(item != null) {
217            int index = item.Index;
218            ItemList.Remove((T)data);
219            ItemList.Insert(index, (T)data);
220            itemsListView.SelectedIndices.Clear();
221            itemsListView.SelectedIndices.Add(index);
222          }
223        }
224      }
225    }
226    #endregion
227
228    #region Item and Item List Events
229    private void ItemList_ItemInserted(object sender, EventArgs<IItem, int> e) {
230      if(InvokeRequired)
231        Invoke(new EventHandler<EventArgs<IItem, int>>(ItemList_ItemInserted), sender, e);
232      else {
233        ListViewItem item = new ListViewItem();
234        item.Text = e.Value.ToString();
235        item.Tag = e.Value;
236        itemsListView.Items.Insert(e.Value2, item);
237        e.Value.Changed += new EventHandler(Item_Changed);
238      }
239    }
240    private void ItemList_ItemRemoved(object sender, EventArgs<IItem, int> e) {
241      if(InvokeRequired)
242        Invoke(new EventHandler<EventArgs<IItem, int>>(ItemList_ItemRemoved), sender, e);
243      else {
244        itemsListView.Items.RemoveAt(e.Value2);
245        e.Value.Changed -= new EventHandler(Item_Changed);
246      }
247    }
248    private void ItemList_Cleared(object sender, EventArgs e) {
249      Refresh();
250    }
251    private void Item_Changed(object sender, EventArgs e) {
252      if(InvokeRequired)
253        Invoke(new EventHandler(Item_Changed), sender, e);
254      else {
255        IItem data = (IItem)sender;
256        foreach(ListViewItem item in itemsListView.Items) {
257          if(item.Tag == data)
258            item.Text = data.ToString();
259        }
260      }
261    }
262    #endregion
263  }
264}
Note: See TracBrowser for help on using the repository browser.