1 19 20 package org.netbeans.modules.xml.xam.ui.column; 21 22 import java.awt.Color ; 23 import java.awt.Cursor ; 24 import java.awt.Font ; 25 import java.awt.Graphics ; 26 import java.awt.Graphics2D ; 27 import java.awt.Insets ; 28 import java.awt.Rectangle ; 29 import java.awt.RenderingHints ; 30 import java.awt.Toolkit ; 31 import java.awt.event.FocusEvent ; 32 import java.awt.event.FocusListener ; 33 import java.awt.event.MouseEvent ; 34 import java.awt.event.MouseListener ; 35 import java.util.Map ; 36 import javax.swing.JButton ; 37 import javax.swing.JLabel ; 38 import javax.swing.UIManager ; 39 import javax.swing.border.EmptyBorder ; 40 41 50 public class LinkButton extends JButton implements FocusListener , MouseListener { 51 private static final long serialVersionUID = 1L; 52 private static final Color LINK_COLOR = new Color (0x00, 0x00, 0xFF); 53 private static final int FONT_SIZE; 54 private boolean ignoreRevalidate = false; 55 private Color defaultColor; 56 57 static { 58 Font defaultFont = UIManager.getFont("TextField.font"); FONT_SIZE = defaultFont != null ? defaultFont.getSize() : 12; 60 } 61 62 public LinkButton(String label) { 63 super(label); 64 setBorder(new EmptyBorder (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 (0, 0, 0, 0)); 70 setBorderPainted(false); 71 setRolloverEnabled(true); 74 setContentAreaFilled(false); 75 addFocusListener(this); 76 defaultColor = getForeground(); 77 } 78 79 public void mouseClicked(MouseEvent e) { 80 } 81 82 public void mousePressed(MouseEvent e) { 83 } 84 85 public void mouseReleased(MouseEvent e) { 86 } 87 88 public void mouseEntered(MouseEvent e) { 89 this.ignoreRevalidate = true; 90 setForeground(LINK_COLOR); 91 this.ignoreRevalidate = false; 92 } 93 94 public void mouseExited(MouseEvent e) { 95 this.ignoreRevalidate = true; 96 setForeground(defaultColor); 97 this.ignoreRevalidate = false; 98 } 99 100 protected void paintComponent(Graphics g) { 101 Graphics2D g2 = (Graphics2D ) g; 102 Map rhints = (Map ) Toolkit.getDefaultToolkit().getDesktopProperty( 103 "awt.font.desktophints"); if (rhints == null && Boolean.getBoolean("swing.aatext")) { 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 e) { 126 Rectangle rect = getBounds(); 127 rect.grow(0, FONT_SIZE); 128 scrollRectToVisible(rect); 129 } 130 131 public void focusLost(FocusEvent e) { 132 } 133 } 134 | Popular Tags |