Package ucar.ui.prefs

Class 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.

    • Constructor Detail

      • Field

        protected Field​(String name,
                        String label,
                        PersistenceManager storeData)
        Constructor
        Parameters:
        name - of the field; must be unique within the store
        label - to display to the user
        storeData - 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()
      • 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