Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3595


Ignore:
Timestamp:
05/03/10 17:38:05 (15 years ago)
Author:
gkronber
Message:

Changed CheckedItemCollectionView and CheckedItemListView to extend from ItemCollectionView and ItemListView. #992 (CheckedItemList and CheckedItemCollection is necessary)

Location:
trunk/sources/HeuristicLab.Core.Views/3.3
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core.Views/3.3/CheckedItemCollectionView.Designer.cs

    r3558 r3595  
    1 #region License Information
     1#region License Information
    22/* HeuristicLab
    33 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     
    2020#endregion
    2121
    22 using System;
    23 using System.Windows.Forms;
    24 using System.ComponentModel;
    25 
    2622namespace HeuristicLab.Core.Views {
    2723  partial class CheckedItemCollectionView<T> {
    28     /// <summary>
     24    /// <summary> 
    2925    /// Required designer variable.
    3026    /// </summary>
    3127    private System.ComponentModel.IContainer components = null;
    3228
    33     /// <summary>
     29    /// <summary> 
    3430    /// Clean up any resources being used.
    3531    /// </summary>
    3632    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    3733    protected override void Dispose(bool disposing) {
    38       if (disposing) {
    39         if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
    40         foreach (ListViewItem item in itemsListView.Items) {
    41           ((T)item.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
    42           ((T)item.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
    43         }
    44         if (components != null) components.Dispose();
     34      if (disposing && (components != null)) {
     35        components.Dispose();
    4536      }
    4637      base.Dispose(disposing);
    4738    }
    4839
    49     #region Windows Form Designer generated code
     40    #region Component Designer generated code
    5041
    51     /// <summary>
    52     /// Required method for Designer support - do not modify
     42    /// <summary> 
     43    /// Required method for Designer support - do not modify 
    5344    /// the contents of this method with the code editor.
    5445    /// </summary>
    5546    private void InitializeComponent() {
    56       this.components = new System.ComponentModel.Container();
    57       this.splitContainer = new System.Windows.Forms.SplitContainer();
    58       this.itemsListView = new System.Windows.Forms.ListView();
    59       this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
    60       this.imageList = new System.Windows.Forms.ImageList(this.components);
    61       this.sortDescendingButton = new System.Windows.Forms.Button();
    62       this.sortAscendingButton = new System.Windows.Forms.Button();
    63       this.removeButton = new System.Windows.Forms.Button();
    64       this.addButton = new System.Windows.Forms.Button();
    65       this.detailsGroupBox = new System.Windows.Forms.GroupBox();
    66       this.viewHost = new HeuristicLab.MainForm.WindowsForms.ViewHost();
    67       this.itemsGroupBox = new System.Windows.Forms.GroupBox();
    68       this.toolTip = new System.Windows.Forms.ToolTip(this.components);
    69       this.splitContainer.Panel1.SuspendLayout();
    70       this.splitContainer.Panel2.SuspendLayout();
    71       this.splitContainer.SuspendLayout();
    72       this.detailsGroupBox.SuspendLayout();
    73       this.itemsGroupBox.SuspendLayout();
    74       this.SuspendLayout();
    75       //
    76       // splitContainer
    77       //
    78       this.splitContainer.Dock = System.Windows.Forms.DockStyle.Fill;
    79       this.splitContainer.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
    80       this.splitContainer.Location = new System.Drawing.Point(3, 16);
    81       this.splitContainer.Name = "splitContainer";
    82       //
    83       // splitContainer.Panel1
    84       //
    85       this.splitContainer.Panel1.Controls.Add(this.itemsListView);
    86       this.splitContainer.Panel1.Controls.Add(this.sortDescendingButton);
    87       this.splitContainer.Panel1.Controls.Add(this.sortAscendingButton);
    88       this.splitContainer.Panel1.Controls.Add(this.removeButton);
    89       this.splitContainer.Panel1.Controls.Add(this.addButton);
    90       this.splitContainer.Panel1MinSize = 100;
    91       //
    92       // splitContainer.Panel2
    93       //
    94       this.splitContainer.Panel2.Controls.Add(this.detailsGroupBox);
    95       this.splitContainer.Size = new System.Drawing.Size(526, 364);
    96       this.splitContainer.SplitterDistance = 250;
    97       this.splitContainer.TabIndex = 0;
    98       //
    99       // itemsListView
    100       //
    101       this.itemsListView.AllowDrop = true;
    102       this.itemsListView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    103                   | System.Windows.Forms.AnchorStyles.Left)
    104                   | System.Windows.Forms.AnchorStyles.Right)));
    105       this.itemsListView.CheckBoxes = true;
    106       this.itemsListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
    107             this.columnHeader1});
    108       this.itemsListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
    109       this.itemsListView.HideSelection = false;
    110       this.itemsListView.Location = new System.Drawing.Point(3, 33);
    111       this.itemsListView.Name = "itemsListView";
    112       this.itemsListView.ShowItemToolTips = true;
    113       this.itemsListView.Size = new System.Drawing.Size(244, 327);
    114       this.itemsListView.SmallImageList = this.imageList;
    115       this.itemsListView.TabIndex = 4;
    116       this.itemsListView.UseCompatibleStateImageBehavior = false;
    117       this.itemsListView.View = System.Windows.Forms.View.Details;
    118       this.itemsListView.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(this.itemsListView_ItemChecked);
    119       this.itemsListView.SelectedIndexChanged += new System.EventHandler(this.itemsListView_SelectedIndexChanged);
    120       this.itemsListView.DoubleClick += new System.EventHandler(this.itemsListView_DoubleClick);
    121       this.itemsListView.DragDrop += new System.Windows.Forms.DragEventHandler(this.itemsListView_DragDrop);
    122       this.itemsListView.DragEnter += new System.Windows.Forms.DragEventHandler(this.itemsListView_DragEnterOver);
    123       this.itemsListView.KeyDown += new System.Windows.Forms.KeyEventHandler(this.itemsListView_KeyDown);
    124       this.itemsListView.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.itemsListView_ItemDrag);
    125       this.itemsListView.DragOver += new System.Windows.Forms.DragEventHandler(this.itemsListView_DragEnterOver);
    126       //
    127       // imageList
    128       //
    129       this.imageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
    130       this.imageList.ImageSize = new System.Drawing.Size(16, 16);
    131       this.imageList.TransparentColor = System.Drawing.Color.Transparent;
    132       //
    133       // sortDescendingButton
    134       //
    135       this.sortDescendingButton.Enabled = false;
    136       this.sortDescendingButton.Image = HeuristicLab.Common.Resources.VS2008ImageLibrary.SortUp;
    137       this.sortDescendingButton.Location = new System.Drawing.Point(33, 3);
    138       this.sortDescendingButton.Name = "sortDescendingButton";
    139       this.sortDescendingButton.Size = new System.Drawing.Size(24, 24);
    140       this.sortDescendingButton.TabIndex = 1;
    141       this.toolTip.SetToolTip(this.sortDescendingButton, "Sort Descending");
    142       this.sortDescendingButton.UseVisualStyleBackColor = true;
    143       this.sortDescendingButton.Click += new System.EventHandler(this.sortDescendingButton_Click);
    144       //
    145       // sortAscendingButton
    146       //
    147       this.sortAscendingButton.Enabled = false;
    148       this.sortAscendingButton.Image = HeuristicLab.Common.Resources.VS2008ImageLibrary.Sort;
    149       this.sortAscendingButton.Location = new System.Drawing.Point(63, 3);
    150       this.sortAscendingButton.Name = "sortAscendingButton";
    151       this.sortAscendingButton.Size = new System.Drawing.Size(24, 24);
    152       this.sortAscendingButton.TabIndex = 2;
    153       this.toolTip.SetToolTip(this.sortAscendingButton, "Sort Ascending");
    154       this.sortAscendingButton.UseVisualStyleBackColor = true;
    155       this.sortAscendingButton.Click += new System.EventHandler(this.sortAscendingButton_Click);
    156       //
    157       // removeButton
    158       //
    159       this.removeButton.Enabled = false;
    160       this.removeButton.Image = HeuristicLab.Common.Resources.VS2008ImageLibrary.Remove;
    161       this.removeButton.Location = new System.Drawing.Point(93, 3);
    162       this.removeButton.Name = "removeButton";
    163       this.removeButton.Size = new System.Drawing.Size(24, 24);
    164       this.removeButton.TabIndex = 3;
    165       this.toolTip.SetToolTip(this.removeButton, "Remove");
    166       this.removeButton.UseVisualStyleBackColor = true;
    167       this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
    168       //
    169       // addButton
    170       //
    171       this.addButton.Image = HeuristicLab.Common.Resources.VS2008ImageLibrary.Add;
    172       this.addButton.Location = new System.Drawing.Point(3, 3);
    173       this.addButton.Name = "addButton";
    174       this.addButton.Size = new System.Drawing.Size(24, 24);
    175       this.addButton.TabIndex = 0;
    176       this.toolTip.SetToolTip(this.addButton, "Add");
    177       this.addButton.UseVisualStyleBackColor = true;
    178       this.addButton.Click += new System.EventHandler(this.addButton_Click);
    179       //
    180       // detailsGroupBox
    181       //
    182       this.detailsGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    183                   | System.Windows.Forms.AnchorStyles.Left)
    184                   | System.Windows.Forms.AnchorStyles.Right)));
    185       this.detailsGroupBox.Controls.Add(this.viewHost);
    186       this.detailsGroupBox.Location = new System.Drawing.Point(3, 27);
    187       this.detailsGroupBox.Name = "detailsGroupBox";
    188       this.detailsGroupBox.Size = new System.Drawing.Size(266, 335);
    189       this.detailsGroupBox.TabIndex = 0;
    190       this.detailsGroupBox.TabStop = false;
    191       this.detailsGroupBox.Text = "Details";
    192       //
    193       // viewHost
    194       //
    195       this.viewHost.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    196                   | System.Windows.Forms.AnchorStyles.Left)
    197                   | System.Windows.Forms.AnchorStyles.Right)));
    198       this.viewHost.Caption = null;
    199       this.viewHost.Content = null;
    200       this.viewHost.Location = new System.Drawing.Point(6, 19);
    201       this.viewHost.Name = "viewHost";
    202       this.viewHost.ReadOnly = false;
    203       this.viewHost.Size = new System.Drawing.Size(254, 310);
    204       this.viewHost.TabIndex = 0;
    205       this.viewHost.ViewType = null;
    206       //
    207       // itemsGroupBox
    208       //
    209       this.itemsGroupBox.Controls.Add(this.splitContainer);
    210       this.itemsGroupBox.Dock = System.Windows.Forms.DockStyle.Fill;
    211       this.itemsGroupBox.Location = new System.Drawing.Point(0, 0);
    212       this.itemsGroupBox.Name = "itemsGroupBox";
    213       this.itemsGroupBox.Size = new System.Drawing.Size(532, 383);
    214       this.itemsGroupBox.TabIndex = 0;
    215       this.itemsGroupBox.TabStop = false;
    216       this.itemsGroupBox.Text = "Items";
    217       //
    218       // CheckedItemCollectionView
    219       //
    220       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    221       this.Controls.Add(this.itemsGroupBox);
    222       this.Name = "CheckedItemCollectionView";
    223       this.Size = new System.Drawing.Size(532, 383);
    224       this.splitContainer.Panel1.ResumeLayout(false);
    225       this.splitContainer.Panel2.ResumeLayout(false);
    226       this.splitContainer.ResumeLayout(false);
    227       this.detailsGroupBox.ResumeLayout(false);
    228       this.itemsGroupBox.ResumeLayout(false);
    229       this.ResumeLayout(false);
    230 
     47      components = new System.ComponentModel.Container();
     48      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     49      base.itemsListView.CheckBoxes = true;
     50      base.itemsListView.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(itemsListView_ItemChecked);
    23151    }
    23252
    23353    #endregion
    234 
    235     protected System.Windows.Forms.SplitContainer splitContainer;
    236     protected System.Windows.Forms.ColumnHeader columnHeader1;
    237     protected GroupBox itemsGroupBox;
    238     protected ListView itemsListView;
    239     protected GroupBox detailsGroupBox;
    240     protected Button addButton;
    241     protected Button removeButton;
    242     protected ToolTip toolTip;
    243     protected ImageList imageList;
    244     protected HeuristicLab.MainForm.WindowsForms.ViewHost viewHost;
    245     protected Button sortAscendingButton;
    246     protected Button sortDescendingButton;
    24754  }
    24855}
  • trunk/sources/HeuristicLab.Core.Views/3.3/CheckedItemCollectionView.cs

    r3588 r3595  
    1 #region License Information
     1#region License Information
    22/* HeuristicLab
    33 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     
    2222using System;
    2323using System.Collections.Generic;
     24using System.ComponentModel;
     25using System.Drawing;
     26using System.Data;
    2427using System.Linq;
     28using System.Text;
    2529using System.Windows.Forms;
    2630using HeuristicLab.Collections;
    2731using HeuristicLab.MainForm;
    28 using HeuristicLab.MainForm.WindowsForms;
    2932
    3033namespace HeuristicLab.Core.Views {
     
    3235  [Content(typeof(ICheckedItemCollection<>), false)]
    3336  [Content(typeof(CheckedItemCollection<>), true)]
    34   public partial class CheckedItemCollectionView<T> : ItemView where T : class, IItem {
    35     protected TypeSelectorDialog typeSelectorDialog;
    36 
     37  public partial class CheckedItemCollectionView<T> : ItemCollectionView<T> where T : class, IItem {
    3738    public new ICheckedItemCollection<T> Content {
    3839      get { return (ICheckedItemCollection<T>)base.Content; }
    3940      set { base.Content = value; }
    4041    }
    41 
    42     public ListView ItemsListView {
    43       get { return itemsListView; }
     42   
     43    public CheckedItemCollectionView()
     44      : base() {
     45      InitializeComponent();
    4446    }
    4547
    46     public CheckedItemCollectionView() {
    47       InitializeComponent();
    48       Caption = "Checked Item Collection";
     48    protected override void RegisterContentEvents() {
     49      Content.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
     50      base.RegisterContentEvents();
    4951    }
    5052
    5153    protected override void DeregisterContentEvents() {
    52       Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
    53       Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
    54       Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
    55       Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<T>(Content_ItemsChecked);
     54      Content.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
    5655      base.DeregisterContentEvents();
    57     }
    58     protected override void RegisterContentEvents() {
    59       base.RegisterContentEvents();
    60       Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
    61       Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
    62       Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
    63       Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<T>(Content_ItemsChecked);
    6456    }
    6557
    6658    protected override void OnContentChanged() {
     59      Caption = "Checked Item Collection";
    6760      base.OnContentChanged();
    68       Caption = "Checked Item Collection";
    69       while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
    70       viewHost.Content = null;
    71       if (Content != null) {
    72         Caption += " (" + Content.GetType().Name + ")";
    73         foreach (T item in Content)
    74           AddListViewItem(CreateListViewItem(item));
    75         SortItemsListView(SortOrder.Ascending);
    76       }
    77       SetEnabledStateOfControls();
    7861    }
    7962
    80     protected override void OnReadOnlyChanged() {
    81       base.OnReadOnlyChanged();
    82       SetEnabledStateOfControls();
    83     }
    84 
    85     private void SetEnabledStateOfControls() {
    86       if (Content == null) {
    87         addButton.Enabled = false;
    88         sortAscendingButton.Enabled = false;
    89         sortDescendingButton.Enabled = false;
    90         removeButton.Enabled = false;
    91         itemsListView.Enabled = false;
    92         detailsGroupBox.Enabled = false;
    93       } else {
    94         addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
    95         sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
    96         sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
    97         removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
    98         itemsListView.Enabled = true;
    99         detailsGroupBox.Enabled = true;
    100       }
    101     }
    102 
    103     protected virtual T CreateItem() {
    104       if (typeSelectorDialog == null) {
    105         typeSelectorDialog = new TypeSelectorDialog();
    106         typeSelectorDialog.Caption = "Select Item";
    107         typeSelectorDialog.TypeSelector.Caption = "Available Items";
    108         typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
    109       }
    110 
    111       if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
    112         try {
    113           return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
    114         }
    115         catch (Exception ex) {
    116           Auxiliary.ShowErrorMessageBox(ex);
    117         }
    118       }
    119       return null;
    120     }
    121     protected virtual ListViewItem CreateListViewItem(T item) {
    122       ListViewItem listViewItem = new ListViewItem();
    123       listViewItem.Text = item.ToString();
    124       listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
    125       listViewItem.Tag = item;
     63    protected override ListViewItem CreateListViewItem(T item) {
     64      ListViewItem listViewItem = base.CreateListViewItem(item);
    12665      listViewItem.Checked = Content.ItemChecked(item);
    127       itemsListView.SmallImageList.Images.Add(item.ItemImage);
    128       listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
    12966      return listViewItem;
    130     }
    131     protected virtual void AddListViewItem(ListViewItem listViewItem) {
    132       itemsListView.Items.Add(listViewItem);
    133       ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
    134       ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
    135       AdjustListViewColumnSizes();
    136       sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
    137       sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
    138     }
    139     protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
    140       ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
    141       ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
    142       listViewItem.Remove();
    143       foreach (ListViewItem other in itemsListView.Items)
    144         if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
    145       itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
    146       sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
    147       sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
    148     }
    149     protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
    150       int i = listViewItem.ImageIndex;
    151       listViewItem.ImageList.Images[i] = ((T)listViewItem.Tag).ItemImage;
    152       listViewItem.ImageIndex = -1;
    153       listViewItem.ImageIndex = i;
    154     }
    155     protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
    156       if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
    157         listViewItem.Text = listViewItem.Tag.ToString();
    158     }
    159     protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
    160       foreach (ListViewItem listViewItem in itemsListView.Items) {
    161         if (((T)listViewItem.Tag) == item)
    162           yield return listViewItem;
    163       }
    16467    }
    16568
    16669    #region ListView Events
    167     private void itemsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
     70    protected virtual void itemsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
    16871      var checkedItem = (T)e.Item.Tag;
    16972      if (Content.ItemChecked(checkedItem) != e.Item.Checked) {
     
    17174      }
    17275    }
    173 
    174     protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
    175       removeButton.Enabled = (Content != null) && !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
    176       if (itemsListView.SelectedItems.Count == 1) {
    177         T item = (T)itemsListView.SelectedItems[0].Tag;
    178         detailsGroupBox.Enabled = true;
    179         viewHost.ViewType = null;
    180         viewHost.Content = item;
    181       } else {
    182         viewHost.Content = null;
    183         detailsGroupBox.Enabled = false;
    184       }
    185     }
    186     protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
    187       if (e.KeyCode == Keys.Delete) {
    188         if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
    189           foreach (ListViewItem item in itemsListView.SelectedItems)
    190             Content.Remove((T)item.Tag);
    191         }
    192       }
    193     }
    194     protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
    195       if (itemsListView.SelectedItems.Count == 1) {
    196         T item = (T)itemsListView.SelectedItems[0].Tag;
    197         IContentView view = MainFormManager.MainForm.ShowContent(item);
    198         if (view != null) {
    199           view.ReadOnly = ReadOnly;
    200           view.Locked = Locked;
    201         }
    202       }
    203     }
    204     protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
    205       if (!Locked) {
    206         ListViewItem listViewItem = (ListViewItem)e.Item;
    207         T item = (T)listViewItem.Tag;
    208         DataObject data = new DataObject();
    209         data.SetData("Type", item.GetType());
    210         data.SetData("Value", item);
    211         if (Content.IsReadOnly || ReadOnly) {
    212           DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
    213         } else {
    214           DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
    215           if ((result & DragDropEffects.Move) == DragDropEffects.Move)
    216             Content.Remove(item);
    217         }
    218       }
    219     }
    220     protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
    221       e.Effect = DragDropEffects.None;
    222       Type type = e.Data.GetData("Type") as Type;
    223       if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
    224         if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Link;  // CTRL key
    225         else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
    226         else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
    227         else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
    228         else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
    229       }
    230     }
    231     protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
    232       if (e.Effect != DragDropEffects.None) {
    233         T item = e.Data.GetData("Value") as T;
    234         if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
    235         Content.Add(item);
    236       }
    237     }
    238     #endregion
    239 
    240     #region Button Events
    241     protected virtual void addButton_Click(object sender, EventArgs e) {
    242       T item = CreateItem();
    243       if (item != null)
    244         Content.Add(item);
    245     }
    246     protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
    247       SortItemsListView(SortOrder.Ascending);
    248     }
    249     protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
    250       SortItemsListView(SortOrder.Descending);
    251     }
    252     protected virtual void removeButton_Click(object sender, EventArgs e) {
    253       if (itemsListView.SelectedItems.Count > 0) {
    254         foreach (ListViewItem item in itemsListView.SelectedItems)
    255           Content.Remove((T)item.Tag);
    256         itemsListView.SelectedItems.Clear();
    257       }
    258     }
    25976    #endregion
    26077
    26178    #region Content Events
    262     protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
     79    protected virtual void Content_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<T> e) {
    26380      if (InvokeRequired)
    264         Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
    265       else
    266         foreach (T item in e.Items)
    267           AddListViewItem(CreateListViewItem(item));
    268     }
    269     protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
    270       if (InvokeRequired)
    271         Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
    272       else {
    273         foreach (T item in e.Items) {
    274           foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
    275             RemoveListViewItem(listViewItem);
    276             break;
    277           }
    278         }
    279       }
    280     }
    281     protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
    282       if (InvokeRequired)
    283         Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
    284       else {
    285         foreach (T item in e.OldItems) {
    286           foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
    287             RemoveListViewItem(listViewItem);
    288             break;
    289           }
    290         }
    291         foreach (T item in e.Items)
    292           AddListViewItem(CreateListViewItem(item));
    293       }
    294     }
    295     protected virtual void Content_ItemsChecked(object sender, CollectionItemsChangedEventArgs<T> e) {
    296       if (InvokeRequired)
    297         Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsChecked), sender, e);
     81        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged), sender, e);
    29882      else {
    29983        foreach (T item in e.Items) {
     
    30488        }
    30589      }
    306     }
    307     #endregion
    308 
    309     #region Item Events
    310     protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
    311       if (InvokeRequired)
    312         Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
    313       else {
    314         T item = (T)sender;
    315         foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
    316           UpdateListViewItemImage(listViewItem);
    317       }
    318     }
    319     protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
    320       if (InvokeRequired)
    321         Invoke(new EventHandler(Item_ToStringChanged), sender, e);
    322       else {
    323         T item = (T)sender;
    324         foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
    325           UpdateListViewItemText(listViewItem);
    326         AdjustListViewColumnSizes();
    327       }
    328     }
    329     #endregion
    330 
    331     #region Helpers
    332     protected virtual void SortItemsListView(SortOrder sortOrder) {
    333       itemsListView.Sorting = SortOrder.None;
    334       itemsListView.Sorting = sortOrder;
    335       itemsListView.Sorting = SortOrder.None;
    336     }
    337     protected virtual void AdjustListViewColumnSizes() {
    338       if (itemsListView.Items.Count > 0) {
    339         for (int i = 0; i < itemsListView.Columns.Count; i++)
    340           itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
    341       }
    342     }
     90    }     
    34391    #endregion
    34492  }
  • trunk/sources/HeuristicLab.Core.Views/3.3/CheckedItemListView.Designer.cs

    r3565 r3595  
    1 #region License Information
     1#region License Information
    22/* HeuristicLab
    33 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     
    2020#endregion
    2121
    22 using System;
    23 using System.Windows.Forms;
    24 
    2522namespace HeuristicLab.Core.Views {
    2623  partial class CheckedItemListView<T> {
    27     /// <summary>
     24    /// <summary> 
    2825    /// Required designer variable.
    2926    /// </summary>
    3027    private System.ComponentModel.IContainer components = null;
    3128
    32     /// <summary>
     29    /// <summary> 
    3330    /// Clean up any resources being used.
    3431    /// </summary>
    3532    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    3633    protected override void Dispose(bool disposing) {
    37       if (disposing) {
    38         if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
    39         foreach (ListViewItem item in itemsListView.Items) {
    40           ((T)item.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
    41           ((T)item.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
    42         }
    43         if (components != null) components.Dispose();
     34      if (disposing && (components != null)) {
     35        components.Dispose();
    4436      }
    4537      base.Dispose(disposing);
    4638    }
    4739
    48     #region Windows Form Designer generated code
     40    #region Component Designer generated code
    4941
    50     /// <summary>
    51     /// Required method for Designer support - do not modify
     42    /// <summary> 
     43    /// Required method for Designer support - do not modify 
    5244    /// the contents of this method with the code editor.
    5345    /// </summary>
    5446    private void InitializeComponent() {
    55       this.components = new System.ComponentModel.Container();
    56       this.splitContainer = new System.Windows.Forms.SplitContainer();
    57       this.removeButton = new System.Windows.Forms.Button();
    58       this.moveUpButton = new System.Windows.Forms.Button();
    59       this.moveDownButton = new System.Windows.Forms.Button();
    60       this.itemsListView = new System.Windows.Forms.ListView();
    61       this.listViewColumnHeader = new System.Windows.Forms.ColumnHeader();
    62       this.imageList = new System.Windows.Forms.ImageList(this.components);
    63       this.addButton = new System.Windows.Forms.Button();
    64       this.detailsGroupBox = new System.Windows.Forms.GroupBox();
    65       this.viewHost = new HeuristicLab.MainForm.WindowsForms.ViewHost();
    66       this.itemsGroupBox = new System.Windows.Forms.GroupBox();
    67       this.toolTip = new System.Windows.Forms.ToolTip(this.components);
    68       this.splitContainer.Panel1.SuspendLayout();
    69       this.splitContainer.Panel2.SuspendLayout();
    70       this.splitContainer.SuspendLayout();
    71       this.detailsGroupBox.SuspendLayout();
    72       this.itemsGroupBox.SuspendLayout();
    73       this.SuspendLayout();
    74       //
    75       // splitContainer
    76       //
    77       this.splitContainer.Dock = System.Windows.Forms.DockStyle.Fill;
    78       this.splitContainer.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
    79       this.splitContainer.Location = new System.Drawing.Point(3, 16);
    80       this.splitContainer.Name = "splitContainer";
    81       //
    82       // splitContainer.Panel1
    83       //
    84       this.splitContainer.Panel1.Controls.Add(this.removeButton);
    85       this.splitContainer.Panel1.Controls.Add(this.moveUpButton);
    86       this.splitContainer.Panel1.Controls.Add(this.moveDownButton);
    87       this.splitContainer.Panel1.Controls.Add(this.itemsListView);
    88       this.splitContainer.Panel1.Controls.Add(this.addButton);
    89       //
    90       // splitContainer.Panel2
    91       //
    92       this.splitContainer.Panel2.Controls.Add(this.detailsGroupBox);
    93       this.splitContainer.Size = new System.Drawing.Size(493, 323);
    94       this.splitContainer.SplitterDistance = 200;
    95       this.splitContainer.TabIndex = 0;
    96       //
    97       // removeButton
    98       //
    99       this.removeButton.Enabled = false;
    100       this.removeButton.Image = HeuristicLab.Common.Resources.VS2008ImageLibrary.Remove;
    101       this.removeButton.Location = new System.Drawing.Point(93, 3);
    102       this.removeButton.Name = "removeButton";
    103       this.removeButton.Size = new System.Drawing.Size(24, 24);
    104       this.removeButton.TabIndex = 3;
    105       this.toolTip.SetToolTip(this.removeButton, "Remove");
    106       this.removeButton.UseVisualStyleBackColor = true;
    107       this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
    108       //
    109       // moveUpButton
    110       //
    111       this.moveUpButton.Enabled = false;
    112       this.moveUpButton.Image = HeuristicLab.Common.Resources.VS2008ImageLibrary.ArrowUp;
    113       this.moveUpButton.Location = new System.Drawing.Point(33, 3);
    114       this.moveUpButton.Name = "moveUpButton";
    115       this.moveUpButton.Size = new System.Drawing.Size(24, 24);
    116       this.moveUpButton.TabIndex = 1;
    117       this.toolTip.SetToolTip(this.moveUpButton, "Move Up");
    118       this.moveUpButton.UseVisualStyleBackColor = true;
    119       this.moveUpButton.Click += new System.EventHandler(this.moveUpButton_Click);
    120       //
    121       // moveDownButton
    122       //
    123       this.moveDownButton.Enabled = false;
    124       this.moveDownButton.Image = HeuristicLab.Common.Resources.VS2008ImageLibrary.ArrowDown;
    125       this.moveDownButton.Location = new System.Drawing.Point(63, 3);
    126       this.moveDownButton.Name = "moveDownButton";
    127       this.moveDownButton.Size = new System.Drawing.Size(24, 24);
    128       this.moveDownButton.TabIndex = 2;
    129       this.toolTip.SetToolTip(this.moveDownButton, "Move Down");
    130       this.moveDownButton.UseVisualStyleBackColor = true;
    131       this.moveDownButton.Click += new System.EventHandler(this.moveDownButton_Click);
    132       //
    133       // itemsListView
    134       //
    135       this.itemsListView.AllowDrop = true;
    136       this.itemsListView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    137                   | System.Windows.Forms.AnchorStyles.Left)
    138                   | System.Windows.Forms.AnchorStyles.Right)));
    139       this.itemsListView.CheckBoxes = true;
    140       this.itemsListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
    141             this.listViewColumnHeader});
    142       this.itemsListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
    143       this.itemsListView.HideSelection = false;
    144       this.itemsListView.Location = new System.Drawing.Point(3, 33);
    145       this.itemsListView.Name = "itemsListView";
    146       this.itemsListView.ShowItemToolTips = true;
    147       this.itemsListView.Size = new System.Drawing.Size(194, 286);
    148       this.itemsListView.SmallImageList = this.imageList;
    149       this.itemsListView.TabIndex = 4;
    150       this.itemsListView.UseCompatibleStateImageBehavior = false;
    151       this.itemsListView.View = System.Windows.Forms.View.Details;
    152       this.itemsListView.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(this.itemsListView_ItemChecked);
    153       this.itemsListView.SelectedIndexChanged += new System.EventHandler(this.itemsListView_SelectedIndexChanged);
    154       this.itemsListView.DoubleClick += new System.EventHandler(this.itemsListView_DoubleClick);
    155       this.itemsListView.DragDrop += new System.Windows.Forms.DragEventHandler(this.itemsListView_DragDrop);
    156       this.itemsListView.DragEnter += new System.Windows.Forms.DragEventHandler(this.itemsListView_DragEnterOver);
    157       this.itemsListView.KeyDown += new System.Windows.Forms.KeyEventHandler(this.itemsListView_KeyDown);
    158       this.itemsListView.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.itemsListView_ItemDrag);
    159       this.itemsListView.DragOver += new System.Windows.Forms.DragEventHandler(this.itemsListView_DragEnterOver);
    160       //
    161       // imageList
    162       //
    163       this.imageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
    164       this.imageList.ImageSize = new System.Drawing.Size(16, 16);
    165       this.imageList.TransparentColor = System.Drawing.Color.Transparent;
    166       //
    167       // addButton
    168       //
    169       this.addButton.Image = HeuristicLab.Common.Resources.VS2008ImageLibrary.Add;
    170       this.addButton.Location = new System.Drawing.Point(3, 3);
    171       this.addButton.Name = "addButton";
    172       this.addButton.Size = new System.Drawing.Size(24, 24);
    173       this.addButton.TabIndex = 0;
    174       this.toolTip.SetToolTip(this.addButton, "Add");
    175       this.addButton.UseVisualStyleBackColor = true;
    176       this.addButton.Click += new System.EventHandler(this.addButton_Click);
    177       //
    178       // detailsGroupBox
    179       //
    180       this.detailsGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    181                   | System.Windows.Forms.AnchorStyles.Left)
    182                   | System.Windows.Forms.AnchorStyles.Right)));
    183       this.detailsGroupBox.Controls.Add(this.viewHost);
    184       this.detailsGroupBox.Location = new System.Drawing.Point(3, 27);
    185       this.detailsGroupBox.Name = "detailsGroupBox";
    186       this.detailsGroupBox.Size = new System.Drawing.Size(283, 294);
    187       this.detailsGroupBox.TabIndex = 0;
    188       this.detailsGroupBox.TabStop = false;
    189       this.detailsGroupBox.Text = "Details";
    190       //
    191       // viewHost
    192       //
    193       this.viewHost.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    194                   | System.Windows.Forms.AnchorStyles.Left)
    195                   | System.Windows.Forms.AnchorStyles.Right)));
    196       this.viewHost.Caption = null;
    197       this.viewHost.Content = null;
    198       this.viewHost.Location = new System.Drawing.Point(6, 19);
    199       this.viewHost.Name = "viewHost";
    200       this.viewHost.ReadOnly = false;
    201       this.viewHost.Size = new System.Drawing.Size(271, 269);
    202       this.viewHost.TabIndex = 0;
    203       this.viewHost.ViewType = null;
    204       //
    205       // itemsGroupBox
    206       //
    207       this.itemsGroupBox.Controls.Add(this.splitContainer);
    208       this.itemsGroupBox.Dock = System.Windows.Forms.DockStyle.Fill;
    209       this.itemsGroupBox.Location = new System.Drawing.Point(0, 0);
    210       this.itemsGroupBox.Name = "itemsGroupBox";
    211       this.itemsGroupBox.Size = new System.Drawing.Size(499, 342);
    212       this.itemsGroupBox.TabIndex = 0;
    213       this.itemsGroupBox.TabStop = false;
    214       this.itemsGroupBox.Text = "Items";
    215       //
    216       // CheckedItemListView
    217       //
    218       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    219       this.Controls.Add(this.itemsGroupBox);
    220       this.Name = "CheckedItemListView";
    221       this.Size = new System.Drawing.Size(499, 342);
    222       this.splitContainer.Panel1.ResumeLayout(false);
    223       this.splitContainer.Panel2.ResumeLayout(false);
    224       this.splitContainer.ResumeLayout(false);
    225       this.detailsGroupBox.ResumeLayout(false);
    226       this.itemsGroupBox.ResumeLayout(false);
    227       this.ResumeLayout(false);
    228 
     47      components = new System.ComponentModel.Container();
     48      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     49      base.itemsListView.CheckBoxes = true;
     50      base.itemsListView.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(itemsListView_ItemChecked);
    22951    }
    23052
    23153    #endregion
    232 
    233     protected System.Windows.Forms.SplitContainer splitContainer;
    234     protected System.Windows.Forms.ColumnHeader listViewColumnHeader;
    235     protected GroupBox itemsGroupBox;
    236     protected ListView itemsListView;
    237     protected GroupBox detailsGroupBox;
    238     protected Button addButton;
    239     protected Button removeButton;
    240     protected Button moveUpButton;
    241     protected Button moveDownButton;
    242     protected ToolTip toolTip;
    243     protected ImageList imageList;
    244     protected HeuristicLab.MainForm.WindowsForms.ViewHost viewHost;
    24554  }
    24655}
  • trunk/sources/HeuristicLab.Core.Views/3.3/CheckedItemListView.cs

    r3588 r3595  
    1 #region License Information
     1#region License Information
    22/* HeuristicLab
    33 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     
    2222using System;
    2323using System.Collections.Generic;
     24using System.ComponentModel;
    2425using System.Drawing;
     26using System.Data;
     27using System.Linq;
     28using System.Text;
    2529using System.Windows.Forms;
    2630using HeuristicLab.Collections;
    2731using HeuristicLab.MainForm;
    28 using HeuristicLab.MainForm.WindowsForms;
    2932
    3033namespace HeuristicLab.Core.Views {
     
    3538  [Content(typeof(CheckedItemList<>), true)]
    3639  [Content(typeof(ICheckedItemList<>), false)]
    37   public partial class CheckedItemListView<T> : ItemView where T : class, IItem {
    38     protected TypeSelectorDialog typeSelectorDialog;
    39 
    40     /// <summary>
    41     /// Gets or sets the content to represent visually.
    42     /// </summary>
    43     /// <remarks>Uses property <see cref="ItemView.Content"/> of base class <see cref="ItemView"/>
    44     /// </remarks>
     40  public partial class CheckedItemListView<T> : ItemListView<T> where T : class, IItem {
    4541    public new ICheckedItemList<T> Content {
    4642      get { return (ICheckedItemList<T>)base.Content; }
     
    4844    }
    4945
    50     public ListView ItemsListView {
    51       get { return itemsListView; }
     46    public CheckedItemListView()
     47      : base() {
     48      InitializeComponent();
    5249    }
    5350
    54     /// <summary>
    55     /// Initializes a new instance of <see cref="CheckedItemListView"/> with caption "Checked Item List".
    56     /// </summary>
    57     public CheckedItemListView() {
    58       InitializeComponent();
    59       Caption = "Checked Item List";
     51    protected override void RegisterContentEvents() {
     52      Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
     53      base.RegisterContentEvents();
    6054    }
    61 
    62     /// <summary>
    63     /// Removes the eventhandlers from the underlying <see cref="ICheckedItemList"/>.
    64     /// </summary>
    65     /// <remarks>Calls <see cref="ItemView.DeregisterContentEvents"/> of base class <see cref="ItemView"/>.</remarks>
    6655    protected override void DeregisterContentEvents() {
    67       Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
    68       Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
    69       Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
    70       Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
    71       Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
    7256      Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
    7357      base.DeregisterContentEvents();
    7458    }
    7559
    76     /// <summary>
    77     /// Adds eventhandlers to the underlying <see cref="ICheckedItemList"/>.
    78     /// </summary>
    79     /// <remarks>Calls <see cref="ItemView.RegisterContentEvents"/> of base class <see cref="ItemView"/>.</remarks>
    80     protected override void RegisterContentEvents() {
    81       base.RegisterContentEvents();
    82       Content.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
    83       Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
    84       Content.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
    85       Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
    86       Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
    87       Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
     60    protected override void OnContentChanged() {
     61      Caption = "Checked Item List";
     62      base.OnContentChanged();
    8863    }
    8964
    90 
    91     protected override void OnContentChanged() {
    92       base.OnContentChanged();
    93       Caption = "Checked Item List";
    94       while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
    95       viewHost.Content = null;
    96       if (Content != null) {
    97         Caption += " (" + Content.GetType().Name + ")";
    98         foreach (T item in Content)
    99           AddListViewItem(CreateListViewItem(item));
    100       }
    101       SetEnabledStateOfControls();
    102     }
    103 
    104     protected override void OnReadOnlyChanged() {
    105       base.OnReadOnlyChanged();
    106       SetEnabledStateOfControls();
    107     }
    108 
    109     private void SetEnabledStateOfControls() {
    110       if (Content == null) {
    111         addButton.Enabled = false;
    112         moveUpButton.Enabled = false;
    113         moveDownButton.Enabled = false;
    114         removeButton.Enabled = false;
    115         itemsListView.Enabled = false;
    116         detailsGroupBox.Enabled = false;
    117       } else {
    118         addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
    119         moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
    120                                itemsListView.SelectedIndices[0] != 0 &&
    121                                !Content.IsReadOnly && !ReadOnly;
    122         moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
    123                                  itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
    124                                  !Content.IsReadOnly && !ReadOnly;
    125         removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
    126                                !Content.IsReadOnly && !ReadOnly;
    127         itemsListView.Enabled = true;
    128         detailsGroupBox.Enabled = true;
    129       }
    130     }
    131 
    132     protected virtual T CreateItem() {
    133       if (typeSelectorDialog == null) {
    134         typeSelectorDialog = new TypeSelectorDialog();
    135         typeSelectorDialog.Caption = "Select Item";
    136         typeSelectorDialog.TypeSelector.Caption = "Available Items";
    137         typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
    138       }
    139 
    140       if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
    141         try {
    142           return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
    143         }
    144         catch (Exception ex) {
    145           Auxiliary.ShowErrorMessageBox(ex);
    146         }
    147       }
    148       return null;
    149     }
    150     protected virtual ListViewItem CreateListViewItem(T item) {
    151       ListViewItem listViewItem = new ListViewItem();
    152       listViewItem.Text = item.ToString();
    153       listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
    154       listViewItem.Tag = item;
     65    protected override ListViewItem CreateListViewItem(T item) {
     66      ListViewItem listViewItem = base.CreateListViewItem(item);
    15567      listViewItem.Checked = Content.ItemChecked(item);
    156       itemsListView.SmallImageList.Images.Add(item.ItemImage);
    157       listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
    15868      return listViewItem;
    159     }
    160     protected virtual void AddListViewItem(ListViewItem listViewItem) {
    161       itemsListView.Items.Add(listViewItem);
    162       ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
    163       ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
    164       AdjustListViewColumnSizes();
    165     }
    166     protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
    167       itemsListView.Items.Insert(index, listViewItem);
    168       ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
    169       ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
    170       AdjustListViewColumnSizes();
    171     }
    172     protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
    173       ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
    174       ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
    175       listViewItem.Remove();
    176       foreach (ListViewItem other in itemsListView.Items)
    177         if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
    178       itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
    179     }
    180     protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
    181       int i = listViewItem.ImageIndex;
    182       listViewItem.ImageList.Images[i] = ((T)listViewItem.Tag).ItemImage;
    183       listViewItem.ImageIndex = -1;
    184       listViewItem.ImageIndex = i;
    185     }
    186     protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
    187       if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
    188         listViewItem.Text = listViewItem.Tag.ToString();
    18969    }
    19070
    19171    #region ListView Events
    192     protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
    193       moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
    194                              itemsListView.SelectedIndices[0] != 0 &&
    195                              (Content != null) && !Content.IsReadOnly && !ReadOnly;
    196       moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
    197                                itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
    198                                (Content != null) && !Content.IsReadOnly && !ReadOnly;
    199       removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
    200                              (Content != null) && !Content.IsReadOnly && !ReadOnly;
    201 
    202       if (itemsListView.SelectedItems.Count == 1) {
    203         T item = (T)itemsListView.SelectedItems[0].Tag;
    204         detailsGroupBox.Enabled = true;
    205         viewHost.ViewType = null;
    206         viewHost.Content = item;
    207       } else {
    208         viewHost.Content = null;
    209         detailsGroupBox.Enabled = false;
    210       }
    211     }
    212 
    213     protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
    214       if (e.KeyCode == Keys.Delete) {
    215         if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
    216           foreach (ListViewItem item in itemsListView.SelectedItems)
    217             Content.RemoveAt(item.Index);
    218         }
    219       }
    220     }
    221 
    222     protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
    223       if (itemsListView.SelectedItems.Count == 1) {
    224         T item = (T)itemsListView.SelectedItems[0].Tag;
    225         IContentView view = MainFormManager.MainForm.ShowContent(item);
    226         if (view != null) {
    227           view.ReadOnly = ReadOnly;
    228           view.Locked = Locked;
    229         }
    230       }
    231     }
    232 
    233     protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
    234       if (!Locked) {
    235         ListViewItem listViewItem = (ListViewItem)e.Item;
    236         T item = (T)listViewItem.Tag;
    237         DataObject data = new DataObject();
    238         data.SetData("Type", item.GetType());
    239         data.SetData("Value", item);
    240         if (Content.IsReadOnly || ReadOnly) {
    241           DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
    242         } else {
    243           DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
    244           if ((result & DragDropEffects.Move) == DragDropEffects.Move)
    245             Content.RemoveAt(listViewItem.Index);
    246         }
    247       }
    248     }
    249     protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
    250       e.Effect = DragDropEffects.None;
    251       Type type = e.Data.GetData("Type") as Type;
    252       if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
    253         if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Link;  // CTRL key
    254         else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
    255         else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
    256         else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
    257         else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
    258       }
    259     }
    260     protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
    261       if (e.Effect != DragDropEffects.None) {
    262         T item = e.Data.GetData("Value") as T;
    263         if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
    264 
    265         Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
    266         ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
    267         if (listViewItem != null) Content.Insert(listViewItem.Index, item);
    268         else Content.Add(item);
    269       }
    270     }
    271 
    27272    protected virtual void itemsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
    27373      var checkedItem = (T)e.Item.Tag;
    274       if (Content.ItemChecked(checkedItem) != e.Item.Checked)
     74      if (Content.ItemChecked(checkedItem) != e.Item.Checked) {
    27575        Content.SetItemCheckedState(checkedItem, e.Item.Checked);
    276     }
    277     #endregion
    278 
    279     #region Button Events
    280     protected virtual void addButton_Click(object sender, EventArgs e) {
    281       T item = CreateItem();
    282       if (item != null)
    283         Content.Add(item);
    284     }
    285     protected virtual void moveUpButton_Click(object sender, EventArgs e) {
    286       if (itemsListView.SelectedItems.Count == 1) {
    287         int index = itemsListView.SelectedIndices[0];
    288         T item = Content[index - 1];
    289         Content[index - 1] = Content[index];
    290         itemsListView.Items[index].Selected = false;
    291         itemsListView.Items[index - 1].Selected = true;
    292         Content[index] = item;
    293       }
    294     }
    295     protected virtual void moveDownButton_Click(object sender, EventArgs e) {
    296       if (itemsListView.SelectedItems.Count == 1) {
    297         int index = itemsListView.SelectedIndices[0];
    298         T item = Content[index + 1];
    299         Content[index + 1] = Content[index];
    300         itemsListView.Items[index].Selected = false;
    301         itemsListView.Items[index + 1].Selected = true;
    302         Content[index] = item;
    303       }
    304     }
    305     protected virtual void removeButton_Click(object sender, EventArgs e) {
    306       if (itemsListView.SelectedItems.Count > 0) {
    307         foreach (ListViewItem item in itemsListView.SelectedItems)
    308           Content.RemoveAt(item.Index);
    30976      }
    31077    }
     
    31279
    31380    #region Content Events
    314     protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    315       if (InvokeRequired)
    316         Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded), sender, e);
    317       else
    318         foreach (IndexedItem<T> item in e.Items)
    319           InsertListViewItem(item.Index, CreateListViewItem(item.Value));
    320     }
    321     protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    322       if (InvokeRequired)
    323         Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved), sender, e);
    324       else {
    325         List<ListViewItem> listViewItems = new List<ListViewItem>();
    326         foreach (IndexedItem<T> item in e.Items)
    327           listViewItems.Add(itemsListView.Items[item.Index]);
    328         foreach (ListViewItem listViewItem in listViewItems)
    329           RemoveListViewItem(listViewItem);
    330       }
    331     }
    332     protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    333       if (InvokeRequired)
    334         Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
    335       else {
    336         int[] selected = new int[itemsListView.SelectedIndices.Count];
    337         itemsListView.SelectedIndices.CopyTo(selected, 0);
    338 
    339         List<ListViewItem> listViewItems = new List<ListViewItem>();
    340         foreach (IndexedItem<T> item in e.OldItems)
    341           listViewItems.Add(itemsListView.Items[item.Index]);
    342         foreach (ListViewItem listViewItem in listViewItems)
    343           RemoveListViewItem(listViewItem);
    344 
    345         foreach (IndexedItem<T> item in e.Items)
    346           InsertListViewItem(item.Index, CreateListViewItem(item.Value));
    347 
    348         for (int i = 0; i < selected.Length; i++)
    349           itemsListView.Items[selected[i]].Selected = true;
    350       }
    351     }
    352     protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    353       if (InvokeRequired)
    354         Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
    355       else {
    356         foreach (IndexedItem<T> item in e.Items) {
    357           ListViewItem listViewItem = itemsListView.Items[item.Index];
    358           listViewItem.Tag = item.Value;
    359           UpdateListViewItemImage(listViewItem);
    360           UpdateListViewItemText(listViewItem);
    361         }
    362       }
    363     }
    364     protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    365       if (InvokeRequired)
    366         Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
    367       else {
    368         List<ListViewItem> listViewItems = new List<ListViewItem>();
    369         foreach (IndexedItem<T> item in e.OldItems)
    370           listViewItems.Add(itemsListView.Items[item.Index]);
    371         foreach (ListViewItem listViewItem in listViewItems)
    372           RemoveListViewItem(listViewItem);
    373 
    374         foreach (IndexedItem<T> item in e.Items)
    375           InsertListViewItem(item.Index, CreateListViewItem(item.Value));
    376       }
    377     }
    37881    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    37982      if (InvokeRequired)
     
    38689      }
    38790    }
    388 
    38991    #endregion
    390 
    391     #region Item Events
    392     protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
    393       if (InvokeRequired)
    394         Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
    395       else {
    396         T item = (T)sender;
    397         foreach (ListViewItem listViewItem in itemsListView.Items) {
    398           if (((T)listViewItem.Tag) == item)
    399             UpdateListViewItemImage(listViewItem);
    400         }
    401       }
    402     }
    403     protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
    404       if (InvokeRequired)
    405         Invoke(new EventHandler(Item_ToStringChanged), sender, e);
    406       else {
    407         T item = (T)sender;
    408         foreach (ListViewItem listViewItem in itemsListView.Items) {
    409           if (((T)listViewItem.Tag) == item)
    410             UpdateListViewItemText(listViewItem);
    411         }
    412         AdjustListViewColumnSizes();
    413       }
    414     }
    415     #endregion
    416 
    417     #region Helpers
    418     protected virtual void AdjustListViewColumnSizes() {
    419       if (itemsListView.Items.Count > 0) {
    420         for (int i = 0; i < itemsListView.Columns.Count; i++)
    421           itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
    422       }
    423     }
    424     #endregion
    425 
    42692  }
    42793}
Note: See TracChangeset for help on using the changeset viewer.