1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
package tim.prune.function;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.gui.WholeNumberField;
/**
* First step of functions which just require a single numeric
* parameter in order to run
*/
public class ChooseSingleParameter extends GenericFunction
{
/** Parent function which needs this parameter */
private final SingleNumericParameterFunction _parent;
/** dialog */
private JDialog _dialog = null;
/** label which might need to be changed */
private JLabel _descLabel = null;
/** entry field */
private WholeNumberField _numberField = null;
/** ok button */
private JButton _okButton = null;
/** Constructor */
public ChooseSingleParameter(App inApp, SingleNumericParameterFunction inFunction)
{
super(inApp);
_parent = inFunction;
}
@Override
public String getNameKey() {
return _parent.getNameKey();
}
@Override
public void begin()
{
// Make dialog window
if (_dialog == null)
{
_dialog = new JDialog(_parentFrame, getName(), true);
_dialog.setLocationRelativeTo(_parentFrame);
_dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
_dialog.getContentPane().add(makeDialogComponents());
_dialog.pack();
}
// refresh and show the dialog
_descLabel.setText(I18nManager.getText(_parent.getDescriptionKey()));
int param = _parent.getCurrentParamValue();
if (param > 0) {
_numberField.setValue(param);
}
else {
_numberField.setText("");
}
_dialog.setVisible(true);
enableOkButton();
}
/**
* Create dialog components
* @return Panel containing all gui elements in dialog
*/
private JPanel makeDialogComponents()
{
JPanel dialogPanel = new JPanel();
dialogPanel.setLayout(new BorderLayout());
// label
_descLabel = new JLabel(I18nManager.getText(_parent.getDescriptionKey()));
dialogPanel.add(_descLabel, BorderLayout.NORTH);
// Centre panel with number entry field
JPanel centrePanel = new JPanel();
centrePanel.setLayout(new BorderLayout(8, 8));
_numberField = new WholeNumberField(4);
centrePanel.add(_numberField, BorderLayout.NORTH);
dialogPanel.add(centrePanel, BorderLayout.CENTER);
// Listener to enable/disable ok button
_numberField.addActionListener(e -> enableOkButton());
_numberField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent inE)
{
int eCode = inE.getKeyCode();
if (eCode == KeyEvent.VK_ESCAPE) {_dialog.dispose();}
else if (eCode == KeyEvent.VK_ENTER) {finish();}
super.keyReleased(inE);
}
});
// button panel at bottom
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
_okButton = new JButton(I18nManager.getText("button.ok"));
_okButton.addActionListener(e -> finish());
_okButton.setEnabled(false);
buttonPanel.add(_okButton);
JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
cancelButton.addActionListener(e -> _dialog.dispose());
buttonPanel.add(cancelButton);
dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
return dialogPanel;
}
/**
* Enable or disable the OK button as appropriate
*/
private void enableOkButton()
{
_okButton.setEnabled(_numberField.getValue() >= _parent.getMinAllowedValue()
&& _numberField.getValue() <= _parent.getMaxAllowedValue());
}
/**
* The OK button (or Enter) has been pressed
*/
private void finish()
{
if (_numberField.getValue() >= _parent.getMinAllowedValue()
&& _numberField.getValue() <= _parent.getMaxAllowedValue())
{
_parent.completeFunction(_numberField.getValue());
_dialog.dispose();
}
}
}
|