Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/ItemListView_T.cs @ 2668

Last change on this file since 2668 was 2558, checked in by swagner, 15 years ago

Applied patch to fix #809.

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