KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > util > JLinkButton


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks.util;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Cursor JavaDoc;
24 import java.awt.FontMetrics JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Insets JavaDoc;
27 import java.awt.Rectangle JavaDoc;
28 import java.net.URL JavaDoc;
29
30 import javax.swing.Action JavaDoc;
31 import javax.swing.ButtonModel JavaDoc;
32 import javax.swing.Icon JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.UIManager JavaDoc;
36 import javax.swing.plaf.ComponentUI JavaDoc;
37 import javax.swing.plaf.metal.MetalButtonUI JavaDoc;
38
39 /**
40  * Clickable Link
41  *
42  * Initial source code from
43  * http://www.java2s.com/Code/Java/Swing-Components/TreeTable.htm
44  *
45  * @author tl
46  */

47 public class JLinkButton extends JButton JavaDoc {
48     private static final String JavaDoc uiString = "LinkButtonUI";
49     
50     /** the text will always be underlined */
51     public static final int ALWAYS_UNDERLINE = 0;
52     
53     /** the text will only be underlined on mouse over. */
54     public static final int HOVER_UNDERLINE = 1;
55     
56     /** never underline the text. */
57     public static final int NEVER_UNDERLINE = 2;
58
59     /**
60      * Registers default UI class for JLinkButton.
61      */

62     public static void registerUI() {
63         UIManager.getDefaults().put("LinkButtonUI", "BasicLinkButtonUI");
64     }
65     
66     private int linkBehavior;
67     private Color JavaDoc linkColor;
68     private Color JavaDoc colorPressed;
69     private Color JavaDoc visitedLinkColor;
70     private Color JavaDoc disabledLinkColor;
71     private URL JavaDoc buttonURL;
72     private boolean isLinkVisited;
73
74     /**
75      * Empty link.
76      */

77     public JLinkButton() {
78         this(null, null, null);
79     }
80     
81     /**
82      * Constructor.
83      *
84      * @param action todo?
85      */

86     public JLinkButton(Action JavaDoc action) {
87         this();
88         setAction(action);
89     }
90     
91     /**
92      * Constructor.
93      *
94      * @param icon shows an icon.
95      */

96     public JLinkButton(Icon JavaDoc icon) {
97         this(null, icon, null);
98     }
99     
100     /**
101      * Constructor.
102      *
103      * @param s text of the button
104      */

105     public JLinkButton(String JavaDoc s) {
106         this(s, null, null);
107     }
108     
109     /**
110      * Constructor.
111      *
112      * @param url target URL
113      */

114     public JLinkButton(URL JavaDoc url) {
115         this(null, null, url);
116     }
117     
118     /**
119      * Constructor.
120      *
121      * @param s text
122      * @param url target url
123      */

124     public JLinkButton(String JavaDoc s, URL JavaDoc url) {
125         this(s, null, url);
126     }
127     
128     /**
129      * Constructor.
130      *
131      * @param icon an icon
132      * @param url target url
133      */

134     public JLinkButton(Icon JavaDoc icon, URL JavaDoc url) {
135         this(null, icon, url);
136     }
137
138     /*
139      * Constructor.
140      *
141      * @param text text of the button
142      * @param icon an icon
143      * @param url target url
144      */

145     public JLinkButton(String JavaDoc text, Icon JavaDoc icon, URL JavaDoc url) {
146         super(text, icon);
147         linkBehavior = ALWAYS_UNDERLINE;
148         linkColor = Color.blue;
149         colorPressed = Color.red;
150         visitedLinkColor = new Color JavaDoc(128, 0, 128);
151         if (text == null && url != null)
152             setText(url.toExternalForm());
153         setLinkURL(url);
154         setCursor(Cursor.getPredefinedCursor(12));
155         setBorderPainted(false);
156         setContentAreaFilled(false);
157         setRolloverEnabled(true);
158         setMargin(new Insets JavaDoc(0, 0, 0, 0));
159     }
160     
161     public void updateUI() {
162         setUI(BasicLinkButtonUI.createUI(this));
163     }
164     
165     
166     public String JavaDoc getUIClassID() {
167         return "LinkButtonUI";
168     }
169     
170     /**
171      * Setups the text for the tooltip.
172      */

173     private void setupToolTipText() {
174         String JavaDoc tip = null;
175         if (buttonURL != null)
176             tip = buttonURL.toExternalForm();
177         setToolTipText(tip);
178     }
179     
180     /**
181      * Sets the behaviour.
182      *
183      * @param bnew one of the *_UNDERLINE constants from this class.
184      */

185     public void setLinkBehavior(int bnew) {
186         if (bnew != ALWAYS_UNDERLINE && bnew != HOVER_UNDERLINE
187                 && bnew != NEVER_UNDERLINE)
188             throw new IllegalArgumentException JavaDoc("Not a legal LinkBehavior");
189
190         int old = linkBehavior;
191         linkBehavior = bnew;
192         firePropertyChange("linkBehavior", old, bnew);
193         repaint();
194     }
195     
196     /**
197      * Returns the current behaviour.
198      *
199      * @return one of the *_UNDERLINE constants from this class.
200      */

201     public int getLinkBehavior() {
202         return linkBehavior;
203     }
204     
205     /**
206      * Sets the color of the link.
207      *
208      * @param color new color
209      */

210     public void setLinkColor(Color JavaDoc color) {
211         Color JavaDoc colorOld = linkColor;
212         linkColor = color;
213         firePropertyChange("linkColor", colorOld, color);
214         repaint();
215     }
216
217     /**
218      * Returns the color of the link.
219      *
220      * @return color
221      */

222     public Color JavaDoc getLinkColor() {
223         return linkColor;
224     }
225     
226     /**
227      * Sets the color of the link used when the mouse is over it.
228      *
229      * @param colorNew new color
230      */

231     public void setActiveLinkColor(Color JavaDoc colorNew) {
232         Color JavaDoc colorOld = colorPressed;
233         colorPressed = colorNew;
234         firePropertyChange("activeLinkColor", colorOld, colorNew);
235         repaint();
236     }
237     
238     /**
239      * Returns the color of the link when the mouse is over it.
240      *
241      * @return color
242      */

243     public Color JavaDoc getActiveLinkColor() {
244         return colorPressed;
245     }
246     
247     /**
248      * Sets disabled color.
249      *
250      * @param color new color
251      */

252     public void setDisabledLinkColor(Color JavaDoc color) {
253         Color JavaDoc colorOld = disabledLinkColor;
254         disabledLinkColor = color;
255         firePropertyChange("disabledLinkColor", colorOld, color);
256         if (!isEnabled())
257             repaint();
258     }
259     
260     /**
261      * Returns disabled color.
262      *
263      * @return disabled color
264      */

265     public Color JavaDoc getDisabledLinkColor() {
266         return disabledLinkColor;
267     }
268     
269     /**
270      * Sets color for the visited state.
271      *
272      * @param colorNew new color
273      */

274     public void setVisitedLinkColor(Color JavaDoc colorNew) {
275         Color JavaDoc colorOld = visitedLinkColor;
276         visitedLinkColor = colorNew;
277         firePropertyChange("visitedLinkColor", colorOld, colorNew);
278         repaint();
279     }
280     
281     /**
282      * Returns color for the visited state.
283      *
284      * @return color
285      */

286     public Color JavaDoc getVisitedLinkColor() {
287         return visitedLinkColor;
288     }
289     
290     /**
291      * Returns the URL.
292      *
293      * @return URL
294      */

295     public URL JavaDoc getLinkURL() {
296         return buttonURL;
297     }
298     
299     /**
300      * Sets new URL.
301      *
302      * @param url new URL
303      */

304     public void setLinkURL(URL JavaDoc url) {
305         URL JavaDoc urlOld = buttonURL;
306         buttonURL = url;
307         setupToolTipText();
308         firePropertyChange("linkURL", urlOld, url);
309         revalidate();
310         repaint();
311     }
312     
313     /**
314      * Sets the visited flag.
315      *
316      * @param flagNew new value for the visited flag
317      */

318     public void setLinkVisited(boolean flagNew) {
319         boolean flagOld = isLinkVisited;
320         isLinkVisited = flagNew;
321         firePropertyChange("linkVisited", flagOld, flagNew);
322         repaint();
323     }
324     
325     /**
326      * Checks whether the link is visited.
327      *
328      * @return true = visited.
329      */

330     public boolean isLinkVisited() {
331         return isLinkVisited;
332     }
333     
334     protected String JavaDoc paramString() {
335         String JavaDoc str;
336         if (linkBehavior == ALWAYS_UNDERLINE)
337             str = "ALWAYS_UNDERLINE";
338         else if (linkBehavior == HOVER_UNDERLINE)
339             str = "HOVER_UNDERLINE";
340         else if (linkBehavior == NEVER_UNDERLINE)
341             str = "NEVER_UNDERLINE";
342         else
343             str = "SYSTEM_DEFAULT";
344         String JavaDoc colorStr = linkColor == null ? "" : linkColor.toString();
345         String JavaDoc colorPressStr = colorPressed == null ? "" : colorPressed
346                 .toString();
347         String JavaDoc disabledLinkColorStr = disabledLinkColor == null ? ""
348                 : disabledLinkColor.toString();
349         String JavaDoc visitedLinkColorStr = visitedLinkColor == null ? ""
350                 : visitedLinkColor.toString();
351         String JavaDoc buttonURLStr = buttonURL == null ? "" : buttonURL.toString();
352         String JavaDoc isLinkVisitedStr = isLinkVisited ? "true" : "false";
353         return super.paramString() + ",linkBehavior=" + str + ",linkURL="
354                 + buttonURLStr + ",linkColor=" + colorStr + ",activeLinkColor="
355                 + colorPressStr + ",disabledLinkColor=" + disabledLinkColorStr
356                 + ",visitedLinkColor=" + visitedLinkColorStr
357                 + ",linkvisitedString=" + isLinkVisitedStr;
358     }
359 }
360
361 /**
362  * Default UI delegate.
363  */

364 class BasicLinkButtonUI extends MetalButtonUI JavaDoc {
365     private static final BasicLinkButtonUI ui = new BasicLinkButtonUI();
366
367     /**
368      * Returns the UI for the specified component.
369      *
370      * @param jcomponent a JLinkButton
371      * @return created UI
372      */

373     public static ComponentUI JavaDoc createUI(JComponent JavaDoc jcomponent) {
374         return ui;
375     }
376     
377     protected void paintText(Graphics JavaDoc g, JComponent JavaDoc com, Rectangle JavaDoc rect,
378             String JavaDoc s) {
379         JLinkButton bn = (JLinkButton) com;
380         ButtonModel JavaDoc bnModel = bn.getModel();
381         Color JavaDoc color = bn.getForeground();
382         Object JavaDoc obj = null;
383         if (bnModel.isEnabled()) {
384             if (bnModel.isPressed())
385                 bn.setForeground(bn.getActiveLinkColor());
386             else if (bn.isLinkVisited())
387                 bn.setForeground(bn.getVisitedLinkColor());
388             
389             else
390                 bn.setForeground(bn.getLinkColor());
391         } else {
392             if (bn.getDisabledLinkColor() != null)
393                 bn.setForeground(bn.getDisabledLinkColor());
394         }
395         super.paintText(g, com, rect, s);
396         int behaviour = bn.getLinkBehavior();
397         boolean drawLine = false;
398         if (behaviour == JLinkButton.HOVER_UNDERLINE) {
399             if (bnModel.isRollover())
400                 drawLine = true;
401         } else if (behaviour == JLinkButton.ALWAYS_UNDERLINE)
402             drawLine = true;
403         if (!drawLine)
404             return;
405         FontMetrics JavaDoc fm = g.getFontMetrics();
406         int x = rect.x + getTextShiftOffset();
407         int y = (rect.y + fm.getAscent() + fm.getDescent() +
408                 getTextShiftOffset()) - 1;
409         if (bnModel.isEnabled()) {
410             g.setColor(bn.getForeground());
411             g.drawLine(x, y, (x + rect.width) - 1, y);
412         } else {
413             g.setColor(bn.getBackground().brighter());
414             g.drawLine(x, y, (x + rect.width) - 1, y);
415         }
416     }
417 }
Popular Tags