Source code: com/hexidec/ekit/dialog/PropertiesDialogDefault.java
1 /*
2 GNU Lesser General Public License
3
4 PropertiesDialog
5 Copyright (C) 2002 Howard A Kistler
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 package com.hexidec.ekit.dialog;
23
24 import java.awt.Frame;
25 import java.awt.event.WindowAdapter;
26 import java.awt.event.WindowEvent;
27 import java.beans.PropertyChangeEvent;
28 import java.beans.PropertyChangeListener;
29 import java.util.Hashtable;
30 import java.util.Iterator;
31 //import java.util.LinkedHashMap;
32 import javax.swing.JCheckBox;
33 import javax.swing.JDialog;
34 import javax.swing.JOptionPane;
35 import javax.swing.JTextField;
36
37 import com.hexidec.util.Translatrix;
38
39 /** Class for providing a dialog that lets the user specify values for tag attributes
40 */
41 public class PropertiesDialogDefault extends JDialog
42 {
43 private JOptionPane jOptionPane;
44 private Hashtable htInputFields;
45
46 public PropertiesDialogDefault(Frame parent, String[] fields, String[] types, String[] initial, String title, boolean bModal)
47 {
48 super(parent, title, bModal);
49 htInputFields = new Hashtable();
50 final Object[] buttonLabels = { Translatrix.getTranslationString("DialogAccept"), Translatrix.getTranslationString("DialogCancel") };
51 Object[] panelContents = new Object[(fields.length * 2)];
52 int objectCount = 0;
53 for(int iter = 0; iter < fields.length; iter++)
54 {
55 String fieldName = fields[iter];
56 String fieldType = types[iter];
57 String fieldInitial = initial[iter];
58
59 Object fieldComponent;
60 if(fieldType.equals("text"))
61 {
62 fieldComponent = new JTextField(fieldInitial, 3);
63 }
64 else if(fieldType.equals("bool"))
65 {
66 fieldComponent = new JCheckBox();
67 }
68 else
69 {
70 fieldComponent = new JTextField(fieldInitial, 3);
71 }
72 htInputFields.put(fieldName, fieldComponent);
73 panelContents[objectCount] = fieldName; // Translatrix.getTranslationString(fieldName);
74 panelContents[objectCount + 1] = fieldComponent;
75 objectCount += 2;
76 }
77 jOptionPane = new JOptionPane(panelContents, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, buttonLabels, buttonLabels[0]);
78
79 setContentPane(jOptionPane);
80 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
81
82 addWindowListener(new WindowAdapter() {
83 public void windowClosing(WindowEvent we)
84 {
85 jOptionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
86 }
87 });
88
89 jOptionPane.addPropertyChangeListener(new PropertyChangeListener() {
90 public void propertyChange(PropertyChangeEvent e)
91 {
92 String prop = e.getPropertyName();
93 if(isVisible()
94 && (e.getSource() == jOptionPane)
95 && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop.equals(JOptionPane.INPUT_VALUE_PROPERTY)))
96 {
97 Object value = jOptionPane.getValue();
98 if (value == JOptionPane.UNINITIALIZED_VALUE)
99 {
100 return;
101 }
102 jOptionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
103 if(value.equals(buttonLabels[0]))
104 {
105 setVisible(false);
106 }
107 else
108 {
109 setVisible(false);
110 }
111 }
112 }
113 });
114 this.pack();
115 }
116
117 public String getFieldValue(String fieldName)
118 {
119 Object dataField = htInputFields.get(fieldName);
120 if(dataField instanceof JTextField)
121 {
122 return ((JTextField)dataField).getText();
123 }
124 else if(dataField instanceof JCheckBox)
125 {
126 if(((JCheckBox)dataField).isSelected())
127 {
128 return "true";
129 }
130 else
131 {
132 return "false";
133 }
134 }
135 else
136 {
137 return (String)null;
138 }
139 }
140
141 }
142