1 /*******************************************************************************
2  * Copyright (c) 2018 Red Hat and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     Red Hat - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.tests.gtk.snippets;
15 
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.Color;
18 import org.eclipse.swt.graphics.GC;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.swt.widgets.Tree;
22 import org.eclipse.swt.widgets.TreeColumn;
23 import org.eclipse.swt.widgets.TreeItem;
24 
25 public class Bug535124_TreeSelection {
26 
main(String[] args)27 	public static void main(String[] args) {
28 		Display display = new Display ();
29 		Shell shell = new Shell(display);
30 		final Color red = display.getSystemColor(SWT.COLOR_RED);
31 		final Color yellow = display.getSystemColor(SWT.COLOR_YELLOW);
32 		final Tree tree = new Tree(shell, SWT.FULL_SELECTION);
33 		tree.setHeaderVisible(true);
34 		new TreeColumn(tree, SWT.NONE).setWidth(100);
35 		new TreeColumn(tree, SWT.NONE).setWidth(100);
36 		new TreeColumn(tree, SWT.NONE).setWidth(100);
37 		for (int i = 0; i < 5; i++) {
38 			TreeItem item = new TreeItem(tree, SWT.NONE);
39 			item.setText(0, "item " + i + " col 0");
40 			item.setText(1, "item " + i + " col 1");
41 			item.setText(2, "item " + i + " col 2");
42 		}
43 		tree.pack();
44 		tree.addListener(SWT.EraseItem, event -> {
45 			event.detail &= ~SWT.HOT;
46 			if ((event.detail & SWT.SELECTED) == 0) {
47 				return; /* item not selected */
48 			}
49 			int clientWidth = tree.getClientArea().width;
50 			GC gc = event.gc;
51 			Color oldForeground = gc.getForeground();
52 			Color oldBackground = gc.getBackground();
53 			gc.setForeground(red);
54 			gc.setBackground(yellow);
55 			gc.fillGradientRectangle(0, event.y, clientWidth, event.height, false);
56 			gc.setForeground(oldForeground);
57 			gc.setBackground(oldBackground);
58 			event.detail &= ~SWT.SELECTED;
59 		});
60 		shell.pack();
61 		shell.open();
62 		while (!shell.isDisposed()) {
63 			if (!display.readAndDispatch())
64 				display.sleep();
65 		}
66 		display.dispose();
67 	}
68 
69 }