Initial revision
[old-projects.git] / ekit / com / hexidec / ekit / component / PropertiesDialog.java
1 /*
2 GNU Lesser General Public License
3
4 PropertiesDialog
5 Copyright (C) 2003 Howard 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.component;
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.StringTokenizer;
32 import javax.swing.JCheckBox;
33 import javax.swing.JComboBox;
34 import javax.swing.JDialog;
35 import javax.swing.JOptionPane;
36 import javax.swing.JTextField;
37
38 import com.hexidec.util.Translatrix;
39
40 /** Class for providing a dialog that lets the user specify values for tag attributes
41 */
42 public class PropertiesDialog extends JDialog
43 {
44 private JOptionPane jOptionPane;
45 private Hashtable htInputFields;
46
47 public PropertiesDialog(Frame parent, String[] fields, String[] types, String[] values, String title, boolean bModal)
48 {
49 super(parent, title, bModal);
50 htInputFields = new Hashtable();
51 final Object[] buttonLabels = { Translatrix.getTranslationString("DialogAccept"), Translatrix.getTranslationString("DialogCancel") };
52 Object[] panelContents = new Object[(fields.length * 2)];
53 int objectCount = 0;
54 for(int iter = 0; iter < fields.length; iter++)
55 {
56 String fieldName = fields[iter];
57 String fieldType = types[iter];
58 Object fieldComponent;
59 if(fieldType.equals("text"))
60 {
61 fieldComponent = new JTextField(3);
62 if(values[iter] != null && values[iter].length() > 0)
63 {
64 ((JTextField)(fieldComponent)).setText(values[iter]);
65 }
66 }
67 else if(fieldType.equals("bool"))
68 {
69 fieldComponent = new JCheckBox();
70 if(values[iter] != null)
71 {
72 ((JCheckBox)(fieldComponent)).setSelected(values[iter] == "true");
73 }
74 }
75 else if(fieldType.equals("combo"))
76 {
77 fieldComponent = new JComboBox();
78 if(values[iter] != null)
79 {
80 StringTokenizer stParse = new StringTokenizer(values[iter], ",", false);
81 while(stParse.hasMoreTokens())
82 {
83 ((JComboBox)(fieldComponent)).addItem(stParse.nextToken());
84 }
85 }
86 }
87 else
88 {
89 fieldComponent = new JTextField(3);
90 }
91 htInputFields.put(fieldName, fieldComponent);
92 panelContents[objectCount] = fieldName; // Translatrix.getTranslationString(fieldName);
93 panelContents[objectCount + 1] = fieldComponent;
94 objectCount += 2;
95 }
96 jOptionPane = new JOptionPane(panelContents, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, buttonLabels, buttonLabels[0]);
97
98 setContentPane(jOptionPane);
99 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
100
101 jOptionPane.addPropertyChangeListener(new PropertyChangeListener() {
102 public void propertyChange(PropertyChangeEvent e)
103 {
104 String prop = e.getPropertyName();
105 if(isVisible() && (e.getSource() == jOptionPane) && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop.equals(JOptionPane.INPUT_VALUE_PROPERTY)))
106 {
107 Object value = jOptionPane.getValue();
108 if(value == JOptionPane.UNINITIALIZED_VALUE)
109 {
110 return;
111 }
112 if(value.equals(buttonLabels[0]))
113 {
114 setVisible(false);
115 }
116 else
117 {
118 setVisible(false);
119 }
120 }
121 }
122 });
123 this.pack();
124 }
125
126 public PropertiesDialog(Frame parent, String[] fields, String[] types, String title, boolean bModal)
127 {
128 this(parent, fields, types, new String[fields.length], title, bModal);
129 }
130
131 public String getFieldValue(String fieldName)
132 {
133 Object dataField = htInputFields.get(fieldName);
134 if(dataField instanceof JTextField)
135 {
136 return ((JTextField)dataField).getText();
137 }
138 else if(dataField instanceof JCheckBox)
139 {
140 if(((JCheckBox)dataField).isSelected())
141 {
142 return "true";
143 }
144 else
145 {
146 return "false";
147 }
148 }
149 else if(dataField instanceof JComboBox)
150 {
151 return (String)(((JComboBox)dataField).getSelectedItem());
152 }
153 else
154 {
155 return (String)null;
156 }
157 }
158
159 public String getDecisionValue()
160 {
161 return jOptionPane.getValue().toString();
162 }
163 }
164