Gridarta Editor
AbstractMapsizeNewMapDialog.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.newmap;
21 
22 import java.awt.GridBagConstraints;
23 import javax.swing.JPanel;
24 import javax.swing.JTextField;
30 import net.sf.gridarta.utils.Size2D;
31 import net.sf.japi.swing.action.ActionBuilder;
32 import net.sf.japi.swing.action.ActionBuilderFactory;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35 
41 public abstract class AbstractMapsizeNewMapDialog<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractNewMapDialog<G, A, R> {
42 
46  private static final long serialVersionUID = 1L;
47 
51  @NotNull
52  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
53 
57  @Nullable
58  private final String widthKey;
59 
63  @Nullable
64  private final String heightKey;
65 
69  private final int defaultWidth;
70 
74  private final int defaultHeight;
75 
79  @NotNull
80  private final JTextField mapWidthField = new JTextField();
81 
85  @NotNull
86  private final JTextField mapHeightField = new JTextField();
87 
97  protected AbstractMapsizeNewMapDialog(@Nullable final String widthKey, @Nullable final String heightKey, final int defaultWidth, final int defaultHeight) {
98  this.widthKey = widthKey;
99  this.heightKey = heightKey;
100  this.defaultWidth = defaultWidth;
101  this.defaultHeight = defaultHeight;
102  addDocumentListener(mapWidthField);
103  addDocumentListener(mapHeightField);
104  }
105 
106  @Override
107  protected void addFields(@NotNull final JPanel panel, @NotNull final GridBagConstraints gbcLabel, @NotNull final GridBagConstraints gbcField) {
108  mapWidthField.setText(Integer.toString(widthKey == null ? defaultWidth : PREFERENCES.getInt(widthKey, defaultWidth)));
109  mapWidthField.setColumns(3);
110  panel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "mapWidth"), gbcLabel);
111  panel.add(mapWidthField, gbcField);
112  mapWidthField.selectAll();
113 
114  mapHeightField.setText(Integer.toString(heightKey == null ? defaultHeight : PREFERENCES.getInt(heightKey, defaultHeight)));
115  mapHeightField.setColumns(3);
116  panel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "mapHeight"), gbcLabel);
117  panel.add(mapHeightField, gbcField);
118  mapHeightField.selectAll();
119  }
120 
125  @Nullable
126  protected Size2D getMapSize() {
127  final int width = getMapWidth();
128  if (width < 0) {
129  return null;
130  }
131 
132  final int height = getMapHeight();
133  if (height < 0) {
134  return null;
135  }
136 
137  if (widthKey != null) {
138  PREFERENCES.putInt(widthKey, width);
139  }
140  if (heightKey != null) {
141  PREFERENCES.putInt(heightKey, height);
142  }
143 
144  return new Size2D(width, height);
145  }
146 
151  protected int getMapWidth() {
152  final String text = mapWidthField.getText();
153  final int width = NumberUtils.parseInt(text);
154  return width < 1 ? -1 : width;
155 
156  }
157 
162  protected int getMapHeight() {
163  final String text = mapHeightField.getText();
164  final int height = NumberUtils.parseInt(text);
165  return height < 1 ? -1 : height;
166 
167  }
168 
173  protected void setMapSizeEnabled(final boolean enabled) {
174  mapWidthField.setEnabled(enabled);
175  mapHeightField.setEnabled(enabled);
176  }
177 
178 }
final JTextField mapHeightField
Textfield for the height of the new map.
final String widthKey
The preference key for storing the map width; may be.
Utility class for parsing strings into numbers.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
static int parseInt(@NotNull final String s)
Parses an integer string.
GameObjects are the objects based on Archetypes found on maps.
final String heightKey
The preference key for storing the map height; may be.
final JTextField mapWidthField
Textfield for the width of the new map.
void addFields(@NotNull final JPanel panel, @NotNull final GridBagConstraints gbcLabel, @NotNull final GridBagConstraints gbcField)
AbstractMapsizeNewMapDialog(@Nullable final String widthKey, @Nullable final String heightKey, final int defaultWidth, final int defaultHeight)
Creates a new instance.
Utility class for ActionBuilder related functions.
An abstract base class implementing a AbstractNewMapDialog supporting map size input fields...
void addDocumentListener( @NotNull final JTextComponent textComponent)
Watches for text changes in a text component and enables the "OK" button accordingly.
Size2D getMapSize()
Validate the map size fields and return the result.
Dialog used to ask the user the properties for the new level.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
void setMapSizeEnabled(final boolean enabled)
Enables or disables the map size input fields.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30