Class Field
- java.lang.Object
-
- ucar.ui.prefs.Field
-
- Direct Known Subclasses:
Field.BeanTableField,Field.CheckBox,Field.Date,Field.Double,Field.EnumCombo,Field.Int,Field.Text,Field.TextCombo
public abstract class Field extends Object
Data input fields, with an optional backing store.Each Field has a name, a JLabel and a JComponent for user editing/input. The name must be unique within the PersistenceManager and/or PrefPanel. A Field can optionally have a tooltip, and can be enabled/disabled from allowing editing.
Each Field has a current "valid value" object. The JComponent has an "edit value", which may be invalid. When the field loses focus, or accept() is called, the value is validated and transferred to the validValue. If not valid, the user cannot leave the field. The edit value is thus not visible until it has been accepted. If the new value is different from the previously valid value, a PropertyChangeEvent is thrown, and the canonical form is written back to the edit value.
Each Field optionally has a PersistenceManager, which may contain the "stored value" of the field. If it exists, it is used as the starting value, otherwise, the "default value" is used. The default value may be null.
When accept() is called, validate() is called on the edit value to ensure the field has a valid format. If so, the value is compared to the previously accepted value, and if different, a PropertyChangeEvent is sent, and the store is updated.
The specialized set/get like setText() or setDouble() set both the edit value and the store value. The setters throw an event if the new value is different from previous.
When the PersistenceManager sends a PreferenceChangeEvent, the field gets the new value and displays it. If different from previous value, a PropertyChangeEvent is sent. Thus the PersistenceManager object is the "model" and the Field is the "view", and they automatically stay in synch.
A PreferencesExt object is optionally used to save state about the Field UI.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classField.BeanTableFieldThis uses a BeanTable to display a list of beans.static classField.CheckBoxA boolean input box using a checkbox.static classField.DateData input for Datestatic classField.DoubleData input for double.static classField.EnumComboThis allows user to make a choice from a collection of "type-safe enumeration" objects.static classField.IntData input for double.static classField.PasswordA text input field which doesnt echo the input, for passwords.static classField.TextString input field.static classField.TextAreaString input field using a TextArea.static classField.TextComboA text input field which keeps track of recent choices in a combobox.
-
Field Summary
Fields Modifier and Type Field Description protected Stringlabelprotected EventListenerListlistenerListprotected Stringnameprotected JPopupMenupopupMenuprotected ObjectpreviousValueprotected PersistenceManagerstoreDataprotected ObjectvalidValue
-
Constructor Summary
Constructors Modifier Constructor Description protectedField(String name, String label, PersistenceManager storeData)Constructor
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract boolean_validate(StringBuffer buff)See if edit value is valid, put error message in buff.protected booleanaccept(StringBuffer buff)Get current value from editComponent, save to store.protected booleanacceptIfDifferent(Object newValue)See if this value is different from current accepted value (using equals()); If so, set old value to accepted value, then accepted value to this value.voidaddPopupMenuAction(String name, AbstractAction action)voidaddPropertyChangeListener(PropertyChangeListener pcl)Register for PropertyChange events when the value of the Field changes.protected voidaddStandardPopups()voidaddValidator(FieldValidator v)Add a validator to this field.static Stringdfrac(double d, int fixed_decimals)Double value formatting with fixed number of digits to the right of the decimal point.protected voidfinish()JComponentgetDeepEditComponent()abstract JComponentgetEditComponent()Return the editing JComponentprotected abstract ObjectgetEditValue()Get current value from editComponentStringgetLabel()Return the JLabel componentStringgetName()Return the name of the fieldPersistenceManagergetPersistenceManager()Return the PersistenceManager componentprotected abstract ObjectgetStoreValue(Object defValue)Get value from Store.StringgetToolTipText()Get the tooltipObjectgetValue()Get valid value as String, Double, Integer, etc.booleanisEditable()Return whether the field is editable, default == enabledbooleanisEnabled()Return whether the field is enabledprotected voidnext()voidremovePropertyChangeListener(PropertyChangeListener pcl)Deregister for when the value changesprotected voidrestoreValue(Object defValue)protected voidsendEvent()voidsetEditable(boolean editable)Set whether the field is editable, default == enabledprotected abstract voidsetEditValue(Object value)Set current value of editComponentvoidsetEnabled(boolean enable)Set whether the field is enabledprotected voidsetNewValueFromStore()The value in the store has changed: update the edit component send event if its different from previousprotected abstract voidsetStoreValue(Object newValue)Put new value into Store.voidsetToolTipText(String tip)Set the tooltipvoidsetValue(Object newValue)Set the current valid and edit value.protected booleanvalidate(StringBuffer buff)See if edit value is valid, put error message in buff.
-
-
-
Field Detail
-
name
protected String name
-
storeData
protected PersistenceManager storeData
-
listenerList
protected EventListenerList listenerList
-
label
protected String label
-
validValue
protected Object validValue
-
previousValue
protected Object previousValue
-
popupMenu
protected JPopupMenu popupMenu
-
-
Constructor Detail
-
Field
protected Field(String name, String label, PersistenceManager storeData)
Constructor- Parameters:
name- of the field; must be unique within the storelabel- to display to the userstoreData- store/fetch data from here, may be null.
-
-
Method Detail
-
finish
protected void finish()
-
getName
public String getName()
Return the name of the field
-
getLabel
public String getLabel()
Return the JLabel component
-
getPersistenceManager
public PersistenceManager getPersistenceManager()
Return the PersistenceManager component
-
isEnabled
public boolean isEnabled()
Return whether the field is enabled
-
setEnabled
public void setEnabled(boolean enable)
Set whether the field is enabled
-
isEditable
public boolean isEditable()
Return whether the field is editable, default == enabled
-
setEditable
public void setEditable(boolean editable)
Set whether the field is editable, default == enabled
-
setToolTipText
public void setToolTipText(String tip)
Set the tooltip
-
getToolTipText
public String getToolTipText()
Get the tooltip
-
addPropertyChangeListener
public void addPropertyChangeListener(PropertyChangeListener pcl)
Register for PropertyChange events when the value of the Field changes. When accept() is called, you will get a new PropertyChangeEvent(this, fldName, oldValue, newValue), where the oldValue, newValue will be String, Integer, Boolean, etc.
-
removePropertyChangeListener
public void removePropertyChangeListener(PropertyChangeListener pcl)
Deregister for when the value changes
-
getEditComponent
public abstract JComponent getEditComponent()
Return the editing JComponent
-
_validate
protected abstract boolean _validate(StringBuffer buff)
See if edit value is valid, put error message in buff.
-
getEditValue
protected abstract Object getEditValue()
Get current value from editComponent
-
setEditValue
protected abstract void setEditValue(Object value)
Set current value of editComponent
-
getStoreValue
protected abstract Object getStoreValue(Object defValue)
Get value from Store. Must be immutable or return a copy
-
setStoreValue
protected abstract void setStoreValue(Object newValue)
Put new value into Store. Must be immutable or make a copy
-
getDeepEditComponent
public JComponent getDeepEditComponent()
-
getValue
public Object getValue()
Get valid value as String, Double, Integer, etc. may be null if default value was null. You may want to call accept() first to send to Store.
-
setValue
public void setValue(Object newValue)
Set the current valid and edit value. No events are thrown.
-
addValidator
public void addValidator(FieldValidator v)
Add a validator to this field.- Parameters:
v- an implementation of FieldValidator.
-
validate
protected boolean validate(StringBuffer buff)
See if edit value is valid, put error message in buff.
-
accept
protected boolean accept(StringBuffer buff)
Get current value from editComponent, save to store. If different from old value, fire PropertyChangeEvent. Return false if invalid format, add error message to buff if not null.
-
acceptIfDifferent
protected boolean acceptIfDifferent(Object newValue)
See if this value is different from current accepted value (using equals()); If so, set old value to accepted value, then accepted value to this value.- Returns:
- true if its different.
-
restoreValue
protected void restoreValue(Object defValue)
-
setNewValueFromStore
protected void setNewValueFromStore()
The value in the store has changed: update the edit component send event if its different from previous
-
sendEvent
protected void sendEvent()
-
next
protected void next()
-
addStandardPopups
protected void addStandardPopups()
-
addPopupMenuAction
public void addPopupMenuAction(String name, AbstractAction action)
-
dfrac
public static String dfrac(double d, int fixed_decimals)
Double value formatting with fixed number of digits to the right of the decimal point.- Parameters:
d- the number to format.fixed_decimals- number of digits to the right of the decimal point- Returns:
- string representation, with specified number of decimal places
-
-