Class Field
- java.lang.Object
-
- ucar.ui.prefs.Field
-
- Direct Known Subclasses:
DateField
,DurationField
,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 class
Field.BeanTableField
This uses a BeanTable to display a list of beans.static class
Field.CheckBox
A boolean input box using a checkbox.static class
Field.Date
Data input for Datestatic class
Field.Double
Data input for double.static class
Field.EnumCombo
This allows user to make a choice from a collection of "type-safe enumeration" objects.static class
Field.Int
Data input for double.static class
Field.Password
A text input field which doesnt echo the input, for passwords.static class
Field.Text
String input field.static class
Field.TextArea
String input field using a TextArea.static class
Field.TextCombo
A text input field which keeps track of recent choices in a combobox.
-
Field Summary
Fields Modifier and Type Field Description protected String
label
protected EventListenerList
listenerList
protected String
name
protected JPopupMenu
popupMenu
protected Object
previousValue
protected PersistenceManager
storeData
protected Object
validValue
-
Constructor Summary
Constructors Modifier Constructor Description protected
Field(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 boolean
accept(StringBuffer buff)
Get current value from editComponent, save to store.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.void
addPopupMenuAction(String name, AbstractAction action)
void
addPropertyChangeListener(PropertyChangeListener pcl)
Register for PropertyChange events when the value of the Field changes.protected void
addStandardPopups()
void
addValidator(FieldValidator v)
Add a validator to this field.static String
dfrac(double d, int fixed_decimals)
Double value formatting with fixed number of digits to the right of the decimal point.protected void
finish()
JComponent
getDeepEditComponent()
abstract JComponent
getEditComponent()
Return the editing JComponentprotected abstract Object
getEditValue()
Get current value from editComponentString
getLabel()
Return the JLabel componentString
getName()
Return the name of the fieldPersistenceManager
getPersistenceManager()
Return the PersistenceManager componentprotected abstract Object
getStoreValue(Object defValue)
Get value from Store.String
getToolTipText()
Get the tooltipObject
getValue()
Get valid value as String, Double, Integer, etc.boolean
isEditable()
Return whether the field is editable, default == enabledboolean
isEnabled()
Return whether the field is enabledprotected void
next()
void
removePropertyChangeListener(PropertyChangeListener pcl)
Deregister for when the value changesprotected void
restoreValue(Object defValue)
protected void
sendEvent()
void
setEditable(boolean editable)
Set whether the field is editable, default == enabledprotected abstract void
setEditValue(Object value)
Set current value of editComponentvoid
setEnabled(boolean enable)
Set whether the field is enabledprotected void
setNewValueFromStore()
The value in the store has changed: update the edit component send event if its different from previousprotected abstract void
setStoreValue(Object newValue)
Put new value into Store.void
setToolTipText(String tip)
Set the tooltipvoid
setValue(Object newValue)
Set the current valid and edit value.protected boolean
validate(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
-
-