Gridarta Editor
GoLocationDialog.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.gui.dialog.golocation;
21 
22 import java.awt.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.Insets;
25 import java.awt.Point;
26 import java.awt.Window;
27 import javax.swing.Box;
28 import javax.swing.BoxLayout;
29 import javax.swing.JButton;
30 import javax.swing.JComponent;
31 import javax.swing.JDialog;
32 import javax.swing.JLabel;
33 import javax.swing.JOptionPane;
34 import javax.swing.JPanel;
35 import javax.swing.JTextField;
36 import javax.swing.WindowConstants;
37 import javax.swing.border.CompoundBorder;
38 import javax.swing.border.EtchedBorder;
39 import javax.swing.border.TitledBorder;
40 import javax.swing.text.JTextComponent;
49 import net.sf.gridarta.utils.Size2D;
50 import net.sf.japi.swing.action.ActionBuilder;
51 import net.sf.japi.swing.action.ActionBuilderFactory;
52 import net.sf.japi.swing.action.ActionMethod;
53 import org.jetbrains.annotations.NotNull;
54 import org.jetbrains.annotations.Nullable;
55 
60 public class GoLocationDialog<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends JOptionPane {
61 
65  private static final long serialVersionUID = 1L;
66 
70  @NotNull
71  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
72 
76  @NotNull
78 
82  @NotNull
83  private final MapView<G, A, R> mapView;
84 
88  @NotNull
89  private final JTextField xCoordinateField = new JTextField();
90 
94  @NotNull
95  private final JTextField yCoordinateField = new JTextField();
96 
100  @NotNull
101  private final JButton okButton = new JButton(ACTION_BUILDER.createAction(false, "goLocationOkay", this));
102 
106  @NotNull
107  private final JButton cancelButton = new JButton(ACTION_BUILDER.createAction(false, "goLocationCancel", this));
108 
112  @NotNull
113  private final JDialog dialog;
114 
120  public GoLocationDialog(@NotNull final AbstractPerMapDialogManager<G, A, R, GoLocationDialog<G, A, R>> goLocationDialogManager, @NotNull final MapView<G, A, R> mapView) {
121  this.goLocationDialogManager = goLocationDialogManager;
122 
123  okButton.setDefaultCapable(true);
124  final JButton applyButton = new JButton(ACTION_BUILDER.createAction(false, "goLocationApply", this));
125  setOptions(new Object[] { okButton, applyButton, cancelButton });
126 
127  this.mapView = mapView;
128  setMessage(createPanel());
129 
130  TextComponentUtils.setAutoSelectOnFocus(xCoordinateField);
131  TextComponentUtils.setAutoSelectOnFocus(yCoordinateField);
132  TextComponentUtils.setActionNextFocus(xCoordinateField, yCoordinateField);
133 
134  dialog = createDialog(mapView.getComponent(), ActionBuilderUtils.getString(ACTION_BUILDER, "goLocation.title"));
135  dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
136  dialog.getRootPane().setDefaultButton(okButton);
137  dialog.setModal(false);
138 
139  dialog.pack();
140  dialog.setVisible(true);
141  }
142 
147  @NotNull
148  private JPanel createPanel() {
149  final JPanel mainPanel = new JPanel();
150  mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
151 
152  mainPanel.setBorder(GUIConstants.DIALOG_BORDER);
153 
154  mainPanel.add(new JLabel(ACTION_BUILDER.format("goLocationText", mapView.getMapControl().getMapModel().getMapArchObject().getMapName())));
155  mainPanel.add(Box.createVerticalStrut(5));
156 
157  final JComponent coordinatesPanel = new JPanel(new GridBagLayout());
158  final GridBagConstraints gbcLabel = new GridBagConstraints();
159  final GridBagConstraints gbcField = new GridBagConstraints();
160  gbcLabel.insets = new Insets(2, 2, 2, 2);
161  gbcField.insets = new Insets(2, 2, 2, 2);
162  gbcLabel.anchor = GridBagConstraints.EAST;
163  gbcField.gridwidth = GridBagConstraints.REMAINDER;
164  coordinatesPanel.setBorder(new CompoundBorder(new TitledBorder(new EtchedBorder()), GUIConstants.DIALOG_BORDER));
165 
166  final Point point = mapView.getMapCursor().getLocation();
167 
168  coordinatesPanel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "goLocationX"), gbcLabel);
169  xCoordinateField.setText(Integer.toString(point.x));
170  xCoordinateField.setColumns(3);
171  coordinatesPanel.add(xCoordinateField, gbcField);
172 
173  coordinatesPanel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "goLocationY"), gbcLabel);
174  yCoordinateField.setText(Integer.toString(point.y));
175  yCoordinateField.setColumns(3);
176  coordinatesPanel.add(yCoordinateField, gbcField);
177 
178  mainPanel.add(coordinatesPanel);
179  mainPanel.add(Box.createVerticalStrut(5));
180 
181  return mainPanel;
182  }
183 
187  @ActionMethod
188  public void goLocationOkay() {
189  if (goLocation()) {
190  setValue(okButton);
191  }
192  }
193 
197  @ActionMethod
198  public void goLocationApply() {
199  goLocation();
200  }
201 
205  @ActionMethod
206  public void goLocationCancel() {
207  setValue(cancelButton);
208  }
209 
215  private boolean goLocation() {
216  final Size2D mapSize = mapView.getMapControl().getMapModel().getMapArchObject().getMapSize();
217  final Point point = new Point();
218  try {
219  point.x = parseCoordinate(xCoordinateField, mapSize.getWidth());
220  point.y = parseCoordinate(yCoordinateField, mapSize.getHeight());
221  } catch (final IllegalArgumentException ignored) {
222  return false;
223  }
224 
225  mapView.setCursorLocation(point);
226  return true;
227  }
228 
236  private int parseCoordinate(@NotNull final JTextComponent textField, final int range) {
237  final int result;
238  try {
239  result = Integer.parseInt(textField.getText());
240  } catch (final NumberFormatException e) {
241  ACTION_BUILDER.showMessageDialog(this, "goLocationCoordinateNotANumber");
242  textField.requestFocus();
243  throw e;
244  } catch (final IllegalArgumentException e) {
245  ACTION_BUILDER.showMessageDialog(this, "goLocationCoordinateOutOfRange");
246  textField.requestFocus();
247  throw e;
248  }
249  if (result < 0 || result >= range) {
250  ACTION_BUILDER.showMessageDialog(this, "goLocationCoordinateOutOfRange");
251  textField.requestFocus();
252  throw new IllegalArgumentException("invalid coordinate value: " + result);
253  }
254 
255  return result;
256  }
257 
258  @Override
259  public void setValue(@Nullable final Object newValue) {
260  super.setValue(newValue);
261  if (newValue != UNINITIALIZED_VALUE) {
262  goLocationDialogManager.disposeDialog(mapView);
263  }
264  }
265 
270  @NotNull
271  public Window getDialog() {
272  return dialog;
273  }
274 
275 }
GoLocationDialog(@NotNull final AbstractPerMapDialogManager< G, A, R, GoLocationDialog< G, A, R >> goLocationDialogManager, @NotNull final MapView< G, A, R > mapView)
Creates a new instance.
Graphical User Interface of Gridarta.
final JTextField xCoordinateField
The text input field for the x coordinate.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
final JButton cancelButton
The JButton for cancel.
static final long serialVersionUID
The serial Version UID.
MapControl< G, A, R > getMapControl()
Return the controller of this view.
Point getLocation()
Get position of cursor.
Definition: MapCursor.java:226
MapModel< G, A, R > getMapModel()
Returns the map model.
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.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Utility class for JTextComponent related functions.
void disposeDialog(final MapView< G, A, R > mapView)
Dispose a dialog.
final AbstractPerMapDialogManager< G, A, R, GoLocationDialog< G, A, R > > goLocationDialogManager
The dialog manager for this dialog.
GameObjects are the objects based on Archetypes found on maps.
Maintains (dialog) instance associated to map view instances.
final MapView< G, A, R > mapView
The affected map view this go location dialog affects.
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
static void setAutoSelectOnFocus(@NotNull final JTextComponent textComponent)
Selects all text of a JTextComponent when the component gains the focus.
Window getDialog()
Returns the Window for this instance.
Base classes for rendering maps.
void setCursorLocation(@NotNull Point point)
Sets the cursor location.
final JTextField yCoordinateField
The text input field for the y coordinate.
Utility class for ActionBuilder related functions.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
int parseCoordinate(@NotNull final JTextComponent textField, final int range)
Parses a coordinate input field.
A dialog to ask the user for coordinates to move the cursor to.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
boolean goLocation()
Moves the cursor to the given coordinates.
Defines common UI constants used in different dialogs.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
static void setActionNextFocus(@NotNull final JTextField textField, @NotNull final Component nextComponent)
Transfers the focus to another component when ENTER is pressed.