[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 | using System.Data;
|
---|
| 27 | using System.Text;
|
---|
| 28 | using System.Windows.Forms;
|
---|
[2474] | 29 | using HeuristicLab.Common;
|
---|
[2] | 30 | using HeuristicLab.Core;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Data {
|
---|
[737] | 33 | /// <summary>
|
---|
| 34 | /// The visual representation of the class <see cref="HeuristicLab.Data.ItemList<T>"/>.
|
---|
| 35 | /// </summary>
|
---|
| 36 | /// <typeparam name="T">The type of the items in the list.</typeparam>
|
---|
[40] | 37 | public partial class ItemListView<T> : ViewBase where T : IItem {
|
---|
[2] | 38 | private ChooseItemDialog chooseItemDialog;
|
---|
| 39 |
|
---|
[737] | 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>
|
---|
[40] | 45 | public ItemList<T> ItemList {
|
---|
| 46 | get { return (ItemList<T>)Item; }
|
---|
[2] | 47 | set { base.Item = value; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[737] | 50 | /// <summary>
|
---|
| 51 | /// Initializes a new instance of the class <see cref="HeuristicLab.Data.ItemListView<T>"/>.
|
---|
| 52 | /// </summary>
|
---|
[2] | 53 | public ItemListView() {
|
---|
| 54 | InitializeComponent();
|
---|
| 55 | itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
|
---|
| 56 | }
|
---|
[737] | 57 | /// <summary>
|
---|
| 58 | /// Initializes a new instance of the class <see cref="HeuristicLab.Data.ItemListView<T>"/> 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>
|
---|
[40] | 63 | public ItemListView(ItemList<T> itemList)
|
---|
[2] | 64 | : this() {
|
---|
| 65 | ItemList = itemList;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[737] | 68 | /// <summary>
|
---|
| 69 | /// Removes the eventhandlers from the underlying <see cref="HeuristicLab.Data.ItemList<T>"/>.
|
---|
| 70 | /// </summary>
|
---|
| 71 | /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.
|
---|
| 72 | /// </remarks>
|
---|
[2] | 73 | protected override void RemoveItemEvents() {
|
---|
[2474] | 74 | ItemList.ItemAdded -= new EventHandler<EventArgs<IItem, int>>(ItemList_ItemInserted);
|
---|
| 75 | ItemList.ItemRemoved -= new EventHandler<EventArgs<IItem, int>>(ItemList_ItemRemoved);
|
---|
[2] | 76 | ItemList.Cleared -= new EventHandler(ItemList_Cleared);
|
---|
| 77 | base.RemoveItemEvents();
|
---|
| 78 | }
|
---|
[737] | 79 | /// <summary>
|
---|
| 80 | /// Adds eventhandlers to the underlying <see cref="HeuristicLab.Data.ItemList<T>"/>.
|
---|
| 81 | /// </summary>
|
---|
| 82 | /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.
|
---|
| 83 | /// </remarks>
|
---|
[2] | 84 | protected override void AddItemEvents() {
|
---|
| 85 | base.AddItemEvents();
|
---|
[2474] | 86 | ItemList.ItemAdded += new EventHandler<EventArgs<IItem, int>>(ItemList_ItemInserted);
|
---|
| 87 | ItemList.ItemRemoved += new EventHandler<EventArgs<IItem, int>>(ItemList_ItemRemoved);
|
---|
[2] | 88 | ItemList.Cleared += new EventHandler(ItemList_Cleared);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[737] | 91 | /// <summary>
|
---|
| 92 | /// Update all controls with the lastest elements in the list.
|
---|
| 93 | /// </summary>
|
---|
[2] | 94 | protected override void UpdateControls() {
|
---|
| 95 | base.UpdateControls();
|
---|
| 96 | detailsGroupBox.Controls.Clear();
|
---|
| 97 | detailsGroupBox.Enabled = false;
|
---|
| 98 | removeButton.Enabled = false;
|
---|
[665] | 99 | if(ItemList == null) {
|
---|
[2] | 100 | typeTextBox.Text = "";
|
---|
| 101 | splitContainer.Enabled = false;
|
---|
| 102 | } else {
|
---|
[40] | 103 | typeTextBox.Text = typeof(T).FullName;
|
---|
[2] | 104 | splitContainer.Enabled = true;
|
---|
[665] | 105 | foreach(ListViewItem item in itemsListView.Items) {
|
---|
[2] | 106 | ((IItem)item.Tag).Changed -= new EventHandler(Item_Changed);
|
---|
| 107 | }
|
---|
| 108 | itemsListView.Items.Clear();
|
---|
[665] | 109 | foreach(IItem data in ItemList) {
|
---|
[2] | 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) {
|
---|
[665] | 120 | if(detailsGroupBox.Controls.Count > 0)
|
---|
[2] | 121 | detailsGroupBox.Controls[0].Dispose();
|
---|
| 122 | detailsGroupBox.Controls.Clear();
|
---|
| 123 | detailsGroupBox.Enabled = false;
|
---|
[2558] | 124 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
[2] | 125 | removeButton.Enabled = true;
|
---|
[2558] | 126 | } else {
|
---|
| 127 | removeButton.Enabled = false;
|
---|
[2] | 128 | }
|
---|
[665] | 129 | if(itemsListView.SelectedItems.Count == 1) {
|
---|
[2] | 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) {
|
---|
[665] | 140 | if(itemsListView.Columns.Count > 0)
|
---|
[2] | 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) {
|
---|
[665] | 147 | if(e.KeyCode == Keys.Delete) {
|
---|
| 148 | while(itemsListView.SelectedIndices.Count > 0)
|
---|
[2] | 149 | ItemList.RemoveAt(itemsListView.SelectedIndices[0]);
|
---|
[665] | 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++) {
|
---|
[1609] | 155 | buffer.Append(itemsListView.Items[i].Text.Replace(';','\t'));
|
---|
[665] | 156 | buffer.Append("\n");
|
---|
| 157 | }
|
---|
| 158 | Clipboard.SetText(buffer.ToString());
|
---|
| 159 | e.Handled = true;
|
---|
[2] | 160 | }
|
---|
| 161 | }
|
---|
| 162 | #endregion
|
---|
| 163 |
|
---|
| 164 | #region Button Events
|
---|
| 165 | private void addButton_Click(object sender, EventArgs e) {
|
---|
[665] | 166 | if(chooseItemDialog == null) {
|
---|
[40] | 167 | chooseItemDialog = new ChooseItemDialog(typeof(T));
|
---|
[2] | 168 | chooseItemDialog.Caption = "Add Item";
|
---|
| 169 | }
|
---|
[665] | 170 | if(chooseItemDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[2] | 171 | try {
|
---|
[40] | 172 | ItemList.Add((T)chooseItemDialog.Item);
|
---|
[665] | 173 | } catch(Exception ex) {
|
---|
[2] | 174 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | private void removeButton_Click(object sender, EventArgs e) {
|
---|
[665] | 179 | while(itemsListView.SelectedIndices.Count > 0)
|
---|
[2] | 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;
|
---|
[665] | 195 | if(e.Data.GetDataPresent("IItem")) {
|
---|
[2] | 196 | Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
|
---|
| 197 | ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
|
---|
[665] | 198 | if(item != null)
|
---|
[2] | 199 | e.Effect = DragDropEffects.Move;
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | private void elementsListView_DragOver(object sender, DragEventArgs e) {
|
---|
| 203 | e.Effect = DragDropEffects.None;
|
---|
[665] | 204 | if(e.Data.GetDataPresent("IItem")) {
|
---|
[2] | 205 | Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
|
---|
| 206 | ListViewItem item = itemsListView.GetItemAt(p.X, p.Y);
|
---|
[665] | 207 | if(item != null)
|
---|
[2] | 208 | e.Effect = DragDropEffects.Move;
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | private void elementsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
[665] | 212 | if(e.Effect != DragDropEffects.None) {
|
---|
| 213 | if(e.Data.GetDataPresent("IItem")) {
|
---|
[2] | 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);
|
---|
[665] | 217 | if(item != null) {
|
---|
[2] | 218 | int index = item.Index;
|
---|
[40] | 219 | ItemList.Remove((T)data);
|
---|
| 220 | ItemList.Insert(index, (T)data);
|
---|
[2] | 221 | itemsListView.SelectedIndices.Clear();
|
---|
| 222 | itemsListView.SelectedIndices.Add(index);
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | #endregion
|
---|
| 228 |
|
---|
| 229 | #region Item and Item List Events
|
---|
[2474] | 230 | private void ItemList_ItemInserted(object sender, EventArgs<IItem, int> e) {
|
---|
[665] | 231 | if(InvokeRequired)
|
---|
[2474] | 232 | Invoke(new EventHandler<EventArgs<IItem, int>>(ItemList_ItemInserted), sender, e);
|
---|
[2] | 233 | else {
|
---|
| 234 | ListViewItem item = new ListViewItem();
|
---|
[2474] | 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);
|
---|
[2] | 239 | }
|
---|
| 240 | }
|
---|
[2474] | 241 | private void ItemList_ItemRemoved(object sender, EventArgs<IItem, int> e) {
|
---|
[665] | 242 | if(InvokeRequired)
|
---|
[2474] | 243 | Invoke(new EventHandler<EventArgs<IItem, int>>(ItemList_ItemRemoved), sender, e);
|
---|
[2] | 244 | else {
|
---|
[2474] | 245 | itemsListView.Items.RemoveAt(e.Value2);
|
---|
| 246 | e.Value.Changed -= new EventHandler(Item_Changed);
|
---|
[2] | 247 | }
|
---|
| 248 | }
|
---|
| 249 | private void ItemList_Cleared(object sender, EventArgs e) {
|
---|
| 250 | Refresh();
|
---|
| 251 | }
|
---|
| 252 | private void Item_Changed(object sender, EventArgs e) {
|
---|
[665] | 253 | if(InvokeRequired)
|
---|
[2] | 254 | Invoke(new EventHandler(Item_Changed), sender, e);
|
---|
| 255 | else {
|
---|
| 256 | IItem data = (IItem)sender;
|
---|
[665] | 257 | foreach(ListViewItem item in itemsListView.Items) {
|
---|
| 258 | if(item.Tag == data)
|
---|
[2] | 259 | item.Text = data.ToString();
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 | #endregion
|
---|
| 264 | }
|
---|
| 265 | }
|
---|