Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Data/3.3/ItemListView_T.cs @ 2273

Last change on this file since 2273 was 1609, checked in by gkronber, 16 years ago

Implemented #592 (Copy-to-clipboard from ItemListView should use tabs as separator for values in a column)

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