001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.xam.ui.column;
043:
044: import java.awt.Color;
045: import java.awt.Cursor;
046: import java.awt.Font;
047: import java.awt.Graphics;
048: import java.awt.Graphics2D;
049: import java.awt.Insets;
050: import java.awt.Rectangle;
051: import java.awt.RenderingHints;
052: import java.awt.Toolkit;
053: import java.awt.event.FocusEvent;
054: import java.awt.event.FocusListener;
055: import java.awt.event.MouseEvent;
056: import java.awt.event.MouseListener;
057: import java.util.Map;
058: import javax.swing.JButton;
059: import javax.swing.JLabel;
060: import javax.swing.UIManager;
061: import javax.swing.border.EmptyBorder;
062:
063: /**
064: * A button that looks and feels like a hyperlink.
065: *
066: * <p><em>Based on HtmlTextLinkButton from ide/welcome module in
067: * NetBeans IDE.</em></p>
068: *
069: * @author S. Aubrecht
070: * @author Nathan Fiedler
071: */
072: public class LinkButton extends JButton implements FocusListener,
073: MouseListener {
074: private static final long serialVersionUID = 1L;
075: private static final Color LINK_COLOR = new Color(0x00, 0x00, 0xFF);
076: private static final int FONT_SIZE;
077: private boolean ignoreRevalidate = false;
078: private Color defaultColor;
079:
080: static {
081: Font defaultFont = UIManager.getFont("TextField.font"); // NOI18N
082: FONT_SIZE = defaultFont != null ? defaultFont.getSize() : 12;
083: }
084:
085: public LinkButton(String label) {
086: super (label);
087: setBorder(new EmptyBorder(1, 1, 1, 1));
088: setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
089: setHorizontalAlignment(JLabel.LEFT);
090: addMouseListener(this );
091: setFocusable(true);
092: setMargin(new Insets(0, 0, 0, 0));
093: setBorderPainted(false);
094: // Focus needs to be painted for accessibility purposes.
095: // setFocusPainted(false);
096: setRolloverEnabled(true);
097: setContentAreaFilled(false);
098: addFocusListener(this );
099: defaultColor = getForeground();
100: }
101:
102: public void mouseClicked(MouseEvent e) {
103: }
104:
105: public void mousePressed(MouseEvent e) {
106: }
107:
108: public void mouseReleased(MouseEvent e) {
109: }
110:
111: public void mouseEntered(MouseEvent e) {
112: this .ignoreRevalidate = true;
113: setForeground(LINK_COLOR);
114: this .ignoreRevalidate = false;
115: }
116:
117: public void mouseExited(MouseEvent e) {
118: this .ignoreRevalidate = true;
119: setForeground(defaultColor);
120: this .ignoreRevalidate = false;
121: }
122:
123: protected void paintComponent(Graphics g) {
124: Graphics2D g2 = (Graphics2D) g;
125: Map rhints = (Map) Toolkit.getDefaultToolkit()
126: .getDesktopProperty("awt.font.desktophints"); // NOI18N
127: if (rhints == null && Boolean.getBoolean("swing.aatext")) { // NOI18N
128: g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
129: RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
130: } else if (rhints != null) {
131: g2.addRenderingHints(rhints);
132: }
133: super .paintComponent(g2);
134: }
135:
136: public void revalidate() {
137: if (!ignoreRevalidate) {
138: super .revalidate();
139: }
140: }
141:
142: public void invalidate() {
143: if (!ignoreRevalidate) {
144: super .invalidate();
145: }
146: }
147:
148: public void focusGained(FocusEvent e) {
149: Rectangle rect = getBounds();
150: rect.grow(0, FONT_SIZE);
151: scrollRectToVisible(rect);
152: }
153:
154: public void focusLost(FocusEvent e) {
155: }
156: }
|