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

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

Introduction

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

Prototype

public int getCaretOffset() 

Source Link

Document

Returns the caret position relative to the start of the text.

Usage

From source file:StyledTextStatistics.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);

    StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);

    styledText.setText("12345");

    System.out.println("Caret Offset: " + styledText.getCaretOffset());
    System.out.println("Total Lines of Text: " + styledText.getLineCount());
    System.out.println("Total Characters: " + styledText.getCharCount());
    System.out.println("Current Line: " + (styledText.getLineAtOffset(styledText.getCaretOffset()) + 1));

    styledText.setBounds(10, 10, 100, 100);
    shell.open();// w  w w  .  j a  va2 s.  c  o m
    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  w  w . j  av  a2s .  co  m
    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();
}