Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6e1e04715972df5b3b2d13bc68d9eef34f1bf7a8 (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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
 * Copyright (c) 2008, 2016 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.SWT;
import org.eclipse.swt.accessibility.ACC;
import org.eclipse.swt.accessibility.AccessibleAdapter;
import org.eclipse.swt.accessibility.AccessibleControlAdapter;
import org.eclipse.swt.accessibility.AccessibleControlEvent;
import org.eclipse.swt.accessibility.AccessibleEvent;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

/**
 * 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 final int SQUARE = 0;
	static final int CIRCLE = 1;
	static final int TRIANGLE = 2;
	static ResourceBundle bundle = ResourceBundle.getBundle("examples_accessibility");
	int color = SWT.COLOR_BLUE;
	int 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(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(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent 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("");
				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}); //$NON_NLS$
			}
		});

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

	/**
	 * 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 color;
	}

	/**
	 * Return the receiver's shape.
	 * The default shape is SQUARE.
	 *
	 * @return the shape, which may be either CIRCLE or SQUARE
	 */
	public int getShape () {
		return 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.
	 * The default shape is SQUARE.
	 *
	 * @param shape an int that can be either CIRCLE or SQUARE
	 */
	public void setShape (int shape) {
		this.shape = shape;
	}
}

Back to the top