1 19 20 package org.netbeans.modules.welcome.content; 21 22 import java.awt.Cursor ; 23 import java.awt.Dimension ; 24 import java.awt.Font ; 25 import java.awt.FontMetrics ; 26 import java.awt.Graphics ; 27 import java.awt.Graphics2D ; 28 import java.awt.Insets ; 29 import java.awt.Rectangle ; 30 import java.awt.event.ActionListener ; 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 javax.swing.ImageIcon ; 36 import javax.swing.JButton ; 37 import javax.swing.JLabel ; 38 import javax.swing.border.EmptyBorder ; 39 import org.openide.util.Utilities; 40 41 45 public abstract class LinkButton extends JButton 46 implements Constants, MouseListener , ActionListener , FocusListener { 47 48 private boolean underline = false; 49 50 final ImageIcon BULLET_ICON = new ImageIcon ( Utilities.loadImage( BULLET_IMAGE ) ); 51 52 public LinkButton( String label, boolean showBullet ) { 53 super( label ); 54 setForeground( Utils.getColor(HEADER_TEXT_COLOR) ); 55 setFont( REGULAR_FONT ); 56 setBorder( new EmptyBorder (1, 1, 1, 1) ); 57 setCursor( Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) ); 58 setHorizontalAlignment( JLabel.LEFT ); 59 addMouseListener(this); 60 setFocusable( true ); 61 if( showBullet ) 62 setIcon( BULLET_ICON ); 63 64 setMargin( new Insets (0, 0, 0, 0) ); 65 setBorderPainted( false ); 66 setFocusPainted( false ); 67 setRolloverEnabled( true ); 68 setContentAreaFilled( false ); 69 70 addActionListener( this ); 71 addFocusListener( this ); 72 } 73 74 public void mouseClicked(MouseEvent e) { 75 } 76 77 public void mousePressed(MouseEvent e) { 78 } 79 80 public void mouseReleased(MouseEvent e) { 81 } 82 83 public void mouseEntered(MouseEvent e) { 84 underline = true; 85 setForeground( Utils.getColor(RSS_LINK_COLOR) ); 86 repaint(); 87 onMouseEntered( e ); 88 } 89 90 public void mouseExited(MouseEvent e) { 91 underline = false; 92 setForeground( Utils.getColor(HEADER_TEXT_COLOR) ); 93 repaint(); 94 onMouseExited( e ); 95 } 96 97 protected void paintComponent(Graphics g) { 98 Graphics2D g2 = Utils.prepareGraphics( g ); 99 super.paintComponent(g2); 100 101 Dimension size = getSize(); 102 if( hasFocus() ) { 103 g2.setStroke( LINK_IN_FOCUS_STROKE ); 104 g2.setColor( Utils.getColor(LINK_IN_FOCUS_COLOR) ); 105 g2.drawRect( 0, 0, size.width - 1, size.height - 1 ); 106 } 107 } 108 109 public void focusLost(FocusEvent e) { 110 } 111 112 public void focusGained(FocusEvent e) { 113 Rectangle rect = getBounds(); 114 rect.grow( 0, FONT_SIZE ); 115 scrollRectToVisible( rect ); 116 } 117 118 protected void onMouseExited(MouseEvent e) { 119 } 120 121 protected void onMouseEntered(MouseEvent e) { 122 } 123 124 public void paint(Graphics g) { 125 super.paint(g); 126 if( underline ) { 127 Font f = getFont(); 128 FontMetrics fm = getFontMetrics(f); 129 int iconWidth = 0; 130 if( null != getIcon() ) { 131 iconWidth = getIcon().getIconWidth()+getIconTextGap(); 132 } 133 int x1 = iconWidth; 134 int y1 = fm.getHeight(); 135 int x2 = fm.stringWidth(getText()) + iconWidth; 136 if( getText().length() > 0 ) 137 g.drawLine(x1, y1, x2, y1); 138 } 139 } 140 } 141 | Popular Tags |