/* Copyright (c) Citrix Systems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, * with or without modification, are permitted provided * that the following conditions are met: * * * Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ using System.Collections.Generic; using XenAdmin.Controls; using System.Collections.ObjectModel; using XenAPI; using XenAdmin.Controls.CustomGridView; using System.Windows.Forms; using XenAdmin.Actions; namespace XenAdmin.Commands { internal abstract class DragDropCommand { private readonly VirtualTreeNode _targetNode; private readonly ReadOnlyCollection _draggedNodes; private readonly GridRowCollection _draggedGridRows; private readonly IMainWindow _mainWindow; protected DragDropCommand(IMainWindow mainWindow, VirtualTreeNode targetNode, IDataObject dragData) { Util.ThrowIfParameterNull(mainWindow, "mainWindow"); Util.ThrowIfParameterNull(dragData, "dragData"); _mainWindow = mainWindow; _targetNode = targetNode; if (dragData.GetDataPresent(typeof(GridRowCollection))) { _draggedGridRows = (GridRowCollection)dragData.GetData(typeof(GridRowCollection)); } else if (dragData.GetDataPresent(typeof(VirtualTreeNode[]))) { _draggedNodes = new ReadOnlyCollection(new List((VirtualTreeNode[])dragData.GetData(typeof(VirtualTreeNode[])))); } } /// /// Gets the main window to be used by the Command. /// public IMainWindow MainWindowCommandInterface { get { return _mainWindow; } } /// /// Determines whether this drop-drop operation can run with the specified target-node and drag-data. /// public bool CanRun() { if ((DraggedNodes == null || DraggedNodes.Count == 0) && DraggedGridRows == null) { return false; } if (_targetNode == null) { return false; } return CanRunCore(); } /// /// Determines whether this drop-drop operation can run with the specified target-node and drag-data. /// protected virtual bool CanRunCore() { return false; } /// /// Runs this drag-drop operation with the specified target-node and drag data. /// public void Run() { RunCore(); } /// /// Runs this drag-drop operation with the specified target-node and drag data. /// protected virtual void RunCore() { } /// /// Gets the node that the mouse is over during the drag-drop operation. /// protected VirtualTreeNode TargetNode => _targetNode; /// /// Gets the node that should be highlighted during the drag-drop operation. /// public virtual VirtualTreeNode HighlightNode => _targetNode; /// /// Gets the nearest ancestor of the target node that has a tag of the specified xen object or grouping tag type and returns that tag. /// protected T GetTargetNodeAncestorAsXenObjectOrGroupingTag() where T : class { VirtualTreeNode node = GetTargetNodeAncestor(); if (node != null) { return (T)node.Tag; } return default(T); } /// /// Gets the nearest ancestor of the target node that has a tag of the specified xen object or grouping-tag type. /// protected VirtualTreeNode GetTargetNodeAncestor() where T : class { VirtualTreeNode node = TargetNode; while (node != null) { if (node.Tag is T) { return node; } node = node.Parent; } return null; } /// /// Gets the dragged grid rows. Returns null if grid rows weren't dragged. /// protected GridRowCollection DraggedGridRows => _draggedGridRows; /// /// Gets the dragged tree nodes. Returns null if tree nodes weren't dragged. /// protected ReadOnlyCollection DraggedNodes => _draggedNodes; /// /// Gets the status bar text tat should be displayed during the drag-drop operation. Returns null /// if no text should be displayed. /// public virtual string StatusBarText => null; /// /// Gets the dragged items as Xen objects. If any items isn't a XenObject then an empty list is returned. /// protected List GetDraggedItemsAsXenObjects() where T : IXenObject { List output = new List(); if (DraggedNodes != null) { foreach (VirtualTreeNode node in DraggedNodes) { if (node != null) { if (node.Tag is T) { output.Add((T)node.Tag); } else { return new List(); } } else { return new List(); } } } else if (DraggedGridRows != null) { foreach (GridRow row in DraggedGridRows) { if (row.Tag is T) { output.Add((T)row.Tag); } else { return new List(); } } } return output; } /// /// Runs the specified s such that they are synchronous per connection but asynchronous across connections. /// public void RunMultipleActions(IEnumerable actions, string title, string startDescription, string endDescription, bool runActionsInParallel) { MultipleActionLauncher launcher = new MultipleActionLauncher(actions, title, startDescription, endDescription, runActionsInParallel); launcher.Run(); } } }