Search in sources :

Example 1 with AccessibleActionAdapter

use of org.eclipse.swt.accessibility.AccessibleActionAdapter in project eclipse.platform.swt by eclipse.

the class AccessibleActionExample method main.

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("Accessible Action Example");
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");
    final Canvas customButton = new Canvas(shell, SWT.NONE) {

        @Override
        public Point computeSize(int wHint, int hHint, boolean changed) {
            GC gc = new GC(this);
            Point point = gc.stringExtent(buttonText);
            gc.dispose();
            point.x += MARGIN;
            point.y += MARGIN;
            return point;
        }
    };
    customButton.addPaintListener(e -> {
        Rectangle clientArea = customButton.getClientArea();
        Point stringExtent = e.gc.stringExtent(buttonText);
        int x = clientArea.x + (clientArea.width - stringExtent.x) / 2;
        int y = clientArea.y + (clientArea.height - stringExtent.y) / 2;
        e.gc.drawString(buttonText, x, y);
    });
    customButton.addMouseListener(MouseListener.mouseDownAdapter(e -> {
        int actionIndex = (e.button == 1) ? 0 : 1;
        customButtonAction(actionIndex);
    }));
    customButton.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            int modifierKeys = e.stateMask & SWT.MODIFIER_MASK;
            if (modifierKeys == SWT.CTRL || modifierKeys == 0) {
                if (e.character == '1')
                    customButtonAction(0);
                else if (e.character == '2')
                    customButtonAction(1);
            }
        }
    });
    Accessible accessible = customButton.getAccessible();
    accessible.addAccessibleListener(new AccessibleAdapter() {

        @Override
        public void getName(AccessibleEvent e) {
            e.result = buttonText;
        }

        @Override
        public void getKeyboardShortcut(AccessibleEvent e) {
            // default action is 'action 1'
            e.result = "CTRL+1";
        }
    });
    accessible.addAccessibleControlListener(new AccessibleControlAdapter() {

        @Override
        public void getRole(AccessibleControlEvent e) {
            e.detail = ACC.ROLE_PUSHBUTTON;
        }
    });
    accessible.addAccessibleActionListener(new AccessibleActionAdapter() {

        @Override
        public void getActionCount(AccessibleActionEvent e) {
            e.count = 2;
        }

        @Override
        public void getName(AccessibleActionEvent e) {
            if (0 <= e.index && e.index <= 1) {
                if (e.localized) {
                    e.result = AccessibleActionExample.getResourceString("action" + e.index);
                } else {
                    // $NON-NLS-1$
                    e.result = "Action" + e.index;
                }
            }
        }

        @Override
        public void getDescription(AccessibleActionEvent e) {
            if (0 <= e.index && e.index <= 1) {
                e.result = AccessibleActionExample.getResourceString("action" + e.index + "description");
            }
        }

        @Override
        public void doAction(AccessibleActionEvent e) {
            if (0 <= e.index && e.index <= 1) {
                customButtonAction(e.index);
                e.result = ACC.OK;
            }
        }

        @Override
        public void getKeyBinding(AccessibleActionEvent e) {
            switch(e.index) {
                case 0:
                    e.result = "1;CTRL+1";
                    break;
                case 1:
                    e.result = "2;CTRL+2";
                    break;
                default:
                    e.result = null;
            }
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Also used : AccessibleControlAdapter(org.eclipse.swt.accessibility.AccessibleControlAdapter) Shell(org.eclipse.swt.widgets.Shell) Button(org.eclipse.swt.widgets.Button) ACC(org.eclipse.swt.accessibility.ACC) AccessibleActionAdapter(org.eclipse.swt.accessibility.AccessibleActionAdapter) MissingResourceException(java.util.MissingResourceException) Rectangle(org.eclipse.swt.graphics.Rectangle) Display(org.eclipse.swt.widgets.Display) AccessibleActionEvent(org.eclipse.swt.accessibility.AccessibleActionEvent) AccessibleAdapter(org.eclipse.swt.accessibility.AccessibleAdapter) GC(org.eclipse.swt.graphics.GC) Point(org.eclipse.swt.graphics.Point) AccessibleControlEvent(org.eclipse.swt.accessibility.AccessibleControlEvent) MouseListener(org.eclipse.swt.events.MouseListener) ResourceBundle(java.util.ResourceBundle) SWT(org.eclipse.swt.SWT) KeyEvent(org.eclipse.swt.events.KeyEvent) Accessible(org.eclipse.swt.accessibility.Accessible) AccessibleEvent(org.eclipse.swt.accessibility.AccessibleEvent) Canvas(org.eclipse.swt.widgets.Canvas) FillLayout(org.eclipse.swt.layout.FillLayout) KeyAdapter(org.eclipse.swt.events.KeyAdapter) AccessibleActionAdapter(org.eclipse.swt.accessibility.AccessibleActionAdapter) AccessibleControlEvent(org.eclipse.swt.accessibility.AccessibleControlEvent) AccessibleControlAdapter(org.eclipse.swt.accessibility.AccessibleControlAdapter) Canvas(org.eclipse.swt.widgets.Canvas) KeyAdapter(org.eclipse.swt.events.KeyAdapter) Rectangle(org.eclipse.swt.graphics.Rectangle) AccessibleAdapter(org.eclipse.swt.accessibility.AccessibleAdapter) FillLayout(org.eclipse.swt.layout.FillLayout) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) KeyEvent(org.eclipse.swt.events.KeyEvent) Shell(org.eclipse.swt.widgets.Shell) Button(org.eclipse.swt.widgets.Button) AccessibleActionEvent(org.eclipse.swt.accessibility.AccessibleActionEvent) GC(org.eclipse.swt.graphics.GC) AccessibleEvent(org.eclipse.swt.accessibility.AccessibleEvent) Display(org.eclipse.swt.widgets.Display) Accessible(org.eclipse.swt.accessibility.Accessible)

Aggregations

MissingResourceException (java.util.MissingResourceException)1 ResourceBundle (java.util.ResourceBundle)1 SWT (org.eclipse.swt.SWT)1 ACC (org.eclipse.swt.accessibility.ACC)1 Accessible (org.eclipse.swt.accessibility.Accessible)1 AccessibleActionAdapter (org.eclipse.swt.accessibility.AccessibleActionAdapter)1 AccessibleActionEvent (org.eclipse.swt.accessibility.AccessibleActionEvent)1 AccessibleAdapter (org.eclipse.swt.accessibility.AccessibleAdapter)1 AccessibleControlAdapter (org.eclipse.swt.accessibility.AccessibleControlAdapter)1 AccessibleControlEvent (org.eclipse.swt.accessibility.AccessibleControlEvent)1 AccessibleEvent (org.eclipse.swt.accessibility.AccessibleEvent)1 KeyAdapter (org.eclipse.swt.events.KeyAdapter)1 KeyEvent (org.eclipse.swt.events.KeyEvent)1 MouseListener (org.eclipse.swt.events.MouseListener)1 GC (org.eclipse.swt.graphics.GC)1 Point (org.eclipse.swt.graphics.Point)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1 FillLayout (org.eclipse.swt.layout.FillLayout)1 Button (org.eclipse.swt.widgets.Button)1 Canvas (org.eclipse.swt.widgets.Canvas)1