001: package org.xorm.tools;
002:
003: import java.util.Properties;
004: import java.util.HashMap;
005: import java.awt.event.*;
006: import javax.swing.*;
007: import java.awt.*;
008: import java.util.*;
009: import java.io.*;
010: import org.xorm.tools.jar.JarClassFinder;
011:
012: public class PropertyEditor extends JPanel implements FocusListener,
013: ActionListener {
014: private static class DescribedValue {
015: private String value;
016: private String description;
017:
018: public DescribedValue(String value, String description) {
019: this .value = value;
020: this .description = description;
021: }
022:
023: public String getValue() {
024: return value;
025: }
026:
027: public String toString() {
028: return description;
029: }
030: }
031:
032: protected Properties properties;
033: protected HashMap widgetToName = new HashMap();
034: protected HashMap nameToWidget = new HashMap();
035: protected static final JarClassFinder finder = new JarClassFinder();
036:
037: public PropertyEditor(Properties properties) {
038: this .properties = properties;
039: setLayout(new GridLayout(0, 2));
040: }
041:
042: public Properties getProperties() {
043: return properties;
044: }
045:
046: public void resetProperties(Properties properties) {
047: this .properties = properties;
048: // Copy any managed properties into the UI
049: Iterator it = widgetToName.keySet().iterator();
050: while (it.hasNext()) {
051: Object o = it.next();
052: String prop = (String) widgetToName.get(o);
053: String value = null;
054: if (o instanceof JCheckBox) {
055: ((JCheckBox) o).setSelected(Boolean.valueOf(
056: properties.getProperty(prop)).booleanValue());
057: } else if (o instanceof JTextField) {
058: ((JTextField) o).setText(properties.getProperty(prop));
059: } else if (o instanceof JComboBox) {
060: ((JComboBox) o).getEditor().setItem(
061: new DescribedValue(
062: properties.getProperty(prop),
063: properties.getProperty(prop)));
064: }
065: ((Component) o).repaint();
066: }
067: }
068:
069: public void addProperty(String name, String description) {
070: addProperty(name, description, null, null);
071: }
072:
073: public void addProperty(String name, String description,
074: String dependsOn, String dependsOnValue) {
075: JTextField field = new JTextField(properties.getProperty(name),
076: 50);
077: field.addFocusListener(this );
078: JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
079: panel.add(new JLabel(description));
080: add(panel);
081: panel = new JPanel(new BorderLayout());
082: panel.add(field, BorderLayout.NORTH);
083: add(panel);
084: addManaged(field, name, dependsOn, dependsOnValue);
085: }
086:
087: private void addManaged(final Component widget, String name,
088: final String dependsOn, final String dependsOnValue) {
089: widgetToName.put(widget, name);
090: nameToWidget.put(name, widget);
091:
092: if (dependsOn != null) {
093: Component other = (Component) nameToWidget.get(dependsOn);
094: if (other instanceof JComboBox) {
095: ((JComboBox) other)
096: .addActionListener(new ActionListener() {
097: public void actionPerformed(ActionEvent e) {
098: Component c = (Component) e.getSource();
099: String name = (String) widgetToName
100: .get(c);
101: String value = properties
102: .getProperty(name);
103: widget.setEnabled(dependsOnValue
104: .equals(value));
105: }
106: });
107: } else {
108: other.addFocusListener(new FocusListener() {
109: public void focusGained(FocusEvent e) {
110: }
111:
112: public void focusLost(FocusEvent e) {
113: Component c = (Component) e.getSource();
114: String name = (String) widgetToName.get(c);
115: String value = properties.getProperty(name);
116: widget.setEnabled(dependsOnValue.equals(value));
117: }
118: });
119: }
120: }
121: }
122:
123: public void addBooleanProperty(String name, String description) {
124: boolean selected = "true".equals(properties.getProperty(name));
125: JCheckBox cb = new JCheckBox(description, selected);
126: cb.addFocusListener(this );
127: addManaged(cb, name, null, null);
128: add(new JLabel(""));
129: add(cb);
130: }
131:
132: public void addFileProperty(String name, String description,
133: final String extension) {
134: JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
135: panel.add(new JLabel(description));
136: add(panel);
137: panel = new JPanel(new BorderLayout());
138: final JTextField field = new JTextField(properties
139: .getProperty(name), 50);
140: field.addFocusListener(this );
141: addManaged(field, name, null, null);
142: JPanel panel2 = new JPanel(new BorderLayout());
143: panel2.add(field, BorderLayout.CENTER);
144: JButton button = new JButton("Browse...");
145: button.addActionListener(new ActionListener() {
146: public void actionPerformed(ActionEvent e) {
147: File selected = doSelectFile("", extension);
148: if (selected != null) {
149: field.setText(selected.toString());
150: }
151: }
152: });
153: panel2.add(button, BorderLayout.EAST);
154: panel.add(panel2, BorderLayout.NORTH);
155: add(panel);
156: }
157:
158: public void addChoiceProperty(String name, String description,
159: String[] choices) {
160: JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
161: panel.add(new JLabel(description));
162: add(panel);
163: panel = new JPanel(new BorderLayout());
164: JComboBox combo = new JComboBox(choices);
165: combo.getEditor().setItem(properties.getProperty(name));
166: combo.addActionListener(this );
167: panel.add(combo, BorderLayout.NORTH);
168: add(panel);
169: addManaged(combo, name, null, null);
170: }
171:
172: public void addClassProperty(final String name, String description,
173: String[] jarPaths, final Class type) {
174: JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
175: panel.add(new JLabel(description));
176: add(panel);
177: ArrayList a = new ArrayList();
178: for (int i = 0; i < jarPaths.length; i++) {
179: try {
180: a.addAll(finder.findClassesOfType(type, jarPaths[i]));
181: } catch (IOException e) {
182: }
183: }
184:
185: Class[] known = (Class[]) a.toArray(new Class[0]);
186: Object[] values = new Object[known.length];
187: for (int i = 0; i < known.length; i++) {
188: try {
189: ResourceBundle rb = ResourceBundle.getBundle(known[i]
190: .getName()
191: + "Parameters");
192: values[i] = new DescribedValue(known[i].getName(), rb
193: .getString("description"));
194: } catch (MissingResourceException e) {
195: values[i] = new DescribedValue(known[i].getName(),
196: known[i].getName());
197: }
198: }
199:
200: // Read the metadata for the class parameters
201: final JComboBox combo = new JComboBox(values);
202: combo.setEditable(true);
203: combo.getEditor().setItem(properties.getProperty(name));
204: combo.addActionListener(this );
205:
206: final JButton button = new JButton("Add...");
207: final JButton config = new JButton("Configure...");
208: button.addActionListener(new ActionListener() {
209: public void actionPerformed(ActionEvent e) {
210: File selectedJar = doSelectFile(
211: "Java Archive (JAR) Files", ".jar");
212: if (selectedJar != null) {
213: try {
214: Collection c = finder.findClassesOfType(type,
215: selectedJar);
216: Iterator it = c.iterator();
217: while (it.hasNext()) {
218: Class clazz = (Class) it.next();
219: combo.addItem(clazz.getName());
220: }
221: } catch (IOException ee) {
222: }
223: }
224: }
225: });
226: config.addActionListener(new ActionListener() {
227: public void actionPerformed(ActionEvent e) {
228: DescribedValue value = (DescribedValue) combo
229: .getEditor().getItem();
230: subeditor(value);
231: }
232: });
233: addManaged(combo, name, null, null);
234: JPanel center = new JPanel(new BorderLayout());
235: center.add(combo, BorderLayout.NORTH);
236:
237: JPanel east = new JPanel();
238: east.add(button);
239: east.add(config);
240:
241: JPanel both = new JPanel(new BorderLayout());
242: both.add(east, BorderLayout.EAST);
243: both.add(center, BorderLayout.CENTER);
244:
245: add(both);
246: }
247:
248: private File doSelectFile(final String description,
249: final String extension) {
250: final JFileChooser fc = new JFileChooser(".");
251: fc.setFileFilter(new javax.swing.filechooser.FileFilter() {
252: public boolean accept(File file) {
253: return file.isDirectory()
254: || file.getName().endsWith(extension);
255: }
256:
257: public String getDescription() {
258: return description;
259: }
260: });
261: fc.setDialogTitle("Choose File");
262: int returnVal = fc.showDialog(this , "Open");
263: if (returnVal != fc.APPROVE_OPTION) {
264: return null;
265: }
266: return fc.getSelectedFile();
267: }
268:
269: // FocusListener interface
270: public void focusGained(FocusEvent e) {
271: }
272:
273: public void focusLost(FocusEvent e) {
274: Component c = (Component) e.getSource();
275: String name = (String) widgetToName.get(c);
276: if (name != null) {
277: if (c instanceof JTextField) {
278: JTextField field = (JTextField) c;
279: properties.setProperty(name, field.getText());
280: } else if (c instanceof JCheckBox) {
281: JCheckBox cb = (JCheckBox) c;
282: properties.setProperty(name, String.valueOf(cb
283: .isSelected()));
284: }
285: }
286: }
287:
288: // ActionListener interface
289: public void actionPerformed(ActionEvent e) {
290: Component c = (Component) e.getSource();
291: String name = (String) widgetToName.get(c);
292: if (c instanceof JComboBox) {
293: JComboBox combo = (JComboBox) c;
294: Object value = combo.getEditor().getItem();
295: if (value == null) {
296: value = "";
297: }
298: if (value instanceof DescribedValue) {
299: properties.setProperty(name, ((DescribedValue) value)
300: .getValue());
301: } else {
302: properties.setProperty(name, value.toString());
303: }
304: }
305: }
306:
307: public void subeditor(DescribedValue value) {
308: String desc = value.getValue();
309: String name = value.toString();
310: ResourceBundle rb = ResourceBundle.getBundle(desc
311: + "Parameters");
312: PropertyEditor sub = new PropertyEditor(properties);
313: Enumeration en = rb.getKeys();
314: while (en.hasMoreElements()) {
315: String prop = (String) en.nextElement();
316: if (prop.endsWith(".description")) {
317: // Add a property to the sheet
318: prop = prop.substring(0, prop.length()
319: - ".description".length());
320: String type = null;
321: try {
322: type = rb.getString(prop + ".type");
323: } catch (MissingResourceException e) {
324: }
325: if ("boolean".equals(type)) {
326: sub.addBooleanProperty(prop, rb.getString(prop
327: + ".description"));
328: } else if ("choice".equals(type)) {
329:
330: sub.addChoiceProperty(prop, rb.getString(prop
331: + ".description"), rb.getString(
332: prop + ".choices").split(","));
333: } else {
334: sub.addProperty(prop, rb.getString(prop
335: + ".description"));
336: }
337: }
338: }
339:
340: final JDialog dialog = new JDialog((Frame) null, name
341: + " Properties", true);
342: BorderLayout layout = new BorderLayout();
343: JPanel panel = new JPanel(layout);
344: panel.add(sub, BorderLayout.CENTER);
345: JPanel holder = new JPanel();
346: JButton close = new JButton("Close");
347: close.addActionListener(new ActionListener() {
348: public void actionPerformed(ActionEvent e) {
349: dialog.setVisible(false);
350: }
351: });
352: holder.add(close);
353: panel.add(holder, BorderLayout.SOUTH);
354: dialog.setContentPane(panel);
355: Dimension d = panel.getMinimumSize();
356: dialog.setSize(d.width, (int) (d.height * 1.5));
357: dialog.show();
358: }
359: }
|