1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: FormPane.java 2091 2011-03-07 04:15:31Z dmsmith $
21   */
22  package org.crosswire.common.swing;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Component;
26  import java.awt.GridBagConstraints;
27  import java.awt.GridBagLayout;
28  import java.awt.Insets;
29  import java.util.Hashtable;
30  
31  import javax.swing.BorderFactory;
32  import javax.swing.JLabel;
33  import javax.swing.JPanel;
34  
35  /**
36   * A Panel customized to hold fields.
37   * 
38   * @see gnu.lgpl.License for license details.<br>
39   *      The copyright to this program is held by it's authors.
40   * @author Joe Walker [joe at eireneh dot com]
41   */
42  public class FormPane extends JPanel {
43      /**
44       * Create a FormPane
45       */
46      public FormPane() {
47          setLayout(new BorderLayout());
48          inner = new JPanel(new GridBagLayout());
49          add(inner, BorderLayout.NORTH);
50          setBorder(BorderFactory.createEmptyBorder());
51          inner.setBorder(BorderFactory.createEmptyBorder());
52          GuiUtil.applyDefaultOrientation(this);
53      }
54  
55      /**
56       * Add a field to this panel
57       * 
58       * @param prompt
59       *            The name for the field
60       * @param comp
61       *            The component to add alongside the label
62       */
63      public void addEntry(String prompt, String tooltip, Component comp) {
64          JLabel label = new JLabel(prompt);
65          label.setToolTipText(tooltip);
66  
67          GridBagConstraints c = new GridBagConstraints();
68          c.anchor = GridBagConstraints.LINE_END;
69          c.insets = new Insets(5, 5, 5, 5);
70  
71          c.gridwidth = GridBagConstraints.RELATIVE; // next-to-last
72          c.fill = GridBagConstraints.NONE; // reset to default
73          c.weightx = 0.0; // reset to default
74          inner.add(label, c);
75  
76          c.gridwidth = GridBagConstraints.REMAINDER; // end row
77          c.fill = GridBagConstraints.HORIZONTAL;
78          c.weightx = 1.0;
79          inner.add(comp, c);
80  
81          comps.put(prompt + SUFFIX_LABEL, label);
82          comps.put(prompt + SUFFIX_COMP, comp);
83      }
84  
85      /**
86       * Add a field to this panel
87       * 
88       * @param prompt
89       *            The name for the field
90       */
91      public void removeEntry(String prompt) {
92          inner.remove(comps.get(prompt + SUFFIX_LABEL));
93          inner.remove(comps.get(prompt + SUFFIX_COMP));
94  
95          comps.remove(prompt + SUFFIX_LABEL);
96          comps.remove(prompt + SUFFIX_COMP);
97      }
98  
99      /**
100      * Is this panel empty
101      */
102     public boolean isEmpty() {
103         return comps.size() == 0;
104     }
105 
106     /**
107      * Get a list of the labels
108      */
109     public String[] getFieldNames() {
110         int count = getComponentCount() / 2;
111         String[] list = new String[count];
112 
113         for (int i = 0; i < count; i++) {
114             JLabel label = (JLabel) getComponent(i * 2);
115             list[i] = label.getText();
116         }
117 
118         return list;
119     }
120 
121     /**
122      * Get at list of the values in the fields
123      */
124     public String[] getFieldValues() {
125         int count = getComponentCount() / 2;
126         String[] list = new String[count];
127 
128         for (int i = 0; i < count; i++) {
129             Component comp = getComponent(i * 2 + 1);
130             list[i] = GuiUtil.getText(comp);
131         }
132 
133         return list;
134     }
135 
136     private static final String SUFFIX_COMP = "_comp";
137     private static final String SUFFIX_LABEL = "_label";
138 
139     /**
140      * Serialization ID
141      */
142     private static final long serialVersionUID = 3258135738867790641L;
143 
144     private JPanel inner;
145 
146     /**
147      * A store of the available components
148      */
149     protected Hashtable<String, Component> comps = new Hashtable<String, Component>();
150 }
151