Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4f05fbf89f174734e0c234a72c7d95fd9ed305b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.examples.accessibility;

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.accessibility.*;

/**
 * This example shows how to use an AccessibleValueListener to provide
 * additional information to an AT.
 */
public class AccessibleValueExample {
	static int value = 40;
	static int min = 0;
	static int max = 100;
	
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setText("Accessible Value Example");
		
		final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				Rectangle rect = canvas.getClientArea();
				String val = String.valueOf(value);
				Point size = e.gc.stringExtent(val);
				e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_LIST_SELECTION));
				e.gc.fillRectangle(0, 0, rect.width * value / (max - min), rect.height);
				e.gc.drawString(val, rect.x + (rect.width - size.x) / 2, rect.y + (rect.height - size.y) / 2, true);
			}
		});
		
		Accessible accessible = canvas.getAccessible();
		accessible.addAccessibleListener(new AccessibleAdapter() {
			@Override
			public void getName(AccessibleEvent e) {
				e.result = "The value of this canvas is " + value;
			}
		});
		accessible.addAccessibleControlListener(new AccessibleControlAdapter() {
			@Override
			public void getRole(AccessibleControlEvent e) {
				e.detail = ACC.ROLE_PROGRESSBAR;
			}
		});
		accessible.addAccessibleValueListener(new AccessibleValueAdapter() {
			@Override
			public void setCurrentValue(AccessibleValueEvent e) {
				value = e.value.intValue();
				canvas.redraw();
			}
			@Override
			public void getMinimumValue(AccessibleValueEvent e) {
				e.value = new Integer(min);
			}
			@Override
			public void getMaximumValue(AccessibleValueEvent e) {
				e.value = new Integer(max);
			}
			@Override
			public void getCurrentValue(AccessibleValueEvent e) {
				e.value = new Integer(value);
			}
		});

		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep();
		}
		display.dispose();
	}
}

Back to the top