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();
}
Aggregations