Chris@2: /* Chris@2: Copyright (C) 2001, 2006 by Simon Dixon Chris@2: Chris@2: This program is free software; you can redistribute it and/or modify Chris@2: it under the terms of the GNU General Public License as published by Chris@2: the Free Software Foundation; either version 2 of the License, or Chris@2: (at your option) any later version. Chris@2: Chris@2: This program is distributed in the hope that it will be useful, Chris@2: but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@2: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@2: GNU General Public License for more details. Chris@2: Chris@2: You should have received a copy of the GNU General Public License along Chris@2: with this program (the file gpl.txt); if not, download it from Chris@2: http://www.gnu.org/licenses/gpl.txt or write to the Chris@2: Free Software Foundation, Inc., Chris@2: 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Chris@2: */ Chris@2: Chris@2: package at.ofai.music.util; Chris@2: Chris@2: import java.awt.Color; Chris@2: import java.awt.Container; Chris@2: import java.awt.Dimension; Chris@2: import java.awt.Frame; Chris@2: import java.awt.GridLayout; Chris@2: import java.awt.event.ActionEvent; Chris@2: import java.awt.event.ActionListener; Chris@2: Chris@2: import javax.swing.BorderFactory; Chris@2: import javax.swing.BoxLayout; Chris@2: import javax.swing.JButton; Chris@2: import javax.swing.JComboBox; Chris@2: import javax.swing.JComponent; Chris@2: import javax.swing.JDialog; Chris@2: import javax.swing.JLabel; Chris@2: import javax.swing.JPanel; Chris@2: import javax.swing.JTextField; Chris@2: Chris@2: import at.ofai.music.util.FrameMargins; Chris@2: Chris@2: public class Parameters extends JDialog implements ActionListener { Chris@2: Chris@2: abstract class Value { Chris@2: protected JComponent component; Chris@2: abstract protected Object getValue(); Chris@2: abstract protected void update(); Chris@2: } // abstract class Value Chris@2: Chris@2: Chris@2: class ChoiceValue extends Value { Chris@2: Chris@2: String[] choices; Chris@2: int currentChoice; Chris@2: Chris@2: protected ChoiceValue(String[] values) { this(values, 0); } Chris@2: protected ChoiceValue(String[] values, int init) { Chris@2: choices = values; Chris@2: currentChoice = init; Chris@2: component = new JComboBox(values); Chris@2: ((JComboBox)component).setSelectedIndex(currentChoice); Chris@2: component.setBackground(colors.getBackground()); Chris@2: component.setForeground(colors.getForeground()); Chris@2: } // constructor Chris@2: Chris@2: protected Object getValue() { return choices[currentChoice]; } Chris@2: public String toString() { return choices[currentChoice]; } Chris@2: Chris@2: protected void update() { Chris@2: int tmp = ((JComboBox)component).getSelectedIndex(); Chris@2: if (tmp >= 0) Chris@2: currentChoice = tmp; Chris@2: } // update() Chris@2: Chris@2: } // class ChoiceValue Chris@2: Chris@2: Chris@2: class StringValue extends Value { Chris@2: Chris@2: String currentValue; Chris@2: Chris@2: protected StringValue() { this(""); } Chris@2: protected StringValue(String init) { Chris@2: currentValue = init; Chris@2: component = new JTextField(currentValue); Chris@2: component.setBackground(colors.getBackground()); Chris@2: component.setForeground(colors.getForeground()); Chris@2: } // constructor Chris@2: Chris@2: protected Object getValue() { return currentValue; } Chris@2: public String toString() { return currentValue; } Chris@2: Chris@2: protected void update() { Chris@2: currentValue = ((JTextField)component).getText(); Chris@2: } // update() Chris@2: Chris@2: } // class StringValue Chris@2: Chris@2: Chris@2: class DoubleValue extends Value { Chris@2: Chris@2: double currentValue; Chris@2: Chris@2: protected DoubleValue() { this(0); } Chris@2: protected DoubleValue(double init) { Chris@2: currentValue = init; Chris@2: component = new JTextField(Double.toString(currentValue)); Chris@2: component.setBackground(colors.getBackground()); Chris@2: component.setForeground(colors.getForeground()); Chris@2: } // constructor Chris@2: Chris@2: protected Object getValue() { return new Double(currentValue); } Chris@2: public String toString() { return "" + currentValue; } Chris@2: Chris@2: protected void update() { Chris@2: try { Chris@2: double tmp = Chris@2: Double.parseDouble(((JTextField)component).getText()); Chris@2: currentValue = tmp; Chris@2: } catch (NumberFormatException e) {} Chris@2: } // update() Chris@2: Chris@2: } // class DoubleValue Chris@2: Chris@2: Chris@2: class IntegerValue extends Value { Chris@2: Chris@2: int currentValue; Chris@2: Chris@2: protected IntegerValue() { this(0); } Chris@2: protected IntegerValue(int init) { Chris@2: currentValue = init; Chris@2: component = new JTextField(Integer.toString(currentValue)); Chris@2: component.setBackground(colors.getBackground()); Chris@2: component.setForeground(colors.getForeground()); Chris@2: } // constructor Chris@2: Chris@2: protected Object getValue() { return new Integer(currentValue); } Chris@2: public String toString() { return "" + currentValue; } Chris@2: Chris@2: protected void update() { Chris@2: try { Chris@2: int tmp = Integer.parseInt(((JTextField)component).getText()); Chris@2: currentValue = tmp; Chris@2: } catch (NumberFormatException e) {} Chris@2: } // update() Chris@2: Chris@2: } // class IntegerValue Chris@2: Chris@2: Chris@2: class BooleanValue extends ChoiceValue { Chris@2: Chris@2: boolean currentValue; Chris@2: Chris@2: protected BooleanValue() { this(true); } Chris@2: protected BooleanValue(boolean init) { Chris@2: super(new String[]{"True", "False"}, init? 0: 1); Chris@2: currentValue = init; Chris@2: } // constructor Chris@2: Chris@2: protected Object getValue() { return new Boolean(currentValue); } Chris@2: public String toString() { return "" + currentValue; } Chris@2: Chris@2: protected void update() { Chris@2: super.update(); Chris@2: currentValue = (currentChoice == 0); Chris@2: } // update() Chris@2: Chris@2: } // class BooleanValue Chris@2: Chris@2: Chris@2: protected ArrayMap map; Chris@2: protected Frame parent; Chris@2: protected JLabel[] keyFields; Chris@2: protected JComponent[] valueFields; Chris@2: protected int sz; Chris@2: protected Colors colors; Chris@2: protected JPanel panel1, panel2; Chris@2: protected JButton okButton, cancelButton; Chris@2: protected boolean cancelled; Chris@2: static final long serialVersionUID = 0; Chris@2: Chris@2: public Parameters(Frame f, String name) { Chris@2: this(f, name, new Colors() { Chris@2: public Color getBackground() { return Color.white; } Chris@2: public Color getForeground() { return Color.black; } Chris@2: public Color getButton() { return Color.white; } Chris@2: public Color getButtonText() { return Color.black; } Chris@2: }); Chris@2: } // constructor Chris@2: Chris@2: public Parameters(Frame f, String name, Colors c) { Chris@2: super(f, name, true); Chris@2: colors = c; Chris@2: setLocationRelativeTo(f); Chris@2: Container pane = getContentPane(); Chris@2: pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS)); Chris@2: panel1 = new JPanel(); Chris@2: panel2 = new JPanel(); Chris@2: pane.add(panel1); Chris@2: pane.add(panel2); Chris@2: panel1.setBackground(colors.getBackground()); Chris@2: panel2.setBackground(colors.getBackground()); Chris@2: getRootPane().setBorder( Chris@2: BorderFactory.createLineBorder(colors.getBackground(), 10)); Chris@2: map = new ArrayMap(); Chris@2: okButton = new JButton("OK"); Chris@2: okButton.setBackground(colors.getButton()); Chris@2: okButton.setForeground(colors.getButtonText()); Chris@2: okButton.addActionListener(this); Chris@2: cancelButton = new JButton("Cancel"); Chris@2: cancelButton.setBackground(colors.getButton()); Chris@2: cancelButton.setForeground(colors.getButtonText()); Chris@2: cancelButton.addActionListener(this); Chris@2: parent = f; Chris@2: cancelled = false; Chris@2: setVisible(false); Chris@2: } // constructor Chris@2: Chris@2: public void print() { Chris@2: sz = map.size(); Chris@2: System.out.println("at.ofai.music.util.Parameters: size = " + sz); Chris@2: for (int i = 0; i < sz; i++) { Chris@2: ArrayMap.Entry e = map.getEntry(i); Chris@2: System.out.println(e.getKey() + " : " + e.getValue()); Chris@2: } Chris@2: } // print() Chris@2: Chris@2: public void actionPerformed(ActionEvent e) { Chris@2: if (e.getSource() == okButton) { Chris@2: for (int i = 0; i < sz; i++) Chris@2: ((Value)map.getEntry(i).getValue()).update(); Chris@2: cancelled = false; Chris@2: } else Chris@2: cancelled = true; Chris@2: setVisible(false); Chris@2: } Chris@2: Chris@2: public boolean wasCancelled() { Chris@2: return cancelled; Chris@2: } Chris@2: Chris@2: public void setVisible(boolean flag) { Chris@2: if (!flag) { Chris@2: super.setVisible(false); Chris@2: return; Chris@2: } Chris@2: sz = map.size(); Chris@2: keyFields = new JLabel[sz]; Chris@2: valueFields = new JComponent[sz]; Chris@2: panel1.removeAll(); Chris@2: panel2.removeAll(); Chris@2: panel1.setLayout(new GridLayout(sz + 1, 1, 10, 5)); Chris@2: panel2.setLayout(new GridLayout(sz + 1, 1, 10, 5)); Chris@2: for (int i = 0; i < sz; i++) { Chris@2: ArrayMap.Entry e = map.getEntry(i); Chris@2: keyFields[i] = new JLabel((String) e.getKey()); Chris@2: panel1.add(keyFields[i]); Chris@2: valueFields[i] = (JComponent) ((Value)e.getValue()).component; Chris@2: panel2.add(valueFields[i]); Chris@2: } Chris@2: panel1.add(okButton); Chris@2: panel2.add(cancelButton); Chris@2: pack(); Chris@2: Dimension dim = getContentPane().getSize(); Chris@2: Dimension margins = FrameMargins.get(false); Chris@2: int wd = dim.width + margins.width + 20; Chris@2: int ht = dim.height + margins.height + 20; Chris@2: int x = 0; Chris@2: int y = 0; Chris@2: if (parent != null) { Chris@2: x = parent.getLocation().x + (parent.getWidth() - wd) / 2; Chris@2: y = parent.getLocation().y + (parent.getHeight() - ht) / 2; Chris@2: } Chris@2: // System.out.println("wd=" + wd + " ht=" + ht + " loc=" + x + "," + y); Chris@2: // java version "1.3.0rc1" has bugs in location/size with fvwm2 Chris@2: // super.setLocation(-wd/2, -ht/2); // x, y); Chris@2: super.setLocation(x, y); Chris@2: super.setSize(wd, ht); Chris@2: super.setVisible(true); Chris@2: } // setVisible() Chris@2: Chris@2: public boolean contains(String key) { Chris@2: return map.containsKey(key); Chris@2: } // contains() Chris@2: Chris@2: public String getString(String key) { Chris@2: return ((StringValue)map.get(key)).currentValue; Chris@2: } // getString() Chris@2: Chris@2: public double getDouble(String key) { Chris@2: return ((DoubleValue)map.get(key)).currentValue; Chris@2: } // getDouble() Chris@2: Chris@2: public int getInt(String key) { Chris@2: return ((IntegerValue)map.get(key)).currentValue; Chris@2: } // getInt() Chris@2: Chris@2: public boolean getBoolean(String key) { Chris@2: return ((BooleanValue)map.get(key)).currentValue; Chris@2: } // getBoolean() Chris@2: Chris@2: public String getChoice(String key) { Chris@2: return (String) ((ChoiceValue)map.get(key)).getValue(); Chris@2: } // getChoice() Chris@2: Chris@2: public void setString(String key, String value) { Chris@2: map.put(key, new StringValue(value)); Chris@2: } // setString() Chris@2: Chris@2: public void setDouble(String key, double value) { Chris@2: map.put(key, new DoubleValue(value)); Chris@2: } // setDouble() Chris@2: Chris@2: public void setInt(String key, int value) { Chris@2: map.put(key, new IntegerValue(value)); Chris@2: } // setInt() Chris@2: Chris@2: public void setBoolean(String key, boolean value) { Chris@2: map.put(key, new BooleanValue(value)); Chris@2: } // setBoolean() Chris@2: Chris@2: public void setChoice(String key, String[] choices, int value) { Chris@2: map.put(key, new ChoiceValue(choices, value)); Chris@2: } // setChoice() Chris@2: Chris@2: } // class Parameters