Chris@2
|
1 /*
|
Chris@2
|
2 Copyright (C) 2001, 2006 by Simon Dixon
|
Chris@2
|
3
|
Chris@2
|
4 This program is free software; you can redistribute it and/or modify
|
Chris@2
|
5 it under the terms of the GNU General Public License as published by
|
Chris@2
|
6 the Free Software Foundation; either version 2 of the License, or
|
Chris@2
|
7 (at your option) any later version.
|
Chris@2
|
8
|
Chris@2
|
9 This program is distributed in the hope that it will be useful,
|
Chris@2
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@2
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@2
|
12 GNU General Public License for more details.
|
Chris@2
|
13
|
Chris@2
|
14 You should have received a copy of the GNU General Public License along
|
Chris@2
|
15 with this program (the file gpl.txt); if not, download it from
|
Chris@2
|
16 http://www.gnu.org/licenses/gpl.txt or write to the
|
Chris@2
|
17 Free Software Foundation, Inc.,
|
Chris@2
|
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
Chris@2
|
19 */
|
Chris@2
|
20
|
Chris@2
|
21 package at.ofai.music.util;
|
Chris@2
|
22
|
Chris@2
|
23 import java.awt.Color;
|
Chris@2
|
24 import java.awt.Container;
|
Chris@2
|
25 import java.awt.Dimension;
|
Chris@2
|
26 import java.awt.Frame;
|
Chris@2
|
27 import java.awt.GridLayout;
|
Chris@2
|
28 import java.awt.event.ActionEvent;
|
Chris@2
|
29 import java.awt.event.ActionListener;
|
Chris@2
|
30
|
Chris@2
|
31 import javax.swing.BorderFactory;
|
Chris@2
|
32 import javax.swing.BoxLayout;
|
Chris@2
|
33 import javax.swing.JButton;
|
Chris@2
|
34 import javax.swing.JComboBox;
|
Chris@2
|
35 import javax.swing.JComponent;
|
Chris@2
|
36 import javax.swing.JDialog;
|
Chris@2
|
37 import javax.swing.JLabel;
|
Chris@2
|
38 import javax.swing.JPanel;
|
Chris@2
|
39 import javax.swing.JTextField;
|
Chris@2
|
40
|
Chris@2
|
41 import at.ofai.music.util.FrameMargins;
|
Chris@2
|
42
|
Chris@2
|
43 public class Parameters extends JDialog implements ActionListener {
|
Chris@2
|
44
|
Chris@2
|
45 abstract class Value {
|
Chris@2
|
46 protected JComponent component;
|
Chris@2
|
47 abstract protected Object getValue();
|
Chris@2
|
48 abstract protected void update();
|
Chris@2
|
49 } // abstract class Value
|
Chris@2
|
50
|
Chris@2
|
51
|
Chris@2
|
52 class ChoiceValue extends Value {
|
Chris@2
|
53
|
Chris@2
|
54 String[] choices;
|
Chris@2
|
55 int currentChoice;
|
Chris@2
|
56
|
Chris@2
|
57 protected ChoiceValue(String[] values) { this(values, 0); }
|
Chris@2
|
58 protected ChoiceValue(String[] values, int init) {
|
Chris@2
|
59 choices = values;
|
Chris@2
|
60 currentChoice = init;
|
Chris@2
|
61 component = new JComboBox(values);
|
Chris@2
|
62 ((JComboBox)component).setSelectedIndex(currentChoice);
|
Chris@2
|
63 component.setBackground(colors.getBackground());
|
Chris@2
|
64 component.setForeground(colors.getForeground());
|
Chris@2
|
65 } // constructor
|
Chris@2
|
66
|
Chris@2
|
67 protected Object getValue() { return choices[currentChoice]; }
|
Chris@2
|
68 public String toString() { return choices[currentChoice]; }
|
Chris@2
|
69
|
Chris@2
|
70 protected void update() {
|
Chris@2
|
71 int tmp = ((JComboBox)component).getSelectedIndex();
|
Chris@2
|
72 if (tmp >= 0)
|
Chris@2
|
73 currentChoice = tmp;
|
Chris@2
|
74 } // update()
|
Chris@2
|
75
|
Chris@2
|
76 } // class ChoiceValue
|
Chris@2
|
77
|
Chris@2
|
78
|
Chris@2
|
79 class StringValue extends Value {
|
Chris@2
|
80
|
Chris@2
|
81 String currentValue;
|
Chris@2
|
82
|
Chris@2
|
83 protected StringValue() { this(""); }
|
Chris@2
|
84 protected StringValue(String init) {
|
Chris@2
|
85 currentValue = init;
|
Chris@2
|
86 component = new JTextField(currentValue);
|
Chris@2
|
87 component.setBackground(colors.getBackground());
|
Chris@2
|
88 component.setForeground(colors.getForeground());
|
Chris@2
|
89 } // constructor
|
Chris@2
|
90
|
Chris@2
|
91 protected Object getValue() { return currentValue; }
|
Chris@2
|
92 public String toString() { return currentValue; }
|
Chris@2
|
93
|
Chris@2
|
94 protected void update() {
|
Chris@2
|
95 currentValue = ((JTextField)component).getText();
|
Chris@2
|
96 } // update()
|
Chris@2
|
97
|
Chris@2
|
98 } // class StringValue
|
Chris@2
|
99
|
Chris@2
|
100
|
Chris@2
|
101 class DoubleValue extends Value {
|
Chris@2
|
102
|
Chris@2
|
103 double currentValue;
|
Chris@2
|
104
|
Chris@2
|
105 protected DoubleValue() { this(0); }
|
Chris@2
|
106 protected DoubleValue(double init) {
|
Chris@2
|
107 currentValue = init;
|
Chris@2
|
108 component = new JTextField(Double.toString(currentValue));
|
Chris@2
|
109 component.setBackground(colors.getBackground());
|
Chris@2
|
110 component.setForeground(colors.getForeground());
|
Chris@2
|
111 } // constructor
|
Chris@2
|
112
|
Chris@2
|
113 protected Object getValue() { return new Double(currentValue); }
|
Chris@2
|
114 public String toString() { return "" + currentValue; }
|
Chris@2
|
115
|
Chris@2
|
116 protected void update() {
|
Chris@2
|
117 try {
|
Chris@2
|
118 double tmp =
|
Chris@2
|
119 Double.parseDouble(((JTextField)component).getText());
|
Chris@2
|
120 currentValue = tmp;
|
Chris@2
|
121 } catch (NumberFormatException e) {}
|
Chris@2
|
122 } // update()
|
Chris@2
|
123
|
Chris@2
|
124 } // class DoubleValue
|
Chris@2
|
125
|
Chris@2
|
126
|
Chris@2
|
127 class IntegerValue extends Value {
|
Chris@2
|
128
|
Chris@2
|
129 int currentValue;
|
Chris@2
|
130
|
Chris@2
|
131 protected IntegerValue() { this(0); }
|
Chris@2
|
132 protected IntegerValue(int init) {
|
Chris@2
|
133 currentValue = init;
|
Chris@2
|
134 component = new JTextField(Integer.toString(currentValue));
|
Chris@2
|
135 component.setBackground(colors.getBackground());
|
Chris@2
|
136 component.setForeground(colors.getForeground());
|
Chris@2
|
137 } // constructor
|
Chris@2
|
138
|
Chris@2
|
139 protected Object getValue() { return new Integer(currentValue); }
|
Chris@2
|
140 public String toString() { return "" + currentValue; }
|
Chris@2
|
141
|
Chris@2
|
142 protected void update() {
|
Chris@2
|
143 try {
|
Chris@2
|
144 int tmp = Integer.parseInt(((JTextField)component).getText());
|
Chris@2
|
145 currentValue = tmp;
|
Chris@2
|
146 } catch (NumberFormatException e) {}
|
Chris@2
|
147 } // update()
|
Chris@2
|
148
|
Chris@2
|
149 } // class IntegerValue
|
Chris@2
|
150
|
Chris@2
|
151
|
Chris@2
|
152 class BooleanValue extends ChoiceValue {
|
Chris@2
|
153
|
Chris@2
|
154 boolean currentValue;
|
Chris@2
|
155
|
Chris@2
|
156 protected BooleanValue() { this(true); }
|
Chris@2
|
157 protected BooleanValue(boolean init) {
|
Chris@2
|
158 super(new String[]{"True", "False"}, init? 0: 1);
|
Chris@2
|
159 currentValue = init;
|
Chris@2
|
160 } // constructor
|
Chris@2
|
161
|
Chris@2
|
162 protected Object getValue() { return new Boolean(currentValue); }
|
Chris@2
|
163 public String toString() { return "" + currentValue; }
|
Chris@2
|
164
|
Chris@2
|
165 protected void update() {
|
Chris@2
|
166 super.update();
|
Chris@2
|
167 currentValue = (currentChoice == 0);
|
Chris@2
|
168 } // update()
|
Chris@2
|
169
|
Chris@2
|
170 } // class BooleanValue
|
Chris@2
|
171
|
Chris@2
|
172
|
Chris@2
|
173 protected ArrayMap map;
|
Chris@2
|
174 protected Frame parent;
|
Chris@2
|
175 protected JLabel[] keyFields;
|
Chris@2
|
176 protected JComponent[] valueFields;
|
Chris@2
|
177 protected int sz;
|
Chris@2
|
178 protected Colors colors;
|
Chris@2
|
179 protected JPanel panel1, panel2;
|
Chris@2
|
180 protected JButton okButton, cancelButton;
|
Chris@2
|
181 protected boolean cancelled;
|
Chris@2
|
182 static final long serialVersionUID = 0;
|
Chris@2
|
183
|
Chris@2
|
184 public Parameters(Frame f, String name) {
|
Chris@2
|
185 this(f, name, new Colors() {
|
Chris@2
|
186 public Color getBackground() { return Color.white; }
|
Chris@2
|
187 public Color getForeground() { return Color.black; }
|
Chris@2
|
188 public Color getButton() { return Color.white; }
|
Chris@2
|
189 public Color getButtonText() { return Color.black; }
|
Chris@2
|
190 });
|
Chris@2
|
191 } // constructor
|
Chris@2
|
192
|
Chris@2
|
193 public Parameters(Frame f, String name, Colors c) {
|
Chris@2
|
194 super(f, name, true);
|
Chris@2
|
195 colors = c;
|
Chris@2
|
196 setLocationRelativeTo(f);
|
Chris@2
|
197 Container pane = getContentPane();
|
Chris@2
|
198 pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
|
Chris@2
|
199 panel1 = new JPanel();
|
Chris@2
|
200 panel2 = new JPanel();
|
Chris@2
|
201 pane.add(panel1);
|
Chris@2
|
202 pane.add(panel2);
|
Chris@2
|
203 panel1.setBackground(colors.getBackground());
|
Chris@2
|
204 panel2.setBackground(colors.getBackground());
|
Chris@2
|
205 getRootPane().setBorder(
|
Chris@2
|
206 BorderFactory.createLineBorder(colors.getBackground(), 10));
|
Chris@2
|
207 map = new ArrayMap();
|
Chris@2
|
208 okButton = new JButton("OK");
|
Chris@2
|
209 okButton.setBackground(colors.getButton());
|
Chris@2
|
210 okButton.setForeground(colors.getButtonText());
|
Chris@2
|
211 okButton.addActionListener(this);
|
Chris@2
|
212 cancelButton = new JButton("Cancel");
|
Chris@2
|
213 cancelButton.setBackground(colors.getButton());
|
Chris@2
|
214 cancelButton.setForeground(colors.getButtonText());
|
Chris@2
|
215 cancelButton.addActionListener(this);
|
Chris@2
|
216 parent = f;
|
Chris@2
|
217 cancelled = false;
|
Chris@2
|
218 setVisible(false);
|
Chris@2
|
219 } // constructor
|
Chris@2
|
220
|
Chris@2
|
221 public void print() {
|
Chris@2
|
222 sz = map.size();
|
Chris@2
|
223 System.out.println("at.ofai.music.util.Parameters: size = " + sz);
|
Chris@2
|
224 for (int i = 0; i < sz; i++) {
|
Chris@2
|
225 ArrayMap.Entry e = map.getEntry(i);
|
Chris@2
|
226 System.out.println(e.getKey() + " : " + e.getValue());
|
Chris@2
|
227 }
|
Chris@2
|
228 } // print()
|
Chris@2
|
229
|
Chris@2
|
230 public void actionPerformed(ActionEvent e) {
|
Chris@2
|
231 if (e.getSource() == okButton) {
|
Chris@2
|
232 for (int i = 0; i < sz; i++)
|
Chris@2
|
233 ((Value)map.getEntry(i).getValue()).update();
|
Chris@2
|
234 cancelled = false;
|
Chris@2
|
235 } else
|
Chris@2
|
236 cancelled = true;
|
Chris@2
|
237 setVisible(false);
|
Chris@2
|
238 }
|
Chris@2
|
239
|
Chris@2
|
240 public boolean wasCancelled() {
|
Chris@2
|
241 return cancelled;
|
Chris@2
|
242 }
|
Chris@2
|
243
|
Chris@2
|
244 public void setVisible(boolean flag) {
|
Chris@2
|
245 if (!flag) {
|
Chris@2
|
246 super.setVisible(false);
|
Chris@2
|
247 return;
|
Chris@2
|
248 }
|
Chris@2
|
249 sz = map.size();
|
Chris@2
|
250 keyFields = new JLabel[sz];
|
Chris@2
|
251 valueFields = new JComponent[sz];
|
Chris@2
|
252 panel1.removeAll();
|
Chris@2
|
253 panel2.removeAll();
|
Chris@2
|
254 panel1.setLayout(new GridLayout(sz + 1, 1, 10, 5));
|
Chris@2
|
255 panel2.setLayout(new GridLayout(sz + 1, 1, 10, 5));
|
Chris@2
|
256 for (int i = 0; i < sz; i++) {
|
Chris@2
|
257 ArrayMap.Entry e = map.getEntry(i);
|
Chris@2
|
258 keyFields[i] = new JLabel((String) e.getKey());
|
Chris@2
|
259 panel1.add(keyFields[i]);
|
Chris@2
|
260 valueFields[i] = (JComponent) ((Value)e.getValue()).component;
|
Chris@2
|
261 panel2.add(valueFields[i]);
|
Chris@2
|
262 }
|
Chris@2
|
263 panel1.add(okButton);
|
Chris@2
|
264 panel2.add(cancelButton);
|
Chris@2
|
265 pack();
|
Chris@2
|
266 Dimension dim = getContentPane().getSize();
|
Chris@2
|
267 Dimension margins = FrameMargins.get(false);
|
Chris@2
|
268 int wd = dim.width + margins.width + 20;
|
Chris@2
|
269 int ht = dim.height + margins.height + 20;
|
Chris@2
|
270 int x = 0;
|
Chris@2
|
271 int y = 0;
|
Chris@2
|
272 if (parent != null) {
|
Chris@2
|
273 x = parent.getLocation().x + (parent.getWidth() - wd) / 2;
|
Chris@2
|
274 y = parent.getLocation().y + (parent.getHeight() - ht) / 2;
|
Chris@2
|
275 }
|
Chris@2
|
276 // System.out.println("wd=" + wd + " ht=" + ht + " loc=" + x + "," + y);
|
Chris@2
|
277 // java version "1.3.0rc1" has bugs in location/size with fvwm2
|
Chris@2
|
278 // super.setLocation(-wd/2, -ht/2); // x, y);
|
Chris@2
|
279 super.setLocation(x, y);
|
Chris@2
|
280 super.setSize(wd, ht);
|
Chris@2
|
281 super.setVisible(true);
|
Chris@2
|
282 } // setVisible()
|
Chris@2
|
283
|
Chris@2
|
284 public boolean contains(String key) {
|
Chris@2
|
285 return map.containsKey(key);
|
Chris@2
|
286 } // contains()
|
Chris@2
|
287
|
Chris@2
|
288 public String getString(String key) {
|
Chris@2
|
289 return ((StringValue)map.get(key)).currentValue;
|
Chris@2
|
290 } // getString()
|
Chris@2
|
291
|
Chris@2
|
292 public double getDouble(String key) {
|
Chris@2
|
293 return ((DoubleValue)map.get(key)).currentValue;
|
Chris@2
|
294 } // getDouble()
|
Chris@2
|
295
|
Chris@2
|
296 public int getInt(String key) {
|
Chris@2
|
297 return ((IntegerValue)map.get(key)).currentValue;
|
Chris@2
|
298 } // getInt()
|
Chris@2
|
299
|
Chris@2
|
300 public boolean getBoolean(String key) {
|
Chris@2
|
301 return ((BooleanValue)map.get(key)).currentValue;
|
Chris@2
|
302 } // getBoolean()
|
Chris@2
|
303
|
Chris@2
|
304 public String getChoice(String key) {
|
Chris@2
|
305 return (String) ((ChoiceValue)map.get(key)).getValue();
|
Chris@2
|
306 } // getChoice()
|
Chris@2
|
307
|
Chris@2
|
308 public void setString(String key, String value) {
|
Chris@2
|
309 map.put(key, new StringValue(value));
|
Chris@2
|
310 } // setString()
|
Chris@2
|
311
|
Chris@2
|
312 public void setDouble(String key, double value) {
|
Chris@2
|
313 map.put(key, new DoubleValue(value));
|
Chris@2
|
314 } // setDouble()
|
Chris@2
|
315
|
Chris@2
|
316 public void setInt(String key, int value) {
|
Chris@2
|
317 map.put(key, new IntegerValue(value));
|
Chris@2
|
318 } // setInt()
|
Chris@2
|
319
|
Chris@2
|
320 public void setBoolean(String key, boolean value) {
|
Chris@2
|
321 map.put(key, new BooleanValue(value));
|
Chris@2
|
322 } // setBoolean()
|
Chris@2
|
323
|
Chris@2
|
324 public void setChoice(String key, String[] choices, int value) {
|
Chris@2
|
325 map.put(key, new ChoiceValue(choices, value));
|
Chris@2
|
326 } // setChoice()
|
Chris@2
|
327
|
Chris@2
|
328 } // class Parameters
|