KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > welcome > content > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.welcome.content;
21
22 import java.awt.Cursor JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.FontMetrics JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Graphics2D JavaDoc;
28 import java.awt.Insets JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30 import java.awt.event.ActionListener 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 javax.swing.ImageIcon JavaDoc;
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.border.EmptyBorder JavaDoc;
39 import org.openide.util.Utilities;
40
41 /**
42  *
43  * @author S. Aubrecht
44  */

45 public abstract class LinkButton extends JButton JavaDoc
46         implements Constants, MouseListener JavaDoc, ActionListener JavaDoc, FocusListener JavaDoc {
47
48     private boolean underline = false;
49
50     final ImageIcon JavaDoc BULLET_ICON = new ImageIcon JavaDoc( Utilities.loadImage( BULLET_IMAGE ) );
51
52     public LinkButton( String JavaDoc label, boolean showBullet ) {
53         super( label );
54         setForeground( Utils.getColor(HEADER_TEXT_COLOR) );
55         setFont( REGULAR_FONT );
56         setBorder( new EmptyBorder JavaDoc(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 JavaDoc(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 JavaDoc e) {
75     }
76
77     public void mousePressed(MouseEvent JavaDoc e) {
78     }
79
80     public void mouseReleased(MouseEvent JavaDoc e) {
81     }
82
83     public void mouseEntered(MouseEvent JavaDoc e) {
84         underline = true;
85         setForeground( Utils.getColor(RSS_LINK_COLOR) );
86         repaint();
87         onMouseEntered( e );
88     }
89
90     public void mouseExited(MouseEvent JavaDoc e) {
91         underline = false;
92         setForeground( Utils.getColor(HEADER_TEXT_COLOR) );
93         repaint();
94         onMouseExited( e );
95     }
96
97     protected void paintComponent(Graphics JavaDoc g) {
98         Graphics2D JavaDoc g2 = Utils.prepareGraphics( g );
99         super.paintComponent(g2);
100
101         Dimension JavaDoc 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 JavaDoc e) {
110     }
111
112     public void focusGained(FocusEvent JavaDoc e) {
113         Rectangle JavaDoc rect = getBounds();
114         rect.grow( 0, FONT_SIZE );
115         scrollRectToVisible( rect );
116     }
117
118     protected void onMouseExited(MouseEvent JavaDoc e) {
119     }
120
121     protected void onMouseEntered(MouseEvent JavaDoc e) {
122     }
123
124     public void paint(Graphics JavaDoc g) {
125         super.paint(g);
126         if( underline ) {
127             Font JavaDoc f = getFont();
128             FontMetrics JavaDoc 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