Search in sources :

Example 21 with TransferData

use of org.eclipse.swt.dnd.TransferData in project windowbuilder by eclipse.

the class LafPreferencePage method configureDND.

/**
 * Configures DND for LAF list viewer.
 */
private void configureDND() {
    Transfer[] transfers = new Transfer[] { LookAndFeelTransfer.INSTANCE };
    m_lafTree.addDragSupport(DND.DROP_MOVE, transfers, new DragSourceListener() {

        public void dragStart(DragSourceEvent event) {
            m_dragEntries = getSelectedEntries();
            m_dragCategory = m_dragEntries.get(0) instanceof CategoryInfo;
            // check that we drag only categories or only entries
            for (Object element : m_dragEntries) {
                if (m_dragCategory != element instanceof CategoryInfo) {
                    event.doit = false;
                }
            }
        }

        public void dragSetData(DragSourceEvent event) {
        }

        public void dragFinished(DragSourceEvent event) {
        }
    });
    ViewerDropAdapter dropAdapter = new ViewerDropAdapter(m_lafTree) {

        @Override
        protected int determineLocation(DropTargetEvent event) {
            if (event.item instanceof Item) {
                Item item = (Item) event.item;
                Point coordinates = m_lafTree.getControl().toControl(event.x, event.y);
                Rectangle bounds = getBounds(item);
                // when dragging LAF entry relation with category can be only ON
                if (!m_dragCategory && determineTarget(event) instanceof CategoryInfo) {
                    return LOCATION_ON;
                }
                // in all other cases, drop before/after
                return coordinates.y < bounds.y + bounds.height / 2 ? LOCATION_BEFORE : LOCATION_AFTER;
            }
            return LOCATION_NONE;
        }

        @Override
        public boolean validateDrop(Object target, int operation, TransferData transferType) {
            // category can be dragged only relative other category
            if (m_dragCategory) {
                return target instanceof CategoryInfo;
            }
            // all other cases are valid
            return true;
        }

        @Override
        public boolean performDrop(Object data) {
            Object target = getCurrentTarget();
            int location = getCurrentLocation();
            if (m_dragCategory) {
                Assert.instanceOf(CategoryInfo.class, target);
                Assert.isTrue(location == LOCATION_BEFORE || location == LOCATION_AFTER);
                // prepare next category
                CategoryInfo nextCategory;
                {
                    List<CategoryInfo> categories = LafSupport.getLAFCategoriesList();
                    int index = categories.indexOf(target);
                    if (location == LOCATION_BEFORE) {
                        nextCategory = categories.get(index);
                    } else {
                        nextCategory = GenericsUtils.getNextOrNull(categories, index);
                    }
                }
                // add commands
                for (Object element : m_dragEntries) {
                    CategoryInfo category = (CategoryInfo) element;
                    commands_add(new MoveCategoryCommand(category, nextCategory));
                }
            } else if (target instanceof CategoryInfo) {
                Assert.isTrue(location == LOCATION_ON);
                CategoryInfo targetCategory = (CategoryInfo) target;
                for (Object entry : m_dragEntries) {
                    commands_add(new MoveCommand((LafInfo) entry, targetCategory, null));
                }
            } else {
                LafInfo targetLAF = (LafInfo) target;
                CategoryInfo targetCategory = targetLAF.getCategory();
                // prepare next LAF
                LafInfo nextLAF;
                {
                    List<LafInfo> entries = targetCategory.getLAFList();
                    int index = entries.indexOf(targetLAF);
                    if (location == LOCATION_BEFORE) {
                        nextLAF = entries.get(index);
                    } else {
                        nextLAF = GenericsUtils.getNextOrNull(entries, index);
                    }
                }
                // add commands
                for (Object entry : m_dragEntries) {
                    commands_add(new MoveCommand((LafInfo) entry, targetCategory, nextLAF));
                }
            }
            // refresh viewer to show result of applying commands
            refreshViewer();
            return true;
        }
    };
    dropAdapter.setScrollExpandEnabled(false);
    m_lafTree.addDropSupport(DND.DROP_MOVE, transfers, dropAdapter);
}
Also used : SeparatorLafInfo(org.eclipse.wb.internal.swing.laf.model.SeparatorLafInfo) LafInfo(org.eclipse.wb.internal.swing.laf.model.LafInfo) CategoryInfo(org.eclipse.wb.internal.swing.laf.model.CategoryInfo) Rectangle(org.eclipse.swt.graphics.Rectangle) ViewerDropAdapter(org.eclipse.jface.viewers.ViewerDropAdapter) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) Point(org.eclipse.swt.graphics.Point) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener) Point(org.eclipse.swt.graphics.Point) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) Item(org.eclipse.swt.widgets.Item) JMenuItem(javax.swing.JMenuItem) TransferData(org.eclipse.swt.dnd.TransferData) Transfer(org.eclipse.swt.dnd.Transfer) ByteArrayTransfer(org.eclipse.swt.dnd.ByteArrayTransfer) MoveCommand(org.eclipse.wb.internal.swing.laf.command.MoveCommand) List(java.util.List) MoveCategoryCommand(org.eclipse.wb.internal.swing.laf.command.MoveCategoryCommand)

Example 22 with TransferData

use of org.eclipse.swt.dnd.TransferData in project bndtools by bndtools.

the class JarListWizardPage method createControl.

@Override
public void createControl(final Composite parent) {
    setTitle("Select JARs");
    final Composite composite = new Composite(parent, SWT.NONE);
    Label lblHint = new Label(composite, SWT.WRAP);
    lblHint.setText("Selected files (hint: drag files from an external application into this list):");
    final Table table = new Table(composite, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
    viewer = new TableViewer(table);
    viewer.setContentProvider(new ArrayContentProvider());
    viewer.setLabelProvider(new ClassPathLabelProvider());
    btnAdd = new Button(composite, SWT.PUSH);
    btnAdd.setText("Add");
    btnAddExternal = new Button(composite, SWT.PUSH);
    btnAddExternal.setText("Add External");
    btnRemove = new Button(composite, SWT.PUSH);
    btnRemove.setText("Remove");
    viewer.setInput(paths);
    update();
    // Listeners
    ViewerDropAdapter dropAdapter = new ViewerDropAdapter(viewer) {

        @Override
        public void dragEnter(DropTargetEvent event) {
            super.dragEnter(event);
            event.detail = DND.DROP_COPY;
        }

        @Override
        public boolean validateDrop(Object target, int operation, TransferData transferType) {
            return true;
        }

        @Override
        public boolean performDrop(Object data) {
            if (data instanceof String[]) {
                String[] newPaths = (String[]) data;
                List<IPath> added = new ArrayList<IPath>(newPaths.length);
                for (String path : newPaths) {
                    added.add(new Path(path));
                }
                if (!added.isEmpty()) {
                    addToPaths(added);
                    viewer.add(added.toArray());
                    update();
                }
            }
            return true;
        }
    };
    dropAdapter.setFeedbackEnabled(false);
    dropAdapter.setSelectionFeedbackEnabled(false);
    viewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { FileTransfer.getInstance() }, dropAdapter);
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(final SelectionChangedEvent event) {
            update();
        }
    });
    btnAdd.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            // IResource newFile = ResourcesPlugin.getWorkspace().getRoot();
            // if(newFile != null) {
            ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(), new WorkbenchLabelProvider(), new WorkbenchContentProvider());
            dialog.setValidator(new ISelectionStatusValidator() {

                @Override
                public IStatus validate(Object[] selection) {
                    if (selection.length > 0 && selection[0] instanceof IFile) {
                        // $NON-NLS-1$
                        return new Status(IStatus.OK, Plugin.PLUGIN_ID, IStatus.OK, "", null);
                    }
                    // $NON-NLS-1$
                    return new Status(IStatus.ERROR, Plugin.PLUGIN_ID, IStatus.ERROR, "", null);
                }
            });
            dialog.setAllowMultiple(true);
            dialog.setTitle("JAR File Selection");
            dialog.setMessage("Select one or more JAR files.");
            // $NON-NLS-1$
            dialog.addFilter(new FileExtensionFilter("jar"));
            dialog.setInput(ResourcesPlugin.getWorkspace());
            if (dialog.open() == Window.OK) {
                Object[] files = dialog.getResult();
                List<IPath> added = new ArrayList<IPath>(files.length);
                for (Object file : files) {
                    added.add(((IResource) file).getFullPath().makeRelative());
                }
                if (!added.isEmpty()) {
                    addToPaths(added);
                    viewer.add(added.toArray());
                }
            }
            // }
            update();
        }
    });
    btnAddExternal.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            FileDialog dialog = new FileDialog(getShell(), SWT.OPEN | SWT.MULTI);
            dialog.setFilterExtensions(new String[] { // $NON-NLS-1$
            "*.jar" });
            String res = dialog.open();
            if (res != null) {
                IPath filterPath = new Path(dialog.getFilterPath());
                String[] fileNames = dialog.getFileNames();
                List<IPath> added = new ArrayList<IPath>(fileNames.length);
                for (String fileName : fileNames) {
                    added.add(filterPath.append(fileName));
                }
                if (!added.isEmpty()) {
                    addToPaths(added);
                    viewer.add(added.toArray());
                }
            }
            update();
        }
    });
    btnRemove.addSelectionListener(new SelectionAdapter() {

        @SuppressWarnings("unchecked")
        @Override
        public void widgetSelected(SelectionEvent e) {
            removeFromPaths(((IStructuredSelection) viewer.getSelection()).toList());
            viewer.remove(((IStructuredSelection) viewer.getSelection()).toArray());
            update();
        }
    });
    // Layout
    composite.setLayout(new GridLayout(2, false));
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 3));
    btnAdd.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
    btnAddExternal.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
    btnRemove.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
    lblHint.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 2, 1));
    setControl(composite);
}
Also used : WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) IFile(org.eclipse.core.resources.IFile) Label(org.eclipse.swt.widgets.Label) ArrayList(java.util.ArrayList) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ClassPathLabelProvider(bndtools.utils.ClassPathLabelProvider) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) TransferData(org.eclipse.swt.dnd.TransferData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) FileExtensionFilter(bndtools.utils.FileExtensionFilter) ISelectionStatusValidator(org.eclipse.ui.dialogs.ISelectionStatusValidator) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) IPath(org.eclipse.core.runtime.IPath) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ViewerDropAdapter(org.eclipse.jface.viewers.ViewerDropAdapter) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) ElementTreeSelectionDialog(org.eclipse.ui.dialogs.ElementTreeSelectionDialog) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) GridData(org.eclipse.swt.layout.GridData) TableViewer(org.eclipse.jface.viewers.TableViewer) FileDialog(org.eclipse.swt.widgets.FileDialog)

Example 23 with TransferData

use of org.eclipse.swt.dnd.TransferData in project webtools.sourceediting by eclipse.

the class ExtendedEditorDropTargetAdapter method dragEnter.

/**
 */
public void dragEnter(DropTargetEvent event) {
    TransferData data = null;
    Transfer[] ts = getTransfers();
    for (int i = 0; i < ts.length; i++) {
        for (int j = 0; j < event.dataTypes.length; j++) {
            if (ts[i].isSupportedType(event.dataTypes[j])) {
                data = event.dataTypes[j];
                break;
            }
        }
        if (data != null) {
            event.currentDataType = data;
            break;
        }
    }
    if (textViewer != null) {
        originalRange = textViewer.getSelectedRange();
    }
}
Also used : TransferData(org.eclipse.swt.dnd.TransferData) FileTransfer(org.eclipse.swt.dnd.FileTransfer) Transfer(org.eclipse.swt.dnd.Transfer) Point(org.eclipse.swt.graphics.Point)

Example 24 with TransferData

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

the class EventObjectList method enableDropSupport.

/**
 * Enable drop support
 */
public 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) {
            TreeSelection selection = (TreeSelection) data;
            List<?> movableSelection = selection.toList();
            for (int i = 0; i < movableSelection.size(); i++) {
                if (((EventObject) movableSelection.get(i)).getCode() != ((EventGroup) getCurrentTarget()).getCode())
                    ((EventGroup) getCurrentTarget()).addChild(((EventObject) movableSelection.get(i)).getCode());
            }
            modifyEventObject((EventGroup) getCurrentTarget(), false);
            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;
            if (!(target instanceof EventGroup))
                return false;
            return true;
        }
    });
}
Also used : TransferData(org.eclipse.swt.dnd.TransferData) ITreeSelection(org.eclipse.jface.viewers.ITreeSelection) TreeSelection(org.eclipse.jface.viewers.TreeSelection) LocalSelectionTransfer(org.eclipse.jface.util.LocalSelectionTransfer) Transfer(org.eclipse.swt.dnd.Transfer) ViewerDropAdapter(org.eclipse.jface.viewers.ViewerDropAdapter) EventObject(org.netxms.client.events.EventObject) List(java.util.List) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) EventObject(org.netxms.client.events.EventObject) EventGroup(org.netxms.client.events.EventGroup)

Example 25 with TransferData

use of org.eclipse.swt.dnd.TransferData in project erlide_eclipse by erlang.

the class MoveFunctionDropHandler method handleDrop.

@Override
public IStatus handleDrop(final CommonDropAdapter dropAdapter, final DropTargetEvent dropTargetEvent, final Object target) {
    // get the source data
    final TransferData td = dropAdapter.getCurrentTransfer();
    final ISelection sel = (ISelection) LocalSelectionTransfer.getTransfer().nativeToJava(td);
    final TreeSelection s = (TreeSelection) sel;
    try {
        GlobalParameters.setSelection(s);
    } catch (final WranglerException e1) {
        e1.printStackTrace();
    }
    // get the target data
    String moduleName;
    IFile file;
    if (target instanceof IFile) {
        file = (IFile) target;
    } else {
        file = (IFile) ((IErlElement) target).getResource();
    }
    moduleName = file.getName();
    moduleName = moduleName.substring(0, moduleName.lastIndexOf("."));
    final MoveFunctionRefactoring refactoring = new MoveFunctionRefactoring();
    refactoring.setUserInput(moduleName);
    final RefactoringWizard wizard = new DefaultWranglerRefactoringWizard(refactoring, RefactoringWizard.DIALOG_BASED_USER_INTERFACE, new ArrayList<WranglerPage>());
    final Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
    final RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
    try {
        op.run(shell, refactoring.getName());
    } catch (final Exception e) {
        ErlLogger.error(e);
    }
    return Status.OK_STATUS;
}
Also used : IFile(org.eclipse.core.resources.IFile) MoveFunctionRefactoring(org.erlide.wrangler.refactoring.core.internal.MoveFunctionRefactoring) WranglerException(org.erlide.wrangler.refactoring.exception.WranglerException) WranglerException(org.erlide.wrangler.refactoring.exception.WranglerException) IErlElement(org.erlide.engine.model.IErlElement) Shell(org.eclipse.swt.widgets.Shell) RefactoringWizardOpenOperation(org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation) TransferData(org.eclipse.swt.dnd.TransferData) DefaultWranglerRefactoringWizard(org.erlide.wrangler.refactoring.ui.wizard.DefaultWranglerRefactoringWizard) RefactoringWizard(org.eclipse.ltk.ui.refactoring.RefactoringWizard) TreeSelection(org.eclipse.jface.viewers.TreeSelection) ISelection(org.eclipse.jface.viewers.ISelection) DefaultWranglerRefactoringWizard(org.erlide.wrangler.refactoring.ui.wizard.DefaultWranglerRefactoringWizard) WranglerPage(org.erlide.wrangler.refactoring.ui.wizardpages.WranglerPage)

Aggregations

TransferData (org.eclipse.swt.dnd.TransferData)50 ViewerDropAdapter (org.eclipse.jface.viewers.ViewerDropAdapter)21 Transfer (org.eclipse.swt.dnd.Transfer)20 DropTargetEvent (org.eclipse.swt.dnd.DropTargetEvent)18 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)17 List (java.util.List)15 DragSourceEvent (org.eclipse.swt.dnd.DragSourceEvent)12 LocalSelectionTransfer (org.eclipse.jface.util.LocalSelectionTransfer)11 ArrayList (java.util.ArrayList)9 IResource (org.eclipse.core.resources.IResource)8 DragSourceListener (org.eclipse.swt.dnd.DragSourceListener)7 Point (org.eclipse.swt.graphics.Point)7 IFile (org.eclipse.core.resources.IFile)5 DragSourceAdapter (org.eclipse.swt.dnd.DragSourceAdapter)5 Test (org.junit.Test)5 IStatus (org.eclipse.core.runtime.IStatus)4 ISelection (org.eclipse.jface.viewers.ISelection)4 TreeSelection (org.eclipse.jface.viewers.TreeSelection)4 FileTransfer (org.eclipse.swt.dnd.FileTransfer)4 AbstractObject (org.netxms.client.objects.AbstractObject)4