/* * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.tools.visualvm.modules.appui.welcome; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import java.awt.Rectangle; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.border.EmptyBorder; /** * * @author S. Aubrecht */ public abstract class LinkButton extends JButton implements Constants, MouseListener, ActionListener, FocusListener { private boolean underline = false; public LinkButton( String label ) { super( label ); setForeground( Utils.getColor(LINK_COLOR) ); setFont( BUTTON_FONT ); setBorder( new EmptyBorder(1, 1, 1, 1) ); setCursor( Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) ); setHorizontalAlignment( JLabel.LEFT ); addMouseListener(this); setFocusable( true ); setMargin( new Insets(0, 0, 0, 0) ); setBorderPainted( false ); setFocusPainted( false ); setRolloverEnabled( true ); setContentAreaFilled( false ); addActionListener( this ); addFocusListener( this ); } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { if( isEnabled() ) { underline = true; setForeground( Utils.getColor(LINK_IN_FOCUS_COLOR) ); repaint(); onMouseEntered( e ); setForeground( Utils.getColor( MOUSE_OVER_LINK_COLOR ) ); } } public void mouseExited(MouseEvent e) { if( isEnabled() ) { underline = false; setForeground( Utils.getColor(isVisited() ? VISITED_LINK_COLOR : LINK_COLOR) ); repaint(); onMouseExited( e ); } } protected void paintComponent(Graphics g) { Graphics2D g2 = Utils.prepareGraphics( g ); super.paintComponent(g2); Dimension size = getSize(); if( hasFocus() && isEnabled() ) { g2.setStroke( LINK_IN_FOCUS_STROKE ); g2.setColor( Utils.getColor(LINK_IN_FOCUS_COLOR) ); g2.drawRect( 0, 0, size.width - 1, size.height - 1 ); } } public void focusLost(FocusEvent e) { } public void focusGained(FocusEvent e) { Rectangle rect = getBounds(); rect.grow( 0, FONT_SIZE ); scrollRectToVisible( rect ); } protected void onMouseExited(MouseEvent e) { } protected void onMouseEntered(MouseEvent e) { } @Override public void paint(Graphics g) { super.paint(g); if( underline && isEnabled() ) { Font f = getFont(); FontMetrics fm = getFontMetrics(f); int iconWidth = 0; if( null != getIcon() ) { iconWidth = getIcon().getIconWidth()+getIconTextGap(); } int x1 = iconWidth; int y1 = fm.getHeight(); int x2 = fm.stringWidth(getText()) + iconWidth; if( getText().length() > 0 ) g.drawLine(x1, y1, x2, y1); } } protected boolean isVisited() { return false; } }