Free cookie consent management tool by TermsFeed Policy Generator

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

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

Applied patch to fix #809.

File size: 10.3 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;
30using HeuristicLab.Core.Views;
31using HeuristicLab.Common;
32using HeuristicLab.MainForm;
33
34namespace HeuristicLab.Data {
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>
39  public partial class ItemListView<T> : ItemViewBase where T : IItem {
40    private ChooseItemDialog chooseItemDialog;
41
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>
47    public ItemList<T> ItemList {
48      get { return (ItemList<T>)Item; }
49      set { base.Item = value; }
50    }
51
52    /// <summary>
53    /// Initializes a new instance of the class <see cref="HeuristicLab.Data.ItemListView&lt;T&gt;"/>.
54    /// </summary>
55    public ItemListView() {
56      InitializeComponent();
57      itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
58    }
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>
65    public ItemListView(ItemList<T> itemList)
66      : this() {
67      ItemList = itemList;
68    }
69
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>
75    protected override void RemoveItemEvents() {
76      ItemList.ItemAdded -= new EventHandler<EventArgs<IItem, int>>(ItemList_ItemInserted);
77      ItemList.ItemRemoved -= new EventHandler<EventArgs<IItem, int>>(ItemList_ItemRemoved);
78      ItemList.Cleared -= new EventHandler(ItemList_Cleared);
79      base.RemoveItemEvents();
80    }
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>
86    protected override void AddItemEvents() {
87      base.AddItemEvents();
88      ItemList.ItemAdded += new EventHandler<EventArgs<IItem, int>>(ItemList_ItemInserted);
89      ItemList.ItemRemoved += new EventHandler<EventArgs<IItem, int>>(ItemList_ItemRemoved);
90      ItemList.Cleared += new EventHandler(ItemList_Cleared);
91    }
92
93    /// <summary>
94    /// Update all controls with the lastest elements in the list.
95    /// </summary>
96    protected override void UpdateControls() {
97      base.UpdateControls();
98      detailsGroupBox.Controls.Clear();
99      detailsGroupBox.Enabled = false;
100      removeButton.Enabled = false;
101      if(ItemList == null) {
102        typeTextBox.Text = "";
103        splitContainer.Enabled = false;
104      } else {
105        typeTextBox.Text = typeof(T).FullName;
106        splitContainer.Enabled = true;
107        foreach(ListViewItem item in itemsListView.Items) {
108          ((IItem)item.Tag).Changed -= new EventHandler(Item_Changed);
109        }
110        itemsListView.Items.Clear();
111        foreach(IItem data in ItemList) {
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) {
122      if(detailsGroupBox.Controls.Count > 0)
123        detailsGroupBox.Controls[0].Dispose();
124      detailsGroupBox.Controls.Clear();
125      detailsGroupBox.Enabled = false;
126      if (itemsListView.SelectedItems.Count > 0) {
127        removeButton.Enabled = true;
128      } else {
129        removeButton.Enabled = false;
130      }
131      if(itemsListView.SelectedItems.Count == 1) {
132        IItem data = (IItem)itemsListView.SelectedItems[0].Tag;
133        Control view = (Control)MainFormManager.CreateDefaultView(data);
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) {
142      if(itemsListView.Columns.Count > 0)
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) {
149      if(e.KeyCode == Keys.Delete) {
150        while(itemsListView.SelectedIndices.Count > 0)
151          ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
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++) {
157          buffer.Append(itemsListView.Items[i].Text.Replace(';','\t'));
158          buffer.Append("\n");
159        }
160        Clipboard.SetText(buffer.ToString());
161        e.Handled = true;
162      }
163    }
164    #endregion
165
166    #region Button Events
167    private void addButton_Click(object sender, EventArgs e) {
168      if(chooseItemDialog == null) {
169        chooseItemDialog = new ChooseItemDialog(typeof(T));
170        chooseItemDialog.Caption = "Add Item";
171      }
172      if(chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
173        try {
174          ItemList.Add((T)chooseItemDialog.Item);
175        } catch(Exception ex) {
176          HeuristicLab.Core.Views.Auxiliary.ShowErrorMessageBox(ex);
177        }
178      }
179    }
180    private void removeButton_Click(object sender, EventArgs e) {
181      while(itemsListView.SelectedIndices.Count > 0)
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;
197      if(e.Data.GetDataPresent("IItem")) {
198        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
199        ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
200        if(item != null)
201          e.Effect = DragDropEffects.Move;
202      }
203    }
204    private void elementsListView_DragOver(object sender, DragEventArgs e) {
205      e.Effect = DragDropEffects.None;
206      if(e.Data.GetDataPresent("IItem")) {
207        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
208        ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
209        if(item != null)
210          e.Effect = DragDropEffects.Move;
211      }
212    }
213    private void elementsListView_DragDrop(object sender, DragEventArgs e) {
214      if(e.Effect != DragDropEffects.None) {
215        if(e.Data.GetDataPresent("IItem")) {
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);
219          if(item != null) {
220            int index = item.Index;
221            ItemList.Remove((T)data);
222            ItemList.Insert(index, (T)data);
223            itemsListView.SelectedIndices.Clear();
224            itemsListView.SelectedIndices.Add(index);
225          }
226        }
227      }
228    }
229    #endregion
230
231    #region Item and Item List Events
232    private void ItemList_ItemInserted(object sender, EventArgs<IItem, int> e) {
233      if(InvokeRequired)
234        Invoke(new EventHandler<EventArgs<IItem, int>>(ItemList_ItemInserted), sender, e);
235      else {
236        ListViewItem item = new ListViewItem();
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);
241      }
242    }
243    private void ItemList_ItemRemoved(object sender, EventArgs<IItem, int> e) {
244      if(InvokeRequired)
245        Invoke(new EventHandler<EventArgs<IItem, int>>(ItemList_ItemRemoved), sender, e);
246      else {
247        itemsListView.Items.RemoveAt(e.Value2);
248        e.Value.Changed -= new EventHandler(Item_Changed);
249      }
250    }
251    private void ItemList_Cleared(object sender, EventArgs e) {
252      Refresh();
253    }
254    private void Item_Changed(object sender, EventArgs e) {
255      if(InvokeRequired)
256        Invoke(new EventHandler(Item_Changed), sender, e);
257      else {
258        IItem data = (IItem)sender;
259        foreach(ListViewItem item in itemsListView.Items) {
260          if(item.Tag == data)
261            item.Text = data.ToString();
262        }
263      }
264    }
265    #endregion
266  }
267}
Note: See TracBrowser for help on using the repository browser.