summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/accessibility/Shape.java
blob: c2e063f1c3dc7e8e0fcb7c3f9e81b857acb1ab1f (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*******************************************************************************
 * 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 java.text.MessageFormat;
import java.util.ResourceBundle;

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

/**
 * Instances of this class represent an accessible, focusable, user interface object
 * that has a shape and a color. From an accessibility point of view, they imitate a
 * "label" or "static text" whose name is its color and shape.
 */
public class Shape extends Canvas {
	static ResourceBundle bundle = ResourceBundle.getBundle("examples_accessibility");
	int color = SWT.COLOR_BLUE;
	String shape = "square";
	
	/**
	 * Constructs a new instance of this class given its parent
	 * and a style value describing its behavior and appearance.
	 *
	 * @param parent a composite control which will be the parent of the new instance (cannot be null)
	 * @param style the style of control to construct
	 */
	public Shape(Composite parent, int style) {
		super (parent, style);

		addListeners();
	}

	void addListeners() {
		addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent 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.equals(bundle.getString("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() {
			public void focusGained(FocusEvent e) {
				redraw();
			}
			public void focusLost(FocusEvent e) {
				redraw();
			}
		});
		
		addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				if (getClientArea().contains(e.x, e.y)) {
					setFocus();
				}
			}
		});
		
		addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				// key listener enables traversal out
			}
		});
		
		addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent e) {
				switch (e.detail) {
					case SWT.TRAVERSE_TAB_NEXT:
					case SWT.TRAVERSE_TAB_PREVIOUS:
						e.doit = true;
						break;
				}
			}
		});

		getAccessible().addAccessibleListener(new AccessibleAdapter() {
			public void getName(AccessibleEvent e) {
				MessageFormat formatter = new MessageFormat("");
				formatter.applyPattern(bundle.getString("name"));	
				String colorName = bundle.getString("color" + color);
				e.result = formatter.format(new String [] {colorName, shape}); //$NON_NLS$
			}
		});
		
		getAccessible().addAccessibleControlListener(new AccessibleControlAdapter() {
			public void getRole(AccessibleControlEvent e) {
				e.detail = ACC.ROLE_LABEL;
			}
			public void getState(AccessibleControlEvent e) {
				e.detail = ACC.STATE_FOCUSABLE;
				if (isFocusControl()) e.detail |= ACC.STATE_FOCUSED;
			}
		});
	}
	
	/**
	 * Return the receiver's color.
	 * The default color is SWT.COLOR_BLUE.
	 * 
	 * @return the color, which may be any of the SWT.COLOR_ constants
	 */
	public int getColor () {
		return this.color;
	}

	/**
	 * Return the receiver's shape.
	 * The default shape is "square".
	 * 
	 * @return the shape name string, which may be either "circle" or "square"
	 */
	public String getShape () {
		return this.shape;
	}

	/**
	 * Set the receiver's color to the specified color.
	 * The default color is SWT.COLOR_BLUE.
	 * 
	 * @param color any of the SWT.COLOR_ constants
	 */
	public void setColor (int color) {
		this.color = color;
	}

	/**
	 * Set the receiver's shape to the specified shape name string.
	 * The default shape is "square".
	 * 
	 * @param shape a string that can be either "circle" or "square"
	 */
	public void setShape (String shape) {
		this.shape = shape;
	}
}