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-2015 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;
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"));
111  TextComponentUtils.setAutoSelectOnFocus(fillDensityTextField);
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(Dialog.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
235  public void setRandomFillSkipAdjacentSquares(final boolean skipAdjacentSquares) {
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 }
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.
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&#39;s contents.
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
void randomFillOkay()
Action method to close the dialog with "OK".
Defines common UI constants used in different dialogs.
int getFillDensity()
Returns the fill density.