Gridarta Editor
RandomFillDialog.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.mainactions;
21 
22 import java.awt.Component;
23 import java.awt.Dialog.ModalityType;
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;
46 
51 public class RandomFillDialog {
52 
56  @NotNull
57  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
58 
62  @NotNull
63  private final JOptionPane optionPane = new JOptionPane();
64 
68  @NotNull
69  private final JButton okButton = new JButton(ACTION_BUILDER.createAction(false, "randomFillOkay", this));
70 
74  @NotNull
75  private final JButton cancelButton = new JButton(ACTION_BUILDER.createAction(false, "randomFillCancel", this));
76 
80  @NotNull
81  private final JTextComponent fillDensityTextField = new JTextField(16);
82 
86  @Nullable
87  private JDialog dialog;
88 
92  private boolean skipAdjacentSquares;
93 
97  public RandomFillDialog() {
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));
102  panel.setBorder(GUIConstants.DIALOG_BORDER);
103  panel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "randomFillDensity.text"));
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);
109 
110  fillDensityTextField.setText(ActionBuilderUtils.getString(ACTION_BUILDER, "randomFillDensity.default"));
112  final DocumentListener documentListener = new DocumentListener() {
113 
114  @Override
115  public void insertUpdate(@NotNull final DocumentEvent e) {
116  updateOkButton();
117  }
118 
119  @Override
120  public void removeUpdate(@NotNull final DocumentEvent e) {
121  updateOkButton();
122  }
123 
124  @Override
125  public void changedUpdate(@NotNull final DocumentEvent e) {
126  updateOkButton();
127  }
128 
129  };
130 
131  fillDensityTextField.getDocument().addDocumentListener(documentListener);
132  }
133 
138  @NotNull
139  private final WindowListener windowListener = new WindowListener() {
140 
141  @Override
142  public void windowOpened(final WindowEvent e) {
143  // ignore
144  }
145 
146  @Override
147  public void windowClosing(final WindowEvent e) {
148  // ignore
149  }
150 
151  @Override
152  public void windowClosed(final WindowEvent e) {
153  // ignore
154  }
155 
156  @Override
157  public void windowIconified(final WindowEvent e) {
158  // ignore
159  }
160 
161  @Override
162  public void windowDeiconified(final WindowEvent e) {
163  // ignore
164  }
165 
166  @Override
167  public void windowActivated(final WindowEvent e) {
168  fillDensityTextField.requestFocusInWindow();
169  assert dialog != null;
170  dialog.removeWindowListener(windowListener);
171  }
172 
173  @Override
174  public void windowDeactivated(final WindowEvent e) {
175  // ignore
176  }
177 
178  };
179 
184  private void updateOkButton() {
185  okButton.setEnabled(isOkButtonEnabled());
186  }
187 
193  public boolean showRandomFillDialog(@NotNull final Component parent) {
194  final JDialog tmpDialog;
195  if (dialog == null) {
196  tmpDialog = optionPane.createDialog(parent, ActionBuilderUtils.getString(ACTION_BUILDER, "randomFillTitle"));
197  dialog = tmpDialog;
198  tmpDialog.getRootPane().setDefaultButton(okButton);
199  optionPane.selectInitialValue();
200  tmpDialog.setModalityType(ModalityType.DOCUMENT_MODAL);
201  } else {
202  tmpDialog = dialog;
203  }
204  okButton.setEnabled(isOkButtonEnabled());
205  tmpDialog.addWindowListener(windowListener);
206  tmpDialog.setVisible(true);
207  tmpDialog.removeWindowListener(windowListener);
208  return optionPane.getValue() == okButton;
209  }
210 
214  @ActionMethod
215  public void randomFillOkay() {
216  if (isOkButtonEnabled()) {
217  optionPane.setValue(okButton);
218  }
219  }
220 
225  @ActionMethod
227  return skipAdjacentSquares;
228  }
229 
234  @ActionMethod
236  this.skipAdjacentSquares = skipAdjacentSquares;
237  }
238 
242  @ActionMethod
243  public void randomFillCancel() {
244  optionPane.setValue(cancelButton);
245  }
246 
251  private boolean isOkButtonEnabled() {
252  final int fillDensity = getFillDensity();
253  return 0 < fillDensity && fillDensity <= 100;
254  }
255 
260  public int getFillDensity() {
261  return NumberUtils.parseInt(fillDensityTextField.getText());
262  }
263 
264 }
net.sf.gridarta.utils.NumberUtils.parseInt
static int parseInt(@NotNull final String s)
Parses an integer string.
Definition: NumberUtils.java:41
net.sf.gridarta.mainactions.RandomFillDialog.RandomFillDialog
RandomFillDialog()
Creates a new instance.
Definition: RandomFillDialog.java:97
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.utils.GUIConstants
Defines common UI constants used in different dialogs.
Definition: GUIConstants.java:33
net.sf.gridarta.mainactions.RandomFillDialog
Displays a dialog asking for parameters for the "random fill" function.
Definition: RandomFillDialog.java:51
net.sf
net.sf.gridarta.mainactions.RandomFillDialog.setRandomFillSkipAdjacentSquares
void setRandomFillSkipAdjacentSquares(final boolean skipAdjacentSquares)
Action method for "skip adjacent squares" action.
Definition: RandomFillDialog.java:235
net.sf.gridarta.mainactions.RandomFillDialog.randomFillOkay
void randomFillOkay()
Action method to close the dialog with "OK".
Definition: RandomFillDialog.java:215
net.sf.gridarta.utils.ActionBuilderUtils.newLabel
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
Definition: ActionBuilderUtils.java:117
net.sf.gridarta.mainactions.RandomFillDialog.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
Definition: RandomFillDialog.java:57
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.mainactions.RandomFillDialog.getFillDensity
int getFillDensity()
Returns the fill density.
Definition: RandomFillDialog.java:260
net.sf.gridarta.mainactions.RandomFillDialog.randomFillCancel
void randomFillCancel()
Action method to close the dialog with "Cancel".
Definition: RandomFillDialog.java:243
net
net.sf.gridarta.mainactions.RandomFillDialog.cancelButton
final JButton cancelButton
The "Cancel" button.
Definition: RandomFillDialog.java:75
net.sf.gridarta.mainactions.RandomFillDialog.windowListener
final WindowListener windowListener
The WindowListener attached to dialog to call {} after the dialog has been shown.
Definition: RandomFillDialog.java:139
net.sf.gridarta.mainactions.RandomFillDialog.isRandomFillSkipAdjacentSquares
boolean isRandomFillSkipAdjacentSquares()
Action method for "skip adjacent squares" action.
Definition: RandomFillDialog.java:226
net.sf.gridarta.utils.ActionBuilderUtils.getString
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Definition: ActionBuilderUtils.java:71
net.sf.gridarta.gui.utils.TextComponentUtils.setAutoSelectOnFocus
static void setAutoSelectOnFocus(@NotNull final JTextComponent textComponent)
Selects all text of a JTextComponent when the component gains the focus.
Definition: TextComponentUtils.java:47
net.sf.gridarta.mainactions.RandomFillDialog.isOkButtonEnabled
boolean isOkButtonEnabled()
Returns whether the "OK" button is enabled.
Definition: RandomFillDialog.java:251
net.sf.gridarta.mainactions.RandomFillDialog.dialog
JDialog dialog
The JDialog instance or.
Definition: RandomFillDialog.java:87
net.sf.gridarta.mainactions.RandomFillDialog.okButton
final JButton okButton
The "OK" button.
Definition: RandomFillDialog.java:69
net.sf.gridarta.utils.ActionBuilderUtils
Utility class for ActionBuilder related functions.
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.mainactions.RandomFillDialog.skipAdjacentSquares
boolean skipAdjacentSquares
Whether adjacent squares are checked.
Definition: RandomFillDialog.java:92
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.mainactions.RandomFillDialog.updateOkButton
void updateOkButton()
Updates the enabled state of the "OK" button depending on the dialog's contents.
Definition: RandomFillDialog.java:184
net.sf.gridarta.gui.utils.TextComponentUtils
Utility class for JTextComponent related functions.
Definition: TextComponentUtils.java:34
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.mainactions.RandomFillDialog.fillDensityTextField
final JTextComponent fillDensityTextField
The text field for specifying the fill density.
Definition: RandomFillDialog.java:81
net.sf.gridarta.mainactions.RandomFillDialog.showRandomFillDialog
boolean showRandomFillDialog(@NotNull final Component parent)
Displays the random fill dialog.
Definition: RandomFillDialog.java:193
net.sf.gridarta.gui.utils.GUIConstants.DIALOG_BORDER
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
Definition: GUIConstants.java:38
net.sf.gridarta.utils.NumberUtils
Utility class for parsing strings into numbers.
Definition: NumberUtils.java:28
net.sf.gridarta.mainactions.RandomFillDialog.optionPane
final JOptionPane optionPane
The JOptionPane instance used to create dialogs.
Definition: RandomFillDialog.java:63