20 package net.sf.gridarta.mainactions;
22 import java.awt.Component;
23 import java.awt.Dialog;
24 import java.awt.event.WindowEvent;
25 import java.awt.event.WindowListener;
26 import javax.swing.Box;
27 import javax.swing.BoxLayout;
28 import javax.swing.JButton;
29 import javax.swing.JDialog;
30 import javax.swing.JOptionPane;
31 import javax.swing.JPanel;
32 import javax.swing.JTextField;
33 import javax.swing.event.DocumentEvent;
34 import javax.swing.event.DocumentListener;
35 import javax.swing.text.JTextComponent;
40 import net.
sf.japi.swing.action.ActionBuilder;
41 import net.
sf.japi.swing.action.ActionBuilderFactory;
42 import net.
sf.japi.swing.action.ActionMethod;
43 import net.
sf.japi.swing.action.ToggleAction;
44 import org.jetbrains.annotations.NotNull;
45 import org.jetbrains.annotations.Nullable;
57 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
63 private final JOptionPane
optionPane =
new JOptionPane();
69 private final JButton
okButton =
new JButton(ACTION_BUILDER.createAction(
false,
"randomFillOkay",
this));
75 private final JButton
cancelButton =
new JButton(ACTION_BUILDER.createAction(
false,
"randomFillCancel",
this));
98 okButton.setDefaultCapable(
true);
99 optionPane.setOptions(
new Object[] {
okButton, cancelButton });
100 final JPanel panel =
new JPanel();
101 panel.setLayout(
new BoxLayout(panel, BoxLayout.Y_AXIS));
104 panel.add(fillDensityTextField);
105 panel.add(Box.createVerticalStrut(5));
106 final ToggleAction skipAdjacentSquaresAction = (ToggleAction) ACTION_BUILDER.createToggle(
false,
"randomFillSkipAdjacentSquares",
this);
107 panel.add(skipAdjacentSquaresAction.createCheckBox());
108 optionPane.setMessage(panel);
112 final DocumentListener documentListener =
new DocumentListener() {
115 public void insertUpdate(@NotNull
final DocumentEvent e) {
120 public void removeUpdate(@NotNull
final DocumentEvent e) {
125 public void changedUpdate(@NotNull
final DocumentEvent e) {
131 fillDensityTextField.getDocument().addDocumentListener(documentListener);
142 public void windowOpened(
final WindowEvent e) {
147 public void windowClosing(
final WindowEvent e) {
152 public void windowClosed(
final WindowEvent e) {
157 public void windowIconified(
final WindowEvent e) {
162 public void windowDeiconified(
final WindowEvent e) {
167 public void windowActivated(
final WindowEvent e) {
168 fillDensityTextField.requestFocusInWindow();
169 assert dialog != null;
170 dialog.removeWindowListener(windowListener);
174 public void windowDeactivated(
final WindowEvent e) {
194 final JDialog tmpDialog;
195 if (dialog == null) {
198 tmpDialog.getRootPane().setDefaultButton(okButton);
199 optionPane.selectInitialValue();
200 tmpDialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
205 tmpDialog.addWindowListener(windowListener);
206 tmpDialog.setVisible(
true);
207 tmpDialog.removeWindowListener(windowListener);
208 return optionPane.getValue() ==
okButton;
217 optionPane.setValue(okButton);
244 optionPane.setValue(cancelButton);
253 return 0 < fillDensity && fillDensity <= 100;
final JButton cancelButton
The "Cancel" button.
final JOptionPane optionPane
The JOptionPane instance used to create dialogs.
void setRandomFillSkipAdjacentSquares(final boolean skipAdjacentSquares)
Action method for "skip adjacent squares" action.
Graphical User Interface of Gridarta.
boolean isOkButtonEnabled()
Returns whether the "OK" button is enabled.
final WindowListener windowListener
The WindowListener attached to dialog to call JOptionPane#selectInitialValue() after the dialog has b...
Utility class for parsing strings into numbers.
boolean showRandomFillDialog(@NotNull final Component parent)
Displays the random fill dialog.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Base package of all Gridarta classes.
JDialog dialog
The JDialog instance or.
Utility class for JTextComponent related functions.
static int parseInt(@NotNull final String s)
Parses an integer string.
boolean isRandomFillSkipAdjacentSquares()
Action method for "skip adjacent squares" action.
RandomFillDialog()
Creates a new instance.
static void setAutoSelectOnFocus(@NotNull final JTextComponent textComponent)
Selects all text of a JTextComponent when the component gains the focus.
Displays a dialog asking for parameters for the "random fill" function.
final JTextComponent fillDensityTextField
The text field for specifying the fill density.
Utility class for ActionBuilder related functions.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
void randomFillCancel()
Action method to close the dialog with "Cancel".
boolean skipAdjacentSquares
Whether adjacent squares are checked.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
void updateOkButton()
Updates the enabled state of the "OK" button depending on the dialog's contents.
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
void randomFillOkay()
Action method to close the dialog with "OK".
final JButton okButton
The "OK" button.
Defines common UI constants used in different dialogs.
int getFillDensity()
Returns the fill density.