Initial revision
[old-projects.git] / ekit / com / hexidec / ekit / component / PropertiesDialog.java
CommitLineData
c2da4d40
JL
1/*
2GNU Lesser General Public License
3
4PropertiesDialog
5Copyright (C) 2003 Howard Kistler
6
7This library is free software; you can redistribute it and/or
8modify it under the terms of the GNU Lesser General Public
9License as published by the Free Software Foundation; either
10version 2.1 of the License, or (at your option) any later version.
11
12This library is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public
18License along with this library; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*/
21
22package com.hexidec.ekit.component;
23
24import java.awt.Frame;
25import java.awt.event.WindowAdapter;
26import java.awt.event.WindowEvent;
27import java.beans.PropertyChangeEvent;
28import java.beans.PropertyChangeListener;
29import java.util.Hashtable;
30import java.util.Iterator;
31import java.util.StringTokenizer;
32import javax.swing.JCheckBox;
33import javax.swing.JComboBox;
34import javax.swing.JDialog;
35import javax.swing.JOptionPane;
36import javax.swing.JTextField;
37
38import com.hexidec.util.Translatrix;
39
40/** Class for providing a dialog that lets the user specify values for tag attributes
41 */
42public 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