filebot/source/net/sourceforge/tuned/ui/LinkButton.java

125 lines
2.4 KiB
Java
Raw Normal View History

package net.sourceforge.tuned.ui;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
public class LinkButton extends JButton {
private Color color = getForeground();
2012-10-08 12:42:23 +00:00
private Color rolloverColor = new Color(0x3399FF);
public LinkButton(String text, Icon icon, URI uri) {
this(new OpenUriAction(text, icon, uri));
}
2012-10-08 12:42:23 +00:00
public LinkButton(Action action) {
setAction(action);
setFocusPainted(false);
setOpaque(false);
setContentAreaFilled(false);
setBorder(null);
setHorizontalAlignment(LEFT);
setIconTextGap(6);
setRolloverEnabled(true);
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
2012-10-08 12:42:23 +00:00
@Override
public void setRolloverEnabled(boolean enabled) {
super.setRolloverEnabled(enabled);
// always remove listener if there is one
removeMouseListener(rolloverListener);
if (enabled) {
// add listener again, if enabled
addMouseListener(rolloverListener);
}
}
2012-10-08 12:42:23 +00:00
public Color getColor() {
return color;
}
2012-10-08 12:42:23 +00:00
public void setColor(Color color) {
this.color = color;
this.setForeground(color);
}
2012-10-08 12:42:23 +00:00
public Color getRolloverColor() {
return rolloverColor;
}
2012-10-08 12:42:23 +00:00
public void setRolloverColor(Color rolloverColor) {
this.rolloverColor = rolloverColor;
}
protected final MouseListener rolloverListener = new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
setForeground(rolloverColor);
}
2012-10-08 12:42:23 +00:00
@Override
public void mouseExited(MouseEvent e) {
setForeground(color);
}
};
2012-10-08 12:42:23 +00:00
protected static class OpenUriAction extends AbstractAction {
public static final String URI = "uri";
2012-10-08 12:42:23 +00:00
public OpenUriAction(String text, Icon icon, URI uri) {
super(text, icon);
putValue(URI, uri);
}
2012-10-08 12:42:23 +00:00
@Override
public void actionPerformed(ActionEvent evt) {
try {
URI uri = (URI) getValue(URI);
if (uri != null) {
Desktop.getDesktop().browse(uri);
}
} catch (Exception e) {
// should not happen
Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.toString(), e);
}
}
}
}