1
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
42 public class FormPane extends JPanel {
43
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
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; c.fill = GridBagConstraints.NONE; c.weightx = 0.0; inner.add(label, c);
75
76 c.gridwidth = GridBagConstraints.REMAINDER; 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
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
102 public boolean isEmpty() {
103 return comps.size() == 0;
104 }
105
106
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
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
142 private static final long serialVersionUID = 3258135738867790641L;
143
144 private JPanel inner;
145
146
149 protected Hashtable<String, Component> comps = new Hashtable<String, Component>();
150 }
151