KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > HyperlinkGroup


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;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.Event;
19 import org.eclipse.swt.widgets.Listener;
20 import org.eclipse.ui.forms.events.HyperlinkEvent;
21 import org.eclipse.ui.forms.events.IHyperlinkListener;
22 import org.eclipse.ui.forms.widgets.Hyperlink;
23
24 /**
25  * Manages a group of hyperlinks. It tracks activation, updates normal and
26  * active colors and updates underline state depending on the underline
27  * preference. Hyperlink labels are added to the group after creation and are
28  * automatically removed from the group when they are disposed.
29  *
30  * @since 3.0
31  */

32
33 public final class HyperlinkGroup extends HyperlinkSettings {
34     private ArrayList JavaDoc links = new ArrayList JavaDoc();
35     private Hyperlink lastActivated;
36     private Hyperlink lastEntered;
37     private GroupListener listener;
38     private boolean isActiveBackgroundSet;
39     private boolean isActiveForegroundSet;
40     private boolean isBackgroundSet;
41     private boolean isForegroundSet;
42
43     private class GroupListener implements Listener, IHyperlinkListener {
44         
45         private Color previousBackground;
46         private Color previousForeground;
47         
48         public void handleEvent(Event e) {
49             switch (e.type) {
50                 case SWT.MouseEnter :
51                     onMouseEnter(e);
52                     break;
53                 case SWT.MouseExit :
54                     onMouseExit(e);
55                     break;
56                 case SWT.MouseDown :
57                     onMouseDown(e);
58                     break;
59                 case SWT.Dispose :
60                     unhook((Hyperlink) e.widget);
61                     break;
62             }
63         }
64         private void onMouseEnter(Event e) {
65             Hyperlink link = (Hyperlink) e.widget;
66             previousBackground = link.getBackground();
67             previousForeground = link.getForeground();
68             if (isActiveBackgroundSet)
69                 link.setBackground(getActiveBackground());
70             if (isActiveForegroundSet)
71                 link.setForeground(getActiveForeground());
72             if (getHyperlinkUnderlineMode() == UNDERLINE_HOVER)
73                 link.setUnderlined(true);
74             link.setCursor(getHyperlinkCursor());
75         }
76         private void onMouseExit(Event e) {
77             Hyperlink link = (Hyperlink) e.widget;
78             if (isActiveBackgroundSet)
79                 link.setBackground(previousBackground);
80             if (isActiveForegroundSet)
81                 link.setForeground(previousForeground);
82             if (getHyperlinkUnderlineMode() == UNDERLINE_HOVER)
83                 link.setUnderlined(false);
84         }
85         public void linkActivated(HyperlinkEvent e) {
86         }
87
88         public void linkEntered(HyperlinkEvent e) {
89             Hyperlink link = (Hyperlink) e.widget;
90             if (lastEntered != null) {
91                 linkExited(lastEntered);
92             }
93             lastEntered = link;
94         }
95
96         public void linkExited(HyperlinkEvent e) {
97             linkExited((Hyperlink) e.widget);
98         }
99         private void linkExited(Hyperlink link) {
100             link.setCursor(null);
101             if (lastEntered == link)
102                 lastEntered = null;
103         }
104     }
105
106     /**
107      * Creates a hyperlink group.
108      */

109
110     public HyperlinkGroup(Display display) {
111         super(display);
112         listener = new GroupListener();
113     }
114
115     /**
116      * Returns the link that has been active the last, or <code>null</code>
117      * if no link has been active yet or the last active link has been
118      * disposed.
119      *
120      * @return the last active link or <code>null</code>
121      */

122     public Hyperlink getLastActivated() {
123         return lastActivated;
124     }
125     /**
126      * Adds a hyperlink to the group to be jointly managed. Hyperlink will be
127      * managed until it is disposed. Settings like colors, cursors and modes
128      * will affect all managed hyperlinks.
129      *
130      * @param link
131      */

132
133     public void add(Hyperlink link) {
134         if (isBackgroundSet)
135             link.setBackground(getBackground());
136         if (isForegroundSet)
137             link.setForeground(getForeground());
138         if (getHyperlinkUnderlineMode() == UNDERLINE_ALWAYS)
139             link.setUnderlined(true);
140         hook(link);
141     }
142     
143     /**
144      * Sets the new active hyperlink background for all the links.
145      *
146      * @param newActiveBackground
147      * the new active background
148      */

149     public void setActiveBackground(Color newActiveBackground) {
150         super.setActiveBackground(newActiveBackground);
151         isActiveBackgroundSet = true;
152     }
153     
154     /**
155      * Sets the new active hyperlink foreground for all the links.
156      *
157      * @param newActiveForeground
158      * the new active foreground
159      */

160     public void setActiveForeground(Color newActiveForeground) {
161         super.setActiveForeground(newActiveForeground);
162         isActiveForegroundSet = true;
163     }
164     
165     /**
166      * Sets the group background and also sets the background of all the
167      * currently managed links.
168      *
169      * @param bg
170      * the new background
171      */

172     public void setBackground(Color bg) {
173         super.setBackground(bg);
174         isBackgroundSet = true;
175         if (links != null) {
176             for (int i = 0; i < links.size(); i++) {
177                 Hyperlink label = (Hyperlink) links.get(i);
178                 label.setBackground(bg);
179             }
180         }
181     }
182     /**
183      * Sets the group foreground and also sets the background of all the
184      * currently managed links.
185      *
186      * @param fg
187      * the new foreground
188      */

189     public void setForeground(Color fg) {
190         super.setForeground(fg);
191         isForegroundSet = true;
192         if (links != null) {
193             for (int i = 0; i < links.size(); i++) {
194                 Hyperlink label = (Hyperlink) links.get(i);
195                 label.setForeground(fg);
196             }
197         }
198     }
199     /**
200      * Sets the hyperlink underline mode.
201      *
202      * @param mode
203      * the new hyperlink underline mode
204      * @see HyperlinkSettings
205      */

206     public void setHyperlinkUnderlineMode(int mode) {
207         super.setHyperlinkUnderlineMode(mode);
208         if (links != null) {
209             for (int i = 0; i < links.size(); i++) {
210                 Hyperlink label = (Hyperlink) links.get(i);
211                 label.setUnderlined(mode == UNDERLINE_ALWAYS);
212             }
213         }
214     }
215
216     private void hook(Hyperlink link) {
217         link.addListener(SWT.MouseDown, listener);
218         link.addHyperlinkListener(listener);
219         link.addListener(SWT.Dispose, listener);
220         link.addListener(SWT.MouseEnter, listener);
221         link.addListener(SWT.MouseExit, listener);
222         links.add(link);
223     }
224
225     private void unhook(Hyperlink link) {
226         link.removeListener(SWT.MouseDown, listener);
227         link.removeHyperlinkListener(listener);
228         link.removeListener(SWT.MouseEnter, listener);
229         link.removeListener(SWT.MouseExit, listener);
230         if (lastActivated == link)
231             lastActivated = null;
232         if (lastEntered == link)
233             lastEntered = null;
234         links.remove(link);
235     }
236
237     private void onMouseDown(Event e) {
238         if (e.button == 1)
239             return;
240         lastActivated = (Hyperlink) e.widget;
241     }
242 }
243
Popular Tags