summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet162.java
blob: 3f55c2253619897f69fe48f283ad670da8c76468 (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
package org.eclipse.swt.snippets;

/*
 * Adding an accessible listener to provide emulated state information
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.*;
import org.eclipse.swt.accessibility.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet162 {

	final static String[] ITEM_NAMES = {"first item", "second", "third", "fourth", "fifth"};

public static void main (String [] args) {
	Display display = new Display ();
	final Image checkedImage = getStateImage (display, true);
	final Image uncheckedImage = getStateImage (display, false);

	Shell shell = new Shell (display);
	shell.setLayout (new FillLayout ());
	
	final Table table = new Table (shell, SWT.FULL_SELECTION | SWT.BORDER);
	for (int i = 0; i < ITEM_NAMES.length; i++) {
		TableItem item = new TableItem (table, SWT.NONE);
		item.setText (ITEM_NAMES[i]);
		item.setImage (i % 2 == 0 ? checkedImage : uncheckedImage);
	}
	table.addSelectionListener(new SelectionAdapter(){
		public void widgetDefaultSelected(SelectionEvent e) {
			TableItem item = (TableItem)e.item;
			item.setImage(item.getImage() == checkedImage ? uncheckedImage : checkedImage);
		}
	});

	table.getAccessible ().addAccessibleControlListener (new AccessibleControlAdapter () {
		public void getRole(AccessibleControlEvent e) {
			e.detail = ACC.ROLE_CHECKBUTTON;
		}
		public void getState (AccessibleControlEvent e) {
			if (e.childID >= 0 && e.childID < table.getItemCount ()) {
				TableItem item = table.getItem (e.childID);
				if (item.getImage() == checkedImage) {
					e.detail |= ACC.STATE_CHECKED;
				}
			}
		}
	});

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

static Image getStateImage (Display display, boolean checked) {
	Image image = new Image (display, 16, 16);
	GC gc = new GC (image);
	gc.setBackground (display.getSystemColor (SWT.COLOR_YELLOW));
	gc.fillOval (0, 0, 16, 16);
	if (checked) {
		gc.setForeground (display.getSystemColor (SWT.COLOR_DARK_GREEN));
		gc.drawLine (0, 0, 16, 16);
		gc.drawLine (16, 0, 0, 16);
	}
	gc.dispose ();
	return image;
}
}