Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/18/11 01:38:23 (13 years ago)
Author:
swagner
Message:

Implemented dragging of multiple items in all collection views and refactored drag & drop code (#1112)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionView.cs

    r5702 r5744  
    2121
    2222using System;
     23using System.Collections;
    2324using System.Collections.Generic;
    2425using System.Drawing;
     
    3738  public sealed partial class RunCollectionView : ItemView {
    3839    private Dictionary<IRun, List<ListViewItem>> itemListViewItemMapping;
     40    private bool validDragOperation;
    3941
    4042    public new IItemCollection<IRun> Content {
     
    271273        if (items.Count > 0) {
    272274          DataObject data = new DataObject();
    273           if (items.Count == 1) {
    274             data.SetData("Type", items[0].GetType());
    275             data.SetData("Value", items[0]);
    276           } else {
    277             data.SetData("Type", typeof(IEnumerable<IRun>));
    278             data.SetData("Value", items);
    279           }
     275          if (items.Count == 1) data.SetData("HeuristicLab", items[0]);
     276          else data.SetData("HeuristicLab", items);
    280277          if (Content.IsReadOnly || ReadOnly) {
    281278            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
    282279          } else {
    283280            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
    284             if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
     281            if (result.HasFlag(DragDropEffects.Move)) {
    285282              foreach (IRun item in items) Content.Remove(item);
    286283            }
     
    289286      }
    290287    }
    291     private void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
     288    private void itemsListView_DragEnter(object sender, DragEventArgs e) {
     289      validDragOperation = false;
     290      if (e.Data.GetData("HeuristicLab") is IRun) {
     291        validDragOperation = true;
     292      } else if (e.Data.GetData("HeuristicLab") is IEnumerable) {
     293        validDragOperation = true;
     294        IEnumerable items = (IEnumerable)e.Data.GetData("HeuristicLab");
     295        foreach (object item in items)
     296          validDragOperation = validDragOperation && (item is IRun);
     297      }
     298      validDragOperation = validDragOperation && !Content.IsReadOnly && !ReadOnly;
     299    }
     300    private void itemsListView_DragOver(object sender, DragEventArgs e) {
    292301      e.Effect = DragDropEffects.None;
    293       Type type = e.Data.GetData("Type") as Type;
    294       if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(IRun).IsAssignableFrom(type) || typeof(IEnumerable<IRun>).IsAssignableFrom(type))) {
     302      if (validDragOperation) {
    295303        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
    296304        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
    297         else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
    298         else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
    299         else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
     305        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
     306        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
     307        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
    300308      }
    301309    }
    302310    private void itemsListView_DragDrop(object sender, DragEventArgs e) {
    303311      if (e.Effect != DragDropEffects.None) {
    304         object value = e.Data.GetData("Value");
    305         IEnumerable<IRun> items = Enumerable.Empty<IRun>();
    306         if (value is IRun)
    307           items = new IRun[] { (IRun)value };
    308         else if (value is IEnumerable<IRun>)
    309           items = (IEnumerable<IRun>)value;
    310 
    311         foreach (IRun item in items) {
    312           if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) Content.Add((IRun)item.Clone());
    313           else Content.Add(item);
     312        if (e.Data.GetData("HeuristicLab") is IRun) {
     313          IRun item = (IRun)e.Data.GetData("HeuristicLab");
     314          Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (IRun)item.Clone() : item);
     315        } else if (e.Data.GetData("HeuristicLab") is IEnumerable) {
     316          IEnumerable<IRun> items = ((IEnumerable)e.Data.GetData("HeuristicLab")).Cast<IRun>();
     317          foreach (IRun item in items)
     318            Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (IRun)item.Clone() : item);
    314319        }
    315320      }
Note: See TracChangeset for help on using the changeset viewer.