KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > column > LinkButton


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui.column;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Cursor JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Graphics2D JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.RenderingHints JavaDoc;
30 import java.awt.Toolkit JavaDoc;
31 import java.awt.event.FocusEvent JavaDoc;
32 import java.awt.event.FocusListener JavaDoc;
33 import java.awt.event.MouseEvent JavaDoc;
34 import java.awt.event.MouseListener JavaDoc;
35 import java.util.Map JavaDoc;
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.UIManager JavaDoc;
39 import javax.swing.border.EmptyBorder JavaDoc;
40
41 /**
42  * A button that looks and feels like a hyperlink.
43  *
44  * <p><em>Based on HtmlTextLinkButton from ide/welcome module in
45  * NetBeans IDE.</em></p>
46  *
47  * @author S. Aubrecht
48  * @author Nathan Fiedler
49  */

50 public class LinkButton extends JButton JavaDoc implements FocusListener JavaDoc, MouseListener JavaDoc {
51     private static final long serialVersionUID = 1L;
52     private static final Color JavaDoc LINK_COLOR = new Color JavaDoc(0x00, 0x00, 0xFF);
53     private static final int FONT_SIZE;
54     private boolean ignoreRevalidate = false;
55     private Color JavaDoc defaultColor;
56
57     static {
58         Font JavaDoc defaultFont = UIManager.getFont("TextField.font"); // NOI18N
59
FONT_SIZE = defaultFont != null ? defaultFont.getSize() : 12;
60     }
61
62     public LinkButton(String JavaDoc label) {
63         super(label);
64         setBorder(new EmptyBorder JavaDoc(1, 1, 1, 1));
65         setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
66         setHorizontalAlignment(JLabel.LEFT);
67         addMouseListener(this);
68         setFocusable(true);
69         setMargin(new Insets JavaDoc(0, 0, 0, 0));
70         setBorderPainted(false);
71         // Focus needs to be painted for accessibility purposes.
72
// setFocusPainted(false);
73
setRolloverEnabled(true);
74         setContentAreaFilled(false);
75         addFocusListener(this);
76         defaultColor = getForeground();
77     }
78
79     public void mouseClicked(MouseEvent JavaDoc e) {
80     }
81
82     public void mousePressed(MouseEvent JavaDoc e) {
83     }
84
85     public void mouseReleased(MouseEvent JavaDoc e) {
86     }
87
88     public void mouseEntered(MouseEvent JavaDoc e) {
89         this.ignoreRevalidate = true;
90         setForeground(LINK_COLOR);
91         this.ignoreRevalidate = false;
92     }
93
94     public void mouseExited(MouseEvent JavaDoc e) {
95         this.ignoreRevalidate = true;
96         setForeground(defaultColor);
97         this.ignoreRevalidate = false;
98     }
99
100     protected void paintComponent(Graphics JavaDoc g) {
101         Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
102         Map JavaDoc rhints = (Map JavaDoc) Toolkit.getDefaultToolkit().getDesktopProperty(
103                 "awt.font.desktophints"); // NOI18N
104
if (rhints == null && Boolean.getBoolean("swing.aatext")) { // NOI18N
105
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
106                     RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
107         } else if (rhints != null) {
108             g2.addRenderingHints(rhints);
109         }
110         super.paintComponent(g2);
111     }
112
113     public void revalidate() {
114         if (!ignoreRevalidate) {
115             super.revalidate();
116         }
117     }
118
119     public void invalidate() {
120         if (!ignoreRevalidate) {
121             super.invalidate();
122         }
123     }
124
125     public void focusGained(FocusEvent JavaDoc e) {
126         Rectangle JavaDoc rect = getBounds();
127         rect.grow(0, FONT_SIZE);
128         scrollRectToVisible(rect);
129     }
130
131     public void focusLost(FocusEvent JavaDoc e) {
132     }
133 }
134
Popular Tags