Free cookie consent management tool by TermsFeed Policy Generator

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

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

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

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