Search in sources :

Example 56 with DropTarget

use of org.eclipse.swt.dnd.DropTarget in project archi by archimatetool.

the class IconSection method createControls.

@Override
protected void createControls(Composite parent) {
    createLabel(parent, Messages.IconSection_9, ITabbedLayoutConstants.STANDARD_LABEL_WIDTH, SWT.NONE);
    final int canvasSize = IIconic.MAX_IMAGE_SIZE;
    fCanvas = new Canvas(parent, SWT.BORDER);
    getWidgetFactory().adapt(fCanvas);
    GridData gd = new GridData(SWT.NONE, SWT.NONE, false, false);
    gd.widthHint = canvasSize;
    gd.heightHint = canvasSize;
    fCanvas.setLayoutData(gd);
    GridLayout layout = new GridLayout();
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    fCanvas.setLayout(layout);
    fCanvas.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            disposeImage();
        }
    });
    fCanvas.addListener(SWT.MouseDoubleClick, new Listener() {

        @Override
        public void handleEvent(Event event) {
            if (!isLocked(getFirstSelectedObject())) {
                chooseImage();
            }
        }
    });
    fCanvas.addPaintListener(new PaintListener() {

        @Override
        public void paintControl(PaintEvent e) {
            if (fImage != null) {
                Rectangle bounds = fImage.getBounds();
                int x = (canvasSize - bounds.width) / 2;
                int y = (canvasSize - bounds.height) / 2;
                e.gc.drawImage(fImage, x, y);
            }
        }
    });
    String tooltip = Messages.IconSection_10;
    fCanvas.setToolTipText(tooltip);
    DropTarget target = new DropTarget(fCanvas, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
    target.setTransfer(new Transfer[] { FileTransfer.getInstance() });
    target.addDropListener(new DropTargetAdapter() {

        @Override
        public void drop(DropTargetEvent event) {
            if (event.data instanceof String[]) {
                if (!isLocked(getFirstSelectedObject())) {
                    File file = new File(((String[]) event.data)[0]);
                    setImage(file);
                }
            }
        }
    });
    // Image Button
    createImageButton(parent);
    // Position
    createLabel(parent, Messages.IconSection_11, ITabbedLayoutConstants.STANDARD_LABEL_WIDTH, SWT.CENTER);
    fComboPosition = new Combo(parent, SWT.READ_ONLY);
    fComboPosition.setItems(fComboPositionItems);
    gd = new GridData(SWT.NONE, SWT.NONE, false, false);
    fComboPosition.setLayoutData(gd);
    fComboPosition.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            CompoundCommand result = new CompoundCommand();
            for (EObject iconic : getEObjects()) {
                if (isAlive(iconic)) {
                    Command cmd = new EObjectFeatureCommand(Messages.IconSection_12, iconic, ICanvasPackage.Literals.ICONIC__IMAGE_POSITION, fComboPosition.getSelectionIndex());
                    if (cmd.canExecute()) {
                        result.add(cmd);
                    }
                }
            }
            executeCommand(result.unwrap());
        }
    });
    // Help
    PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, HELP_ID);
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) PaintListener(org.eclipse.swt.events.PaintListener) DisposeListener(org.eclipse.swt.events.DisposeListener) Listener(org.eclipse.swt.widgets.Listener) Rectangle(org.eclipse.swt.graphics.Rectangle) Combo(org.eclipse.swt.widgets.Combo) DisposeEvent(org.eclipse.swt.events.DisposeEvent) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) GridLayout(org.eclipse.swt.layout.GridLayout) EObjectFeatureCommand(com.archimatetool.editor.model.commands.EObjectFeatureCommand) EObject(org.eclipse.emf.ecore.EObject) SelectionEvent(org.eclipse.swt.events.SelectionEvent) PaintEvent(org.eclipse.swt.events.PaintEvent) PaintListener(org.eclipse.swt.events.PaintListener) Canvas(org.eclipse.swt.widgets.Canvas) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) EObjectFeatureCommand(com.archimatetool.editor.model.commands.EObjectFeatureCommand) Command(org.eclipse.gef.commands.Command) GridData(org.eclipse.swt.layout.GridData) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Event(org.eclipse.swt.widgets.Event) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) PaintEvent(org.eclipse.swt.events.PaintEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DropTarget(org.eclipse.swt.dnd.DropTarget) File(java.io.File)

Example 57 with DropTarget

use of org.eclipse.swt.dnd.DropTarget in project eclipse.platform.swt by eclipse.

the class DNDExample method createDropTarget.

private void createDropTarget() {
    if (dropTarget != null)
        dropTarget.dispose();
    dropTarget = new DropTarget(dropControl, dropOperation);
    dropTarget.setTransfer(dropTypes);
    dropTarget.addDropListener(new DropTargetListener() {

        @Override
        public void dragEnter(DropTargetEvent event) {
            dropConsole.append(">>dragEnter\n");
            printEvent(event);
            if (event.detail == DND.DROP_DEFAULT) {
                event.detail = dropDefaultOperation;
            }
            event.feedback = dropFeedback;
        }

        @Override
        public void dragLeave(DropTargetEvent event) {
            dropConsole.append(">>dragLeave\n");
            printEvent(event);
        }

        @Override
        public void dragOperationChanged(DropTargetEvent event) {
            dropConsole.append(">>dragOperationChanged\n");
            printEvent(event);
            if (event.detail == DND.DROP_DEFAULT) {
                event.detail = dropDefaultOperation;
            }
            event.feedback = dropFeedback;
        }

        @Override
        public void dragOver(DropTargetEvent event) {
            dropConsole.append(">>dragOver\n");
            printEvent(event);
            event.feedback = dropFeedback;
        }

        @Override
        public void drop(DropTargetEvent event) {
            dropConsole.append(">>drop\n");
            printEvent(event);
            String[] strings = null;
            if (TextTransfer.getInstance().isSupportedType(event.currentDataType) || RTFTransfer.getInstance().isSupportedType(event.currentDataType) || HTMLTransfer.getInstance().isSupportedType(event.currentDataType) || URLTransfer.getInstance().isSupportedType(event.currentDataType)) {
                strings = new String[] { (String) event.data };
            }
            if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {
                strings = (String[]) event.data;
            }
            if (strings == null || strings.length == 0) {
                dropConsole.append("!!Invalid data dropped");
                return;
            }
            if (strings.length == 1 && (dropControlType == TABLE || dropControlType == TREE || dropControlType == LIST)) {
                // convert string separated by "\n" into an array of strings
                String string = strings[0];
                int count = 0;
                int offset = string.indexOf("\n", 0);
                while (offset > 0) {
                    count++;
                    offset = string.indexOf("\n", offset + 1);
                }
                if (count > 0) {
                    strings = new String[count + 1];
                    int start = 0;
                    int end = string.indexOf("\n");
                    int index = 0;
                    while (start < end) {
                        strings[index++] = string.substring(start, end);
                        start = end + 1;
                        end = string.indexOf("\n", start);
                        if (end == -1)
                            end = string.length();
                    }
                }
            }
            switch(dropControlType) {
                case BUTTON_CHECK:
                case BUTTON_TOGGLE:
                case BUTTON_RADIO:
                    {
                        Button b = (Button) dropControl;
                        b.setText(strings[0]);
                        break;
                    }
                case STYLED_TEXT:
                    {
                        StyledText text = (StyledText) dropControl;
                        for (String string : strings) {
                            text.insert(string);
                        }
                        break;
                    }
                case TABLE:
                    {
                        Table table = (Table) dropControl;
                        Point p = event.display.map(null, table, event.x, event.y);
                        TableItem dropItem = table.getItem(p);
                        int index = dropItem == null ? table.getItemCount() : table.indexOf(dropItem);
                        for (String string : strings) {
                            TableItem item = new TableItem(table, SWT.NONE, index);
                            item.setText(0, string);
                            item.setText(1, "dropped item");
                        }
                        TableColumn[] columns = table.getColumns();
                        for (TableColumn column : columns) {
                            column.pack();
                        }
                        break;
                    }
                case TEXT:
                    {
                        Text text = (Text) dropControl;
                        for (String string : strings) {
                            text.append(string + "\n");
                        }
                        break;
                    }
                case TREE:
                    {
                        Tree tree = (Tree) dropControl;
                        Point p = event.display.map(null, tree, event.x, event.y);
                        TreeItem parentItem = tree.getItem(p);
                        for (String string : strings) {
                            TreeItem item = parentItem != null ? new TreeItem(parentItem, SWT.NONE) : new TreeItem(tree, SWT.NONE);
                            item.setText(string);
                        }
                        break;
                    }
                case CANVAS:
                    {
                        dropControl.setData("STRINGS", strings);
                        dropControl.redraw();
                        break;
                    }
                case LABEL:
                    {
                        Label label = (Label) dropControl;
                        label.setText(strings[0]);
                        break;
                    }
                case LIST:
                    {
                        List list = (List) dropControl;
                        for (String string : strings) {
                            list.add(string);
                        }
                        break;
                    }
                case COMBO:
                    {
                        Combo combo = (Combo) dropControl;
                        combo.setText(strings[0]);
                        break;
                    }
                default:
                    throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED);
            }
        }

        @Override
        public void dropAccept(DropTargetEvent event) {
            dropConsole.append(">>dropAccept\n");
            printEvent(event);
        }
    });
}
Also used : SWTError(org.eclipse.swt.SWTError) StyledText(org.eclipse.swt.custom.StyledText) Table(org.eclipse.swt.widgets.Table) TreeItem(org.eclipse.swt.widgets.TreeItem) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) StyledText(org.eclipse.swt.custom.StyledText) Text(org.eclipse.swt.widgets.Text) Combo(org.eclipse.swt.widgets.Combo) Point(org.eclipse.swt.graphics.Point) TableColumn(org.eclipse.swt.widgets.TableColumn) Point(org.eclipse.swt.graphics.Point) DropTargetListener(org.eclipse.swt.dnd.DropTargetListener) Button(org.eclipse.swt.widgets.Button) Tree(org.eclipse.swt.widgets.Tree) List(org.eclipse.swt.widgets.List) DropTarget(org.eclipse.swt.dnd.DropTarget)

Example 58 with DropTarget

use of org.eclipse.swt.dnd.DropTarget in project eclipse.platform.swt by eclipse.

the class FileViewer method createTreeDropTarget.

/**
 * Creates the Drag & Drop DropTarget for items being dropped onto the tree.
 *
 * @return the DropTarget for the tree
 */
private DropTarget createTreeDropTarget(final Tree tree) {
    DropTarget dropTarget = new DropTarget(tree, DND.DROP_MOVE | DND.DROP_COPY);
    dropTarget.setTransfer(FileTransfer.getInstance());
    dropTarget.addDropListener(new DropTargetAdapter() {

        @Override
        public void dragEnter(DropTargetEvent event) {
            isDropping = true;
        }

        @Override
        public void dragLeave(DropTargetEvent event) {
            isDropping = false;
            handleDeferredRefresh();
        }

        @Override
        public void dragOver(DropTargetEvent event) {
            dropTargetValidate(event, getTargetFile(event));
            event.feedback |= DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
        }

        @Override
        public void drop(DropTargetEvent event) {
            File targetFile = getTargetFile(event);
            if (dropTargetValidate(event, targetFile))
                dropTargetHandleDrop(event, targetFile);
        }

        private File getTargetFile(DropTargetEvent event) {
            // Determine the target File for the drop
            TreeItem item = tree.getItem(tree.toControl(new Point(event.x, event.y)));
            File targetFile = null;
            if (item != null) {
                // We are over a particular item in the tree, use the item's file
                targetFile = (File) item.getData(TREEITEMDATA_FILE);
            }
            return targetFile;
        }
    });
    return dropTarget;
}
Also used : DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) TreeItem(org.eclipse.swt.widgets.TreeItem) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DropTarget(org.eclipse.swt.dnd.DropTarget) Point(org.eclipse.swt.graphics.Point) File(java.io.File)

Example 59 with DropTarget

use of org.eclipse.swt.dnd.DropTarget in project eclipse.platform.swt by eclipse.

the class FileViewer method createTableDropTarget.

/**
 * Creates the Drag & Drop DropTarget for items being dropped onto the table.
 *
 * @return the DropTarget for the table
 */
private DropTarget createTableDropTarget(final Table table) {
    DropTarget dropTarget = new DropTarget(table, DND.DROP_MOVE | DND.DROP_COPY);
    dropTarget.setTransfer(FileTransfer.getInstance());
    dropTarget.addDropListener(new DropTargetAdapter() {

        @Override
        public void dragEnter(DropTargetEvent event) {
            isDropping = true;
        }

        @Override
        public void dragLeave(DropTargetEvent event) {
            isDropping = false;
            handleDeferredRefresh();
        }

        @Override
        public void dragOver(DropTargetEvent event) {
            dropTargetValidate(event, getTargetFile(event));
            event.feedback |= DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
        }

        @Override
        public void drop(DropTargetEvent event) {
            File targetFile = getTargetFile(event);
            if (dropTargetValidate(event, targetFile))
                dropTargetHandleDrop(event, targetFile);
        }

        private File getTargetFile(DropTargetEvent event) {
            // Determine the target File for the drop
            TableItem item = table.getItem(table.toControl(new Point(event.x, event.y)));
            File targetFile = null;
            if (item == null) {
                // If it is a COPY, we can use the table's root file.
                if (event.detail == DND.DROP_COPY) {
                    targetFile = (File) table.getData(TABLEDATA_DIR);
                }
            } else {
                // We are over a particular item in the table, use the item's file
                targetFile = (File) item.getData(TABLEITEMDATA_FILE);
            }
            return targetFile;
        }
    });
    return dropTarget;
}
Also used : DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) TableItem(org.eclipse.swt.widgets.TableItem) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DropTarget(org.eclipse.swt.dnd.DropTarget) Point(org.eclipse.swt.graphics.Point) File(java.io.File)

Example 60 with DropTarget

use of org.eclipse.swt.dnd.DropTarget in project eclipse.platform.swt by eclipse.

the class Bug307441_DnDOverlappingControls method setDropTargetForLabel.

public static void setDropTargetForLabel(final Label control) {
    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
    DropTarget target = new DropTarget(control, operations);
    target.setTransfer(new Transfer[] { URLTransfer.getInstance() });
    target.addDropListener(new DropTargetListener() {

        @Override
        public void dragEnter(DropTargetEvent e) {
            System.out.println("dragEnter=" + control.getText());
            if (e.detail == DND.DROP_NONE)
                e.detail = DND.DROP_LINK;
        }

        @Override
        public void dragOperationChanged(DropTargetEvent e) {
            if (e.detail == DND.DROP_NONE)
                e.detail = DND.DROP_LINK;
        }

        @Override
        public void drop(DropTargetEvent event) {
            if (event.data == null) {
                event.detail = DND.DROP_NONE;
                return;
            }
            control.setText(((String) event.data));
        }

        @Override
        public void dragLeave(DropTargetEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println("dragLeave=" + control.getText());
        }

        @Override
        public void dragOver(DropTargetEvent arg0) {
        // TODO Auto-generated method stub
        }

        @Override
        public void dropAccept(DropTargetEvent arg0) {
        // TODO Auto-generated method stub
        }
    });
}
Also used : DropTargetListener(org.eclipse.swt.dnd.DropTargetListener) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DropTarget(org.eclipse.swt.dnd.DropTarget)

Aggregations

DropTarget (org.eclipse.swt.dnd.DropTarget)85 DropTargetEvent (org.eclipse.swt.dnd.DropTargetEvent)46 DropTargetAdapter (org.eclipse.swt.dnd.DropTargetAdapter)27 DropTargetListener (org.eclipse.swt.dnd.DropTargetListener)24 Transfer (org.eclipse.swt.dnd.Transfer)21 GridData (org.eclipse.swt.layout.GridData)17 Composite (org.eclipse.swt.widgets.Composite)17 SelectionEvent (org.eclipse.swt.events.SelectionEvent)15 Point (org.eclipse.swt.graphics.Point)15 GridLayout (org.eclipse.swt.layout.GridLayout)15 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)13 Button (org.eclipse.swt.widgets.Button)13 Label (org.eclipse.swt.widgets.Label)13 DragSource (org.eclipse.swt.dnd.DragSource)11 ArrayList (java.util.ArrayList)10 List (java.util.List)10 TextTransfer (org.eclipse.swt.dnd.TextTransfer)10 Control (org.eclipse.swt.widgets.Control)10 TreeItem (org.eclipse.swt.widgets.TreeItem)10 DragSourceEvent (org.eclipse.swt.dnd.DragSourceEvent)9