1
22 package org.crosswire.common.config.swing;
23
24 import java.awt.BorderLayout;
25 import java.awt.FlowLayout;
26 import java.awt.Font;
27 import java.awt.GridBagConstraints;
28 import java.awt.GridBagLayout;
29 import java.awt.Insets;
30 import java.io.IOException;
31 import java.io.ObjectInputStream;
32
33 import javax.swing.BorderFactory;
34 import javax.swing.DefaultComboBoxModel;
35 import javax.swing.JButton;
36 import javax.swing.JComponent;
37 import javax.swing.JLabel;
38 import javax.swing.JList;
39 import javax.swing.JOptionPane;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTextField;
43 import javax.swing.ListSelectionModel;
44 import javax.swing.border.Border;
45
46 import org.crosswire.common.config.Choice;
47 import org.crosswire.common.swing.ActionFactory;
48 import org.crosswire.common.swing.CWMsg;
49 import org.crosswire.common.swing.CWOptionPane;
50 import org.crosswire.common.swing.CWScrollPane;
51 import org.crosswire.common.swing.GuiUtil;
52 import org.crosswire.common.swing.CWOtherMsg;
53 import org.crosswire.common.util.Convert;
54
55
64 public class StringArrayField extends JPanel implements Field {
65
68 public StringArrayField() {
69 actions = new ActionFactory(this);
70
71 list_model = new DefaultComboBoxModel();
72 list = new JList(list_model);
73
74 JPanel buttons = new JPanel(new FlowLayout());
75
76 list.setFont(new Font("Monospaced", Font.PLAIN, 12));
77 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
78
79 JScrollPane scroll = new CWScrollPane(list);
80
81 buttons.add(new JButton(actions.addAction("Add", CWMsg.gettext("Add"))));
83 buttons.add(new JButton(actions.addAction("Remove", CWMsg.gettext("Remove"))));
85 buttons.add(new JButton(actions.addAction("Update", CWMsg.gettext("Update"))));
87
88 Border title = BorderFactory.createTitledBorder(CWOtherMsg.lookupText("Component Editor"));
89 Border pad = BorderFactory.createEmptyBorder(5, 5, 5, 5);
90 setBorder(BorderFactory.createCompoundBorder(title, pad));
91
92 setLayout(new BorderLayout());
93 add(scroll, BorderLayout.CENTER);
94 add(buttons, BorderLayout.PAGE_END);
95 GuiUtil.applyDefaultOrientation(this);
96 }
97
98
105 public void setChoice(Choice param) {
106 }
107
108
113 public String getValue() {
114 return Convert.stringArray2String(getArray(), SEPARATOR);
115 }
116
117
122 public String[] getArray() {
123 String[] retcode = new String[list_model.getSize()];
124 for (int i = 0; i < retcode.length; i++) {
125 retcode[i] = (String) list_model.getElementAt(i);
126 }
127
128 return retcode;
129 }
130
131
136 public void setValue(String value) {
137 setArray(Convert.string2StringArray(value, SEPARATOR));
138 }
139
140
146 public void setArray(String[] value) {
147 list_model = new DefaultComboBoxModel(value.clone());
148 list.setModel(list_model);
149 }
150
151
156 public JComponent getComponent() {
157 return this;
158 }
159
160
163 public void doAddEntry() {
164 InputPane input = new InputPane();
165
166 if (CWOptionPane.showConfirmDialog(this, input, CWOtherMsg.lookupText("New Class"), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
167 String new_name = input.name_field.getText();
168
169 list_model.addElement(new_name);
170 }
171 }
172
173
176 public void doUpdateEntry() {
177 InputPane input = new InputPane();
178 input.name_field.setText(currentValue());
179
180 if (CWOptionPane.showConfirmDialog(this, input, CWOtherMsg.lookupText("Edit Class"), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
181 String new_name = input.name_field.getText();
182
183 list_model.removeElement(currentValue());
184 list_model.addElement(new_name);
185 }
186 }
187
188
191 public void doRemoveEntry() {
192 list_model.removeElement(currentValue());
193 }
194
195
200 private String currentValue() {
201 return (String) list_model.getElementAt(list.getSelectedIndex());
202 }
203
204
211 private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
212 actions = new ActionFactory(this);
213 is.defaultReadObject();
214 }
215
216
219 public static class InputPane extends JPanel {
220
223 public InputPane() {
224 super(new GridBagLayout());
225
226 GridBagConstraints c = new GridBagConstraints();
227 c.anchor = GridBagConstraints.LINE_END;
228 c.insets = new Insets(0, 5, 0, 5);
229
230 c.gridwidth = GridBagConstraints.RELATIVE; c.fill = GridBagConstraints.NONE; c.weightx = 0.0; add(new JLabel(CWOtherMsg.lookupText("Name") + ':'), c);
234
235 c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.HORIZONTAL;
237 c.weightx = 1.0;
238 add(name_field, c);
239
240 setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
241 }
242
243
246 protected JTextField name_field = new JTextField();
247
248
251 private static final long serialVersionUID = 3256444715753878326L;
252 }
253
254
257 private static final String SEPARATOR = "#";
258
259 private transient ActionFactory actions;
260
261
264 private DefaultComboBoxModel list_model;
265
266
269 private JList list;
270
271
274 private static final long serialVersionUID = 3256444715753878326L;
275 }
276