KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > widgets > TreeNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.forms.widgets;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.PaintEvent;
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.widgets.Composite;
16 /**
17  * A custom selectable control that can be used to control areas that can be
18  * expanded or collapsed. The control control can be toggled between selected
19  * and deselected state with a mouse or by pressing 'Enter' while the control
20  * has focus.
21  * <p>
22  * The control is rendered as box with a '+' or '-' sign, depending on the
23  * expansion state. Focus indication is rendered around the box when the
24  * control has keyboard focus.
25  *
26  * @see Twistie
27  * @since 3.0
28  */

29 public class TreeNode extends ToggleHyperlink {
30     /**
31      * Creates a control in a provided composite.
32      *
33      * @param parent
34      * the parent
35      * @param style
36      * the style
37      */

38     public TreeNode(Composite parent, int style) {
39         super(parent, style);
40         innerWidth = 10;
41         innerHeight = 10;
42     }
43     protected void paint(PaintEvent e) {
44         paintHyperlink(e.gc);
45     }
46     protected void paintHyperlink(GC gc) {
47         Rectangle box = getBoxBounds(gc);
48         gc.setForeground(getDisplay().getSystemColor(
49                 SWT.COLOR_WIDGET_NORMAL_SHADOW));
50         gc.drawRectangle(box);
51         gc.setForeground(getForeground());
52         gc.drawLine(box.x + 2, box.y + 4, box.x + 6, box.y + 4);
53         if (!isExpanded()) {
54             gc.drawLine(box.x + 4, box.y + 2, box.x + 4, box.y + 6);
55         }
56         if (paintFocus && getSelection()) {
57             gc.setForeground(getForeground());
58             gc.drawFocus(box.x - 1, box.y - 1, box.width + 3, box.height + 3);
59         }
60     }
61     private Rectangle getBoxBounds(GC gc) {
62         int x = 1;
63         int y = 0;
64         gc.setFont(getFont());
65         //int height = gc.getFontMetrics().getHeight();
66
//y = height / 2 - 4;
67
//y = Math.max(y, 0);
68
y = 2;
69         return new Rectangle(x, y, 8, 8);
70     }
71 }
72
Popular Tags