package com.onaro.util.jfc; import java.awt.Font; import javax.swing.JLabel; import org.apache.commons.lang3.StringUtils; import com.onaro.util.jfc.TextComponentFactory.LabelFont; import com.onaro.util.jfc.TextComponentFactory.NullValue; /** * An {@link ISummaryField} component that displays a * {@link String} or a {@link NullValue}. */ public class DataField extends JLabel implements ISummaryField { private static final long serialVersionUID = 1L; private Font normalFont; private boolean isUnknownValueVisible = true; /** * Constant used to document and set isUnknownValueVisible argument * passed to {@link #setUnknownValueVisible} and {@link #DataField(String, LabelFont, boolean)} */ public static final boolean HIDE_UNKNOWN_VALUES = false; /** * Creates a {@link DataField} * @param name of the field, for testing purposes */ public DataField (String name) { super(); setName (name); normalFont = getFont(); } /** * Creates a {@link DataField} * @param name of the field, for testing purposes * @param font {@link LabelFont} */ public DataField (String name, LabelFont font) { this (name); setFont (font.getFont()); } /** * Creates a {@link DataField} * @param name of the field, for testing purposes * @param font {@link LabelFont} * @param isUnknownValueVisible if {@code true}, the field's visibility will * be independent of its value; if {@code false} (or {@link #HIDE_UNKNOWN_VALUES}) * the field will be initially invisible with {@link #setValue} changing its * visibility according to the value * @see #isUnknownValueVisible * @see #setUnknownValueVisible(boolean) */ public DataField (String name, LabelFont font, boolean isUnknownValueVisible) { this (name, font); this.isUnknownValueVisible = isUnknownValueVisible; if (! isUnknownValueVisible) { setVisible (false); } } @Override public void setFont (Font font) { super.setFont (font); normalFont = font; } /** * Determines whether the field should be be visible even if its value * is unknown or blank * @return {@code true} if visibility is independent of value; {@code false} * if {@link #setValue} changes the visibility according to the value * @see #setUnknownValueVisible(boolean) */ public boolean isUnknownValueVisible() { return isUnknownValueVisible; } /** * If {@link #isUnknownValueVisible()} is {@code true}, sets the field to display the value * whenever the field is visible. *

* If {@link #isUnknownValueVisible()} is {@code false}, then the field is hidden if value is * {@code null}, empty or all whitespace. Otherwise, it is shown with value displayed. *

* No formatting is applied to the value. * @param value {@link String} * @see #setValue(NullValue) */ public void setValue (String value) { if (isUnknownValueVisible) { // Just set value. setValueImpl (value); } else if (StringUtils.isNotBlank (value)) { // Set and show non-blank value. setValueImpl (value); setVisible (true); } else { // Null, empty, or blank values are considered unknown values and are hidden. setVisible (false); } } private void setValueImpl (String value) { setHighlight (false); resetFont(); setText (value); setToolTipText (value); } /** * If {@link #isUnknownValueVisible()} is {@code true}, sets the field to display the reason for a * null value whenever the field is visible. *

* If {@link #isUnknownValueVisible()} is {@code false}, hides the field. * @param value {@link NullValue} that explains why it's null */ public void setValue (NullValue value) { if (isUnknownValueVisible) { // TODO: highlight should be able to be more than binary (red/black/gray): setHighlight (false); Font font = getFont(); if (!font.isItalic()) { super.setFont (font.deriveFont (font.getStyle() + Font.ITALIC)); } setText (value.getDisplayText()); setToolTipText (null); } else { // A NullValue is considered an unknown value and is hidden. setVisible (false); } } @Override public void clear() { setText (null); setToolTipText (null); setHighlight (false); resetFont(); } @Override public boolean isHighlighted() { return Highlighter.isHighlighted (this); } @Override public void setHighlight (boolean doHighlight) { Highlighter.setHighlighted (this, doHighlight); TextComponentFactory.renderHighlight (this, doHighlight); } public void setHighlight (TextComponentFactory.ValueStatus status) { Highlighter.setHighlighted (this, ! status.isNormal()); TextComponentFactory.renderHighlight (this, status); } protected void resetFont() { setForeground (TextComponentFactory.NORMAL_VALUE_COLOR); super.setFont (normalFont); } }