Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/ItemListView_T.cs @ 834

Last change on this file since 834 was 737, checked in by vdorfer, 16 years ago

Created API documentation for HeuristLab.Data namespace (#331)

File size: 10.1 KB
RevLine 
[2]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 {
[737]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>
[40]36  public partial class ItemListView<T> : ViewBase where T : IItem {
[2]37    private ChooseItemDialog chooseItemDialog;
38
[737]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>
[40]44    public ItemList<T> ItemList {
45      get { return (ItemList<T>)Item; }
[2]46      set { base.Item = value; }
47    }
48
[737]49    /// <summary>
50    /// Initializes a new instance of the class <see cref="HeuristicLab.Data.ItemListView&lt;T&gt;"/>.
51    /// </summary>
[2]52    public ItemListView() {
53      InitializeComponent();
54      itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
55    }
[737]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>
[40]62    public ItemListView(ItemList<T> itemList)
[2]63      : this() {
64      ItemList = itemList;
65    }
66
[737]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>
[2]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    }
[737]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>
[2]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
[737]90    /// <summary>
91    /// Update all controls with the lastest elements in the list.
92    /// </summary>
[2]93    protected override void UpdateControls() {
94      base.UpdateControls();
95      detailsGroupBox.Controls.Clear();
96      detailsGroupBox.Enabled = false;
97      removeButton.Enabled = false;
[665]98      if(ItemList == null) {
[2]99        typeTextBox.Text = "";
100        splitContainer.Enabled = false;
101      } else {
[40]102        typeTextBox.Text = typeof(T).FullName;
[2]103        splitContainer.Enabled = true;
[665]104        foreach(ListViewItem item in itemsListView.Items) {
[2]105          ((IItem)item.Tag).Changed -= new EventHandler(Item_Changed);
106        }
107        itemsListView.Items.Clear();
[665]108        foreach(IItem data in ItemList) {
[2]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) {
[665]119      if(detailsGroupBox.Controls.Count > 0)
[2]120        detailsGroupBox.Controls[0].Dispose();
121      detailsGroupBox.Controls.Clear();
122      detailsGroupBox.Enabled = false;
123      removeButton.Enabled = false;
[665]124      if(itemsListView.SelectedItems.Count > 0) {
[2]125        removeButton.Enabled = true;
126      }
[665]127      if(itemsListView.SelectedItems.Count == 1) {
[2]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) {
[665]138      if(itemsListView.Columns.Count > 0)
[2]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) {
[665]145      if(e.KeyCode == Keys.Delete) {
146        while(itemsListView.SelectedIndices.Count > 0)
[2]147          ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
[665]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);
154          buffer.Append("\n");
155        }
156        Clipboard.SetText(buffer.ToString());
157        e.Handled = true;
[2]158      }
159    }
160    #endregion
161
162    #region Button Events
163    private void addButton_Click(object sender, EventArgs e) {
[665]164      if(chooseItemDialog == null) {
[40]165        chooseItemDialog = new ChooseItemDialog(typeof(T));
[2]166        chooseItemDialog.Caption = "Add Item";
167      }
[665]168      if(chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
[2]169        try {
[40]170          ItemList.Add((T)chooseItemDialog.Item);
[665]171        } catch(Exception ex) {
[2]172          Auxiliary.ShowErrorMessageBox(ex);
173        }
174      }
175    }
176    private void removeButton_Click(object sender, EventArgs e) {
[665]177      while(itemsListView.SelectedIndices.Count > 0)
[2]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;
[665]193      if(e.Data.GetDataPresent("IItem")) {
[2]194        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
195        ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
[665]196        if(item != null)
[2]197          e.Effect = DragDropEffects.Move;
198      }
199    }
200    private void elementsListView_DragOver(object sender, DragEventArgs e) {
201      e.Effect = DragDropEffects.None;
[665]202      if(e.Data.GetDataPresent("IItem")) {
[2]203        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
204        ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
[665]205        if(item != null)
[2]206          e.Effect = DragDropEffects.Move;
207      }
208    }
209    private void elementsListView_DragDrop(object sender, DragEventArgs e) {
[665]210      if(e.Effect != DragDropEffects.None) {
211        if(e.Data.GetDataPresent("IItem")) {
[2]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);
[665]215          if(item != null) {
[2]216            int index = item.Index;
[40]217            ItemList.Remove((T)data);
218            ItemList.Insert(index, (T)data);
[2]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) {
[665]229      if(InvokeRequired)
[2]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) {
[665]240      if(InvokeRequired)
[2]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) {
[665]251      if(InvokeRequired)
[2]252        Invoke(new EventHandler(Item_Changed), sender, e);
253      else {
254        IItem data = (IItem)sender;
[665]255        foreach(ListViewItem item in itemsListView.Items) {
256          if(item.Tag == data)
[2]257            item.Text = data.ToString();
258        }
259      }
260    }
261    #endregion
262  }
263}
Note: See TracBrowser for help on using the repository browser.