/* * UFO Saved Game Editor * Copyright (C) 2010 Christopher Davoren * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package net.rubikscomplex.ufosge.gui; import java.awt.Color; import java.awt.Cursor; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.geom.Rectangle2D; import java.util.LinkedList; import javax.swing.JLabel; /** * Implements a JLabel that contains a clickable hyperlink. * * Code taken from the Sun forums: * http://forums.sun.com/thread.jspa?threadID=613854 * * @author Chris Davoren */ public class HyperlinkLabel extends JLabel { protected final Color LINK_COLOR = Color.blue; protected LinkedList<ActionListener> actionListenerList = new LinkedList<ActionListener>(); protected boolean underline; protected String actionCommand; protected MouseListener mouseListener = new MouseAdapter() { @Override public void mouseEntered(MouseEvent me) { underline = true; repaint(); } @Override public void mouseExited(MouseEvent me) { underline = false; repaint(); } @Override public void mouseClicked(MouseEvent me) { fireActionEvent(); } }; public HyperlinkLabel() { this(""); } public HyperlinkLabel(String text) { super(text); setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); setForeground(LINK_COLOR); addMouseListener(mouseListener); setActionCommand(text); } public void setActionCommand(String command) { actionCommand = command; } public void addActionListener(ActionListener l) { if (!actionListenerList.contains(l)) { actionListenerList.add(l); } } public void removeActionListener(ActionListener l) { actionListenerList.remove(l); } protected void fireActionEvent() { ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand); for (ActionListener l : actionListenerList) { l.actionPerformed(e); } } @Override public void paint(Graphics g) { super.paint(g); if (underline) { // really all this size stuff below only needs to be recalculated if font or text changes Rectangle2D textBounds = getFontMetrics(getFont()).getStringBounds(getText(), g); //this layout stuff assumes the icon is to the left, or null int y = getHeight() / 2 + (int) (textBounds.getHeight() / 2); int w = (int) textBounds.getWidth(); int x = getIcon() == null ? 0 : getIcon().getIconWidth() + getIconTextGap(); g.setColor(getForeground()); g.drawLine(0, y, x + w, y); } } }