KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.help.ui.internal;
12 import java.util.Enumeration JavaDoc;
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.swt.*;
16 import org.eclipse.swt.events.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.widgets.*;
19
20 public class HyperlinkHandler
21         implements
22             MouseListener,
23             MouseTrackListener,
24             PaintListener,
25             Listener {
26     public static final int UNDERLINE_NEVER = 1;
27     public static final int UNDERLINE_ROLLOVER = 2;
28     public static final int UNDERLINE_ALWAYS = 3;
29     private Cursor hyperlinkCursor;
30     private Cursor busyCursor;
31     private boolean hyperlinkCursorUsed = true;
32     private int hyperlinkUnderlineMode = UNDERLINE_ALWAYS;
33     private Color background;
34     private Color foreground;
35     private Color activeBackground;
36     private Color activeForeground;
37     private Hashtable JavaDoc hyperlinkListeners;
38     private Control lastLink;
39     /**
40      * HyperlinkHandler constructor comment.
41      */

42     public HyperlinkHandler() {
43         hyperlinkListeners = new Hashtable JavaDoc();
44         hyperlinkCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND);
45         busyCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_WAIT);
46     }
47     /**
48      */

49     public void dispose() {
50         hyperlinkCursor.dispose();
51         busyCursor.dispose();
52     }
53     /**
54      * @return org.eclipse.swt.graphics.Color
55      */

56     public Color getActiveBackground() {
57         return activeBackground;
58     }
59     /**
60      * @return org.eclipse.swt.graphics.Color
61      */

62     public Color getActiveForeground() {
63         return activeForeground;
64     }
65     /**
66      * @return org.eclipse.swt.graphics.Color
67      */

68     public Color getBackground() {
69         return background;
70     }
71     /**
72      * @return org.eclipse.swt.graphics.Cursor
73      */

74     public Cursor getBusyCursor() {
75         return busyCursor;
76     }
77     /**
78      * @return org.eclipse.swt.graphics.Color
79      */

80     public Color getForeground() {
81         return foreground;
82     }
83     /**
84      * @return org.eclipse.swt.graphics.Cursor
85      */

86     public Cursor getHyperlinkCursor() {
87         return hyperlinkCursor;
88     }
89     /**
90      * @return int
91      */

92     public int getHyperlinkUnderlineMode() {
93         return hyperlinkUnderlineMode;
94     }
95     /**
96      * @return org.eclipse.swt.widgets.Control
97      */

98     public Control getLastLink() {
99         return lastLink;
100     }
101     /**
102      * @return boolean
103      */

104     public boolean isHyperlinkCursorUsed() {
105         return hyperlinkCursorUsed;
106     }
107     public void mouseDoubleClick(MouseEvent e) {
108     }
109     public void mouseDown(MouseEvent e) {
110         if (e.button == 1)
111             return;
112         lastLink = (Control) e.widget;
113     }
114     public void mouseEnter(MouseEvent e) {
115         Control control = (Control) e.widget;
116
117         if (isHyperlinkCursorUsed())
118             control.setCursor(hyperlinkCursor);
119         if (activeBackground != null)
120             control.setBackground(activeBackground);
121         if (activeForeground != null)
122             control.setForeground(activeForeground);
123         if (hyperlinkUnderlineMode == UNDERLINE_ROLLOVER)
124             underline(control, true);
125         IHyperlinkListener action = getLinkListener(control);
126         if (action != null)
127             action.linkEntered(control);
128     }
129     public void mouseExit(MouseEvent e) {
130         Control control = (Control) e.widget;
131
132         if (isHyperlinkCursorUsed())
133             control.setCursor(null);
134         if (hyperlinkUnderlineMode == UNDERLINE_ROLLOVER)
135             underline(control, false);
136         if (background != null)
137             control.setBackground(background);
138         if (foreground != null)
139             control.setForeground(foreground);
140         IHyperlinkListener action = getLinkListener(control);
141         if (action != null)
142             action.linkExited(control);
143     }
144     public void mouseHover(MouseEvent e) {
145     }
146     public void mouseUp(MouseEvent e) {
147         if (e.button != 1)
148             return;
149         IHyperlinkListener action = getLinkListener((Control) e.widget);
150
151         if (action != null) {
152             Control c = (Control) e.widget;
153             c.setCursor(busyCursor);
154             action.linkActivated(c);
155             if (!c.isDisposed())
156                 c.setCursor(isHyperlinkCursorUsed() ? hyperlinkCursor : null);
157         }
158     }
159     public void paintControl(PaintEvent e) {
160         Control control = (Control) e.widget;
161         if (hyperlinkUnderlineMode == UNDERLINE_ALWAYS)
162             HyperlinkHandler.underline(control, true);
163     }
164     /**
165      * @param control
166      * org.eclipse.swt.widgets.Control
167      * @param listener
168      * org.eclipse.help.ui.internal.IHyperlinkListener
169      */

170     public void registerHyperlink(Control control, IHyperlinkListener listener) {
171         if (background != null)
172             control.setBackground(background);
173         if (foreground != null)
174             control.setForeground(foreground);
175         control.addMouseListener(this);
176         control.addMouseTrackListener(this);
177         control.addListener(SWT.DefaultSelection, this);
178
179         if (hyperlinkUnderlineMode == UNDERLINE_ALWAYS)
180             control.addPaintListener(this);
181         hyperlinkListeners.put(control, listener);
182         removeDisposedLinks();
183     }
184     public IHyperlinkListener getLinkListener(Control c) {
185         if (c instanceof Label)
186             c = c.getParent();
187         return (IHyperlinkListener) hyperlinkListeners.get(c);
188     }
189
190     private void removeDisposedLinks() {
191         for (Enumeration JavaDoc keys = hyperlinkListeners.keys(); keys
192                 .hasMoreElements();) {
193             Control control = (Control) keys.nextElement();
194             if (control.isDisposed()) {
195                 hyperlinkListeners.remove(control);
196             }
197         }
198     }
199     /**
200      */

201     public void reset() {
202         hyperlinkListeners.clear();
203     }
204     /**
205      * @param newActiveBackground
206      * org.eclipse.swt.graphics.Color
207      */

208     public void setActiveBackground(Color newActiveBackground) {
209         activeBackground = newActiveBackground;
210     }
211     /**
212      * @param newActiveForeground
213      * org.eclipse.swt.graphics.Color
214      */

215     public void setActiveForeground(Color newActiveForeground) {
216         activeForeground = newActiveForeground;
217     }
218     /**
219      * @param newBackground
220      * org.eclipse.swt.graphics.Color
221      */

222     public void setBackground(Color newBackground) {
223         background = newBackground;
224     }
225     /**
226      * @param newForeground
227      * org.eclipse.swt.graphics.Color
228      */

229     public void setForeground(Color newForeground) {
230         foreground = newForeground;
231     }
232     /**
233      * @param newHyperlinkCursorUsed
234      * boolean
235      */

236     public void setHyperlinkCursorUsed(boolean newHyperlinkCursorUsed) {
237         hyperlinkCursorUsed = newHyperlinkCursorUsed;
238     }
239     /**
240      * @param newHyperlinkUnderlineMode
241      * int
242      */

243     public void setHyperlinkUnderlineMode(int newHyperlinkUnderlineMode) {
244         hyperlinkUnderlineMode = newHyperlinkUnderlineMode;
245     }
246     /**
247      * @param control
248      * org.eclipse.swt.widgets.Control
249      * @param inside
250      * boolean
251      */

252     public static void underline(Control control, boolean inside) {
253
254         if (control instanceof HyperlinkLabel)
255             control = ((HyperlinkLabel) control).getLabel();
256
257         Composite parent = control.getParent();
258         Rectangle bounds = control.getBounds();
259         GC gc = new GC(parent);
260         Color color = inside ? control.getForeground() : control
261                 .getBackground();
262         gc.setForeground(color);
263         int y = bounds.y + bounds.height;
264         gc.drawLine(bounds.x, y, bounds.x + bounds.width, y);
265         gc.dispose();
266     }
267
268     /**
269      * Sent when an event that the receiver has registered for occurs.
270      *
271      * @param event
272      * the event which occurred
273      */

274     public void handleEvent(Event event) {
275         IHyperlinkListener listener = getLinkListener((Control) event.widget);
276         listener.linkActivated((Control) event.widget);
277     }
278 }
279
Popular Tags