Package ucar.ui.prefs

Class PrefPanel

  • All Implemented Interfaces:
    ImageObserver, MenuContainer, Serializable, Accessible

    public class PrefPanel
    extends JPanel
    Create a User Preferences Panel or Dialog. A PrefPanel manages a set of Fields with convenience methods for rapidly creating User Dialogs whose values are made persistent by a Preferences Store. All Fields contained in the PrefPanel share the same Preferences, and so must have unique names. Send ActionEvent when "accept" button is pressed. Can also listen on individual Fields. You must call one finish() method exactly once, when you are done adding Fields.

    Example of use:

     PreferencesExt store = null;
     try {
       xstore = XMLStore.createFromFile("E:/dev/prefs/test/panel/panel.xml", null);
       store = xstore.getPreferences();
     } catch (Exception e) {
       System.exit(1);
     }
     PrefPanel pp = new PrefPanel("test", store);
     pp.addTextField("name", "name", "defValue");
     pp.newColumn();
     pp.addTextField("name2", "name2", "defValue22");
     pp.newColumn();
     pp.addTextField("name3", "name3", "defValue22 asd jalskdjalksjd");
     pp.finish();
     
     pp.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         // accept was called
       }
     });
     

    Form layout

    The PrefPanel is layed out with the jgoodies FormLayout layout manager and PanelBuilder, which use a row, colummn grid. Fields in the same column are aligned.

    There are 2 ways to do form layout: implicit and explicit. With implicit, the position is implicitly specified by the order the fields are added, using, for example:

       addDoubleField(String fldName, String label, double defValue)
     
    The fields are all added in a column. To start a new column, use setCursor().

    With explicit, you specify the row and col, and an optional constraint:

       addDoubleField(String fldName, String label, double defValue, int col, int row, String constraint)
     
    Row and column numbers are 0 based. Each field has a width of 2 columns (one for the label and one for the component) and a height of 1 row, unless you specify otherwise using a constraint. A heading takes up an entire row, spanning all columns
    See Also:
    Serialized Form
    • Constructor Detail

      • PrefPanel

        public PrefPanel​(String name,
                         PreferencesExt prefs)
        Constructor.
        Parameters:
        name - may be null.
        prefs - keep values in here; may be null.
      • PrefPanel

        public PrefPanel​(String name,
                         Preferences prefs,
                         PersistenceManager storeData)
        Constructor.
        Parameters:
        name - may be null.
        storeData - keep values in here; may be null.
    • Method Detail

      • addActionListener

        public void addActionListener​(ActionListener l)
        Add listener: action event sent if "apply" button is pressed
      • removeActionListener

        public void removeActionListener​(ActionListener l)
        Remove listener
      • accept

        public boolean accept()
        Call Field.accept() on all Fields. This puts any edits into the Store, and fires PropertyChangeEvents if any values change, and sends an ActionEvent to any listeners.
      • setEnabled

        public void setEnabled​(boolean enable)
        Set enabled on all the fields in the prefPanel
        Overrides:
        setEnabled in class JComponent
        Parameters:
        enable - enable if true
      • getFields

        public Iterator<Field> getFields()
        Iterator over the fields
      • getField

        @Nullable
        public Field getField​(String name)
        Find the field with the specified name.
        Parameters:
        name - of Field
        Returns:
        Field or null if not found
      • getFieldValue

        public Object getFieldValue​(String name)
        Get current value of the named field
        Parameters:
        name - of field
        Returns:
        value of named field
      • setFieldValue

        public void setFieldValue​(String name,
                                  Object value)
        Set the current value of the named field
        Parameters:
        name - of field
        value - of field
      • addButton

        public void addButton​(JComponent b)
        Add a button to the button panel
      • addField

        public Field addField​(Field fld)
        Add a field created by the user.
        Parameters:
        fld - add this field.
      • addField

        public Field addField​(Field fld,
                              int col,
                              int row,
                              String constraint)
      • addCheckBoxField

        public Field.CheckBox addCheckBoxField​(String fldName,
                                               String label,
                                               boolean defValue)
        Add a boolean field as a checkbox.
        Parameters:
        fldName - the name to store the data in the PersistenceManagerData
        label - used as the label on the panel
        defValue - default value
      • addCheckBoxField

        public Field.CheckBox addCheckBoxField​(String fldName,
                                               String label,
                                               boolean defValue,
                                               int col,
                                               int row)
      • addDateField

        public Field.Date addDateField​(String fldName,
                                       String label,
                                       Date defValue)
        Add a field that edits a date
        Parameters:
        fldName - the name to store the data in the PersistenceManagerData
        label - used as the label on the panel
        defValue - default value
      • addDoubleField

        public Field.Double addDoubleField​(String fldName,
                                           String label,
                                           double defValue)
        Add a field that edits a double
        Parameters:
        fldName - the name to store the data in the PersistenceManagerData
        label - used as the label on the panel
        defValue - default value
      • addDoubleField

        public Field.Double addDoubleField​(String fldName,
                                           String label,
                                           double defValue,
                                           int nfracDig,
                                           int col,
                                           int row,
                                           String constraint)
      • addIntField

        public Field.Int addIntField​(String fldName,
                                     String label,
                                     int defValue)
        Add a field that edits an integer
        Parameters:
        fldName - the name to store the data in the PersistenceManagerData
        label - used as the label on the panel
        defValue - default value
      • addPasswordField

        public Field.Password addPasswordField​(String fldName,
                                               String label,
                                               String defValue)
        Add a password text field.
        Parameters:
        fldName - the name to store the data in the PersistenceManagerData
        label - used as the label on the panel
        defValue - default value
      • addTextField

        public Field.Text addTextField​(String fldName,
                                       String label,
                                       String defValue)
        Add a text field.
        Parameters:
        fldName - the name to store the data in the PersistenceManagerData
        label - used as the label on the panel
        defValue - default value
        Returns:
        the Field.Text object that was added
      • addTextComboField

        public Field.TextCombo addTextComboField​(String fldName,
                                                 String label,
                                                 Collection defValues,
                                                 int nKeep,
                                                 boolean editable)
        Add a text combobox field.
        Parameters:
        fldName - the name to store the data in the PersistenceManagerData
        label - used as the label on the panel
        defValues - list of default values (Strings) to include in the comboBox. May be null. These are added to the combobox (at the end) no matter how many there are.
        nKeep - number of most recently used values to keep
        editable - whether the user can add new entries the list to select from.
      • addTextAreaField

        public Field.TextArea addTextAreaField​(String fldName,
                                               String label,
                                               String def,
                                               int nrows)
        Add a TextArea field.
        Parameters:
        fldName - the name to store the data in the PersistenceManagerData
        label - used as the label on the panel
        def - default value
        nrows - number of rows
      • addHeading

        public void addHeading​(String heading)
        Add a heading that takes no input
      • addHeading

        public void addHeading​(String heading,
                               int row)
        Add a heading at the specified row. this spans all columns
      • addComponent

        public void addComponent​(Component comp,
                                 int col,
                                 int row,
                                 String constraint)
        Add a Component.
      • addSeparator

        public void addSeparator()
        Add a seperator after the last field added.
      • addEmptyRow

        public void addEmptyRow​(int row,
                                int size)
        Add a seperator after the last field added.
      • setCursor

        public void setCursor​(int col,
                              int row)
        Start a new column. Everything added goes into a vertical column until another call to newColumn().
      • finish

        public void finish()
        Call this when you have finish constructing the panel, adding buttons in default spot
      • finish

        public void finish​(boolean addButtons)
        Call this when you have finish constructing the panel.
        Parameters:
        addButtons - if true, add buttons in default spot
      • finish

        public void finish​(boolean addButtons,
                           String where)
        Call when finished adding components to the PrefPanel.
        Parameters:
        addButtons - if true, add buttons
        where - BorderLayout.NORTH, SOUTH, EAST, WEST
      • findActiveFrame

        @Nullable
        public static Frame findActiveFrame()