Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/ItemListView.cs @ 22

Last change on this file since 22 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 9.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.PluginInfrastructure;
30using HeuristicLab.Core;
31
32namespace HeuristicLab.Data {
33  public partial class ItemListView : ViewBase {
34    private ChooseItemDialog chooseItemDialog;
35
36    public ItemList ItemList {
37      get { return (ItemList)Item; }
38      set { base.Item = value; }
39    }
40
41    public ItemListView() {
42      InitializeComponent();
43      itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
44    }
45    public ItemListView(ItemList itemList)
46      : this() {
47      ItemList = itemList;
48    }
49
50    protected override void RemoveItemEvents() {
51      ItemList.ItemTypeChanged -= new EventHandler(ItemList_ItemTypeChanged);
52      ItemList.ItemAdded -= new EventHandler<ItemIndexEventArgs>(ItemList_ItemInserted);
53      ItemList.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(ItemList_ItemRemoved);
54      ItemList.Cleared -= new EventHandler(ItemList_Cleared);
55      base.RemoveItemEvents();
56    }
57    protected override void AddItemEvents() {
58      base.AddItemEvents();
59      ItemList.ItemTypeChanged += new EventHandler(ItemList_ItemTypeChanged);
60      ItemList.ItemAdded += new EventHandler<ItemIndexEventArgs>(ItemList_ItemInserted);
61      ItemList.ItemRemoved += new EventHandler<ItemIndexEventArgs>(ItemList_ItemRemoved);
62      ItemList.Cleared += new EventHandler(ItemList_Cleared);
63    }
64
65    protected override void UpdateControls() {
66      base.UpdateControls();
67      detailsGroupBox.Controls.Clear();
68      detailsGroupBox.Enabled = false;
69      removeButton.Enabled = false;
70      if (ItemList == null) {
71        typeTextBox.Text = "";
72        setTypeButton.Enabled = false;
73        splitContainer.Enabled = false;
74      } else {
75        typeTextBox.Text = ItemList.ItemType.FullName;
76        setTypeButton.Enabled = true;
77        splitContainer.Enabled = true;
78        foreach (ListViewItem item in itemsListView.Items) {
79          ((IItem)item.Tag).Changed -= new EventHandler(Item_Changed);
80        }
81        itemsListView.Items.Clear();
82        foreach (IItem data in ItemList) {
83          ListViewItem item = new ListViewItem();
84          item.Text = data.ToString();
85          item.Tag = data;
86          itemsListView.Items.Add(item);
87          data.Changed += new EventHandler(Item_Changed);
88        }
89      }
90    }
91
92    private void elementsListView_SelectedIndexChanged(object sender, EventArgs e) {
93      if (detailsGroupBox.Controls.Count > 0)
94        detailsGroupBox.Controls[0].Dispose();
95      detailsGroupBox.Controls.Clear();
96      detailsGroupBox.Enabled = false;
97      removeButton.Enabled = false;
98      if (itemsListView.SelectedItems.Count > 0) {
99        removeButton.Enabled = true;
100      }
101      if (itemsListView.SelectedItems.Count == 1) {
102        IItem data = (IItem)itemsListView.SelectedItems[0].Tag;
103        Control view = (Control)data.CreateView();
104        detailsGroupBox.Controls.Add(view);
105        view.Dock = DockStyle.Fill;
106        detailsGroupBox.Enabled = true;
107      }
108    }
109
110    #region Size Changed Events
111    private void elementsListView_SizeChanged(object sender, EventArgs e) {
112      if (itemsListView.Columns.Count > 0)
113        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
114    }
115    #endregion
116
117    #region Key Events
118    private void elementsListView_KeyDown(object sender, KeyEventArgs e) {
119      if (e.KeyCode == Keys.Delete) {
120        while (itemsListView.SelectedIndices.Count > 0)
121          ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
122      }
123    }
124    #endregion
125
126    #region Button Events
127    private void setTypeButton_Click(object sender, EventArgs e) {
128      ChooseTypeDialog dialog = new ChooseTypeDialog();
129      dialog.Caption = "Set Item Type";
130      if (dialog.ShowDialog(this) == DialogResult.OK) {
131        try {
132          ItemList.ItemType = dialog.Type;
133        }
134        catch (Exception ex) {
135          Auxiliary.ShowErrorMessageBox(ex);
136        }
137      }
138      dialog.Dispose();
139    }
140    private void addButton_Click(object sender, EventArgs e) {
141      if (chooseItemDialog == null) {
142        chooseItemDialog = new ChooseItemDialog(ItemList.ItemType);
143        chooseItemDialog.Caption = "Add Item";
144      }
145      if (chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
146        try {
147          ItemList.Add(chooseItemDialog.Item);
148        }
149        catch (Exception ex) {
150          Auxiliary.ShowErrorMessageBox(ex);
151        }
152      }
153    }
154    private void removeButton_Click(object sender, EventArgs e) {
155      while (itemsListView.SelectedIndices.Count > 0)
156        ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
157    }
158    #endregion
159
160    #region Drag and Drop Events
161    private void elementsListView_ItemDrag(object sender, ItemDragEventArgs e) {
162      ListViewItem item = (ListViewItem)e.Item;
163      IItem data = (IItem)item.Tag;
164      DataObject dataObject = new DataObject();
165      dataObject.SetData("IItem", data);
166      dataObject.SetData("DragSource", itemsListView);
167      DoDragDrop(dataObject, DragDropEffects.Move);
168    }
169    private void elementsListView_DragEnter(object sender, DragEventArgs e) {
170      e.Effect = DragDropEffects.None;
171      if (e.Data.GetDataPresent("IItem")) {
172        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
173        ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
174        if (item != null)
175          e.Effect = DragDropEffects.Move;
176      }
177    }
178    private void elementsListView_DragOver(object sender, DragEventArgs e) {
179      e.Effect = DragDropEffects.None;
180      if (e.Data.GetDataPresent("IItem")) {
181        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
182        ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
183        if (item != null)
184          e.Effect = DragDropEffects.Move;
185      }
186    }
187    private void elementsListView_DragDrop(object sender, DragEventArgs e) {
188      if (e.Effect != DragDropEffects.None) {
189        if (e.Data.GetDataPresent("IItem")) {
190          IItem data = (IItem)e.Data.GetData("IItem");
191          Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
192          ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
193          if (item != null) {
194            int index = item.Index;
195            ItemList.Remove(data);
196            ItemList.Insert(index, data);
197            itemsListView.SelectedIndices.Clear();
198            itemsListView.SelectedIndices.Add(index);
199          }
200        }
201      }
202    }
203    #endregion
204
205    #region Item and Item List Events
206    private void ItemList_ItemTypeChanged(object sender, EventArgs e) {
207      if (InvokeRequired)
208        Invoke(new EventHandler(ItemList_ItemTypeChanged), sender, e);
209      else {
210        typeTextBox.Text = ItemList.ItemType.FullName;
211        if (chooseItemDialog != null) chooseItemDialog.Dispose();
212        chooseItemDialog = new ChooseItemDialog(ItemList.ItemType);
213        chooseItemDialog.Caption = "Add Item";
214      }
215    }
216    private void ItemList_ItemInserted(object sender, ItemIndexEventArgs e) {
217      if (InvokeRequired)
218        Invoke(new EventHandler<ItemIndexEventArgs>(ItemList_ItemInserted), sender, e);
219      else {
220        ListViewItem item = new ListViewItem();
221        item.Text = e.Item.ToString();
222        item.Tag = e.Item;
223        itemsListView.Items.Insert(e.Index, item);
224        e.Item.Changed += new EventHandler(Item_Changed);
225      }
226    }
227    private void ItemList_ItemRemoved(object sender, ItemIndexEventArgs e) {
228      if (InvokeRequired)
229        Invoke(new EventHandler<ItemIndexEventArgs>(ItemList_ItemRemoved), sender, e);
230      else {
231        itemsListView.Items.RemoveAt(e.Index);
232        e.Item.Changed -= new EventHandler(Item_Changed);
233      }
234    }
235    private void ItemList_Cleared(object sender, EventArgs e) {
236      Refresh();
237    }
238    private void Item_Changed(object sender, EventArgs e) {
239      if (InvokeRequired)
240        Invoke(new EventHandler(Item_Changed), sender, e);
241      else {
242        IItem data = (IItem)sender;
243        foreach (ListViewItem item in itemsListView.Items) {
244          if (item.Tag == data)
245            item.Text = data.ToString();
246        }
247      }
248    }
249    #endregion
250  }
251}
Note: See TracBrowser for help on using the repository browser.