1 package tim.prune.function;
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.KeyAdapter;
8 import java.awt.event.KeyEvent;
10 import javax.swing.BorderFactory;
11 import javax.swing.JButton;
12 import javax.swing.JDialog;
13 import javax.swing.JLabel;
14 import javax.swing.JPanel;
17 import tim.prune.GenericFunction;
18 import tim.prune.I18nManager;
19 import tim.prune.gui.WholeNumberField;
22 * First step of functions which just require a single numeric
23 * parameter in order to run
25 public class ChooseSingleParameter extends GenericFunction
27 /** Parent function which needs this parameter */
28 private SingleNumericParameterFunction _parent = null;
30 private JDialog _dialog = null;
31 /** label which might need to be changed */
32 private JLabel _descLabel = null;
34 private WholeNumberField _numberField = null;
36 private JButton _okButton = null;
40 public ChooseSingleParameter(App inApp, SingleNumericParameterFunction inFunction)
47 public String getNameKey() {
48 return _parent.getNameKey();
57 _dialog = new JDialog(_parentFrame, I18nManager.getText(_parent.getNameKey()), true);
58 _dialog.setLocationRelativeTo(_parentFrame);
59 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
60 _dialog.getContentPane().add(makeDialogComponents());
63 // refresh and show the dialog
64 _descLabel.setText(I18nManager.getText(_parent.getDescriptionKey()));
65 int param = _parent.getCurrentParamValue();
67 _numberField.setValue(param);
70 _numberField.setText("");
72 _dialog.setVisible(true);
77 * Create dialog components
78 * @return Panel containing all gui elements in dialog
80 private JPanel makeDialogComponents()
82 JPanel dialogPanel = new JPanel();
83 dialogPanel.setLayout(new BorderLayout());
85 _descLabel = new JLabel(I18nManager.getText(_parent.getDescriptionKey()));
86 dialogPanel.add(_descLabel, BorderLayout.NORTH);
87 // Centre panel with number entry field
88 JPanel centrePanel = new JPanel();
89 centrePanel.setLayout(new BorderLayout(8, 8));
90 _numberField = new WholeNumberField(4);
91 centrePanel.add(_numberField, BorderLayout.NORTH);
92 dialogPanel.add(centrePanel, BorderLayout.CENTER);
94 // Listener to enable/disable ok button
95 _numberField.addActionListener(new ActionListener() {
96 public void actionPerformed(ActionEvent arg0) {
100 _numberField.addKeyListener(new KeyAdapter() {
101 public void keyReleased(KeyEvent inE)
103 int eCode = inE.getKeyCode();
104 if (eCode == KeyEvent.VK_ESCAPE) {_dialog.dispose();}
105 else if (eCode == KeyEvent.VK_ENTER) {finish();}
106 super.keyReleased(inE);
110 // button panel at bottom
111 JPanel buttonPanel = new JPanel();
112 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
113 _okButton = new JButton(I18nManager.getText("button.ok"));
114 ActionListener okListener = new ActionListener() {
115 public void actionPerformed(ActionEvent e)
120 _okButton.addActionListener(okListener);
121 _okButton.setEnabled(false);
122 buttonPanel.add(_okButton);
123 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
124 cancelButton.addActionListener(new ActionListener() {
125 public void actionPerformed(ActionEvent e)
130 buttonPanel.add(cancelButton);
131 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
132 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
138 * Enable or disable the OK button as appropriate
140 private void enableOkButton()
142 _okButton.setEnabled(_numberField.getValue() >= _parent.getMinAllowedValue()
143 && _numberField.getValue() <= _parent.getMaxAllowedValue());
147 * The OK button (or Enter) has been pressed
149 private void finish()
151 if (_numberField.getValue() >= _parent.getMinAllowedValue()
152 && _numberField.getValue() <= _parent.getMaxAllowedValue())
154 _parent.completeFunction(_numberField.getValue());