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-2023 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;
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 }
net.sf.gridarta.gui.dialog.newmap.AbstractNewMapDialog< G, A, R >::PREFERENCES
static final Preferences PREFERENCES
Preferences.
Definition: AbstractNewMapDialog.java:79
net.sf.gridarta.utils.NumberUtils.parseInt
static int parseInt(@NotNull final String s)
Parses an integer string.
Definition: NumberUtils.java:41
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.heightKey
final String heightKey
The preference key for storing the map height; may be.
Definition: AbstractMapsizeNewMapDialog.java:64
net.sf
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.defaultHeight
final int defaultHeight
The default height for new maps.
Definition: AbstractMapsizeNewMapDialog.java:74
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.utils.ActionBuilderUtils.newLabel
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
Definition: ActionBuilderUtils.java:117
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.getMapHeight
int getMapHeight()
Returns the current map height value.
Definition: AbstractMapsizeNewMapDialog.java:162
net.sf.gridarta.gui.dialog.newmap.AbstractNewMapDialog< G, A, R >::addDocumentListener
void addDocumentListener( @NotNull final JTextComponent textComponent)
Watches for text changes in a text component and enables the "OK" button accordingly.
Definition: AbstractNewMapDialog.java:250
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.mapHeightField
final JTextField mapHeightField
Textfield for the height of the new map.
Definition: AbstractMapsizeNewMapDialog.java:86
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.gui.dialog.newmap.AbstractNewMapDialog
Dialog used to ask the user the properties for the new level.
Definition: AbstractNewMapDialog.java:62
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.widthKey
final String widthKey
The preference key for storing the map width; may be.
Definition: AbstractMapsizeNewMapDialog.java:58
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.defaultWidth
final int defaultWidth
The default width for new maps.
Definition: AbstractMapsizeNewMapDialog.java:69
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.getMapSize
Size2D getMapSize()
Validate the map size fields and return the result.
Definition: AbstractMapsizeNewMapDialog.java:126
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog
An abstract base class implementing a AbstractNewMapDialog supporting map size input fields.
Definition: AbstractMapsizeNewMapDialog.java:41
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.getMapWidth
int getMapWidth()
Returns the current map width value.
Definition: AbstractMapsizeNewMapDialog.java:151
net.sf.gridarta.utils.ActionBuilderUtils
Utility class for ActionBuilder related functions.
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.mapWidthField
final JTextField mapWidthField
Textfield for the width of the new map.
Definition: AbstractMapsizeNewMapDialog.java:80
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.setMapSizeEnabled
void setMapSizeEnabled(final boolean enabled)
Enables or disables the map size input fields.
Definition: AbstractMapsizeNewMapDialog.java:173
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.AbstractMapsizeNewMapDialog
AbstractMapsizeNewMapDialog(@Nullable final String widthKey, @Nullable final String heightKey, final int defaultWidth, final int defaultHeight)
Creates a new instance.
Definition: AbstractMapsizeNewMapDialog.java:97
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.addFields
void addFields(@NotNull final JPanel panel, @NotNull final GridBagConstraints gbcLabel, @NotNull final GridBagConstraints gbcField)
Definition: AbstractMapsizeNewMapDialog.java:107
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Action Builder.
Definition: AbstractMapsizeNewMapDialog.java:52
net.sf.gridarta.gui.dialog.newmap.AbstractMapsizeNewMapDialog.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: AbstractMapsizeNewMapDialog.java:46
net.sf.gridarta.utils.NumberUtils
Utility class for parsing strings into numbers.
Definition: NumberUtils.java:28