Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 06e6c3b4b8e57a5b9ab2b8253ccd2315c245a5a6 (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
/*******************************************************************************
 * Copyright (c) 2000, 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 snippet: modify the clipping of custom background paints
 * 
 * For a detailed explanation of this snippet see
 * http://www.eclipse.org/articles/Article-CustomDrawingTableAndTreeItems/customDraw.htm#_example4
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class Snippet273 {

public static void main(String[] args) {
	final String[] MONTHS = {
		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
	};
	final int[] HIGHS = {-7, -4, 1, 11, 18, 24, 26, 25, 20, 13, 5, -4};
	final int[] LOWS = {-15, -13, -7, 1, 7, 13, 15, 14, 10, 4, -2, -11};
	final int SCALE_MIN = -30; final int SCALE_MAX = 30;
	final int SCALE_RANGE = Math.abs(SCALE_MIN - SCALE_MAX);

	Display display = new Display();
	Shell shell = new Shell(display);
	shell.setBounds(10,10,400,350);
	shell.setText("Ottawa Average Daily Temperature Ranges");
	final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
	final Color white = display.getSystemColor(SWT.COLOR_WHITE);
	final Color red = display.getSystemColor(SWT.COLOR_RED);
	// final Image parliamentImage = new Image(display, "./parliament.jpg");
	final Table table = new Table(shell, SWT.NONE);
	table.setBounds(10,10,350,300);
	// table.setBackgroundImage(parliamentImage);
	for (int i = 0; i < 12; i++) {
		TableItem item = new TableItem(table, SWT.NONE);
		item.setText(MONTHS[i] + " (" + LOWS[i] + "C..." + HIGHS[i] + "C)");
	}
	final int clientWidth = table.getClientArea().width;

	/*
	 * NOTE: MeasureItem and EraseItem are called repeatedly. Therefore it is
	 * critical for performance that these methods be as efficient as possible.
	 */
	table.addListener(SWT.MeasureItem, new Listener() {
		public void handleEvent(Event event) {
			int itemIndex = table.indexOf((TableItem)event.item);
			int rightX = (HIGHS[itemIndex] - SCALE_MIN) * clientWidth / SCALE_RANGE;
			event.width = rightX;
		}
	});
	table.addListener(SWT.EraseItem, new Listener() {
		public void handleEvent(Event event) {
			int itemIndex = table.indexOf((TableItem)event.item);
			int leftX = (LOWS[itemIndex] - SCALE_MIN) * clientWidth / SCALE_RANGE;
			int rightX = (HIGHS[itemIndex] - SCALE_MIN) * clientWidth / SCALE_RANGE;
			GC gc = event.gc;
			Rectangle clipping = gc.getClipping();
			clipping.x = leftX;
			clipping.width = rightX - leftX;
			gc.setClipping(clipping);
			Color oldForeground = gc.getForeground();
			Color oldBackground = gc.getBackground();
			gc.setForeground(blue);
			gc.setBackground(white);
			gc.fillGradientRectangle(event.x, event.y, event.width / 2, event.height, false);
			gc.setForeground(white);
			gc.setBackground(red);
			gc.fillGradientRectangle(
				event.x + event.width / 2, event.y, event.width / 2, event.height, false);
			gc.setForeground(oldForeground);
			gc.setBackground(oldBackground);
			event.detail &= ~SWT.BACKGROUND;
			event.detail &= ~SWT.HOT;
		}
	});

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

Back to the top