Search in sources :

Example 21 with TextTransfer

use of org.eclipse.swt.dnd.TextTransfer in project eclipse.platform.text by eclipse.

the class TextViewer method paste.

private void paste() {
    // ignoreAutoEditStrategies(true);
    if (!fTextWidget.getBlockSelection()) {
        fTextWidget.paste();
    } else {
        wrapCompoundChange(new Runnable() {

            @Override
            public void run() {
                SelectionProcessor processor = new SelectionProcessor(TextViewer.this);
                Clipboard clipboard = new Clipboard(getDisplay());
                try {
                    /*
						 * Paste in block selection mode. If the pasted text is not a multi-line
						 * text, pasting behaves like typing, i.e. the pasted text replaces
						 * the selection on each line. If the pasted text is multi-line (e.g. from
						 * copying a column selection), the selection is replaced, line-by-line, by
						 * the corresponding contents of the pasted text. If the selection touches
						 * more lines than the pasted text, the selection on the remaining lines
						 * is deleted (assuming an empty text being pasted). If the pasted
						 * text contains more lines than the selection, the selection is extended
						 * to the succeeding lines, or more lines are added to accommodate the
						 * paste operation.
						 */
                    ISelection selection = getSelection();
                    TextTransfer plainTextTransfer = TextTransfer.getInstance();
                    String contents = (String) clipboard.getContents(plainTextTransfer, DND.CLIPBOARD);
                    String toInsert;
                    if (TextUtilities.indexOf(fDocument.getLegalLineDelimiters(), contents, 0)[0] != -1) {
                        // multi-line insertion
                        toInsert = contents;
                    } else {
                        // single-line insertion
                        int length = contents.length();
                        int lines = processor.getCoveredLines(selection);
                        String delim = fDocument.getLegalLineDelimiters()[0];
                        StringBuilder text = new StringBuilder(lines * length + (lines - 1) * delim.length());
                        text.append(contents);
                        for (int i = 0; i < lines - 1; i++) {
                            text.append(delim);
                            text.append(contents);
                        }
                        toInsert = text.toString();
                    }
                    processor.doReplace(selection, toInsert);
                } catch (BadLocationException x) {
                    if (TRACE_ERRORS)
                        // $NON-NLS-1$
                        System.out.println(JFaceTextMessages.getString("TextViewer.error.bad_location.paste"));
                } finally {
                    clipboard.dispose();
                }
            }
        });
    }
    Point selection = fTextWidget.getSelectionRange();
    fireSelectionChanged(selection.x, selection.y);
// ignoreAutoEditStrategies(false);
}
Also used : SelectionProcessor(org.eclipse.jface.internal.text.SelectionProcessor) ISelection(org.eclipse.jface.viewers.ISelection) Clipboard(org.eclipse.swt.dnd.Clipboard) Point(org.eclipse.swt.graphics.Point) TextTransfer(org.eclipse.swt.dnd.TextTransfer)

Example 22 with TextTransfer

use of org.eclipse.swt.dnd.TextTransfer 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 23 with TextTransfer

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

the class Test_org_eclipse_swt_custom_StyledText method test_paste.

@Test
public void test_paste() {
    if (SwtTestUtil.isCocoa) {
        // TODO Fix Cocoa failure.
        if (SwtTestUtil.verbose) {
            System.out.println("Excluded test_paste(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_custom_StyledText).");
        }
        return;
    }
    Clipboard clipboard = new Clipboard(text.getDisplay());
    TextTransfer transfer = TextTransfer.getInstance();
    String convertedText;
    clipboard.setContents(new String[] { "x" }, new Transfer[] { transfer });
    text.copy();
    text.paste();
    assertTrue(":a:", text.getCharCount() == 1);
    text.setSelectionRange(0, 0);
    text.copy();
    text.paste();
    assertTrue(":b:", text.getCharCount() == 2);
    text.setText("0123456789");
    text.setSelectionRange(0, 1);
    text.copy();
    text.setCaretOffset(0);
    text.paste();
    assertTrue(":c:", text.getText().equals("00123456789"));
    text.setSelectionRange(1, 2);
    text.copy();
    text.setText("");
    text.paste();
    assertTrue(":d:", text.getText().equals("01"));
    text.setText("");
    // test line delimiter conversion
    clipboard.setContents(new String[] { "\rLine1\nLine2\r\nLine3\n\rLine4\n" }, new Transfer[] { transfer });
    text.paste();
    if (SwtTestUtil.isWindowsOS) {
        convertedText = "\r\nLine1\r\nLine2\r\nLine3\r\n\r\nLine4\r\n";
    } else {
        convertedText = "\nLine1\nLine2\nLine3\n\nLine4\n";
    }
    assertTrue(":f:", text.getText() != null && text.getText().equals(convertedText));
    text.setText("");
    // test line delimiter conversion
    clipboard.setContents(new String[] { "Line1\r\nLine2" }, new Transfer[] { transfer });
    text.paste();
    if (SwtTestUtil.isWindowsOS) {
        convertedText = "Line1\r\nLine2";
    } else {
        convertedText = "Line1\nLine2";
    }
    assertTrue(":g:", text.getText() != null && text.getText().equals(convertedText));
    text.setText("");
    // test line delimiter conversion
    clipboard.setContents(new String[] { "Line1\rLine2" }, new Transfer[] { transfer });
    text.paste();
    if (SwtTestUtil.isWindowsOS) {
        convertedText = "Line1\r\nLine2";
    } else {
        convertedText = "Line1\nLine2";
    }
    assertTrue(":h:", text.getText() != null && text.getText().equals(convertedText));
    text.setText("");
    // test line delimiter conversion
    clipboard.setContents(new String[] { "Line1\nLine2" }, new Transfer[] { transfer });
    text.paste();
    if (SwtTestUtil.isWindowsOS) {
        convertedText = "Line1\r\nLine2";
    } else {
        convertedText = "Line1\nLine2";
    }
    assertTrue(":i:", text.getText() != null && text.getText().equals(convertedText));
    text.setText("");
    // test paste with text limit with no selection
    clipboard.setContents(new String[] { "abcde" }, new Transfer[] { transfer });
    text.setTextLimit(3);
    text.copy();
    text.paste();
    assertTrue(":j:", text.getText() != null && text.getText().equals("abc"));
    text.setText("");
    // test paste with text limit with full selection
    clipboard.setContents(new String[] { "abcde" }, new Transfer[] { transfer });
    text.setTextLimit(3);
    text.setText("123");
    text.setSelection(0, 3);
    text.paste();
    assertTrue(":k:", text.getText() != null && text.getText().equals("abc"));
    text.setText("");
    // test paste with text limit with partial selection
    clipboard.setContents(new String[] { "abcde" }, new Transfer[] { transfer });
    text.setTextLimit(3);
    text.setText("123");
    text.setSelection(0, 1);
    text.paste();
    assertTrue(":l:", text.getText() != null && text.getText().equals("a23"));
    text.setText("");
    clipboard.dispose();
}
Also used : Clipboard(org.eclipse.swt.dnd.Clipboard) TextTransfer(org.eclipse.swt.dnd.TextTransfer) Test(org.junit.Test)

Example 24 with TextTransfer

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

the class Test_org_eclipse_swt_custom_StyledText method test_copy.

@Test
public void test_copy() {
    if (SwtTestUtil.isCocoa) {
        // TODO Fix Cocoa failure.
        if (SwtTestUtil.verbose) {
            System.out.println("Excluded test_copy(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_custom_StyledText).");
        }
        return;
    }
    Clipboard clipboard = new Clipboard(text.getDisplay());
    TextTransfer transfer = TextTransfer.getInstance();
    String clipboardText;
    String convertedText;
    String before = (String) clipboard.getContents(transfer);
    text.setSelectionRange(0, 0);
    text.copy();
    clipboardText = (String) clipboard.getContents(transfer);
    assertTrue(":a:", before == null ? clipboardText == null : before.equals(clipboardText));
    before = (String) clipboard.getContents(transfer);
    text.setText("0123456789");
    text.setSelectionRange(0, 0);
    text.copy();
    clipboardText = (String) clipboard.getContents(transfer);
    assertTrue(":c:", before == null ? clipboardText == null : before.equals(clipboardText));
    text.setSelectionRange(0, 1);
    text.copy();
    clipboardText = (String) clipboard.getContents(transfer);
    assertTrue(":d:", clipboardText != null && clipboardText.equals("0"));
    text.setSelectionRange(1, 2);
    text.copy();
    clipboardText = (String) clipboard.getContents(transfer);
    assertTrue(":e:", clipboardText != null && clipboardText.equals("12"));
    // test line delimiter conversion
    text.setText("\rLine1\nLine2\r\nLine3\n\rLine4\n");
    text.setSelectionRange(0, text.getCharCount());
    text.copy();
    clipboardText = (String) clipboard.getContents(transfer);
    if (SwtTestUtil.isWindowsOS) {
        convertedText = "\r\nLine1\r\nLine2\r\nLine3\r\n\r\nLine4\r\n";
    } else {
        convertedText = "\nLine1\nLine2\nLine3\n\nLine4\n";
    }
    assertTrue(":f:", clipboardText != null && clipboardText.equals(convertedText));
    // test line delimiter conversion
    text.setText("Line1\r\nLine2");
    text.setSelectionRange(0, text.getCharCount());
    text.copy();
    clipboardText = (String) clipboard.getContents(transfer);
    if (SwtTestUtil.isWindowsOS) {
        convertedText = "Line1\r\nLine2";
    } else {
        convertedText = "Line1\nLine2";
    }
    assertTrue(":g:", clipboardText != null && clipboardText.equals(convertedText));
    rtfCopy();
    clipboard.dispose();
}
Also used : Clipboard(org.eclipse.swt.dnd.Clipboard) TextTransfer(org.eclipse.swt.dnd.TextTransfer) Test(org.junit.Test)

Example 25 with TextTransfer

use of org.eclipse.swt.dnd.TextTransfer in project yamcs-studio by yamcs.

the class CommandClipboard method textToClipboard.

private static void textToClipboard(String text, Display display) {
    final Clipboard cb = new Clipboard(display);
    TextTransfer textTransfer = TextTransfer.getInstance();
    cb.setContents(new Object[] { text }, new Transfer[] { textTransfer });
}
Also used : Clipboard(org.eclipse.swt.dnd.Clipboard) TextTransfer(org.eclipse.swt.dnd.TextTransfer)

Aggregations

TextTransfer (org.eclipse.swt.dnd.TextTransfer)77 Clipboard (org.eclipse.swt.dnd.Clipboard)65 Point (org.eclipse.swt.graphics.Point)19 Transfer (org.eclipse.swt.dnd.Transfer)18 TableItem (org.eclipse.swt.widgets.TableItem)14 StyledText (org.eclipse.swt.custom.StyledText)10 Control (org.eclipse.swt.widgets.Control)8 Text (org.eclipse.swt.widgets.Text)6 IAction (org.eclipse.jface.action.IAction)5 HTMLTransfer (org.eclipse.swt.dnd.HTMLTransfer)5 SelectionEvent (org.eclipse.swt.events.SelectionEvent)5 Button (org.eclipse.swt.widgets.Button)5 Composite (org.eclipse.swt.widgets.Composite)5 ArrayList (java.util.ArrayList)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 DragSourceEvent (org.eclipse.swt.dnd.DragSourceEvent)4 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)4 GridData (org.eclipse.swt.layout.GridData)4 GridLayout (org.eclipse.swt.layout.GridLayout)4 Label (org.eclipse.swt.widgets.Label)4