Search in sources :

Example 1 with FileTransfer

use of org.eclipse.swt.dnd.FileTransfer in project translationstudio8 by heartsome.

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 
     */
protected boolean updateSelection(IStructuredSelection selection) {
    if (!super.updateSelection(selection)) {
        return false;
    }
    final IResource[][] clipboardData = new IResource[1][];
    shell.getDisplay().syncExec(new Runnable() {

        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 selectedResources = getSelectedResources();
    if (selectedResources.size() > 1) {
        for (int i = 0; i < selectedResources.size(); i++) {
            IResource resource = (IResource) 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 && targetResource.getType() != IResource.FOLDER) {
            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) TransferData(org.eclipse.swt.dnd.TransferData) FileTransfer(org.eclipse.swt.dnd.FileTransfer) List(java.util.List) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject)

Example 2 with FileTransfer

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

the class DNDExample method createDragSource.

private void createDragSource() {
    if (dragSource != null)
        dragSource.dispose();
    dragSource = new DragSource(dragControl, dragOperation);
    dragSource.setTransfer(dragTypes);
    dragSource.addDragListener(new DragSourceListener() {

        @Override
        public void dragFinished(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragFinished\n");
            printEvent(event);
            dragDataText = dragDataRTF = dragDataHTML = dragDataURL = null;
            dragDataFiles = null;
            if (event.detail == DND.DROP_MOVE) {
                switch(dragControlType) {
                    case BUTTON_CHECK:
                    case BUTTON_TOGGLE:
                    case BUTTON_RADIO:
                        {
                            Button b = (Button) dragControl;
                            b.setText("");
                            break;
                        }
                    case STYLED_TEXT:
                        {
                            StyledText text = (StyledText) dragControl;
                            text.insert("");
                            break;
                        }
                    case TABLE:
                        {
                            Table table = (Table) dragControl;
                            TableItem[] items = table.getSelection();
                            for (TableItem item : items) {
                                item.dispose();
                            }
                            break;
                        }
                    case TEXT:
                        {
                            Text text = (Text) dragControl;
                            text.insert("");
                            break;
                        }
                    case TREE:
                        {
                            Tree tree = (Tree) dragControl;
                            TreeItem[] items = tree.getSelection();
                            for (TreeItem item : items) {
                                item.dispose();
                            }
                            break;
                        }
                    case CANVAS:
                        {
                            dragControl.setData("STRINGS", null);
                            dragControl.redraw();
                            break;
                        }
                    case LABEL:
                        {
                            Label label = (Label) dragControl;
                            label.setText("");
                            break;
                        }
                    case LIST:
                        {
                            List list = (List) dragControl;
                            int[] indices = list.getSelectionIndices();
                            list.remove(indices);
                            break;
                        }
                    case COMBO:
                        {
                            Combo combo = (Combo) dragControl;
                            combo.setText("");
                            break;
                        }
                }
            }
        }

        @Override
        public void dragSetData(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragSetData\n");
            printEvent(event);
            if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataText;
            }
            if (RTFTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataRTF;
            }
            if (HTMLTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataHTML;
            }
            if (URLTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataURL;
            }
            if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataFiles;
            }
        }

        @Override
        public void dragStart(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragStart\n");
            printEvent(event);
            dragDataFiles = fileList.getItems();
            switch(dragControlType) {
                case BUTTON_CHECK:
                case BUTTON_TOGGLE:
                case BUTTON_RADIO:
                    {
                        Button b = (Button) dragControl;
                        dragDataText = b.getSelection() ? "true" : "false";
                        break;
                    }
                case STYLED_TEXT:
                    {
                        StyledText text = (StyledText) dragControl;
                        String s = text.getSelectionText();
                        if (s.length() == 0) {
                            event.doit = false;
                        } else {
                            dragDataText = s;
                        }
                        break;
                    }
                case TABLE:
                    {
                        Table table = (Table) dragControl;
                        TableItem[] items = table.getSelection();
                        if (items.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < items.length; i++) {
                                buffer.append(items[i].getText());
                                if (items.length > 1 && i < items.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case TEXT:
                    {
                        Text text = (Text) dragControl;
                        String s = text.getSelectionText();
                        if (s.length() == 0) {
                            event.doit = false;
                        } else {
                            dragDataText = s;
                        }
                        break;
                    }
                case TREE:
                    {
                        Tree tree = (Tree) dragControl;
                        TreeItem[] items = tree.getSelection();
                        if (items.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < items.length; i++) {
                                buffer.append(items[i].getText());
                                if (items.length > 1 && i < items.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case CANVAS:
                    {
                        String[] strings = (String[]) dragControl.getData("STRINGS");
                        if (strings == null || strings.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < strings.length; i++) {
                                buffer.append(strings[i]);
                                if (strings.length > 1 && i < strings.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case LABEL:
                    {
                        Label label = (Label) dragControl;
                        String string = label.getText();
                        if (string.length() == 0) {
                            event.doit = false;
                        } else {
                            dragDataText = string;
                        }
                        break;
                    }
                case LIST:
                    {
                        List list = (List) dragControl;
                        String[] selection = list.getSelection();
                        if (selection.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < selection.length; i++) {
                                buffer.append(selection[i]);
                                if (selection.length > 1 && i < selection.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case COMBO:
                    {
                        Combo combo = (Combo) dragControl;
                        String string = combo.getText();
                        Point selection = combo.getSelection();
                        if (selection.x == selection.y) {
                            event.doit = false;
                        } else {
                            dragDataText = string.substring(selection.x, selection.y);
                        }
                        break;
                    }
                default:
                    throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED);
            }
            if (dragDataText != null) {
                dragDataRTF = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\cf1\\b " + dragDataText + "}";
                dragDataHTML = "<b>" + dragDataText + "</b>";
                dragDataURL = "http://" + dragDataText.replace(' ', '.');
                try {
                    new URL(dragDataURL);
                } catch (MalformedURLException e) {
                    dragDataURL = null;
                }
            }
            for (Transfer dragType : dragTypes) {
                if (dragType instanceof TextTransfer && dragDataText == null) {
                    event.doit = false;
                }
                if (dragType instanceof RTFTransfer && dragDataRTF == null) {
                    event.doit = false;
                }
                if (dragType instanceof HTMLTransfer && dragDataHTML == null) {
                    event.doit = false;
                }
                if (dragType instanceof URLTransfer && dragDataURL == null) {
                    event.doit = false;
                }
                if (dragType instanceof FileTransfer && (dragDataFiles == null || dragDataFiles.length == 0)) {
                    event.doit = false;
                }
            }
        }
    });
}
Also used : MalformedURLException(java.net.MalformedURLException) TreeItem(org.eclipse.swt.widgets.TreeItem) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) Combo(org.eclipse.swt.widgets.Combo) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) URL(java.net.URL) Button(org.eclipse.swt.widgets.Button) Tree(org.eclipse.swt.widgets.Tree) List(org.eclipse.swt.widgets.List) SWTError(org.eclipse.swt.SWTError) StyledText(org.eclipse.swt.custom.StyledText) Table(org.eclipse.swt.widgets.Table) URLTransfer(org.eclipse.swt.dnd.URLTransfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) StyledText(org.eclipse.swt.custom.StyledText) Text(org.eclipse.swt.widgets.Text) HTMLTransfer(org.eclipse.swt.dnd.HTMLTransfer) DragSource(org.eclipse.swt.dnd.DragSource) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) URLTransfer(org.eclipse.swt.dnd.URLTransfer) Transfer(org.eclipse.swt.dnd.Transfer) HTMLTransfer(org.eclipse.swt.dnd.HTMLTransfer) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) TextTransfer(org.eclipse.swt.dnd.TextTransfer)

Example 3 with FileTransfer

use of org.eclipse.swt.dnd.FileTransfer in project translationstudio8 by heartsome.

the class PasteAction method run.

/**
     * Implementation of method defined on <code>IAction</code>.
     */
public void run() {
    // try a resource transfer
    ResourceTransfer resTransfer = ResourceTransfer.getInstance();
    IResource[] resourceData = (IResource[]) clipboard.getContents(resTransfer);
    if (resourceData != null && resourceData.length > 0) {
        if (resourceData[0].getType() == IResource.PROJECT) {
            // enablement checks for all projects
            for (int i = 0; i < resourceData.length; i++) {
                CopyProjectOperation operation = new CopyProjectOperation(this.shell);
                operation.copyProject((IProject) resourceData[i]);
            }
        } else {
            // enablement should ensure that we always have access to a container
            IContainer container = getContainer();
            CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(this.shell);
            operation.copyResources(resourceData, container);
        }
        return;
    }
    // try a file transfer
    FileTransfer fileTransfer = FileTransfer.getInstance();
    String[] fileData = (String[]) clipboard.getContents(fileTransfer);
    if (fileData != null) {
        // enablement should ensure that we always have access to a container
        IContainer container = getContainer();
        CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(this.shell);
        operation.copyFiles(fileData, container);
    }
}
Also used : ResourceTransfer(org.eclipse.ui.part.ResourceTransfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) CopyProjectOperation(org.eclipse.ui.actions.CopyProjectOperation) IContainer(org.eclipse.core.resources.IContainer) CopyFilesAndFoldersOperation(org.eclipse.ui.actions.CopyFilesAndFoldersOperation) IResource(org.eclipse.core.resources.IResource)

Example 4 with FileTransfer

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

the class ClipboardExample method createFileTransfer.

void createFileTransfer(Composite copyParent, Composite pasteParent) {
    // File Transfer
    Label l = new Label(copyParent, SWT.NONE);
    // $NON-NLS-1$
    l.setText("FileTransfer:");
    GridData data = new GridData();
    data.verticalSpan = 3;
    l.setLayoutData(data);
    final Table copyFileTable = new Table(copyParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    data = new GridData(GridData.FILL_BOTH);
    data.widthHint = HSIZE;
    data.heightHint = VSIZE;
    data.verticalSpan = 3;
    copyFileTable.setLayoutData(data);
    Button b = new Button(copyParent, SWT.PUSH);
    b.setText("Select file(s)");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
        String result = dialog.open();
        if (result != null && result.length() > 0) {
            String path = dialog.getFilterPath();
            String[] names = dialog.getFileNames();
            for (String name : names) {
                TableItem item = new TableItem(copyFileTable, SWT.NONE);
                item.setText(path + File.separator + name);
            }
        }
    }));
    b = new Button(copyParent, SWT.PUSH);
    b.setText("Select directory");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        DirectoryDialog dialog = new DirectoryDialog(shell, SWT.OPEN);
        String result = dialog.open();
        if (result != null && result.length() > 0) {
            // copyFileTable.removeAll();
            TableItem item = new TableItem(copyFileTable, SWT.NONE);
            item.setText(result);
        }
    }));
    b = new Button(copyParent, SWT.PUSH);
    b.setText("Copy");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        TableItem[] items = copyFileTable.getItems();
        if (items.length > 0) {
            status.setText("");
            String[] itemsData = new String[items.length];
            for (int i = 0; i < itemsData.length; i++) {
                itemsData[i] = items[i].getText();
            }
            clipboard.setContents(new Object[] { itemsData }, new Transfer[] { FileTransfer.getInstance() });
        } else {
            status.setText("No file to copy");
        }
    }));
    l = new Label(pasteParent, SWT.NONE);
    // $NON-NLS-1$
    l.setText("FileTransfer:");
    final Table pasteFileTable = new Table(pasteParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    data = new GridData(GridData.FILL_BOTH);
    data.widthHint = HSIZE;
    data.heightHint = VSIZE;
    pasteFileTable.setLayoutData(data);
    b = new Button(pasteParent, SWT.PUSH);
    b.setText("Paste");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        String[] textData = (String[]) clipboard.getContents(FileTransfer.getInstance());
        if (textData != null && textData.length > 0) {
            status.setText("");
            pasteFileTable.removeAll();
            for (String element : textData) {
                TableItem item = new TableItem(pasteFileTable, SWT.NONE);
                item.setText(element);
            }
        } else {
            status.setText("No file to paste");
        }
    }));
}
Also used : RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) StyledText(org.eclipse.swt.custom.StyledText) Image(org.eclipse.swt.graphics.Image) Rectangle(org.eclipse.swt.graphics.Rectangle) ImageData(org.eclipse.swt.graphics.ImageData) Table(org.eclipse.swt.widgets.Table) GC(org.eclipse.swt.graphics.GC) Point(org.eclipse.swt.graphics.Point) Clipboard(org.eclipse.swt.dnd.Clipboard) ImageDataProvider(org.eclipse.swt.graphics.ImageDataProvider) SelectionListener.widgetSelectedAdapter(org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter) ImageTransfer(org.eclipse.swt.dnd.ImageTransfer) Composite(org.eclipse.swt.widgets.Composite) TextTransfer(org.eclipse.swt.dnd.TextTransfer) Canvas(org.eclipse.swt.widgets.Canvas) GridData(org.eclipse.swt.layout.GridData) FillLayout(org.eclipse.swt.layout.FillLayout) FileTransfer(org.eclipse.swt.dnd.FileTransfer) TableItem(org.eclipse.swt.widgets.TableItem) Text(org.eclipse.swt.widgets.Text) Combo(org.eclipse.swt.widgets.Combo) Shell(org.eclipse.swt.widgets.Shell) Button(org.eclipse.swt.widgets.Button) FileDialog(org.eclipse.swt.widgets.FileDialog) Display(org.eclipse.swt.widgets.Display) Group(org.eclipse.swt.widgets.Group) File(java.io.File) Transfer(org.eclipse.swt.dnd.Transfer) SWT(org.eclipse.swt.SWT) DirectoryDialog(org.eclipse.swt.widgets.DirectoryDialog) HTMLTransfer(org.eclipse.swt.dnd.HTMLTransfer) List(org.eclipse.swt.widgets.List) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) ScrollBar(org.eclipse.swt.widgets.ScrollBar) Label(org.eclipse.swt.widgets.Label) GridLayout(org.eclipse.swt.layout.GridLayout) Table(org.eclipse.swt.widgets.Table) Button(org.eclipse.swt.widgets.Button) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) GridData(org.eclipse.swt.layout.GridData) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) ImageTransfer(org.eclipse.swt.dnd.ImageTransfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) Transfer(org.eclipse.swt.dnd.Transfer) HTMLTransfer(org.eclipse.swt.dnd.HTMLTransfer) FileDialog(org.eclipse.swt.widgets.FileDialog) DirectoryDialog(org.eclipse.swt.widgets.DirectoryDialog)

Example 5 with FileTransfer

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

the class ExtendedEditorDropTargetAdapter method doDrop.

protected boolean doDrop(Transfer transfer, DropTargetEvent event) {
    TransferBuilder tb = new TransferBuilder(useProxy);
    IDropAction[] as = null;
    if (editorIds != null && editorIds.length > 0)
        as = tb.getDropActions(editorIds, transfer);
    else
        as = tb.getDropActions(getTargetEditor().getClass().getName(), transfer);
    for (int i = 0; i < as.length; ++i) {
        IDropAction da = as[i];
        Transfer actualTransfer;
        if (transfer instanceof TransferProxyForDelayLoading) {
            actualTransfer = ((TransferProxyForDelayLoading) transfer).getTransferClass();
        } else {
            actualTransfer = transfer;
        }
        if (actualTransfer instanceof FileTransfer) {
            if (event.data == null) {
                // $NON-NLS-1$
                Logger.log(Logger.ERROR, "No data in DropTargetEvent from " + event.widget);
                return false;
            }
            String[] strs = (String[]) event.data;
            boolean[] bs = new boolean[strs.length];
            int c = 0;
            for (int j = 0; j < strs.length; ++j) {
                bs[j] = false;
                if (da.isSupportedData(strs[j])) {
                    event.data = new String[] { strs[j] };
                    if (!da.run(event, targetEditor)) {
                        bs[j] = true;
                        c++;
                    }
                } else {
                    bs[j] = true;
                    c++;
                }
            }
            if (c == 0) {
                return true;
            }
            int k = 0;
            String[] rests = new String[c];
            for (int j = 0; j < strs.length; ++j) {
                if (bs[j])
                    rests[k++] = strs[j];
            }
            event.data = rests;
        } else if (da.isSupportedData(event.data)) {
            if (da.run(event, targetEditor)) {
                return true;
            }
        }
    }
    return false;
}
Also used : FileTransfer(org.eclipse.swt.dnd.FileTransfer) Transfer(org.eclipse.swt.dnd.Transfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) TransferProxyForDelayLoading(org.eclipse.wst.sse.ui.internal.TransferBuilder.TransferProxyForDelayLoading) Point(org.eclipse.swt.graphics.Point)

Aggregations

FileTransfer (org.eclipse.swt.dnd.FileTransfer)6 Transfer (org.eclipse.swt.dnd.Transfer)4 Point (org.eclipse.swt.graphics.Point)3 MalformedURLException (java.net.MalformedURLException)2 IResource (org.eclipse.core.resources.IResource)2 StyledText (org.eclipse.swt.custom.StyledText)2 HTMLTransfer (org.eclipse.swt.dnd.HTMLTransfer)2 ImageTransfer (org.eclipse.swt.dnd.ImageTransfer)2 RTFTransfer (org.eclipse.swt.dnd.RTFTransfer)2 TextTransfer (org.eclipse.swt.dnd.TextTransfer)2 GridData (org.eclipse.swt.layout.GridData)2 Button (org.eclipse.swt.widgets.Button)2 Combo (org.eclipse.swt.widgets.Combo)2 Composite (org.eclipse.swt.widgets.Composite)2 File (java.io.File)1 URL (java.net.URL)1 List (java.util.List)1 FileObject (org.apache.commons.vfs2.FileObject)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1 IContainer (org.eclipse.core.resources.IContainer)1