Search in sources :

Example 31 with KeyAdapter

use of org.eclipse.swt.events.KeyAdapter in project eclipse.platform.swt by eclipse.

the class Bug510905_Browser_JsConsole method main.

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(500, 600);
    GridLayout gridLayout = new GridLayout();
    shell.setLayout(gridLayout);
    final Text jsConsole = new Text(shell, SWT.BORDER);
    // jsConsole.setText("document.body.innerHTML = theJavaFunction(123, 'hello', null, true)");
    // Case where there are no paramaters.
    jsConsole.setText("document.body.innerHTML = theJavaFunction()");
    jsConsole.setSelection(jsConsole.getText().length());
    GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
    jsConsole.setLayoutData(data);
    final Browser browser = new Browser(shell, SWT.NONE);
    browser.setText("hello <b>world!</b>");
    data = new GridData(SWT.FILL, SWT.FILL, true, true);
    browser.setLayoutData(data);
    jsConsole.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.keyCode == 13) {
                // 13 = Enter
                browser.execute(jsConsole.getText());
            }
        }
    });
    Button loadNewPage = new Button(shell, SWT.PUSH);
    loadNewPage.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    loadNewPage.setText("Load new Page");
    loadNewPage.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            browser.setText("New page!" + count++);
        }
    });
    // BrowserFunction Code
    @SuppressWarnings("unused") final BrowserFunction function = new CustomFunction(browser, "theJavaFunction");
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Also used : KeyAdapter(org.eclipse.swt.events.KeyAdapter) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Text(org.eclipse.swt.widgets.Text) KeyEvent(org.eclipse.swt.events.KeyEvent) Shell(org.eclipse.swt.widgets.Shell) GridLayout(org.eclipse.swt.layout.GridLayout) BrowserFunction(org.eclipse.swt.browser.BrowserFunction) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Display(org.eclipse.swt.widgets.Display) Browser(org.eclipse.swt.browser.Browser)

Example 32 with KeyAdapter

use of org.eclipse.swt.events.KeyAdapter in project eclipse.platform.swt by eclipse.

the class Bug510905_Browser_TwoJsConsoles method makeBrowserWithConsole.

/**
 * @param leftBrowser
 * @return
 */
private static Browser makeBrowserWithConsole(Composite leftBrowser, String funcName) {
    GridLayout gridLayout = new GridLayout();
    leftBrowser.setLayout(gridLayout);
    final Text jsConsole = new Text(leftBrowser, SWT.BORDER);
    // Case where there are no paramaters.
    jsConsole.setText("document.body.innerHTML = " + funcName + "(123)");
    jsConsole.setSelection(jsConsole.getText().length());
    GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
    jsConsole.setLayoutData(data);
    final Browser browser = new Browser(leftBrowser, SWT.NONE);
    browser.setText("hello <b>world!</b>");
    data = new GridData(SWT.FILL, SWT.FILL, true, true);
    browser.setLayoutData(data);
    jsConsole.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.keyCode == 13) {
                // 13 = Enter
                browser.execute(jsConsole.getText());
            }
        }
    });
    Button loadNewPage = new Button(leftBrowser, SWT.PUSH);
    loadNewPage.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    loadNewPage.setText("Load new Page");
    loadNewPage.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            browser.setText("New page!" + count++);
        }
    });
    return browser;
}
Also used : KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) KeyAdapter(org.eclipse.swt.events.KeyAdapter) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Text(org.eclipse.swt.widgets.Text) Browser(org.eclipse.swt.browser.Browser)

Example 33 with KeyAdapter

use of org.eclipse.swt.events.KeyAdapter in project eclipse.platform.swt by eclipse.

the class ControlsWithLabelsExample method main.

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setLayout(new GridLayout(4, true));
    shell.setText("All Controls Test");
    new Label(shell, SWT.NONE).setText("Label for Label");
    label = new Label(shell, SWT.NONE);
    label.setText("Label");
    new Label(shell, SWT.NONE).setText("Label for CLabel");
    cLabel = new CLabel(shell, SWT.NONE);
    cLabel.setText("CLabel");
    new Label(shell, SWT.NONE).setText("Label for Push Button");
    buttonPush = new Button(shell, SWT.PUSH);
    buttonPush.setText("Push Button");
    new Label(shell, SWT.NONE).setText("Label for Radio Button");
    buttonRadio = new Button(shell, SWT.RADIO);
    buttonRadio.setText("Radio Button");
    new Label(shell, SWT.NONE).setText("Label for Check Button");
    buttonCheck = new Button(shell, SWT.CHECK);
    buttonCheck.setText("Check Button");
    new Label(shell, SWT.NONE).setText("Label for Toggle Button");
    buttonToggle = new Button(shell, SWT.TOGGLE);
    buttonToggle.setText("Toggle Button");
    new Label(shell, SWT.NONE).setText("Label for Editable Combo");
    combo = new Combo(shell, SWT.BORDER);
    for (int i = 0; i < 4; i++) {
        combo.add("item" + i);
    }
    combo.select(0);
    new Label(shell, SWT.NONE).setText("Label for Read-Only Combo");
    combo = new Combo(shell, SWT.READ_ONLY | SWT.BORDER);
    for (int i = 0; i < 4; i++) {
        combo.add("item" + i);
    }
    combo.select(0);
    new Label(shell, SWT.NONE).setText("Label for CCombo");
    cCombo = new CCombo(shell, SWT.BORDER);
    for (int i = 0; i < 5; i++) {
        cCombo.add("item" + i);
    }
    cCombo.select(0);
    new Label(shell, SWT.NONE).setText("Label for List");
    list = new List(shell, SWT.SINGLE | SWT.BORDER);
    list.setItems("Item0", "Item1", "Item2");
    new Label(shell, SWT.NONE).setText("Label for Spinner");
    spinner = new Spinner(shell, SWT.BORDER);
    new Label(shell, SWT.NONE).setText("Label for Single-line Text");
    textSingle = new Text(shell, SWT.SINGLE | SWT.BORDER);
    textSingle.setText("Contents of Single-line Text");
    new Label(shell, SWT.NONE).setText("Label for Multi-line Text");
    textMulti = new Text(shell, SWT.MULTI | SWT.BORDER);
    textMulti.setText("\nContents of Multi-line Text\n");
    new Label(shell, SWT.NONE).setText("Label for StyledText");
    styledText = new StyledText(shell, SWT.MULTI | SWT.BORDER);
    styledText.setText("\nContents of Multi-line StyledText\n");
    new Label(shell, SWT.NONE).setText("Label for Table");
    table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    for (int col = 0; col < 3; col++) {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Col " + col);
        column.setWidth(50);
    }
    for (int row = 0; row < 3; row++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(new String[] { "C0R" + row, "C1R" + row, "C2R" + row });
    }
    new Label(shell, SWT.NONE).setText("Label for Tree");
    tree = new Tree(shell, SWT.BORDER | SWT.MULTI);
    for (int i = 0; i < 3; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("Item" + i);
        for (int j = 0; j < 4; j++) {
            new TreeItem(item, SWT.NONE).setText("Item" + i + j);
        }
    }
    new Label(shell, SWT.NONE).setText("Label for Tree with columns");
    treeTable = new Tree(shell, SWT.BORDER | SWT.MULTI);
    treeTable.setHeaderVisible(true);
    treeTable.setLinesVisible(true);
    for (int col = 0; col < 3; col++) {
        TreeColumn column = new TreeColumn(treeTable, SWT.NONE);
        column.setText("Col " + col);
        column.setWidth(50);
    }
    for (int i = 0; i < 3; i++) {
        TreeItem item = new TreeItem(treeTable, SWT.NONE);
        item.setText(new String[] { "I" + i + "C0", "I" + i + "C1", "I" + i + "C2" });
        for (int j = 0; j < 4; j++) {
            new TreeItem(item, SWT.NONE).setText(new String[] { "I" + i + j + "C0", "I" + i + j + "C1", "I" + i + j + "C2" });
        }
    }
    new Label(shell, SWT.NONE).setText("Label for ToolBar");
    toolBar = new ToolBar(shell, SWT.FLAT);
    for (int i = 0; i < 3; i++) {
        ToolItem item = new ToolItem(toolBar, SWT.PUSH);
        item.setText("Item" + i);
        item.setToolTipText("ToolItem ToolTip" + i);
    }
    new Label(shell, SWT.NONE).setText("Label for CoolBar");
    coolBar = new CoolBar(shell, SWT.FLAT);
    for (int i = 0; i < 2; i++) {
        CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
        ToolBar coolItemToolBar = new ToolBar(coolBar, SWT.FLAT);
        int toolItemWidth = 0;
        for (int j = 0; j < 2; j++) {
            ToolItem item = new ToolItem(coolItemToolBar, SWT.PUSH);
            item.setText("Item" + i + j);
            item.setToolTipText("ToolItem ToolTip" + i + j);
            if (item.getWidth() > toolItemWidth)
                toolItemWidth = item.getWidth();
        }
        coolItem.setControl(coolItemToolBar);
        Point size = coolItemToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        Point coolSize = coolItem.computeSize(size.x, size.y);
        coolItem.setMinimumSize(toolItemWidth, coolSize.y);
        coolItem.setPreferredSize(coolSize);
        coolItem.setSize(coolSize);
    }
    new Label(shell, SWT.NONE).setText("Label for Canvas");
    canvas = new Canvas(shell, SWT.BORDER);
    canvas.setLayoutData(new GridData(64, 64));
    canvas.addPaintListener(e -> e.gc.drawString("Canvas", 15, 25));
    canvas.setCaret(new Caret(canvas, SWT.NONE));
    /* Hook key listener so canvas will take focus during traversal in. */
    canvas.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    /* Hook traverse listener to make canvas give up focus during traversal out. */
    canvas.addTraverseListener(e -> e.doit = true);
    new Label(shell, SWT.NONE).setText("Label for Group");
    group = new Group(shell, SWT.NONE);
    group.setText("Group");
    group.setLayout(new FillLayout());
    new Text(group, SWT.SINGLE | SWT.BORDER).setText("Text in Group");
    new Label(shell, SWT.NONE).setText("Label for TabFolder");
    tabFolder = new TabFolder(shell, SWT.NONE);
    for (int i = 0; i < 3; i++) {
        TabItem item = new TabItem(tabFolder, SWT.NONE);
        item.setText("TabItem &" + i);
        item.setToolTipText("TabItem ToolTip" + i);
        Text itemText = new Text(tabFolder, SWT.SINGLE | SWT.BORDER);
        itemText.setText("Text for TabItem " + i);
        item.setControl(itemText);
    }
    new Label(shell, SWT.NONE).setText("Label for CTabFolder");
    cTabFolder = new CTabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 3; i++) {
        CTabItem item = new CTabItem(cTabFolder, SWT.NONE);
        item.setText("CTabItem &" + i);
        item.setToolTipText("CTabItem ToolTip" + i);
        Text itemText = new Text(cTabFolder, SWT.SINGLE | SWT.BORDER);
        itemText.setText("Text for CTabItem " + i);
        item.setControl(itemText);
    }
    cTabFolder.setSelection(cTabFolder.getItem(0));
    new Label(shell, SWT.NONE).setText("Label for Scale");
    scale = new Scale(shell, SWT.NONE);
    new Label(shell, SWT.NONE).setText("Label for Slider");
    slider = new Slider(shell, SWT.NONE);
    new Label(shell, SWT.NONE).setText("Label for ProgressBar");
    progressBar = new ProgressBar(shell, SWT.NONE);
    progressBar.setSelection(50);
    new Label(shell, SWT.NONE).setText("Label for Sash");
    sash = new Sash(shell, SWT.NONE);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Also used : CLabel(org.eclipse.swt.custom.CLabel) Group(org.eclipse.swt.widgets.Group) CTabFolder(org.eclipse.swt.custom.CTabFolder) Slider(org.eclipse.swt.widgets.Slider) TreeItem(org.eclipse.swt.widgets.TreeItem) Spinner(org.eclipse.swt.widgets.Spinner) TableItem(org.eclipse.swt.widgets.TableItem) KeyAdapter(org.eclipse.swt.events.KeyAdapter) CLabel(org.eclipse.swt.custom.CLabel) Label(org.eclipse.swt.widgets.Label) CCombo(org.eclipse.swt.custom.CCombo) Combo(org.eclipse.swt.widgets.Combo) CTabItem(org.eclipse.swt.custom.CTabItem) KeyEvent(org.eclipse.swt.events.KeyEvent) Shell(org.eclipse.swt.widgets.Shell) GridLayout(org.eclipse.swt.layout.GridLayout) CoolBar(org.eclipse.swt.widgets.CoolBar) Button(org.eclipse.swt.widgets.Button) TreeColumn(org.eclipse.swt.widgets.TreeColumn) Tree(org.eclipse.swt.widgets.Tree) List(org.eclipse.swt.widgets.List) CoolItem(org.eclipse.swt.widgets.CoolItem) ProgressBar(org.eclipse.swt.widgets.ProgressBar) ToolItem(org.eclipse.swt.widgets.ToolItem) StyledText(org.eclipse.swt.custom.StyledText) Table(org.eclipse.swt.widgets.Table) Sash(org.eclipse.swt.widgets.Sash) Canvas(org.eclipse.swt.widgets.Canvas) TabFolder(org.eclipse.swt.widgets.TabFolder) CTabFolder(org.eclipse.swt.custom.CTabFolder) StyledText(org.eclipse.swt.custom.StyledText) Text(org.eclipse.swt.widgets.Text) Scale(org.eclipse.swt.widgets.Scale) Point(org.eclipse.swt.graphics.Point) FillLayout(org.eclipse.swt.layout.FillLayout) TableColumn(org.eclipse.swt.widgets.TableColumn) Point(org.eclipse.swt.graphics.Point) TabItem(org.eclipse.swt.widgets.TabItem) CTabItem(org.eclipse.swt.custom.CTabItem) CCombo(org.eclipse.swt.custom.CCombo) ToolBar(org.eclipse.swt.widgets.ToolBar) GridData(org.eclipse.swt.layout.GridData) Caret(org.eclipse.swt.widgets.Caret) Display(org.eclipse.swt.widgets.Display)

Example 34 with KeyAdapter

use of org.eclipse.swt.events.KeyAdapter in project eclipse.platform.swt by eclipse.

the class Shape method addListeners.

void addListeners() {
    addPaintListener(e -> {
        GC gc = e.gc;
        Display display = getDisplay();
        Color c = display.getSystemColor(color);
        gc.setBackground(c);
        Rectangle rect = getClientArea();
        int length = Math.min(rect.width, rect.height);
        if (shape == CIRCLE) {
            gc.fillOval(0, 0, length, length);
        } else {
            gc.fillRectangle(0, 0, length, length);
        }
        if (isFocusControl())
            gc.drawFocus(rect.x, rect.y, rect.width, rect.height);
    });
    addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            redraw();
        }

        @Override
        public void focusLost(FocusEvent e) {
            redraw();
        }
    });
    addMouseListener(MouseListener.mouseDownAdapter(e -> {
        if (getClientArea().contains(e.x, e.y)) {
            setFocus();
        }
    }));
    addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
        // key listener enables traversal out
        }
    });
    addTraverseListener(e -> {
        switch(e.detail) {
            case SWT.TRAVERSE_TAB_NEXT:
            case SWT.TRAVERSE_TAB_PREVIOUS:
                e.doit = true;
                break;
        }
    });
    getAccessible().addAccessibleListener(new AccessibleAdapter() {

        @Override
        public void getName(AccessibleEvent e) {
            MessageFormat formatter = new MessageFormat("");
            // $NON_NLS$
            formatter.applyPattern(bundle.getString("name"));
            // $NON_NLS$
            String colorName = bundle.getString("color" + color);
            // $NON_NLS$
            String shapeName = bundle.getString("shape" + shape);
            // $NON_NLS$
            e.result = formatter.format(new String[] { colorName, shapeName });
        }
    });
    getAccessible().addAccessibleControlListener(new AccessibleControlAdapter() {

        @Override
        public void getRole(AccessibleControlEvent e) {
            e.detail = ACC.ROLE_GRAPHIC;
        }

        @Override
        public void getState(AccessibleControlEvent e) {
            e.detail = ACC.STATE_FOCUSABLE;
            if (isFocusControl())
                e.detail |= ACC.STATE_FOCUSED;
        }
    });
}
Also used : AccessibleControlAdapter(org.eclipse.swt.accessibility.AccessibleControlAdapter) ACC(org.eclipse.swt.accessibility.ACC) Rectangle(org.eclipse.swt.graphics.Rectangle) FocusEvent(org.eclipse.swt.events.FocusEvent) Display(org.eclipse.swt.widgets.Display) AccessibleAdapter(org.eclipse.swt.accessibility.AccessibleAdapter) GC(org.eclipse.swt.graphics.GC) MessageFormat(java.text.MessageFormat) AccessibleControlEvent(org.eclipse.swt.accessibility.AccessibleControlEvent) MouseListener(org.eclipse.swt.events.MouseListener) ResourceBundle(java.util.ResourceBundle) Color(org.eclipse.swt.graphics.Color) Composite(org.eclipse.swt.widgets.Composite) SWT(org.eclipse.swt.SWT) KeyEvent(org.eclipse.swt.events.KeyEvent) AccessibleEvent(org.eclipse.swt.accessibility.AccessibleEvent) Canvas(org.eclipse.swt.widgets.Canvas) FocusAdapter(org.eclipse.swt.events.FocusAdapter) KeyAdapter(org.eclipse.swt.events.KeyAdapter) FocusAdapter(org.eclipse.swt.events.FocusAdapter) MessageFormat(java.text.MessageFormat) AccessibleControlEvent(org.eclipse.swt.accessibility.AccessibleControlEvent) AccessibleControlAdapter(org.eclipse.swt.accessibility.AccessibleControlAdapter) Color(org.eclipse.swt.graphics.Color) KeyAdapter(org.eclipse.swt.events.KeyAdapter) Rectangle(org.eclipse.swt.graphics.Rectangle) AccessibleAdapter(org.eclipse.swt.accessibility.AccessibleAdapter) FocusEvent(org.eclipse.swt.events.FocusEvent) KeyEvent(org.eclipse.swt.events.KeyEvent) GC(org.eclipse.swt.graphics.GC) AccessibleEvent(org.eclipse.swt.accessibility.AccessibleEvent) Display(org.eclipse.swt.widgets.Display)

Example 35 with KeyAdapter

use of org.eclipse.swt.events.KeyAdapter in project eclipse.platform.swt by eclipse.

the class BarChart method addListeners.

void addListeners() {
    addPaintListener(e -> {
        GC gc = e.gc;
        Rectangle rect = getClientArea();
        Display display = getDisplay();
        int count = data.size();
        Point valueSize = gc.stringExtent(Integer.valueOf(valueMax).toString());
        int leftX = rect.x + 2 * GAP + valueSize.x;
        int bottomY = rect.y + rect.height - 2 * GAP - valueSize.y;
        int unitWidth = (rect.width - 4 * GAP - valueSize.x - AXIS_WIDTH) / count - GAP;
        int unitHeight = (rect.height - 3 * GAP - AXIS_WIDTH - 2 * valueSize.y) / ((valueMax - valueMin) / valueIncrement);
        // draw the title
        int titleWidth = gc.stringExtent(title).x;
        int center = (Math.max(titleWidth, count * (unitWidth + GAP) + GAP) - titleWidth) / 2;
        gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
        gc.drawString(title, leftX + AXIS_WIDTH + center, rect.y + GAP);
        // draw the y axis and value labels
        gc.setLineWidth(AXIS_WIDTH);
        gc.drawLine(leftX, rect.y + GAP + valueSize.y, leftX, bottomY);
        for (int i1 = valueMin; i1 <= valueMax; i1 += valueIncrement) {
            int y = bottomY - i1 * unitHeight;
            gc.drawLine(leftX, y, leftX - GAP, y);
            gc.drawString(Integer.valueOf(i1).toString(), rect.x + GAP, y - valueSize.y);
        }
        // draw the x axis and item labels
        gc.drawLine(leftX, bottomY, rect.x + rect.width - GAP, bottomY);
        for (int i2 = 0; i2 < count; i2++) {
            Object[] dataItem1 = data.get(i2);
            String itemLabel = (String) dataItem1[0];
            int x1 = leftX + AXIS_WIDTH + GAP + i2 * (unitWidth + GAP);
            gc.drawString(itemLabel, x1, bottomY + GAP);
        }
        // draw the bars
        gc.setBackground(display.getSystemColor(color));
        for (int i3 = 0; i3 < count; i3++) {
            Object[] dataItem2 = data.get(i3);
            int itemValue1 = ((Integer) dataItem2[1]).intValue();
            int x2 = leftX + AXIS_WIDTH + GAP + i3 * (unitWidth + GAP);
            gc.fillRectangle(x2, bottomY - AXIS_WIDTH - itemValue1 * unitHeight, unitWidth, itemValue1 * unitHeight);
        }
        if (isFocusControl()) {
            if (selectedItem == -1) {
                // draw the focus rectangle around the whole bar chart
                gc.drawFocus(rect.x, rect.y, rect.width, rect.height);
            } else {
                // draw the focus rectangle around the selected item
                Object[] dataItem3 = data.get(selectedItem);
                int itemValue2 = ((Integer) dataItem3[1]).intValue();
                int x3 = leftX + AXIS_WIDTH + GAP + selectedItem * (unitWidth + GAP);
                gc.drawFocus(x3, bottomY - itemValue2 * unitHeight - AXIS_WIDTH, unitWidth, itemValue2 * unitHeight + AXIS_WIDTH + GAP + valueSize.y);
            }
        }
    });
    addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            redraw();
        }

        @Override
        public void focusLost(FocusEvent e) {
            redraw();
        }
    });
    addMouseListener(MouseListener.mouseDownAdapter(e -> {
        if (getClientArea().contains(e.x, e.y)) {
            setFocus();
            int item = -1;
            int count = data.size();
            for (int i = 0; i < count; i++) {
                if (itemBounds(i).contains(e.x, e.y)) {
                    item = i;
                    break;
                }
            }
            if (item != selectedItem) {
                selectedItem = item;
                redraw();
                getAccessible().setFocus(item);
                getAccessible().selectionChanged();
            }
        }
    }));
    addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            boolean change = false;
            switch(e.keyCode) {
                case SWT.ARROW_DOWN:
                case SWT.ARROW_RIGHT:
                    selectedItem++;
                    if (selectedItem >= data.size())
                        selectedItem = 0;
                    change = true;
                    break;
                case SWT.ARROW_UP:
                case SWT.ARROW_LEFT:
                    selectedItem--;
                    if (selectedItem <= -1)
                        selectedItem = data.size() - 1;
                    change = true;
                    break;
                case SWT.HOME:
                    selectedItem = 0;
                    change = true;
                    break;
                case SWT.END:
                    selectedItem = data.size() - 1;
                    change = true;
                    break;
            }
            if (change) {
                redraw();
                getAccessible().setFocus(selectedItem);
                getAccessible().selectionChanged();
            }
        }
    });
    addTraverseListener(e -> {
        switch(e.detail) {
            case SWT.TRAVERSE_TAB_NEXT:
            case SWT.TRAVERSE_TAB_PREVIOUS:
                e.doit = true;
                break;
        }
    });
    getAccessible().addAccessibleListener(new AccessibleAdapter() {

        @Override
        public void getName(AccessibleEvent e) {
            // $NON_NLS$
            MessageFormat formatter = new MessageFormat("");
            // $NON_NLS$
            formatter.applyPattern(bundle.getString("name"));
            int childID = e.childID;
            if (childID == ACC.CHILDID_SELF) {
                e.result = title;
            } else {
                Object[] item = data.get(childID);
                e.result = formatter.format(item);
            }
        }

        @Override
        public void getDescription(AccessibleEvent e) {
            int childID = e.childID;
            if (childID != ACC.CHILDID_SELF) {
                Object[] item = data.get(childID);
                String value = item[1].toString();
                // $NON_NLS$
                String colorName = bundle.getString("color" + color);
                // $NON_NLS$
                MessageFormat formatter = new MessageFormat("");
                // $NON_NLS$
                formatter.applyPattern(bundle.getString("color_value"));
                e.result = formatter.format(new String[] { colorName, value });
            }
        }
    });
    getAccessible().addAccessibleControlListener(new AccessibleControlAdapter() {

        @Override
        public void getRole(AccessibleControlEvent e) {
            if (e.childID == ACC.CHILDID_SELF) {
                e.detail = ACC.ROLE_LIST;
            } else {
                e.detail = ACC.ROLE_LISTITEM;
            }
        }

        @Override
        public void getChildCount(AccessibleControlEvent e) {
            e.detail = data.size();
        }

        @Override
        public void getChildren(AccessibleControlEvent e) {
            int count = data.size();
            Object[] children = new Object[count];
            for (int i = 0; i < count; i++) {
                children[i] = Integer.valueOf(i);
            }
            e.children = children;
        }

        @Override
        public void getChildAtPoint(AccessibleControlEvent e) {
            Point testPoint = toControl(e.x, e.y);
            int childID = ACC.CHILDID_NONE;
            if (getClientArea().contains(testPoint)) {
                childID = ACC.CHILDID_SELF;
                int count = data.size();
                for (int i = 0; i < count; i++) {
                    if (itemBounds(i).contains(testPoint)) {
                        childID = i;
                        break;
                    }
                }
            }
            e.childID = childID;
        }

        @Override
        public void getLocation(AccessibleControlEvent e) {
            Rectangle location = null;
            Point pt = null;
            int childID = e.childID;
            if (childID == ACC.CHILDID_SELF) {
                location = getClientArea();
                pt = getParent().toDisplay(location.x, location.y);
            } else {
                location = itemBounds(childID);
                pt = toDisplay(location.x, location.y);
            }
            e.x = pt.x;
            e.y = pt.y;
            e.width = location.width;
            e.height = location.height;
        }

        @Override
        public void getFocus(AccessibleControlEvent e) {
            int childID = ACC.CHILDID_NONE;
            if (isFocusControl()) {
                if (selectedItem == -1) {
                    childID = ACC.CHILDID_SELF;
                } else {
                    childID = selectedItem;
                }
            }
            e.childID = childID;
        }

        @Override
        public void getSelection(AccessibleControlEvent e) {
            e.childID = (selectedItem == -1) ? ACC.CHILDID_NONE : selectedItem;
        }

        @Override
        public void getValue(AccessibleControlEvent e) {
            int childID = e.childID;
            if (childID != ACC.CHILDID_SELF) {
                Object[] dataItem = data.get(childID);
                e.result = ((Integer) dataItem[1]).toString();
            }
        }

        @Override
        public void getState(AccessibleControlEvent e) {
            int childID = e.childID;
            e.detail = ACC.STATE_FOCUSABLE;
            if (isFocusControl())
                e.detail |= ACC.STATE_FOCUSED;
            if (childID != ACC.CHILDID_SELF) {
                e.detail |= ACC.STATE_SELECTABLE;
                if (childID == selectedItem)
                    e.detail |= ACC.STATE_SELECTED;
            }
        }
    });
}
Also used : AccessibleControlAdapter(org.eclipse.swt.accessibility.AccessibleControlAdapter) ACC(org.eclipse.swt.accessibility.ACC) Rectangle(org.eclipse.swt.graphics.Rectangle) FocusEvent(org.eclipse.swt.events.FocusEvent) Display(org.eclipse.swt.widgets.Display) AccessibleAdapter(org.eclipse.swt.accessibility.AccessibleAdapter) GC(org.eclipse.swt.graphics.GC) Point(org.eclipse.swt.graphics.Point) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) AccessibleControlEvent(org.eclipse.swt.accessibility.AccessibleControlEvent) List(java.util.List) MouseListener(org.eclipse.swt.events.MouseListener) ResourceBundle(java.util.ResourceBundle) Composite(org.eclipse.swt.widgets.Composite) SWT(org.eclipse.swt.SWT) KeyEvent(org.eclipse.swt.events.KeyEvent) AccessibleEvent(org.eclipse.swt.accessibility.AccessibleEvent) Canvas(org.eclipse.swt.widgets.Canvas) FocusAdapter(org.eclipse.swt.events.FocusAdapter) KeyAdapter(org.eclipse.swt.events.KeyAdapter) FocusAdapter(org.eclipse.swt.events.FocusAdapter) MessageFormat(java.text.MessageFormat) AccessibleControlEvent(org.eclipse.swt.accessibility.AccessibleControlEvent) AccessibleControlAdapter(org.eclipse.swt.accessibility.AccessibleControlAdapter) KeyAdapter(org.eclipse.swt.events.KeyAdapter) Rectangle(org.eclipse.swt.graphics.Rectangle) AccessibleAdapter(org.eclipse.swt.accessibility.AccessibleAdapter) Point(org.eclipse.swt.graphics.Point) FocusEvent(org.eclipse.swt.events.FocusEvent) Point(org.eclipse.swt.graphics.Point) KeyEvent(org.eclipse.swt.events.KeyEvent) GC(org.eclipse.swt.graphics.GC) AccessibleEvent(org.eclipse.swt.accessibility.AccessibleEvent) Display(org.eclipse.swt.widgets.Display)

Aggregations

KeyAdapter (org.eclipse.swt.events.KeyAdapter)80 KeyEvent (org.eclipse.swt.events.KeyEvent)80 GridData (org.eclipse.swt.layout.GridData)50 GridLayout (org.eclipse.swt.layout.GridLayout)40 Composite (org.eclipse.swt.widgets.Composite)39 SelectionEvent (org.eclipse.swt.events.SelectionEvent)33 Text (org.eclipse.swt.widgets.Text)30 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)28 Label (org.eclipse.swt.widgets.Label)24 Button (org.eclipse.swt.widgets.Button)20 TableViewer (org.eclipse.jface.viewers.TableViewer)18 ModifyEvent (org.eclipse.swt.events.ModifyEvent)17 ModifyListener (org.eclipse.swt.events.ModifyListener)16 FocusEvent (org.eclipse.swt.events.FocusEvent)15 Point (org.eclipse.swt.graphics.Point)15 Table (org.eclipse.swt.widgets.Table)14 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)12 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)12 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)12 FocusAdapter (org.eclipse.swt.events.FocusAdapter)11