Search in sources :

Example 26 with TransferData

use of org.eclipse.swt.dnd.TransferData in project Pydev by fabioz.

the class PasteAction method updateSelection.

/**
 * The <code>PasteAction</code> implementation of this
 * <code>SelectionListenerAction</code> method enables this action if
 * a resource compatible with what is on the clipboard is selected.
 *
 * -Clipboard must have IResource or java.io.File
 * -Projects can always be pasted if they are open
 * -Workspace folder may not be copied into itself
 * -Files and folders may be pasted to a single selected folder in open
 *  project or multiple selected files in the same folder
 */
@Override
protected boolean updateSelection(IStructuredSelection selection) {
    if (!super.updateSelection(selection)) {
        return false;
    }
    final IResource[][] clipboardData = new IResource[1][];
    shell.getDisplay().syncExec(new Runnable() {

        @Override
        public void run() {
            // clipboard must have resources or files
            ResourceTransfer resTransfer = ResourceTransfer.getInstance();
            clipboardData[0] = (IResource[]) clipboard.getContents(resTransfer);
        }
    });
    IResource[] resourceData = clipboardData[0];
    boolean isProjectRes = resourceData != null && resourceData.length > 0 && resourceData[0].getType() == IResource.PROJECT;
    if (isProjectRes) {
        for (int i = 0; i < resourceData.length; i++) {
            // can paste open projects regardless of selection
            if (resourceData[i].getType() != IResource.PROJECT || ((IProject) resourceData[i]).isOpen() == false) {
                return false;
            }
        }
        return true;
    }
    if (getSelectedNonResources().size() > 0) {
        return false;
    }
    IResource targetResource = getTarget();
    // or selection is empty
    if (targetResource == null) {
        return false;
    }
    // can paste files and folders to a single selection (file, folder,
    // open project) or multiple file selection with the same parent
    List<? extends IResource> selectedResources = getSelectedResources();
    if (selectedResources.size() > 1) {
        for (int i = 0; i < selectedResources.size(); i++) {
            IResource resource = selectedResources.get(i);
            if (resource.getType() != IResource.FILE) {
                return false;
            }
            if (!targetResource.equals(resource.getParent())) {
                return false;
            }
        }
    }
    if (resourceData != null) {
        // linked resources can only be pasted into projects
        if (isLinked(resourceData) && targetResource.getType() != IResource.PROJECT) {
            return false;
        }
        if (targetResource.getType() == IResource.FOLDER) {
            // don't try to copy folder to self
            for (int i = 0; i < resourceData.length; i++) {
                if (targetResource.equals(resourceData[i])) {
                    return false;
                }
            }
        }
        return true;
    }
    TransferData[] transfers = clipboard.getAvailableTypes();
    FileTransfer fileTransfer = FileTransfer.getInstance();
    for (int i = 0; i < transfers.length; i++) {
        if (fileTransfer.isSupportedType(transfers[i])) {
            return true;
        }
    }
    return false;
}
Also used : ResourceTransfer(org.eclipse.ui.part.ResourceTransfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) IProject(org.eclipse.core.resources.IProject) TransferData(org.eclipse.swt.dnd.TransferData) IResource(org.eclipse.core.resources.IResource)

Example 27 with TransferData

use of org.eclipse.swt.dnd.TransferData in project netxms-ocilib by stevemqeen.

the class ScriptEditorView method canPaste.

/**
 * Check if paste action can work
 *
 * @return
 */
private boolean canPaste() {
    Clipboard cb = new Clipboard(Display.getCurrent());
    TransferData[] available = cb.getAvailableTypes();
    boolean enabled = false;
    for (int i = 0; i < available.length; i++) {
        if (TextTransfer.getInstance().isSupportedType(available[i])) {
            enabled = true;
            break;
        }
    }
    cb.dispose();
    return enabled;
}
Also used : TransferData(org.eclipse.swt.dnd.TransferData) Clipboard(org.eclipse.swt.dnd.Clipboard)

Example 28 with TransferData

use of org.eclipse.swt.dnd.TransferData in project netxms-ocilib by stevemqeen.

the class AgentFileManager method enableDropSupport.

/**
 * Enable drop support in object tree
 */
public // SubtreeType infrastructure
void enableDropSupport() {
    final Transfer[] transfers = new Transfer[] { LocalSelectionTransfer.getTransfer() };
    viewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, transfers, new ViewerDropAdapter(viewer) {

        @Override
        public boolean performDrop(Object data) {
            IStructuredSelection selection = (IStructuredSelection) data;
            List<?> movableSelection = selection.toList();
            for (int i = 0; i < movableSelection.size(); i++) {
                AgentFile movableObject = (AgentFile) movableSelection.get(i);
                moveFile((AgentFile) getCurrentTarget(), movableObject);
            }
            return true;
        }

        @Override
        public boolean validateDrop(Object target, int operation, TransferData transferType) {
            if ((target == null) || !LocalSelectionTransfer.getTransfer().isSupportedType(transferType))
                return false;
            IStructuredSelection selection = (IStructuredSelection) LocalSelectionTransfer.getTransfer().getSelection();
            if (selection.isEmpty())
                return false;
            for (final Object object : selection.toList()) {
                if (!(object instanceof AgentFile))
                    return false;
            }
            if (!(target instanceof AgentFile)) {
                return false;
            } else {
                if (!((AgentFile) target).isDirectory()) {
                    return false;
                }
            }
            return true;
        }
    });
}
Also used : TransferData(org.eclipse.swt.dnd.TransferData) AgentFile(org.netxms.client.server.AgentFile) LocalSelectionTransfer(org.eclipse.jface.util.LocalSelectionTransfer) Transfer(org.eclipse.swt.dnd.Transfer) ViewerDropAdapter(org.eclipse.jface.viewers.ViewerDropAdapter) List(java.util.List) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 29 with TransferData

use of org.eclipse.swt.dnd.TransferData in project netxms-ocilib by stevemqeen.

the class PredefinedMap method addDropSupport.

/**
 * Add drop support
 */
private void addDropSupport() {
    final Transfer[] transfers = new Transfer[] { LocalSelectionTransfer.getTransfer() };
    viewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, transfers, new ViewerDropAdapter(viewer) {

        private int x;

        private int y;

        @SuppressWarnings("rawtypes")
        @Override
        public boolean validateDrop(Object target, int operation, TransferData transferType) {
            if (!LocalSelectionTransfer.getTransfer().isSupportedType(transferType))
                return false;
            IStructuredSelection selection = (IStructuredSelection) LocalSelectionTransfer.getTransfer().getSelection();
            Iterator it = selection.iterator();
            while (it.hasNext()) {
                Object object = it.next();
                if (!((object instanceof AbstractObject) && ((AbstractObject) object).isAllowedOnMap()))
                    return false;
            }
            return true;
        }

        @SuppressWarnings("unchecked")
        @Override
        public boolean performDrop(Object data) {
            IStructuredSelection selection = (IStructuredSelection) LocalSelectionTransfer.getTransfer().getSelection();
            addObjectsFromList(selection.toList(), viewer.getControl().toControl(x, y));
            return true;
        }

        /*
			 * (non-Javadoc)
			 * 
			 * @see
			 * org.eclipse.jface.viewers.ViewerDropAdapter#dropAccept(org.eclipse
			 * .swt.dnd.DropTargetEvent)
			 */
        @Override
        public void dropAccept(DropTargetEvent event) {
            x = event.x;
            y = event.y;
            super.dropAccept(event);
        }
    });
}
Also used : TransferData(org.eclipse.swt.dnd.TransferData) AbstractObject(org.netxms.client.objects.AbstractObject) LocalSelectionTransfer(org.eclipse.jface.util.LocalSelectionTransfer) Transfer(org.eclipse.swt.dnd.Transfer) Iterator(java.util.Iterator) ViewerDropAdapter(org.eclipse.jface.viewers.ViewerDropAdapter) AbstractObject(org.netxms.client.objects.AbstractObject) NetworkMapObject(org.netxms.client.maps.elements.NetworkMapObject) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Point(org.eclipse.swt.graphics.Point)

Example 30 with TransferData

use of org.eclipse.swt.dnd.TransferData in project netxms-ocilib by stevemqeen.

the class ObjectTree method enableDropSupport.

/**
 * Enable drop support in object tree
 */
public // SubtreeType infrastructure
void enableDropSupport(// SubtreeType infrastructure
final ObjectBrowser obj) {
    final Transfer[] transfers = new Transfer[] { LocalSelectionTransfer.getTransfer() };
    objectTree.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, transfers, new ViewerDropAdapter(objectTree) {

        @Override
        public boolean performDrop(Object data) {
            TreeSelection selection = (TreeSelection) data;
            List<?> movableSelection = selection.toList();
            for (int i = 0; i < movableSelection.size(); i++) {
                AbstractObject movableObject = (AbstractObject) movableSelection.get(i);
                TreePath path = selection.getPaths()[0];
                AbstractObject parent = (AbstractObject) path.getSegment(path.getSegmentCount() - 2);
                obj.performObjectMove((AbstractObject) getCurrentTarget(), parent, movableObject);
            }
            return true;
        }

        @Override
        public boolean validateDrop(Object target, int operation, TransferData transferType) {
            if ((target == null) || !LocalSelectionTransfer.getTransfer().isSupportedType(transferType))
                return false;
            IStructuredSelection selection = (IStructuredSelection) LocalSelectionTransfer.getTransfer().getSelection();
            if (selection.isEmpty())
                return false;
            for (final Object object : selection.toList()) {
                SubtreeType subtree = null;
                if ((object instanceof AbstractObject)) {
                    if (obj.isValidSelectionForMove(SubtreeType.INFRASTRUCTURE))
                        subtree = SubtreeType.INFRASTRUCTURE;
                    else if (obj.isValidSelectionForMove(SubtreeType.TEMPLATES))
                        subtree = SubtreeType.TEMPLATES;
                    else if (obj.isValidSelectionForMove(SubtreeType.BUSINESS_SERVICES))
                        subtree = SubtreeType.BUSINESS_SERVICES;
                    else if (obj.isValidSelectionForMove(SubtreeType.DASHBOARDS))
                        subtree = SubtreeType.DASHBOARDS;
                    else if (obj.isValidSelectionForMove(SubtreeType.MAPS))
                        subtree = SubtreeType.MAPS;
                    else if (obj.isValidSelectionForMove(SubtreeType.POLICIES))
                        subtree = SubtreeType.POLICIES;
                }
                if (subtree == null)
                    return false;
                Set<Integer> filter;
                switch(subtree) {
                    case INFRASTRUCTURE:
                        filter = ObjectSelectionDialog.createContainerSelectionFilter();
                        break;
                    case TEMPLATES:
                        filter = ObjectSelectionDialog.createTemplateGroupSelectionFilter();
                        break;
                    case BUSINESS_SERVICES:
                        filter = ObjectSelectionDialog.createBusinessServiceSelectionFilter();
                        break;
                    case DASHBOARDS:
                        filter = ObjectSelectionDialog.createDashboardSelectionFilter();
                        break;
                    case MAPS:
                        filter = ObjectSelectionDialog.createNetworkMapGroupsSelectionFilter();
                        break;
                    case POLICIES:
                        filter = ObjectSelectionDialog.createPolicyGroupSelectionFilter();
                        break;
                    default:
                        filter = null;
                        break;
                }
                if ((filter == null) || !filter.contains(((AbstractObject) target).getObjectClass()) || target.equals(object))
                    return false;
            }
            return true;
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ViewerDropAdapter(org.eclipse.jface.viewers.ViewerDropAdapter) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) SubtreeType(org.netxms.ui.eclipse.objectbrowser.api.SubtreeType) TreePath(org.eclipse.jface.viewers.TreePath) TransferData(org.eclipse.swt.dnd.TransferData) TreeSelection(org.eclipse.jface.viewers.TreeSelection) AbstractObject(org.netxms.client.objects.AbstractObject) LocalSelectionTransfer(org.eclipse.jface.util.LocalSelectionTransfer) Transfer(org.eclipse.swt.dnd.Transfer) AbstractObject(org.netxms.client.objects.AbstractObject) List(java.util.List)

Aggregations

TransferData (org.eclipse.swt.dnd.TransferData)30 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)14 ViewerDropAdapter (org.eclipse.jface.viewers.ViewerDropAdapter)12 LocalSelectionTransfer (org.eclipse.jface.util.LocalSelectionTransfer)11 Transfer (org.eclipse.swt.dnd.Transfer)10 List (java.util.List)9 ArrayList (java.util.ArrayList)7 IResource (org.eclipse.core.resources.IResource)7 DropTargetEvent (org.eclipse.swt.dnd.DropTargetEvent)7 IFile (org.eclipse.core.resources.IFile)5 ISelection (org.eclipse.jface.viewers.ISelection)5 CoreException (org.eclipse.core.runtime.CoreException)4 IStatus (org.eclipse.core.runtime.IStatus)4 TreeSelection (org.eclipse.jface.viewers.TreeSelection)4 AbstractObject (org.netxms.client.objects.AbstractObject)4 URI (java.net.URI)3 IContainer (org.eclipse.core.resources.IContainer)3 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)3 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)3 Point (org.eclipse.swt.graphics.Point)3