KickJava   Java API By Example, From Geeks To Geeks.

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


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.accessibility.*;
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.widgets.*;
16 import org.eclipse.ui.forms.events.*;
17 import org.eclipse.ui.internal.forms.Messages;
18 /**
19  * A custom selectable control that can be used to control areas that can be
20  * expanded or collapsed.
21  * <p>
22  * This is an abstract class. Subclasses are responsible for rendering the
23  * control using decoration and hover decoration color. Control should be
24  * rendered based on the current expansion state.
25  *
26  * @since 3.0
27  */

28 public abstract class ToggleHyperlink extends AbstractHyperlink {
29     protected int innerWidth;
30     protected int innerHeight;
31     protected boolean hover;
32     private boolean expanded;
33     private Color decorationColor;
34     private Color hoverColor;
35     /**
36      * Creates a control in a provided composite.
37      *
38      * @param parent
39      * the parent
40      * @param style
41      * the style
42      */

43     public ToggleHyperlink(Composite parent, int style) {
44         super(parent, style);
45         Listener listener = new Listener() {
46             public void handleEvent(Event e) {
47                 switch (e.type) {
48                     case SWT.MouseEnter:
49                         hover=true;
50                         redraw();
51                         break;
52                     case SWT.MouseExit:
53                         hover = false;
54                         redraw();
55                         break;
56                     case SWT.KeyDown:
57                         onKeyDown(e);
58                         break;
59                 }
60             }
61         };
62         addListener(SWT.MouseEnter, listener);
63         addListener(SWT.MouseExit, listener);
64         addListener(SWT.KeyDown, listener);
65         addHyperlinkListener(new HyperlinkAdapter() {
66             public void linkActivated(HyperlinkEvent e) {
67                 setExpanded(!isExpanded());
68             }
69         });
70         initAccessible();
71     }
72     /**
73      * Sets the color of the decoration.
74      *
75      * @param decorationColor
76      */

77     public void setDecorationColor(Color decorationColor) {
78         this.decorationColor = decorationColor;
79     }
80     /**
81      * Returns the color of the decoration.
82      *
83      * @return decoration color
84      */

85     public Color getDecorationColor() {
86         return decorationColor;
87     }
88     /**
89      * Sets the hover color of decoration. Hover color will be used when mouse
90      * enters the decoration area.
91      *
92      * @param hoverColor
93      * the hover color to use
94      */

95     public void setHoverDecorationColor(Color hoverColor) {
96         this.hoverColor = hoverColor;
97     }
98     /**
99      * Returns the hover color of the decoration.
100      *
101      * @return the hover color of the decoration.
102      * @since 3.1
103      */

104     public Color getHoverDecorationColor() {
105         return hoverColor;
106     }
107     
108     /**
109      * Returns the hover color of the decoration.
110      *
111      * @return the hover color of the decoration.
112      * @deprecated use <code>getHoverDecorationColor</code>
113      * @see #getHoverDecorationColor()
114      */

115     public Color geHoverDecorationColor() {
116         return hoverColor;
117     }
118     /**
119      * Computes the size of the control.
120      *
121      * @param wHint
122      * width hint
123      * @param hHint
124      * height hint
125      * @param changed
126      * if true, flush any saved layout state
127      */

128     public Point computeSize(int wHint, int hHint, boolean changed) {
129         int width, height;
130         /*
131         if (wHint != SWT.DEFAULT)
132             width = wHint;
133         else */

134             width = innerWidth + 2 * marginWidth;
135         /*
136         if (hHint != SWT.DEFAULT)
137             height = hHint;
138         else */

139             height = innerHeight + 2 * marginHeight;
140         return new Point(width, height);
141     }
142     /**
143      * Returns the expansion state of the toggle control. When toggle is in the
144      * normal (downward) state, the value is <samp>true </samp>. Collapsed
145      * control will return <samp>false </samp>.
146      *
147      * @return <samp>false </samp> if collapsed, <samp>true </samp> otherwise.
148      */

149     public boolean isExpanded() {
150         return expanded;
151     }
152     /**
153      * Sets the expansion state of the twistie control
154      *
155      * @param expanded the expansion state
156      */

157     public void setExpanded(boolean expanded) {
158         this.expanded = expanded;
159         getAccessible().selectionChanged();
160         redraw();
161     }
162     private void initAccessible() {
163         getAccessible().addAccessibleListener(new AccessibleAdapter() {
164             public void getHelp(AccessibleEvent e) {
165                 e.result = getToolTipText();
166             }
167             public void getName(AccessibleEvent e) {
168                 String JavaDoc name=Messages.ToggleHyperlink_accessibleName;
169                 if (getParent() instanceof ExpandableComposite) {
170                     name += Messages.ToggleHyperlink_accessibleColumn+((ExpandableComposite)getParent()).getText();
171                     int index = name.indexOf('&');
172                     if (index != -1) {
173                         name = name.substring(0, index) + name.substring(index + 1);
174                     }
175                 }
176                 e.result = name;
177             }
178             public void getDescription(AccessibleEvent e) {
179                 getName(e);
180             }
181         });
182         getAccessible().addAccessibleControlListener(
183                 new AccessibleControlAdapter() {
184                     public void getChildAtPoint(AccessibleControlEvent e) {
185                         Point testPoint = toControl(new Point(e.x, e.y));
186                         if (getBounds().contains(testPoint)) {
187                             e.childID = ACC.CHILDID_SELF;
188                         }
189                     }
190                     public void getLocation(AccessibleControlEvent e) {
191                         Rectangle location = getBounds();
192                         Point pt = toDisplay(new Point(location.x, location.y));
193                         e.x = pt.x;
194                         e.y = pt.y;
195                         e.width = location.width;
196                         e.height = location.height;
197                     }
198                     public void getSelection (AccessibleControlEvent e) {
199                         if (ToggleHyperlink.this.getSelection())
200                             e.childID = ACC.CHILDID_SELF;
201                     }
202                     
203                     public void getFocus (AccessibleControlEvent e) {
204                         if (ToggleHyperlink.this.getSelection())
205                             e.childID = ACC.CHILDID_SELF;
206                     }
207                     public void getChildCount(AccessibleControlEvent e) {
208                         e.detail = 0;
209                     }
210                     public void getRole(AccessibleControlEvent e) {
211                         e.detail = ACC.ROLE_TREE;
212                     }
213                     public void getState(AccessibleControlEvent e) {
214                         int state = ACC.STATE_FOCUSABLE;
215                         if (ToggleHyperlink.this.getSelection())
216                             state |= ACC.STATE_FOCUSED;
217                         state |= ToggleHyperlink.this.isExpanded()
218                                 ? ACC.STATE_EXPANDED
219                                 : ACC.STATE_COLLAPSED;
220                         e.detail = state;
221                     }
222                 });
223     }
224     private void onKeyDown(Event e) {
225         if (e.keyCode==SWT.ARROW_RIGHT) {
226             // expand if collapsed
227
if (!isExpanded()) {
228                 handleActivate(e);
229             }
230             e.doit=false;
231         }
232         else if (e.keyCode==SWT.ARROW_LEFT) {
233             // collapse if expanded
234
if (isExpanded()) {
235                 handleActivate(e);
236             }
237             e.doit=false;
238         }
239     }
240 }
241
Popular Tags