summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet283.java
blob: 952e0cd1bb9cd564b06d7e044ed7f721a594bcd5 (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
/*******************************************************************************
 * Copyright (c) 2007 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.snippets;

/*
 * Table example snippet: Draw left aligned icon, text and selection
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */

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

public class Snippet283 {
	public static void main(String[] args) {
		Display display = new Display();
		Image image = new Image (display, Snippet283.class.getResourceAsStream("eclipse.png"));
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		final Table table = new Table(shell, SWT.FULL_SELECTION);
		for (int i = 0; i < 8; i++) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText ("Item " + i + " with long text that scrolls.");
			if (i % 2 == 1) item.setImage (image);
		}
		table.addListener(SWT.MouseDown, new Listener() {
			public void handleEvent(Event event) {
				Rectangle rect = table.getClientArea ();
				Point point = new Point (event.x, event.y);
				if (table.getItem(point) != null) return;
				for (int i=table.getTopIndex (); i<table.getItemCount(); i++) {
					TableItem item = table.getItem (i);
					Rectangle itemRect = item.getBounds ();
					if (!itemRect.intersects (rect)) return;
					itemRect.x = rect.x;
					itemRect.width = rect.width;
					if (itemRect.contains (point)) {
						table.setSelection (item);
						Event selectionEvent = new Event ();
						selectionEvent.item = item;
						table.notifyListeners(SWT.Selection, selectionEvent);
						return;
					}
				}
			}
		});
		/*
		 * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
		 * Therefore, it is critical for performance that these methods be
		 * as efficient as possible.
		 */
		table.addListener(SWT.EraseItem, new Listener() {
			public void handleEvent(Event event) {
				event.detail &= ~SWT.FOREGROUND;
				String osName = System.getProperty("os.name");
				if (osName != null && osName.indexOf ("Windows") != -1) {
					if (osName.indexOf ("Vista") != -1 || osName.indexOf ("unknown") != -1) {
						return;
					}
				}
				event.detail &= ~(SWT.FOREGROUND | SWT.SELECTED | SWT.HOT | SWT.FOCUSED);
				GC gc = event.gc;
				TableItem item = (TableItem)event.item;
				Rectangle rect = table.getClientArea ();
				Rectangle itemRect = item.getBounds ();
				itemRect.x = rect.x;
				itemRect.width = rect.width;
				gc.setClipping ((Rectangle) null);
				gc.fillRectangle (itemRect);
			}
		});
		table.addListener(SWT.PaintItem, new Listener() {
			public void handleEvent(Event event) {
				TableItem item = (TableItem)event.item;
				GC gc = event.gc;
				Image image = item.getImage (0);
				String text = item.getText (0);
				Point textExtent = gc.stringExtent (text);
				Rectangle imageRect = item.getImageBounds(0);
				Rectangle textRect = item.getTextBounds (0);
				int textY = textRect.y + Math.max (0, (textRect.height - textExtent.y) / 2);
				if (image == null) {
					gc.drawString(text, imageRect.x, textY, true);
				} else {
					Rectangle imageExtent = image.getBounds ();
					int imageY = imageRect.y + Math.max (0, (imageRect.height - imageExtent.height) / 2);
					gc.drawImage (image, imageRect.x, imageY);
					gc.drawString (text, textRect.x, textY, true);
				}
			}
		});
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep();
		}
		display.dispose();
}
}