Gridarta Editor
TileStretchingDialog.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 java.util.StringTokenizer;
27 import javax.swing.Box;
28 import javax.swing.BoxLayout;
29 import javax.swing.JButton;
30 import javax.swing.JDialog;
31 import javax.swing.JOptionPane;
32 import javax.swing.JPanel;
33 import javax.swing.JTextField;
34 import javax.swing.event.DocumentEvent;
35 import javax.swing.event.DocumentListener;
36 import javax.swing.text.JTextComponent;
41 import net.sf.japi.swing.action.ActionBuilder;
42 import net.sf.japi.swing.action.ActionBuilderFactory;
43 import net.sf.japi.swing.action.ActionMethod;
44 import net.sf.japi.swing.action.ToggleAction;
45 import org.jetbrains.annotations.NotNull;
46 import org.jetbrains.annotations.Nullable;
47 
53 public class TileStretchingDialog {
54 
58  @NotNull
59  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
60 
64  @NotNull
65  private final JOptionPane optionPane = new JOptionPane();
66 
70  @NotNull
71  private final JButton okButton = new JButton(ACTION_BUILDER.createAction(false, "tileStretchingOkay", this));
72 
76  @NotNull
77  private final JButton cancelButton = new JButton(ACTION_BUILDER.createAction(false, "tileStretchingCancel", this));
78 
82  @NotNull
83  private final JTextComponent heightValueTextField = new JTextField(16);
84 
88  @NotNull
89  private final JTextComponent subLayerTextField = new JTextField(16);
90 
94  @Nullable
95  private JDialog dialog;
96 
100  private boolean isAbsolute;
101 
106  okButton.setDefaultCapable(true);
107  optionPane.setOptions(new Object[] { okButton, cancelButton });
108  final JPanel panel = new JPanel();
109  panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
110  panel.setBorder(GUIConstants.DIALOG_BORDER);
111  panel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "tileStretchingHeightValue.text"));
112  panel.add(heightValueTextField);
113  panel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "tileStretchingSubLayer.text"));
114  panel.add(subLayerTextField);
115  panel.add(Box.createVerticalStrut(5));
116  final ToggleAction isAbsoluteAction = (ToggleAction) ACTION_BUILDER.createToggle(false, "tileStretchingAbsolute", this);
117  panel.add(isAbsoluteAction.createCheckBox());
118  optionPane.setMessage(panel);
119 
120  heightValueTextField.setText(ActionBuilderUtils.getString(ACTION_BUILDER, "tileStretchingHeightValue.default"));
121  TextComponentUtils.setAutoSelectOnFocus(heightValueTextField);
122  isAbsolute = ActionBuilderUtils.getBoolean(ACTION_BUILDER, "tileStretchingAbsolute.default");
123  isAbsoluteAction.setSelected(isAbsolute);
124  final DocumentListener documentListener = new DocumentListener() {
125 
126  @Override
127  public void insertUpdate(@NotNull final DocumentEvent e) {
128  updateOkButton();
129  }
130 
131  @Override
132  public void removeUpdate(@NotNull final DocumentEvent e) {
133  updateOkButton();
134  }
135 
136  @Override
137  public void changedUpdate(@NotNull final DocumentEvent e) {
138  updateOkButton();
139  }
140 
141  };
142 
143  heightValueTextField.getDocument().addDocumentListener(documentListener);
144  }
145 
150  @NotNull
151  private final WindowListener windowListener = new WindowListener() {
152 
153  @Override
154  public void windowOpened(final WindowEvent e) {
155  // ignore
156  }
157 
158  @Override
159  public void windowClosing(final WindowEvent e) {
160  // ignore
161  }
162 
163  @Override
164  public void windowClosed(final WindowEvent e) {
165  // ignore
166  }
167 
168  @Override
169  public void windowIconified(final WindowEvent e) {
170  // ignore
171  }
172 
173  @Override
174  public void windowDeiconified(final WindowEvent e) {
175  // ignore
176  }
177 
178  @Override
179  public void windowActivated(final WindowEvent e) {
180  heightValueTextField.requestFocusInWindow();
181  assert dialog != null;
182  dialog.removeWindowListener(windowListener);
183  }
184 
185  @Override
186  public void windowDeactivated(final WindowEvent e) {
187  // ignore
188  }
189 
190  };
191 
196  private void updateOkButton() {
197  okButton.setEnabled(isOkButtonEnabled());
198  }
199 
205  public boolean showTileStretchingDialog(@NotNull final Component parent) {
206  final JDialog tmpDialog;
207  if (dialog == null) {
208  tmpDialog = optionPane.createDialog(parent, ActionBuilderUtils.getString(ACTION_BUILDER, "tileStretchingTitle"));
209  dialog = tmpDialog;
210  tmpDialog.getRootPane().setDefaultButton(okButton);
211  optionPane.selectInitialValue();
212  tmpDialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
213  } else {
214  tmpDialog = dialog;
215  }
216  okButton.setEnabled(isOkButtonEnabled());
217  tmpDialog.addWindowListener(windowListener);
218  tmpDialog.setVisible(true);
219  tmpDialog.removeWindowListener(windowListener);
220  return optionPane.getValue() == okButton;
221  }
222 
226  @ActionMethod
227  public void tileStretchingOkay() {
228  if (isOkButtonEnabled()) {
229  optionPane.setValue(okButton);
230  }
231  }
232 
237  @ActionMethod
238  public boolean isTileStretchingAbsolute() {
239  return isAbsolute;
240  }
241 
246  @ActionMethod
247  public void setTileStretchingAbsolute(final boolean isAbsolute) {
248  this.isAbsolute = isAbsolute;
249  }
250 
254  @ActionMethod
255  public void tileStretchingCancel() {
256  optionPane.setValue(cancelButton);
257  }
258 
263  private boolean isOkButtonEnabled() {
264  return !heightValueTextField.getText().isEmpty();
265  }
266 
271  public int getHeightValue() {
272  return NumberUtils.parseInt(heightValueTextField.getText());
273  }
274 
279  @NotNull
280  public Integer[] getSubLayers() {
281  final StringTokenizer st = new StringTokenizer(subLayerTextField.getText(), ",");
282  final Integer[] subLayers = new Integer[st.countTokens()];
283 
284  for (int i = 0; st.hasMoreTokens(); i++) {
285  subLayers[i] = NumberUtils.parseInt(st.nextToken());
286  }
287 
288  return subLayers;
289  }
290 
291 }
Graphical User Interface of Gridarta.
boolean isTileStretchingAbsolute()
Action method for "absolute" action.
Integer [] getSubLayers()
Returns the sub-layers to affect.
final JTextComponent subLayerTextField
The text field for specifying the sub-layer value.
Utility class for parsing strings into numbers.
void setTileStretchingAbsolute(final boolean isAbsolute)
Action method for "absolute" action.
final WindowListener windowListener
The WindowListener attached to dialog to call JOptionPane#selectInitialValue() after the dialog has b...
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.
Utility class for JTextComponent related functions.
static int parseInt(@NotNull final String s)
Parses an integer string.
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
Displays a dialog asking for parameters for the "tile stretching set" function.
boolean showTileStretchingDialog(@NotNull final Component parent)
Displays the tile stretching dialog.
static void setAutoSelectOnFocus(@NotNull final JTextComponent textComponent)
Selects all text of a JTextComponent when the component gains the focus.
boolean isOkButtonEnabled()
Returns whether the "OK" button is enabled.
final JOptionPane optionPane
The JOptionPane instance used to create dialogs.
void updateOkButton()
Updates the enabled state of the "OK" button depending on the dialog's contents.
void tileStretchingCancel()
Action method to close the dialog with "Cancel".
Utility class for ActionBuilder related functions.
boolean isAbsolute
Whether adjacent squares are checked.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
static boolean getBoolean(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Returns the value of a key as a.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
final JTextComponent heightValueTextField
The text field for specifying the height value.
void tileStretchingOkay()
Action method to close the dialog with "OK".
Defines common UI constants used in different dialogs.