KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Color;
15 import org.eclipse.swt.graphics.GC;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.widgets.Composite;
18
19 /**
20  * A custom selectable control that can be used to control areas that can be
21  * expanded or collapsed. The control control can be toggled between selected
22  * and deselected state with a mouse or by pressing 'Enter' while the control
23  * has focus.
24  * <p>
25  * The control is rendered as a triangle that points to the right in the
26  * collapsed and down in the expanded state. Triangle color can be changed.
27  *
28  * @see TreeNode
29  * @since 3.0
30  */

31 public class Twistie extends ToggleHyperlink {
32     private static final int[] onPoints = { 0, 2, 8, 2, 4, 6 };
33
34     private static final int[] offPoints = { 2, -1, 2, 8, 6, 4 };
35
36     /**
37      * Creates a control in a provided composite.
38      *
39      * @param parent
40      * the parent
41      * @param style
42      * the style
43      */

44     public Twistie(Composite parent, int style) {
45         super(parent, style);
46         innerWidth = 9;
47         innerHeight = 9;
48     }
49
50     /*
51      * @see SelectableControl#paint(GC)
52      */

53     protected void paintHyperlink(GC gc) {
54         Color bg;
55         if (!isEnabled())
56             bg = getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
57         else if (hover && getHoverDecorationColor() != null)
58             bg = getHoverDecorationColor();
59         else if (getDecorationColor() != null)
60             bg = getDecorationColor();
61         else
62             bg = getForeground();
63         gc.setBackground(bg);
64         int[] data;
65         Point size = getSize();
66         int x = (size.x - 9) / 2;
67         int y = (size.y - 9) / 2;
68         if (isExpanded())
69             data = translate(onPoints, x, y);
70         else
71             data = translate(offPoints, x, y);
72         gc.fillPolygon(data);
73         gc.setBackground(getBackground());
74     }
75
76     private int[] translate(int[] data, int x, int y) {
77         int[] target = new int[data.length];
78         for (int i = 0; i < data.length; i += 2) {
79             target[i] = data[i] + x;
80         }
81         for (int i = 1; i < data.length; i += 2) {
82             target[i] = data[i] + y;
83         }
84         return target;
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see org.eclipse.swt.widgets.Control#setEnabled(boolean)
91      */

92     public void setEnabled(boolean enabled) {
93         super.setEnabled(enabled);
94         redraw();
95     }
96 }
97
Popular Tags