package com.onaro.util.jfc; import java.awt.Font; import javax.swing.Icon; import javax.swing.JButton; import com.onaro.commons.swing.OnaroSwingUtilities; import com.onaro.util.jfc.TextComponentFactory.LabelFont; /** * An {@link ISummaryField} component that looks like a label * but is actually a {@link JButton}. */ public class ButtonField extends JButton implements ISummaryField { private static final long serialVersionUID = 1L; private Font normalFont; /** * Creates a {@link ButtonField}. * @param text displayed by the component. * The text is also used as the name of the component. */ public ButtonField (String text) { this (text, (Icon)null); } /** * Creates a {@link ButtonField}. * @param text displayed by the component. * The text is also used as the name of the component. * @param font {@link LabelFont} */ public ButtonField (String text, LabelFont font) { this (text, (Icon)null); setFont (font.getFont()); } /** * Creates a {@link ButtonField}. * @param icon displayed by the component */ public ButtonField (Icon icon) { this (null, icon); } /** * Creates a {@link ButtonField}. * @param text displayed by the component; * may be {@code null} to display no text. * If not null, it is also used as the name of the component. * @param icon displayed by the component; * may be {@code null} to display no icon */ public ButtonField (String text, Icon icon){ super(); setHorizontalAlignment (JButton.LEADING); setMargin (OnaroSwingUtilities.NO_INSETS); setBorder (OnaroSwingUtilities.NO_BORDER); setBorderPainted (false); setFocusPainted (false); setContentAreaFilled (false); setDefaultCapable (false); setDisplayedMnemonicIndex (-1); // Don't show mnemonic. setText (text); setIcon (icon); // If there's a text label, use it as the field's name. if (text != null) { setName (text); } normalFont = getFont(); } @Override public void setFont (Font font) { super.setFont (font); normalFont = font; } @Override public void clear() { setText (null); resetFont(); } @Override public boolean isHighlighted() { return Highlighter.isHighlighted (this); } @Override public void setHighlight (boolean doHighlight) { Highlighter.setHighlighted (this, doHighlight); TextComponentFactory.renderHighlight (this, doHighlight); } private void resetFont() { setForeground (TextComponentFactory.LABEL_TEXT_COLOR); super.setFont (normalFont); } }