package com.onaro.util.jfc.mixedvalue; import static com.onaro.util.jfc.mixedvalue.MixedValueConstants.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * An extension to {@link JTextField} allowing for a special "mixed" valued. Used when editing multiple values which are * not the same, though the user can set all of them to the same new value typed in to the field. *

* Once the user clics or types in the field, the mixed mode, if set, is cleared and the value clears. */ public class MixedValueTextField extends JTextField implements MouseListener, KeyListener { private static final long serialVersionUID = 1L; /** * Tells if the current value is this special "mixed". */ private boolean mixed; /** * Keeps the standard forground color while showing the special "mixed" color. */ private Color regularColor; /** * Font to use for the special "mixed" value. */ private Font italicFont; /** * Standard font to use for any value but the special "mixed" value. */ private Font plainFont; /** * Initializes this mixed-value text field. Records the default colors and fonts. Registers the listeners that will * clear the "mixed mode" once the user clicks or types in the field. */ public MixedValueTextField() { this.regularColor = this.getForeground(); italicFont = getFont().deriveFont(Font.ITALIC); plainFont = getFont().deriveFont(Font.PLAIN); addMouseListener(this); addKeyListener(this); } /** * Sets the mixed mode of this field. When set to true, the field will show "mixed" in italic, blue font. * @param mixed true if the field should enter the "mixed mode" */ public void setMixed(boolean mixed) { if (this.mixed == mixed) return; this.mixed = mixed; if (mixed) { super.setText(MIXED_VALUE); setForeground(UIManager.getColor("mixedValue.color")); //$NON-NLS-1$ setFont(italicFont); } else { setText(null); setForeground(regularColor); setFont(plainFont); } } /** * Tells if the field is currently in "mixed mode". * @return true if the field is in "mixed mode" */ public boolean isMixed() { return mixed; } /** * Clears the "mixed mode" upon mouse-click. */ public void mouseClicked(MouseEvent e) { setMixed(false); } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void keyTyped(KeyEvent e) {} /** * Clears the "mixed mode" upon key-press in the field (needed when the field is in focus due to usage of tabs. */ public void keyPressed(KeyEvent e) { setMixed(false); } public void keyReleased(KeyEvent e) {} }