Example usage for org.eclipse.swt.custom StyledText getSelection

List of usage examples for org.eclipse.swt.custom StyledText getSelection

Introduction

In this page you can find the example usage for org.eclipse.swt.custom StyledText getSelection.

Prototype

public Point getSelection() 

Source Link

Document

Returns the selection.

Usage

From source file:DragTextInStyledText.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    int style = SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
    final StyledText text1 = new StyledText(shell, style);
    text1.setText(string1);/*  ww  w.  j  a v  a2  s.c  o m*/
    DragSource source = new DragSource(text1, DND.DROP_COPY | DND.DROP_MOVE);
    source.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    source.addDragListener(new DragSourceAdapter() {
        Point selection;

        public void dragStart(DragSourceEvent e) {
            selection = text1.getSelection();
            e.doit = selection.x != selection.y;
        }

        public void dragSetData(DragSourceEvent e) {
            e.data = text1.getText(selection.x, selection.y - 1);
        }

        public void dragFinished(DragSourceEvent e) {
            if (e.detail == DND.DROP_MOVE) {
                text1.replaceTextRange(selection.x, selection.y - selection.x, "");
            }
            selection = null;
        }
    });

    final StyledText text2 = new StyledText(shell, style);
    text2.setText(string2);
    DropTarget target = new DropTarget(text2, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
    target.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    target.addDropListener(new DropTargetAdapter() {
        public void dragEnter(DropTargetEvent e) {
            if (e.detail == DND.DROP_DEFAULT)
                e.detail = DND.DROP_COPY;
        }

        public void dragOperationChanged(DropTargetEvent e) {
            if (e.detail == DND.DROP_DEFAULT)
                e.detail = DND.DROP_COPY;
        }

        public void drop(DropTargetEvent e) {
            text2.insert((String) e.data);
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet210.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 210");
    shell.setLayout(new FillLayout());
    int style = SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
    final StyledText text1 = new StyledText(shell, style);
    text1.setText(string1);/*from  ww w  .  j  a v  a 2 s  .  c o m*/
    DragSource source = new DragSource(text1, DND.DROP_COPY | DND.DROP_MOVE);
    source.setTransfer(TextTransfer.getInstance());
    source.addDragListener(new DragSourceAdapter() {
        Point selection;

        @Override
        public void dragStart(DragSourceEvent e) {
            selection = text1.getSelection();
            e.doit = selection.x != selection.y;
        }

        @Override
        public void dragSetData(DragSourceEvent e) {
            e.data = text1.getText(selection.x, selection.y - 1);
        }

        @Override
        public void dragFinished(DragSourceEvent e) {
            if (e.detail == DND.DROP_MOVE) {
                text1.replaceTextRange(selection.x, selection.y - selection.x, "");
            }
            selection = null;
        }
    });

    final StyledText text2 = new StyledText(shell, style);
    text2.setText(string2);
    DropTarget target = new DropTarget(text2, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
    target.setTransfer(TextTransfer.getInstance());
    target.addDropListener(new DropTargetAdapter() {
        @Override
        public void dragEnter(DropTargetEvent e) {
            if (e.detail == DND.DROP_DEFAULT)
                e.detail = DND.DROP_COPY;
        }

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

        @Override
        public void drop(DropTargetEvent e) {
            text2.insert((String) e.data);
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet257.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 257");
    shell.setLayout(new FillLayout());
    shell.setSize(100, 300);/*from  w ww  .ja  va  2  s . c  om*/
    int style = SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
    final StyledText text = new StyledText(shell, style);
    text.setText(string1);
    final DragSource source = new DragSource(text, DND.DROP_COPY | DND.DROP_MOVE);
    source.setDragSourceEffect(new DragSourceEffect(text) {
        @Override
        public void dragStart(DragSourceEvent event) {
            event.image = display.getSystemImage(SWT.ICON_WARNING);
        }
    });
    source.setTransfer(TextTransfer.getInstance());
    source.addDragListener(new DragSourceAdapter() {
        Point selection;

        @Override
        public void dragStart(DragSourceEvent event) {
            selection = text.getSelection();
            event.doit = selection.x != selection.y;
            text.setData(DRAG_START_DATA, selection);
        }

        @Override
        public void dragSetData(DragSourceEvent e) {
            e.data = text.getText(selection.x, selection.y - 1);
        }

        @Override
        public void dragFinished(DragSourceEvent event) {
            if (event.detail == DND.DROP_MOVE) {
                Point newSelection = text.getSelection();
                int length = selection.y - selection.x;
                int delta = 0;
                if (newSelection.x < selection.x)
                    delta = length;
                text.replaceTextRange(selection.x + delta, length, "");
            }
            selection = null;
            text.setData(DRAG_START_DATA, null);
        }
    });

    DropTarget target = new DropTarget(text, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
    target.setTransfer(TextTransfer.getInstance());
    target.addDropListener(new DropTargetAdapter() {
        @Override
        public void dragEnter(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT) {
                if (text.getData(DRAG_START_DATA) == null)
                    event.detail = DND.DROP_COPY;
                else
                    event.detail = DND.DROP_MOVE;
            }
        }

        @Override
        public void dragOperationChanged(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT) {
                if (text.getData(DRAG_START_DATA) == null)
                    event.detail = DND.DROP_COPY;
                else
                    event.detail = DND.DROP_MOVE;
            }
        }

        @Override
        public void dragOver(DropTargetEvent event) {
            event.feedback = DND.FEEDBACK_SCROLL | DND.FEEDBACK_SELECT;
        }

        @Override
        public void drop(DropTargetEvent event) {
            if (event.detail != DND.DROP_NONE) {
                Point selection = (Point) text.getData(DRAG_START_DATA);
                int insertPos = text.getCaretOffset();
                if (event.detail == DND.DROP_MOVE && selection != null && selection.x <= insertPos
                        && insertPos <= selection.y
                        || event.detail == DND.DROP_COPY && selection != null && selection.x < insertPos
                                && insertPos < selection.y) {
                    text.setSelection(selection);
                    event.detail = DND.DROP_COPY; // prevent source from deleting selection
                } else {
                    String string = (String) event.data;
                    text.insert(string);
                    if (selection != null)
                        text.setSelectionRange(insertPos, string.length());
                }
            }
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}