Search in sources :

Example 1 with SelectionProcessor

use of org.eclipse.jface.internal.text.SelectionProcessor 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 2 with SelectionProcessor

use of org.eclipse.jface.internal.text.SelectionProcessor in project eclipse.platform.text by eclipse.

the class TextViewer method delete.

private void delete() {
    if (!fTextWidget.getBlockSelection()) {
        fTextWidget.invokeAction(ST.DELETE_NEXT);
    } else {
        wrapCompoundChange(new Runnable() {

            @Override
            public void run() {
                try {
                    new SelectionProcessor(TextViewer.this).doDelete(getSelection());
                } catch (BadLocationException e) {
                    if (TRACE_ERRORS)
                        // $NON-NLS-1$
                        System.out.println(JFaceTextMessages.getString("TextViewer.error.bad_location.delete"));
                }
            }
        });
    }
    Point selection = fTextWidget.getSelectionRange();
    fireSelectionChanged(selection.x, selection.y);
}
Also used : SelectionProcessor(org.eclipse.jface.internal.text.SelectionProcessor) Point(org.eclipse.swt.graphics.Point)

Example 3 with SelectionProcessor

use of org.eclipse.jface.internal.text.SelectionProcessor in project eclipse.platform.text by eclipse.

the class TextViewer method verifyEventInBlockSelection.

/**
 * Simulates typing behavior in block selection mode.
 *
 * @param e the verify event.
 * @since 3.5
 */
private void verifyEventInBlockSelection(final VerifyEvent e) {
    /*
		 Implementation Note: StyledText sends a sequence of n events
		 for a single character typed, where n is the number of affected lines. Since
		 the events share no manifest attribute to group them together or to detect the last event
		 of a sequence, we simulate the modification at the first event and veto any following
		 events with an equal event time.

		 See also bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=268044
		 */
    e.doit = false;
    boolean isFirst = e.time != fLastEventTime;
    fLastEventTime = e.time;
    if (isFirst) {
        wrapCompoundChange(new Runnable() {

            @Override
            public void run() {
                SelectionProcessor processor = new SelectionProcessor(TextViewer.this);
                try {
                    /* Use the selection instead of the event's coordinates. Is this dangerous? */
                    ISelection selection = getSelection();
                    int length = e.text.length();
                    if (length == 0 && e.character == '\0') {
                        // backspace in StyledText block selection mode...
                        TextEdit edit = processor.backspace(selection);
                        edit.apply(fDocument, TextEdit.UPDATE_REGIONS);
                        ISelection empty = processor.makeEmpty(selection, true);
                        setSelection(empty);
                    } else {
                        int lines = processor.getCoveredLines(selection);
                        String delim = fDocument.getLegalLineDelimiters()[0];
                        StringBuilder text = new StringBuilder(lines * length + (lines - 1) * delim.length());
                        text.append(e.text);
                        for (int i = 0; i < lines - 1; i++) {
                            text.append(delim);
                            text.append(e.text);
                        }
                        processor.doReplace(selection, text.toString());
                    }
                } catch (BadLocationException x) {
                    if (TRACE_ERRORS)
                        // $NON-NLS-1$
                        System.out.println(JFaceTextMessages.getString("TextViewer.error.bad_location.verifyText"));
                }
            }
        });
    }
}
Also used : SelectionProcessor(org.eclipse.jface.internal.text.SelectionProcessor) TextEdit(org.eclipse.text.edits.TextEdit) ISelection(org.eclipse.jface.viewers.ISelection)

Aggregations

SelectionProcessor (org.eclipse.jface.internal.text.SelectionProcessor)3 ISelection (org.eclipse.jface.viewers.ISelection)2 Point (org.eclipse.swt.graphics.Point)2 Clipboard (org.eclipse.swt.dnd.Clipboard)1 TextTransfer (org.eclipse.swt.dnd.TextTransfer)1 TextEdit (org.eclipse.text.edits.TextEdit)1