Example usage for org.eclipse.swt.widgets Composite setFocus

List of usage examples for org.eclipse.swt.widgets Composite setFocus

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Composite setFocus.

Prototype

@Override
    public boolean setFocus() 

Source Link

Usage

From source file:org.gumtree.vis.plot1d.Plot1D.java

private void createStatusBar() {
    Composite statusComposite = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(6, false);
    layout.marginLeft = 6;//from w w w . j  a v a  2s  .c om
    layout.marginRight = 6;
    layout.marginTop = 1;
    layout.marginBottom = 1;
    layout.horizontalSpacing = 3;
    layout.verticalSpacing = 1;
    statusComposite.setLayout(layout);

    GridData gridData = new GridData(SWT.FILL);
    gridData.grabExcessHorizontalSpace = true;
    gridData.grabExcessVerticalSpace = false;
    statusComposite.setLayoutData(gridData);

    final Label xLabel = new Label(statusComposite, SWT.NONE);
    xLabel.setText("X:");
    gridData = new GridData(SWT.DEFAULT);
    xLabel.setLayoutData(gridData);
    //      GridDataFactory.swtDefaults().applyTo(xLabel);
    final Text xText = new Text(statusComposite, SWT.BORDER);
    gridData = new GridData(SWT.FILL);
    gridData.widthHint = 50;
    xText.setLayoutData(gridData);
    //      GridDataFactory.fillDefaults().hint(50, SWT.DEFAULT).applyTo(xText);
    xText.setEditable(false);

    final Label yLabel = new Label(statusComposite, SWT.NONE);
    yLabel.setText("Y:");
    gridData = new GridData(SWT.DEFAULT);
    yLabel.setLayoutData(gridData);
    //      GridDataFactory.swtDefaults().applyTo(yLabel);
    final Text yText = new Text(statusComposite, SWT.BORDER);
    gridData = new GridData(SWT.FILL);
    gridData.widthHint = 50;
    yText.setLayoutData(gridData);
    //      GridDataFactory.fillDefaults().hint(50, SWT.DEFAULT).applyTo(yText);
    yText.setEditable(false);

    final Composite composite = this;
    panel.addChartMouseListener(new ChartMouseListener() {

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
            if (event instanceof XYChartMouseEvent) {
                final String xString = String.format("%.2f", ((XYChartMouseEvent) event).getX());
                final String yString = String.format("%.2f", ((XYChartMouseEvent) event).getY());
                //                  panel.requestFocus();

                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        xText.setText(xString);
                        yText.setText(yString);
                        if (!composite.isFocusControl()) {
                            composite.setFocus();
                        }
                    }
                });
            }
        }

        @Override
        public void chartMouseClicked(ChartMouseEvent event) {
            Display.getDefault().asyncExec(new Runnable() {

                @Override
                public void run() {
                    if (!composite.isFocusControl()) {
                        composite.setFocus();
                    }
                }
            });
        }
    });
}

From source file:org.gumtree.vis.hist2d.Hist2D.java

private void createStatusBar() {
    Composite statusComposite = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(6, false);
    layout.marginLeft = 6;/*  w  w w  .j  ava2s . c  om*/
    layout.marginRight = 6;
    layout.marginTop = 1;
    layout.marginBottom = 1;
    layout.horizontalSpacing = 3;
    layout.verticalSpacing = 1;
    statusComposite.setLayout(layout);

    GridData gridData = new GridData(SWT.FILL);
    gridData.grabExcessHorizontalSpace = true;
    gridData.grabExcessVerticalSpace = false;
    statusComposite.setLayoutData(gridData);

    final Label xLabel = new Label(statusComposite, SWT.NONE);
    xLabel.setText("X:");
    gridData = new GridData(SWT.DEFAULT);
    xLabel.setLayoutData(gridData);
    //      GridDataFactory.swtDefaults().applyTo(xLabel);
    final Text xText = new Text(statusComposite, SWT.BORDER);
    gridData = new GridData(SWT.FILL);
    gridData.widthHint = 50;
    xText.setLayoutData(gridData);
    //      GridDataFactory.fillDefaults().hint(50, SWT.DEFAULT).applyTo(xText);
    xText.setEditable(false);

    final Label yLabel = new Label(statusComposite, SWT.NONE);
    yLabel.setText("Y:");
    gridData = new GridData(SWT.DEFAULT);
    yLabel.setLayoutData(gridData);
    //      GridDataFactory.swtDefaults().applyTo(yLabel);
    final Text yText = new Text(statusComposite, SWT.BORDER);
    gridData = new GridData(SWT.FILL);
    gridData.widthHint = 50;
    yText.setLayoutData(gridData);
    //      GridDataFactory.fillDefaults().hint(50, SWT.DEFAULT).applyTo(yText);
    yText.setEditable(false);

    final Label zLabel = new Label(statusComposite, SWT.NONE);
    zLabel.setText("Z:");
    gridData = new GridData(SWT.DEFAULT);
    zLabel.setLayoutData(gridData);
    //      GridDataFactory.swtDefaults().applyTo(zLabel);
    final Text zText = new Text(statusComposite, SWT.BORDER);
    gridData = new GridData(SWT.FILL);
    gridData.widthHint = 50;
    zText.setLayoutData(gridData);
    //      GridDataFactory.fillDefaults().hint(50, SWT.DEFAULT).applyTo(zText);
    zText.setEditable(false);

    final Composite composite = this;
    panel.addChartMouseListener(new ChartMouseListener() {

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
            if (event instanceof XYZChartMouseEvent) {
                final String xString = String.format("%.2f", ((XYZChartMouseEvent) event).getX());
                final String yString = String.format("%.2f", ((XYZChartMouseEvent) event).getY());
                final String zString = String.format("%.2f", ((XYZChartMouseEvent) event).getZ());
                panel.requestFocus();

                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        xText.setText(xString);
                        yText.setText(yString);
                        zText.setText(zString);
                        if (!composite.isFocusControl()) {
                            composite.setFocus();
                        }
                    }
                });
            }
        }

        @Override
        public void chartMouseClicked(ChartMouseEvent event) {
            // TODO Auto-generated method stub

        }
    });
}

From source file:org.gumtree.vis.swt.PlotComposite.java

private void addListeners() {
    if (plot != null) {
        //         mouseWheelListener = new MouseWheelListener() {
        ////from   www.ja  v  a  2  s .c om
        //            @Override
        //            public void mouseScrolled(MouseEvent event) {
        //               JPanel panel = null;
        //               if (plot instanceof JPanel) {
        //                  panel = (JPanel) plot;
        //               }
        //               MouseWheelEvent awtEvent = org.gumtree.vis.listener.SWT_AWT.toMouseWheelEvent(
        //                     event, panel);
        //               plot.processMouseWheelEvent(awtEvent);
        //            }
        //         };
        //         addMouseWheelListener(mouseWheelListener);

        keyListener = new KeyListener() {

            boolean keyPressed = false;

            @Override
            public void keyReleased(KeyEvent event) {
                switch (event.keyCode) {
                case SWT.DEL:
                    plot.removeSelectedMask();
                    plot.removeSelectedText();
                    break;
                default:
                    break;
                }
                switch (event.character) {
                default:
                    break;
                }
                keyPressed = false;
            }

            @Override
            public void keyPressed(KeyEvent event) {
                switch (event.stateMask) {
                case SWT.CTRL:
                    if (event.keyCode == 'c' || event.keyCode == 'C') {
                        if (!keyPressed) {
                            plot.doCopy();
                        }
                    } else if (event.keyCode == 'z' || event.keyCode == 'Z' || event.keyCode == 'r'
                            || event.keyCode == 'R') {
                        if (!keyPressed) {
                            plot.restoreAutoBounds();
                        }
                    } else if (event.keyCode == 'p' || event.keyCode == 'P') {
                        if (!keyPressed) {
                            Thread newThread = new Thread(new Runnable() {

                                @Override
                                public void run() {
                                    plot.createChartPrintJob();
                                }
                            });
                            newThread.start();
                        }
                    } else if (event.keyCode == 'e' || event.keyCode == 'E') {
                        if (!keyPressed) {
                            Thread newThread = new Thread(new Runnable() {

                                @Override
                                public void run() {
                                    try {
                                        plot.doSaveAs();
                                    } catch (IOException e) {
                                        handleException(e);
                                    }
                                }
                            });
                            newThread.start();
                        }
                    }
                    keyPressed = true;
                    break;
                case SWT.ALT:
                    break;
                default:
                    switch (event.keyCode) {
                    case SWT.ARROW_UP:
                        plot.moveSelectedMask(event.keyCode);
                        if (plot.isCurrentlyInputtingText()) {
                            if (plot.getTextInputContent() != null) {
                                if (plot.getTextInputCursorIndex() > 0) {
                                    String text = plot.getTextInputContent();
                                    int cursorIndex = plot.getTextInputCursorIndex();
                                    String[] lines = text.split("\n", 100);
                                    int cursorX = 0;
                                    int charCount = 0;
                                    int newCursorIndex = cursorIndex;
                                    for (int i = 0; i < lines.length; i++) {
                                        if (cursorIndex > charCount
                                                && cursorIndex < charCount + lines[i].length() + 1) {
                                            cursorX = cursorIndex - charCount;
                                            if (i > 0) {
                                                if (cursorX <= lines[i - 1].length()) {
                                                    newCursorIndex = charCount - lines[i - 1].length() - 1
                                                            + cursorX;
                                                } else {
                                                    newCursorIndex = charCount - 1;
                                                }
                                                plot.setTextInputCursorIndex(newCursorIndex);
                                            }
                                            break;
                                        } else if (cursorIndex == charCount + lines[i].length() + 1) {
                                            newCursorIndex = charCount;
                                            plot.setTextInputCursorIndex(newCursorIndex);
                                            break;
                                        }
                                        charCount += lines[i].length() + 1;
                                    }
                                }
                            }
                        }
                        break;
                    case SWT.ARROW_LEFT:
                        plot.moveSelectedMask(event.keyCode);
                        if (plot.isCurrentlyInputtingText()) {
                            if (plot.getTextInputContent() != null) {
                                if (plot.getTextInputCursorIndex() > 0) {
                                    plot.setTextInputCursorIndex(plot.getTextInputCursorIndex() - 1);
                                }
                            }
                        }
                        break;
                    case SWT.ARROW_RIGHT:
                        plot.moveSelectedMask(event.keyCode);
                        if (plot.isCurrentlyInputtingText()) {
                            if (plot.getTextInputContent() != null) {
                                if (plot.getTextInputCursorIndex() < plot.getTextInputContent().length()) {
                                    plot.setTextInputCursorIndex(plot.getTextInputCursorIndex() + 1);
                                }
                            }
                        }
                        break;
                    case SWT.ARROW_DOWN:
                        plot.moveSelectedMask(event.keyCode);
                        if (plot.isCurrentlyInputtingText()) {
                            if (plot.getTextInputContent() != null) {
                                if (plot.getTextInputCursorIndex() >= 0) {
                                    String text = plot.getTextInputContent();
                                    int cursorIndex = plot.getTextInputCursorIndex();
                                    String[] lines = text.split("\n", 100);
                                    int cursorX = 0;
                                    int charCount = 0;
                                    int newCursorIndex = cursorIndex;
                                    for (int i = 0; i < lines.length; i++) {
                                        if (cursorIndex >= charCount
                                                && cursorIndex < charCount + lines[i].length() + 1) {
                                            cursorX = cursorIndex - charCount;
                                            if (i < lines.length - 1) {
                                                if (cursorX <= lines[i + 1].length()) {
                                                    newCursorIndex = charCount + lines[i].length() + 1
                                                            + cursorX;
                                                } else {
                                                    newCursorIndex = charCount + lines[i].length() + 1
                                                            + lines[i + 1].length();
                                                }
                                                plot.setTextInputCursorIndex(newCursorIndex);
                                            }
                                            break;
                                        }
                                        charCount += lines[i].length() + 1;
                                    }
                                }
                            }
                        }
                        break;
                    case SWT.ESC:
                        plot.cancelTextInput();
                    case SWT.SHIFT:
                        break;
                    case SWT.CTRL:
                        break;
                    case SWT.ALT:
                        break;
                    case SWT.F1:
                        break;
                    case SWT.F2:
                        break;
                    case SWT.F3:
                        break;
                    case SWT.F4:
                        break;
                    case SWT.F5:
                        break;
                    case SWT.F6:
                        break;
                    case SWT.F7:
                        break;
                    case SWT.F8:
                        break;
                    case SWT.F9:
                        break;
                    case SWT.F10:
                        break;
                    case SWT.F11:
                        break;
                    case SWT.F12:
                        break;
                    case SWT.PAGE_UP:
                        if (plot.isCurrentlyInputtingText()) {
                            if (plot.getTextInputContent() != null) {
                                if (plot.getTextInputCursorIndex() >= 0) {
                                    String text = plot.getTextInputContent();
                                    int cursorIndex = plot.getTextInputCursorIndex();
                                    String[] lines = text.split("\n", 100);
                                    int cursorX = 0;
                                    int charCount = 0;
                                    int newLine = 0;
                                    for (int i = 0; i < lines.length; i++) {
                                        if (cursorIndex >= charCount
                                                && cursorIndex < charCount + lines[i].length() + 1) {
                                            cursorX = cursorIndex - charCount;
                                            if (i > 0) {
                                                newLine = i - 5;
                                                if (newLine < 0) {
                                                    newLine = 0;
                                                }
                                                jumpToPosition(newLine, cursorX);
                                            }
                                            break;
                                        }
                                        charCount += lines[i].length() + 1;
                                    }
                                }
                            }
                        }
                        break;
                    case SWT.PAGE_DOWN:
                        if (plot.isCurrentlyInputtingText()) {
                            if (plot.getTextInputContent() != null) {
                                if (plot.getTextInputCursorIndex() >= 0) {
                                    String text = plot.getTextInputContent();
                                    int cursorIndex = plot.getTextInputCursorIndex();
                                    String[] lines = text.split("\n", 100);
                                    int cursorX = 0;
                                    int charCount = 0;
                                    int newLine = 0;
                                    for (int i = 0; i < lines.length; i++) {
                                        if (cursorIndex >= charCount
                                                && cursorIndex < charCount + lines[i].length() + 1) {
                                            cursorX = cursorIndex - charCount;
                                            if (i < lines.length - 1) {
                                                newLine = i + 5;
                                                if (newLine >= lines.length) {
                                                    newLine = lines.length - 1;
                                                }
                                                jumpToPosition(newLine, cursorX);
                                            }
                                            break;
                                        }
                                        charCount += lines[i].length() + 1;
                                    }
                                }
                            }
                        }
                        break;
                    case SWT.HOME:
                        if (plot.isCurrentlyInputtingText()) {
                            if (plot.getTextInputContent() != null) {
                                if (plot.getTextInputCursorIndex() >= 0) {
                                    String text = plot.getTextInputContent();
                                    int cursorIndex = plot.getTextInputCursorIndex();
                                    String[] lines = text.split("\n", 100);
                                    int charCount = 0;
                                    for (int i = 0; i < lines.length; i++) {
                                        if (cursorIndex >= charCount
                                                && cursorIndex <= charCount + lines[i].length()) {
                                            plot.setTextInputCursorIndex(charCount);
                                            break;
                                        }
                                        charCount += lines[i].length() + 1;
                                    }
                                }
                            }
                        }
                        break;
                    case SWT.END:
                        if (plot.isCurrentlyInputtingText()) {
                            if (plot.getTextInputContent() != null) {
                                if (plot.getTextInputCursorIndex() >= 0) {
                                    String text = plot.getTextInputContent();
                                    int cursorIndex = plot.getTextInputCursorIndex();
                                    String[] lines = text.split("\n", 100);
                                    int charCount = 0;
                                    for (int i = 0; i < lines.length; i++) {
                                        if (cursorIndex >= charCount
                                                && cursorIndex <= charCount + lines[i].length()) {
                                            plot.setTextInputCursorIndex(charCount + lines[i].length());
                                            break;
                                        }
                                        charCount += lines[i].length() + 1;
                                    }
                                }
                            }
                        }
                        break;
                    case SWT.BS:
                        if (plot.isCurrentlyInputtingText()) {
                            String inputText;
                            String textInputContent = plot.getTextInputContent();
                            int cursorIndex = plot.getTextInputCursorIndex();
                            int newIndex;
                            if (textInputContent == null || cursorIndex <= 0
                                    || textInputContent.length() == 0) {
                                return;
                            } else if (cursorIndex == 1) {
                                inputText = textInputContent.substring(1);
                                newIndex = 0;
                            } else if (cursorIndex < textInputContent.length()) {
                                newIndex = cursorIndex - 1;
                                inputText = textInputContent.substring(0, newIndex)
                                        + textInputContent.substring(cursorIndex);
                            } else {
                                inputText = textInputContent.substring(0, textInputContent.length() - 1);
                                newIndex = inputText.length();
                            }
                            plot.setTextInputContent(inputText);
                            plot.setTextInputCursorIndex(newIndex);
                        }
                        break;
                    case SWT.DEL:
                        if (plot.isCurrentlyInputtingText()) {
                            String inputText;
                            String textInputContent = plot.getTextInputContent();
                            int cursorIndex = plot.getTextInputCursorIndex();
                            int newIndex = cursorIndex;
                            if (textInputContent == null || textInputContent.length() == 0
                                    || cursorIndex >= textInputContent.length()) {
                                return;
                            } else if (cursorIndex == 0) {
                                inputText = textInputContent.substring(1);
                            } else {
                                inputText = textInputContent.substring(0, cursorIndex)
                                        + textInputContent.substring(cursorIndex + 1);
                            }
                            plot.setTextInputContent(inputText);
                            plot.setTextInputCursorIndex(newIndex);
                        }
                        break;
                    case SWT.CAPS_LOCK:
                        break;
                    case SWT.INSERT:
                        break;
                    case SWT.NUM_LOCK:
                        break;
                    case SWT.PRINT_SCREEN:
                        break;
                    case SWT.SCROLL_LOCK:
                        break;
                    case SWT.PAUSE:
                        break;
                    default:
                        if (plot.isCurrentlyInputtingText()) {
                            if (Character.isWhitespace(event.character) && event.keyCode != SWT.SPACE) {
                                if (event.keyCode == SWT.CR || event.keyCode == SWT.LF
                                        || event.keyCode == 16777296) {
                                    addStringToTextInput("\n", plot.getTextInputCursorIndex());
                                }
                            } else {
                                addStringToTextInput(String.valueOf(event.character),
                                        plot.getTextInputCursorIndex());
                            }
                        }
                        break;
                    }
                    plot.repaint();
                }
            }

        };
        addKeyListener(keyListener);

        final Composite composite = this;

        chartMouseListener = new ChartMouseListener() {

            @Override
            public void chartMouseMoved(ChartMouseEvent event) {

            }

            @Override
            public void chartMouseClicked(ChartMouseEvent event) {
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        if (!composite.isDisposed() && !composite.isFocusControl()) {
                            composite.setFocus();
                        }
                    }
                });
            }
        };
        plot.addChartMouseListener(chartMouseListener);
    }
}