Gridarta Editor
NewMapDialog.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.Component;
23 import java.awt.FlowLayout;
24 import java.awt.GridBagConstraints;
25 import javax.swing.AbstractButton;
26 import javax.swing.JCheckBox;
27 import javax.swing.JPanel;
28 import javax.swing.JTextField;
29 import javax.swing.text.JTextComponent;
37 import net.sf.gridarta.utils.Size2D;
38 import net.sf.japi.swing.action.ActionBuilder;
39 import net.sf.japi.swing.action.ActionBuilderFactory;
40 import net.sf.japi.swing.action.ActionMethod;
41 import org.jetbrains.annotations.NotNull;
42 import org.jetbrains.annotations.Nullable;
43 
48 public class NewMapDialog<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractMapsizeNewMapDialog<G, A, R> {
49 
53  @NotNull
54  private static final String DEFAULT_LEVEL_WIDTH_KEY = "NewMapDialog.mapWidth";
55 
59  @NotNull
60  private static final String DEFAULT_LEVEL_HEIGHT_KEY = "NewMapDialog.mapHeight";
61 
65  @NotNull
66  private static final String DEFAULT_DIFFICULTY_KEY = "NewMapDialog.difficulty";
67 
71  private static final long serialVersionUID = 1L;
72 
76  @NotNull
77  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
78 
82  @NotNull
84 
88  @NotNull
90 
94  private final boolean showMapSizeDefault;
95 
102  private boolean mapSizeDefault;
103 
107  private final boolean showMapDifficulty;
108 
112  private final int defaultWidth;
113 
117  private final int defaultHeight;
118 
122  private final int defaultDifficulty;
123 
128  @NotNull
129  private final JTextComponent mapNameField = new JTextField(16);
130 
134  @NotNull
135  private final AbstractButton mapSizeDefaultCheckbox = new JCheckBox(ACTION_BUILDER.createToggle(false, "mapSizeDefault", this));
136 
140  @NotNull
141  private final JTextField mapDifficultyField = new JTextField();
142 
154  public NewMapDialog(@NotNull final MapViewsManager<G, A, R> mapViewsManager, @NotNull final MapArchObjectFactory<A> mapArchObjectFactory, @NotNull final Component parentComponent, final boolean showMapSizeDefault, final boolean showMapDifficulty, final int defaultWidth, final int defaultHeight, final int defaultDifficulty) {
155  super(DEFAULT_LEVEL_WIDTH_KEY, DEFAULT_LEVEL_HEIGHT_KEY, defaultWidth, defaultHeight);
156  this.mapViewsManager = mapViewsManager;
157  this.mapArchObjectFactory = mapArchObjectFactory;
158  this.showMapSizeDefault = showMapSizeDefault;
159  this.showMapDifficulty = showMapDifficulty;
160  this.defaultWidth = defaultWidth;
161  this.defaultHeight = defaultHeight;
162  this.defaultDifficulty = defaultDifficulty;
163  mapSizeDefault = showMapSizeDefault;
164 
165  init1(parentComponent, ActionBuilderUtils.getString(ACTION_BUILDER, "newMap.title"));
166  init2();
167  mapNameField.selectAll();
168 
169  addDocumentListener(mapNameField);
170  addDocumentListener(mapDifficultyField);
171  updateOkButton();
172  }
173 
174  @Override
175  protected void init2() {
176  if (showMapSizeDefault) {
177  setMapSizeDefault(true);
178  }
179  super.init2();
180  }
181 
182  @NotNull
183  @Override
184  protected JPanel createMapNamePanel() {
185  final JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
186  panel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "newMapMapName"));
187  panel.add(mapNameField);
188  return panel;
189  }
190 
191  @Override
192  protected void addFields(@NotNull final JPanel panel, @NotNull final GridBagConstraints gbcLabel, @NotNull final GridBagConstraints gbcField) {
193  if (showMapSizeDefault) {
194  panel.add(mapSizeDefaultCheckbox, gbcField);
195  }
196  super.addFields(panel, gbcLabel, gbcField);
197  if (showMapDifficulty) {
198  mapDifficultyField.setText(Integer.toString(PREFERENCES.getInt(DEFAULT_DIFFICULTY_KEY, defaultDifficulty)));
199  panel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "mapDifficulty"), gbcLabel);
200  mapDifficultyField.setColumns(3);
201  panel.add(mapDifficultyField, gbcField);
202  }
203  }
204 
205  @Override
206  protected boolean createNew() {
207  final A mapArchObject = mapArchObjectFactory.newMapArchObject(true);
208 
209  final Size2D mapSize = getMapSize();
210  if (mapSize == null) {
211  return false;
212  }
213  mapArchObject.setMapSize(mapSize);
214 
215  final String mapName = getMapName();
216  if (mapName == null) {
217  return false;
218  }
219 
220  mapArchObject.setMapName(mapName);
221  if (showMapDifficulty) {
222  final int difficulty = getDifficulty();
223  if (difficulty < 0) {
224  return false;
225  }
226  mapArchObject.setDifficulty(difficulty);
227  }
228 
229  mapViewsManager.newMapWithView(null, mapArchObject, null, null);
230  return true;
231  }
232 
233  @Override
234  protected boolean isOkButtonEnabled() {
235  return super.isOkButtonEnabled() && (mapSizeDefault || (getMapWidth() >= 0 && getMapHeight() >= 0)) && getMapName() != null && (!showMapDifficulty || getDifficulty() >= 0);
236  }
237 
242  @Nullable
243  private String getMapName() {
244  final String mapName = mapNameField.getText();
245  return !mapName.isEmpty() ? mapName : null;
246  }
247 
252  private int getDifficulty() {
253  final String text = mapDifficultyField.getText();
254  final int difficulty = NumberUtils.parseInt(text);
255  return difficulty >= 1 ? difficulty : -1;
256  }
257 
263  @ActionMethod
264  public boolean isMapSizeDefault() {
265  return mapSizeDefault;
266  }
267 
273  public void setMapSizeDefault(final boolean mapSizeDefault) {
274  mapSizeDefaultCheckbox.setSelected(mapSizeDefault);
275  this.mapSizeDefault = mapSizeDefault;
276  setMapSizeEnabled(!mapSizeDefault);
277  updateOkButton();
278  }
279 
284  @Nullable
285  @Override
286  protected Size2D getMapSize() {
287  return mapSizeDefault ? new Size2D(defaultWidth, defaultHeight) : super.getMapSize();
288  }
289 
290 }
static final ActionBuilder ACTION_BUILDER
Action Builder.
final boolean showMapSizeDefault
Whether to show the "mapSizeDefault" checkbox.
Graphical User Interface of Gridarta.
NewMapDialog(@NotNull final MapViewsManager< G, A, R > mapViewsManager, @NotNull final MapArchObjectFactory< A > mapArchObjectFactory, @NotNull final Component parentComponent, final boolean showMapSizeDefault, final boolean showMapDifficulty, final int defaultWidth, final int defaultHeight, final int defaultDifficulty)
Creates a "new map" dialog.
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager to use.
final boolean showMapDifficulty
Whether to show the "mapDifficulty" field.
final int defaultWidth
The default width for new maps.
Utility class for parsing strings into numbers.
Factory for creating MapArchObject instances.
void newMapWithView(@Nullable final List< G > objects, @NotNull final A mapArchObject, @Nullable final Point viewPosition, @Nullable final MapFile mapFile)
Create a new map control and an initial map view.
static final long serialVersionUID
Serial Version UID.
final int defaultHeight
The default height for new maps.
A newMapArchObject(boolean addDefaultAttributes)
Creates a new MapArchObject instance.
final JTextComponent mapNameField
Textfield for the name of the map.
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.
boolean mapSizeDefault
Whether to use the default map size.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void addFields(@NotNull final JPanel panel, @NotNull final GridBagConstraints gbcLabel, @NotNull final GridBagConstraints gbcField)
Size2D getMapSize()
Validate the map size fields and return the result.
static int parseInt(@NotNull final String s)
Parses an integer string.
final int defaultDifficulty
The default height for new maps.
int getDifficulty()
Returns the current difficulty value.
GameObjects are the objects based on Archetypes found on maps.
final MapArchObjectFactory< A > mapArchObjectFactory
The map arch object factory instance.
Dialog to create a new map file.
final AbstractButton mapSizeDefaultCheckbox
Checkbox to set whether the default width / height should be overridden.
Base classes for rendering maps.
static final String DEFAULT_LEVEL_HEIGHT_KEY
The key used to store the last used map height in preferences.
Utility class for ActionBuilder related functions.
An abstract base class implementing a AbstractNewMapDialog supporting map size input fields...
boolean isMapSizeDefault()
Set whether to override the default map size.
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.
void setMapSizeDefault(final boolean mapSizeDefault)
Set whether to override the default map size.
String getMapName()
Returns the current map name value.
static final String DEFAULT_DIFFICULTY_KEY
The key used to store the last used difficulty in preferences.
static final String DEFAULT_LEVEL_WIDTH_KEY
The key used to store the last used map width in preferences.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
final JTextField mapDifficultyField
Textfield for the difficulty of the new map.