KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > HyperlinkLabel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.help.ui.internal;
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.accessibility.*;
16 import org.eclipse.swt.events.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.layout.*;
19 import org.eclipse.swt.widgets.*;
20
21 /**
22  *
23  * A canvas holding a hyperlink label. Need this to deal with focus selection.
24  */

25 public class HyperlinkLabel extends Canvas {
26     Label label;
27     boolean hasFocus;
28
29     /**
30      * Constructor for Hyperlink.
31      *
32      * @param parent
33      * @param style
34      */

35     public HyperlinkLabel(Composite parent, int style) {
36         super(parent, style);
37
38         GridLayout layout = new GridLayout();
39         layout.marginHeight = 3;
40         layout.marginWidth = 2;
41         layout.numColumns = 1;
42         this.setLayout(layout);
43
44         this.label = new Label(this, style);
45
46         addPaintListener(new PaintListener() {
47             public void paintControl(PaintEvent e) {
48                 paint(e);
49             }
50         });
51
52         addKeyListener(new KeyAdapter() {
53             public void keyPressed(KeyEvent e) {
54                 if (e.character == '\r') {
55                     // Activation
56
notifyListeners(SWT.DefaultSelection);
57                 }
58             }
59         });
60
61         addListener(SWT.Traverse, new Listener() {
62             public void handleEvent(Event e) {
63                 switch (e.detail) {
64                     // let arrows move focus
65
case SWT.TRAVERSE_ARROW_NEXT :
66                         e.detail = SWT.TRAVERSE_TAB_NEXT;
67                         break;
68                     case SWT.TRAVERSE_ARROW_PREVIOUS :
69                         e.detail = SWT.TRAVERSE_TAB_PREVIOUS;
70                         break;
71
72                     case SWT.TRAVERSE_PAGE_NEXT :
73                     case SWT.TRAVERSE_PAGE_PREVIOUS :
74                     case SWT.TRAVERSE_RETURN :
75                         e.doit = false;
76                         return;
77                 }
78                 e.doit = true;
79             }
80         });
81
82         addFocusListener(new FocusListener() {
83             public void focusGained(FocusEvent e) {
84                 if (!hasFocus) {
85                     hasFocus = true;
86                     notifyListeners(SWT.Selection);
87                     redraw();
88                 }
89             }
90             public void focusLost(FocusEvent e) {
91                 if (hasFocus) {
92                     hasFocus = false;
93                     notifyListeners(SWT.Selection);
94                     redraw();
95                 }
96             }
97         });
98
99         GridData data = new GridData();
100         data.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
101         data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
102         label.setLayoutData(data);
103
104         initAccessibleLink();
105         initAccessibleLabel();
106     }
107
108     public void setText(String JavaDoc text) {
109         label.setText(text);
110     }
111
112     public boolean getSelection() {
113         return hasFocus;
114     }
115
116     public Label getLabel() {
117         return label;
118     }
119
120     void notifyListeners(int eventType) {
121         Event event = new Event();
122         event.type = eventType;
123         event.widget = this;
124         notifyListeners(eventType, event);
125     }
126
127     protected void paint(PaintEvent e) {
128         if (hasFocus) {
129             GC gc = e.gc;
130             Point size = getSize();
131             gc.setForeground(getForeground());
132             gc.drawFocus(0, 0, size.x, size.y);
133         }
134     }
135
136     public void addSelectionListener(SelectionListener listener) {
137         checkWidget();
138         if (listener == null)
139             return;
140         TypedListener typedListener = new TypedListener(listener);
141         addListener(SWT.Selection, typedListener);
142         addListener(SWT.DefaultSelection, typedListener);
143     }
144
145     public void removeSelectionListener(SelectionListener listener) {
146         checkWidget();
147         if (listener == null)
148             return;
149         removeListener(SWT.Selection, listener);
150         removeListener(SWT.DefaultSelection, listener);
151     }
152
153     public Point computeSize(int wHint, int hHint, boolean changed) {
154         int innerWidth = wHint;
155         if (innerWidth != SWT.DEFAULT)
156             innerWidth -= 4;
157         Point textSize = label.computeSize(wHint, hHint, changed);//computeTextSize(innerWidth,
158
// hHint);
159
int textWidth = textSize.x + 4;
160         int textHeight = textSize.y + 6;
161         return new Point(textWidth, textHeight);
162     }
163
164     public void addMouseListener(MouseListener l) {
165         //super.addMouseListener(l);
166
label.addMouseListener(l);
167     }
168
169     public void addMouseTrackListener(MouseTrackListener l) {
170         //super.addMouseTrackListener(l);
171
label.addMouseTrackListener(l);
172     }
173
174     public void addPaintListener(PaintListener l) {
175         super.addPaintListener(l);
176         label.addPaintListener(l);
177     }
178
179     public void addListener(int e, Listener l) {
180         super.addListener(e, l);
181         //label.addListener(e, l);
182
}
183
184     public void setBackground(Color c) {
185         super.setBackground(c);
186         label.setBackground(c);
187     }
188
189     public void setForeground(Color c) {
190         super.setForeground(c);
191         label.setForeground(c);
192     }
193
194     public void setCursor(Cursor c) {
195         super.setCursor(c);
196         label.setCursor(c);
197     }
198
199     private void initAccessibleLink() {
200         Accessible accessible = this.getAccessible();
201         accessible.addAccessibleListener(new AccessibleAdapter() {
202             public void getName(AccessibleEvent e) {
203                 e.result = label.getText();
204             }
205
206             public void getHelp(AccessibleEvent e) {
207                 e.result = label.getToolTipText();
208             }
209         });
210
211         accessible.addAccessibleControlListener(new AccessibleControlAdapter() {
212             public void getRole(AccessibleControlEvent e) {
213                 e.detail = ACC.ROLE_LINK;
214             }
215
216             public void getState(AccessibleControlEvent e) {
217                 if (hasFocus)
218                     e.detail = ACC.STATE_FOCUSABLE | ACC.STATE_LINKED
219                             | ACC.STATE_FOCUSED;
220                 else
221                     e.detail = ACC.STATE_FOCUSABLE | ACC.STATE_LINKED;
222
223             }
224         });
225     }
226     private void initAccessibleLabel() {
227         Accessible accessible = label.getAccessible();
228         accessible.addAccessibleControlListener(new AccessibleControlAdapter() {
229             public void getState(AccessibleControlEvent e) {
230                 if (hasFocus)
231                     e.detail = ACC.STATE_READONLY | ACC.STATE_FOCUSABLE
232                             | ACC.STATE_SELECTABLE | ACC.STATE_LINKED
233                             | ACC.STATE_FOCUSED;
234                 else
235                     e.detail = ACC.STATE_READONLY | ACC.STATE_FOCUSABLE
236                             | ACC.STATE_SELECTABLE | ACC.STATE_LINKED;
237
238             }
239         });
240     }
241 }
242
Popular Tags