comparison org.eclipse.ui.forms/src/org/eclipse/ui/forms/HyperlinkGroup.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.ui.forms.HyperlinkGroup;
14
15 import org.eclipse.ui.forms.HyperlinkSettings;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Event;
21 import org.eclipse.swt.widgets.Listener;
22 import org.eclipse.ui.forms.events.HyperlinkEvent;
23 import org.eclipse.ui.forms.events.IHyperlinkListener;
24 import org.eclipse.ui.forms.widgets.Hyperlink;
25
26 import java.lang.all;
27 import java.util.ArrayList;
28 import java.util.Set;
29
30 /**
31 * Manages a group of hyperlinks. It tracks activation, updates normal and
32 * active colors and updates underline state depending on the underline
33 * preference. Hyperlink labels are added to the group after creation and are
34 * automatically removed from the group when they are disposed.
35 *
36 * @since 3.0
37 */
38
39 public final class HyperlinkGroup : HyperlinkSettings {
40 private ArrayList links;
41 private Hyperlink lastActivated;
42 private Hyperlink lastEntered;
43 private GroupListener listener;
44 private bool isActiveBackgroundSet;
45 private bool isActiveForegroundSet;
46 private bool isBackgroundSet;
47 private bool isForegroundSet;
48
49 private class GroupListener : Listener, IHyperlinkListener {
50
51 private Color previousBackground;
52 private Color previousForeground;
53
54 public void handleEvent(Event e) {
55 switch (e.type) {
56 case SWT.MouseEnter :
57 onMouseEnter(e);
58 break;
59 case SWT.MouseExit :
60 onMouseExit(e);
61 break;
62 case SWT.MouseDown :
63 onMouseDown(e);
64 break;
65 case SWT.Dispose :
66 unhook(cast(Hyperlink) e.widget);
67 break;
68 default:
69 }
70 }
71 private void onMouseEnter(Event e) {
72 Hyperlink link = cast(Hyperlink) e.widget;
73 previousBackground = link.getBackground();
74 previousForeground = link.getForeground();
75 if (isActiveBackgroundSet)
76 link.setBackground(getActiveBackground());
77 if (isActiveForegroundSet)
78 link.setForeground(getActiveForeground());
79 if (getHyperlinkUnderlineMode() is UNDERLINE_HOVER)
80 link.setUnderlined(true);
81 link.setCursor(getHyperlinkCursor());
82 }
83 private void onMouseExit(Event e) {
84 Hyperlink link = cast(Hyperlink) e.widget;
85 if (isActiveBackgroundSet)
86 link.setBackground(previousBackground);
87 if (isActiveForegroundSet)
88 link.setForeground(previousForeground);
89 if (getHyperlinkUnderlineMode() is UNDERLINE_HOVER)
90 link.setUnderlined(false);
91 }
92 public void linkActivated(HyperlinkEvent e) {
93 }
94
95 public void linkEntered(HyperlinkEvent e) {
96 Hyperlink link = cast(Hyperlink) e.widget;
97 if (lastEntered !is null) {
98 linkExited(lastEntered);
99 }
100 lastEntered = link;
101 }
102
103 public void linkExited(HyperlinkEvent e) {
104 linkExited(cast(Hyperlink) e.widget);
105 }
106 private void linkExited(Hyperlink link) {
107 link.setCursor(null);
108 if (lastEntered is link)
109 lastEntered = null;
110 }
111 }
112
113 /**
114 * Creates a hyperlink group.
115 */
116
117 public this(Display display) {
118 links = new ArrayList();
119 super(display);
120 listener = new GroupListener();
121 }
122
123 /**
124 * Returns the link that has been active the last, or <code>null</code>
125 * if no link has been active yet or the last active link has been
126 * disposed.
127 *
128 * @return the last active link or <code>null</code>
129 */
130 public Hyperlink getLastActivated() {
131 return lastActivated;
132 }
133 /**
134 * Adds a hyperlink to the group to be jointly managed. Hyperlink will be
135 * managed until it is disposed. Settings like colors, cursors and modes
136 * will affect all managed hyperlinks.
137 *
138 * @param link
139 */
140
141 public void add(Hyperlink link) {
142 if (isBackgroundSet)
143 link.setBackground(getBackground());
144 if (isForegroundSet)
145 link.setForeground(getForeground());
146 if (getHyperlinkUnderlineMode() is UNDERLINE_ALWAYS)
147 link.setUnderlined(true);
148 hook(link);
149 }
150
151 /**
152 * Sets the new active hyperlink background for all the links.
153 *
154 * @param newActiveBackground
155 * the new active background
156 */
157 public void setActiveBackground(Color newActiveBackground) {
158 super.setActiveBackground(newActiveBackground);
159 isActiveBackgroundSet = true;
160 }
161
162 /**
163 * Sets the new active hyperlink foreground for all the links.
164 *
165 * @param newActiveForeground
166 * the new active foreground
167 */
168 public void setActiveForeground(Color newActiveForeground) {
169 super.setActiveForeground(newActiveForeground);
170 isActiveForegroundSet = true;
171 }
172
173 /**
174 * Sets the group background and also sets the background of all the
175 * currently managed links.
176 *
177 * @param bg
178 * the new background
179 */
180 public void setBackground(Color bg) {
181 super.setBackground(bg);
182 isBackgroundSet = true;
183 if (links !is null) {
184 for (int i = 0; i < links.size(); i++) {
185 Hyperlink label = cast(Hyperlink) links.get(i);
186 label.setBackground(bg);
187 }
188 }
189 }
190 /**
191 * Sets the group foreground and also sets the background of all the
192 * currently managed links.
193 *
194 * @param fg
195 * the new foreground
196 */
197 public void setForeground(Color fg) {
198 super.setForeground(fg);
199 isForegroundSet = true;
200 if (links !is null) {
201 for (int i = 0; i < links.size(); i++) {
202 Hyperlink label = cast(Hyperlink) links.get(i);
203 label.setForeground(fg);
204 }
205 }
206 }
207 /**
208 * Sets the hyperlink underline mode.
209 *
210 * @param mode
211 * the new hyperlink underline mode
212 * @see HyperlinkSettings
213 */
214 public void setHyperlinkUnderlineMode(int mode) {
215 super.setHyperlinkUnderlineMode(mode);
216 if (links !is null) {
217 for (int i = 0; i < links.size(); i++) {
218 Hyperlink label = cast(Hyperlink) links.get(i);
219 label.setUnderlined(mode is UNDERLINE_ALWAYS);
220 }
221 }
222 }
223
224 private void hook(Hyperlink link) {
225 link.addListener(SWT.MouseDown, listener);
226 link.addHyperlinkListener(listener);
227 link.addListener(SWT.Dispose, listener);
228 link.addListener(SWT.MouseEnter, listener);
229 link.addListener(SWT.MouseExit, listener);
230 links.add(link);
231 }
232
233 private void unhook(Hyperlink link) {
234 link.removeListener(SWT.MouseDown, listener);
235 link.removeHyperlinkListener(listener);
236 link.removeListener(SWT.MouseEnter, listener);
237 link.removeListener(SWT.MouseExit, listener);
238 if (lastActivated is link)
239 lastActivated = null;
240 if (lastEntered is link)
241 lastEntered = null;
242 links.remove(link);
243 }
244
245 private void onMouseDown(Event e) {
246 if (e.button is 1)
247 return;
248 lastActivated = cast(Hyperlink) e.widget;
249 }
250 }