Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PluginInfrastructure Refactoring/HeuristicLab.Data/3.2/ItemListView_T.cs @ 2583

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

Applied patch to fix #809.

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      if (itemsListView.SelectedItems.Count > 0) {
125        removeButton.Enabled = true;
126      } else {
127        removeButton.Enabled = false;
128      }
129      if(itemsListView.SelectedItems.Count == 1) {
130        IItem data = (IItem)itemsListView.SelectedItems[0].Tag;
131        Control view = (Control)data.CreateView();
132        detailsGroupBox.Controls.Add(view);
133        view.Dock = DockStyle.Fill;
134        detailsGroupBox.Enabled = true;
135      }
136    }
137
138    #region Size Changed Events
139    private void elementsListView_SizeChanged(object sender, EventArgs e) {
140      if(itemsListView.Columns.Count > 0)
141        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
142    }
143    #endregion
144
145    #region Key Events
146    private void elementsListView_KeyDown(object sender, KeyEventArgs e) {
147      if(e.KeyCode == Keys.Delete) {
148        while(itemsListView.SelectedIndices.Count > 0)
149          ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
150        e.Handled = true;
151      } else if((e.KeyCode == Keys.C) && (e.Modifiers == Keys.Control)) {
152        // copy contents of itemlist to clipboard
153        StringBuilder buffer = new StringBuilder();
154        for(int i = 0; i < itemsListView.Items.Count; i++) {
155          buffer.Append(itemsListView.Items[i].Text.Replace(';','\t'));
156          buffer.Append("\n");
157        }
158        Clipboard.SetText(buffer.ToString());
159        e.Handled = true;
160      }
161    }
162    #endregion
163
164    #region Button Events
165    private void addButton_Click(object sender, EventArgs e) {
166      if(chooseItemDialog == null) {
167        chooseItemDialog = new ChooseItemDialog(typeof(T));
168        chooseItemDialog.Caption = "Add Item";
169      }
170      if(chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
171        try {
172          ItemList.Add((T)chooseItemDialog.Item);
173        } catch(Exception ex) {
174          Auxiliary.ShowErrorMessageBox(ex);
175        }
176      }
177    }
178    private void removeButton_Click(object sender, EventArgs e) {
179      while(itemsListView.SelectedIndices.Count > 0)
180        ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
181    }
182    #endregion
183
184    #region Drag and Drop Events
185    private void elementsListView_ItemDrag(object sender, ItemDragEventArgs e) {
186      ListViewItem item = (ListViewItem)e.Item;
187      IItem data = (IItem)item.Tag;
188      DataObject dataObject = new DataObject();
189      dataObject.SetData("IItem", data);
190      dataObject.SetData("DragSource", itemsListView);
191      DoDragDrop(dataObject, DragDropEffects.Move);
192    }
193    private void elementsListView_DragEnter(object sender, DragEventArgs e) {
194      e.Effect = DragDropEffects.None;
195      if(e.Data.GetDataPresent("IItem")) {
196        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
197        ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
198        if(item != null)
199          e.Effect = DragDropEffects.Move;
200      }
201    }
202    private void elementsListView_DragOver(object sender, DragEventArgs e) {
203      e.Effect = DragDropEffects.None;
204      if(e.Data.GetDataPresent("IItem")) {
205        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
206        ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
207        if(item != null)
208          e.Effect = DragDropEffects.Move;
209      }
210    }
211    private void elementsListView_DragDrop(object sender, DragEventArgs e) {
212      if(e.Effect != DragDropEffects.None) {
213        if(e.Data.GetDataPresent("IItem")) {
214          IItem data = (IItem)e.Data.GetData("IItem");
215          Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
216          ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
217          if(item != null) {
218            int index = item.Index;
219            ItemList.Remove((T)data);
220            ItemList.Insert(index, (T)data);
221            itemsListView.SelectedIndices.Clear();
222            itemsListView.SelectedIndices.Add(index);
223          }
224        }
225      }
226    }
227    #endregion
228
229    #region Item and Item List Events
230    private void ItemList_ItemInserted(object sender, EventArgs<IItem, int> e) {
231      if(InvokeRequired)
232        Invoke(new EventHandler<EventArgs<IItem, int>>(ItemList_ItemInserted), sender, e);
233      else {
234        ListViewItem item = new ListViewItem();
235        item.Text = e.Value.ToString();
236        item.Tag = e.Value;
237        itemsListView.Items.Insert(e.Value2, item);
238        e.Value.Changed += new EventHandler(Item_Changed);
239      }
240    }
241    private void ItemList_ItemRemoved(object sender, EventArgs<IItem, int> e) {
242      if(InvokeRequired)
243        Invoke(new EventHandler<EventArgs<IItem, int>>(ItemList_ItemRemoved), sender, e);
244      else {
245        itemsListView.Items.RemoveAt(e.Value2);
246        e.Value.Changed -= new EventHandler(Item_Changed);
247      }
248    }
249    private void ItemList_Cleared(object sender, EventArgs e) {
250      Refresh();
251    }
252    private void Item_Changed(object sender, EventArgs e) {
253      if(InvokeRequired)
254        Invoke(new EventHandler(Item_Changed), sender, e);
255      else {
256        IItem data = (IItem)sender;
257        foreach(ListViewItem item in itemsListView.Items) {
258          if(item.Tag == data)
259            item.Text = data.ToString();
260        }
261      }
262    }
263    #endregion
264  }
265}
Note: See TracBrowser for help on using the repository browser.