Gridarta Editor
ShrinkMapSizeDialog.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.shrinkmapsize;
21 
22 import java.awt.Component;
23 import java.awt.Window;
24 import javax.swing.AbstractButton;
25 import javax.swing.Box;
26 import javax.swing.BoxLayout;
27 import javax.swing.JButton;
28 import javax.swing.JCheckBox;
29 import javax.swing.JComponent;
30 import javax.swing.JDialog;
31 import javax.swing.JLabel;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34 import javax.swing.WindowConstants;
42 import net.sf.japi.swing.action.ActionBuilder;
43 import net.sf.japi.swing.action.ActionBuilderFactory;
44 import net.sf.japi.swing.action.ActionMethod;
45 import org.jetbrains.annotations.NotNull;
46 import org.jetbrains.annotations.Nullable;
47 
52 public class ShrinkMapSizeDialog<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends JOptionPane {
53 
57  private static final long serialVersionUID = 1L;
58 
62  @NotNull
63  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
64 
68  @NotNull
70 
74  @NotNull
75  private final MapView<G, A, R> mapView;
76 
80  @NotNull
81  private final AbstractButton eastCheckBox = new JCheckBox(ACTION_BUILDER.createToggle(false, "shrinkMapSizeDialogEast", this));
82 
86  @NotNull
87  private final AbstractButton southCheckBox = new JCheckBox(ACTION_BUILDER.createToggle(false, "shrinkMapSizeDialogSouth", this));
88 
92  @NotNull
93  private final JLabel warnings = new JLabel();
94 
98  @NotNull
99  private final JButton okButton = new JButton(ACTION_BUILDER.createAction(false, "shrinkMapSizeDialogOk", this));
100 
104  @NotNull
105  private final JButton cancelButton = new JButton(ACTION_BUILDER.createAction(false, "shrinkMapSizeDialogCancel", this));
106 
110  @Nullable
111  private final JDialog dialog;
112 
119  this.shrinkMapSizeDialogManager = shrinkMapSizeDialogManager;
120 
121  okButton.setDefaultCapable(true);
122  setOptions(new Object[] { okButton, cancelButton });
123 
124  this.mapView = mapView;
125  setMessage(createPanel());
126 
128  dialog.setVisible(true);
129  }
130 
131  @NotNull
132  @Override
133  public final JDialog createDialog(@NotNull final Component parentComponent, @NotNull final String title) {
134  final JDialog result = super.createDialog(parentComponent, title);
135  result.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
136  result.getRootPane().setDefaultButton(okButton);
137  result.setModal(false);
138  result.pack();
139  return result;
140  }
141 
146  @NotNull
147  private JPanel createPanel() {
148  final JComponent label = ActionBuilderUtils.newLabel(ACTION_BUILDER, "shrinkMapSizeDialogLabel");
149  label.setAlignmentX(0.0F);
150  eastCheckBox.setAlignmentX(0.0F);
151  southCheckBox.setAlignmentX(0.0F);
152  warnings.setAlignmentX(0.0F);
153  final JPanel mainPanel = new JPanel();
154  mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
155  mainPanel.setBorder(GUIConstants.DIALOG_BORDER);
156  mainPanel.add(label);
157  mainPanel.add(Box.createVerticalStrut(5));
158  mainPanel.add(eastCheckBox);
159  mainPanel.add(southCheckBox);
160  mainPanel.add(Box.createVerticalStrut(5));
161  mainPanel.add(warnings);
162  mainPanel.add(Box.createVerticalStrut(5));
164  eastCheckBox.setEnabled((shrinkFlags & ShrinkMapSizeUtils.SHRINK_EAST) != 0);
165  eastCheckBox.setSelected((shrinkFlags & ShrinkMapSizeUtils.SHRINK_EAST) != 0);
166  southCheckBox.setEnabled((shrinkFlags & ShrinkMapSizeUtils.SHRINK_SOUTH) != 0);
167  southCheckBox.setSelected((shrinkFlags & ShrinkMapSizeUtils.SHRINK_SOUTH) != 0);
168  updateWarnings();
169  return mainPanel;
170  }
171 
175  private void updateWarnings() {
176  final StringBuilder sb = new StringBuilder(100);
177  if (!eastCheckBox.isEnabled() && !southCheckBox.isEnabled()) {
178  sb.append("<p>");
179  sb.append(ActionBuilderUtils.getString(ACTION_BUILDER, "shrinkMapSizeDialogNoEmptySpace"));
180  } else {
182  if (eastCheckBox.isEnabled() && eastCheckBox.isSelected()) {
183  if (!mapArchObject.getTilePath(Direction.NORTH).isEmpty()) {
184  sb.append("<p>");
185  sb.append(ActionBuilderUtils.getString(ACTION_BUILDER, "shrinkMapSizeDialogEastBreaksNorthTiling"));
186  }
187  if (!mapArchObject.getTilePath(Direction.SOUTH).isEmpty()) {
188  sb.append("<p>");
189  sb.append(ActionBuilderUtils.getString(ACTION_BUILDER, "shrinkMapSizeDialogEastBreaksSouthTiling"));
190  }
191  }
192  if (southCheckBox.isEnabled() && southCheckBox.isSelected()) {
193  if (!mapArchObject.getTilePath(Direction.EAST).isEmpty()) {
194  sb.append("<p>");
195  sb.append(ActionBuilderUtils.getString(ACTION_BUILDER, "shrinkMapSizeDialogSouthBreaksEastTiling"));
196  }
197  if (!mapArchObject.getTilePath(Direction.WEST).isEmpty()) {
198  sb.append("<p>");
199  sb.append(ActionBuilderUtils.getString(ACTION_BUILDER, "shrinkMapSizeDialogSouthBreaksWestTiling"));
200  }
201  }
202  }
203  okButton.setEnabled(eastCheckBox.isSelected() || southCheckBox.isSelected());
204  if (sb.length() > 0) {
205  sb.insert(0, ActionBuilderUtils.getString(ACTION_BUILDER, "shrinkMapSizeDialogWarning"));
206  sb.insert(0, "<html>");
207  warnings.setText(sb.toString());
208  } else {
209  warnings.setText("");
210  }
211  }
212 
216  @ActionMethod
217  public void shrinkMapSizeDialogOk() {
218  shrinkMapSize();
220  }
221 
225  @ActionMethod
228  }
229 
233  private void shrinkMapSize() {
234  int shrinkFlags = 0;
235  if (eastCheckBox.isSelected()) {
236  shrinkFlags |= ShrinkMapSizeUtils.SHRINK_EAST;
237  }
238  if (southCheckBox.isSelected()) {
239  shrinkFlags |= ShrinkMapSizeUtils.SHRINK_SOUTH;
240  }
242  }
243 
244  @Override
245  public void setValue(@Nullable final Object newValue) {
246  super.setValue(newValue);
247  if (newValue != UNINITIALIZED_VALUE) {
249  }
250  }
251 
256  @Nullable
257  public Window getDialog() {
258  return dialog;
259  }
260 
265  @ActionMethod
266  public boolean isShrinkMapSizeDialogEast() {
267  return eastCheckBox.isSelected();
268  }
269 
274  @ActionMethod
275  public void setShrinkMapSizeDialogEast(final boolean flag) {
276  eastCheckBox.setSelected(!flag);
277  updateWarnings();
278  }
279 
284  @ActionMethod
285  public boolean isShrinkMapSizeDialogSouth() {
286  return southCheckBox.isSelected();
287  }
288 
293  @ActionMethod
294  public void setShrinkMapSizeDialogSouth(final boolean flag) {
295  southCheckBox.setSelected(!flag);
296  updateWarnings();
297  }
298 
299 }
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialogManager< G, A, R >
net.sf.gridarta.model.direction.Direction
A direction.
Definition: Direction.java:28
net.sf.gridarta.model.mapmodel.MapModel.getMapArchObject
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.shrinkMapSize
void shrinkMapSize()
Shrinks the map model.
Definition: ShrinkMapSizeDialog.java:233
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.dialog
final JDialog dialog
The associated JDialog instance.
Definition: ShrinkMapSizeDialog.java:111
net.sf.gridarta.gui.map.mapview.MapView.getMapControl
MapControl< G, A, R > getMapControl()
Return the controller of this view.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.createDialog
final JDialog createDialog(@NotNull final Component parentComponent, @NotNull final String title)
Definition: ShrinkMapSizeDialog.java:133
net.sf.gridarta.gui.utils.GUIConstants
Defines common UI constants used in different dialogs.
Definition: GUIConstants.java:33
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils.getShrinkFlags
static int getShrinkFlags(@NotNull final MapModel<?, ?, ?> mapModel)
Returns which borders contain empty squares.
Definition: ShrinkMapSizeUtils.java:80
net.sf.gridarta.model.direction.Direction.SOUTH
SOUTH
South.
Definition: Direction.java:43
net.sf.gridarta.gui.map.mapview.MapView.getComponent
Component getComponent()
Returns the component associated with this MapView that can be used as parent for dialogs.
net.sf
net.sf.gridarta.model.maparchobject.MapArchObject.getTilePath
String getTilePath(@NotNull Direction direction)
Returns a tile path.
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.shrinkMapSizeDialogOk
void shrinkMapSizeDialogOk()
Action method for ok.
Definition: ShrinkMapSizeDialog.java:217
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.isShrinkMapSizeDialogEast
boolean isShrinkMapSizeDialogEast()
Action method for "shrinkMapSizeDialogEast".
Definition: ShrinkMapSizeDialog.java:266
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.serialVersionUID
static final long serialVersionUID
Serial Version UID.
Definition: ShrinkMapSizeDialog.java:57
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.shrinkmapsize.ShrinkMapSizeDialog.southCheckBox
final AbstractButton southCheckBox
Checkbox to remove empty squares at the south edge.
Definition: ShrinkMapSizeDialog.java:87
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.model.direction.Direction.NORTH
NORTH
North.
Definition: Direction.java:33
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.ShrinkMapSizeDialog
ShrinkMapSizeDialog(@NotNull final ShrinkMapSizeDialogManager< G, A, R > shrinkMapSizeDialogManager, @NotNull final MapView< G, A, R > mapView)
Creates a new instance.
Definition: ShrinkMapSizeDialog.java:118
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.updateWarnings
void updateWarnings()
Updates the warnings label.
Definition: ShrinkMapSizeDialog.java:175
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.shrinkmapsize.ShrinkMapSizeDialog.createPanel
JPanel createPanel()
Creates the GUI.
Definition: ShrinkMapSizeDialog.java:147
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.warnings
final JLabel warnings
Text area for displaying warning messages.
Definition: ShrinkMapSizeDialog.java:93
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.shrinkMapSizeDialogManager
final ShrinkMapSizeDialogManager< G, A, R > shrinkMapSizeDialogManager
The manager for this dialog.
Definition: ShrinkMapSizeDialog.java:69
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.eastCheckBox
final AbstractButton eastCheckBox
Checkbox to remove empty squares at the east edge.
Definition: ShrinkMapSizeDialog.java:81
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.gui.map.mapview
Definition: AbstractMapView.java:20
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.okButton
final JButton okButton
JButton for ok.
Definition: ShrinkMapSizeDialog.java:99
net.sf.gridarta.gui.map.mapview.MapView
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.mapView
final MapView< G, A, R > mapView
The affected map view of this dialog.
Definition: ShrinkMapSizeDialog.java:75
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils.SHRINK_SOUTH
static final int SHRINK_SOUTH
Flag value: remove empty space from south border.
Definition: ShrinkMapSizeUtils.java:40
net.sf.gridarta.utils.ActionBuilderUtils.getString
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Definition: ActionBuilderUtils.java:71
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils.SHRINK_EAST
static final int SHRINK_EAST
Flag value: remove empty space from east border.
Definition: ShrinkMapSizeUtils.java:35
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.setValue
void setValue(@Nullable final Object newValue)
Definition: ShrinkMapSizeDialog.java:245
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.isShrinkMapSizeDialogSouth
boolean isShrinkMapSizeDialogSouth()
Action method for "shrinkMapSizeDialogSouth".
Definition: ShrinkMapSizeDialog.java:285
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.setShrinkMapSizeDialogSouth
void setShrinkMapSizeDialogSouth(final boolean flag)
Action method for "shrinkMapSizeDialogSouth".
Definition: ShrinkMapSizeDialog.java:294
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.model.direction.Direction.EAST
EAST
East.
Definition: Direction.java:38
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.shrinkMapSizeDialogCancel
void shrinkMapSizeDialogCancel()
Action method for cancel.
Definition: ShrinkMapSizeDialog.java:226
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Action Builder.
Definition: ShrinkMapSizeDialog.java:63
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog
Dialog to ask which empty borders to remove from a map.
Definition: ShrinkMapSizeDialog.java:52
net.sf.gridarta.utils.ActionBuilderUtils
Utility class for ActionBuilder related functions.
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.model.mapcontrol.MapControl.getMapModel
MapModel< G, A, R > getMapModel()
Returns the map model.
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.setShrinkMapSizeDialogEast
void setShrinkMapSizeDialogEast(final boolean flag)
Action method for "shrinkMapSizeDialogEast".
Definition: ShrinkMapSizeDialog.java:275
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.cancelButton
final JButton cancelButton
JButton for cancel.
Definition: ShrinkMapSizeDialog.java:105
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialogManager.disposeDialog
void disposeDialog(@NotNull final MapView< G, A, R > mapView)
Disposes a dialog.
Definition: ShrinkMapSizeDialogManager.java:99
net.sf.gridarta.model.direction.Direction.WEST
WEST
West.
Definition: Direction.java:48
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeDialog.getDialog
Window getDialog()
Returns the JDialog for this instance.
Definition: ShrinkMapSizeDialog.java:257
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.direction
Definition: Direction.java:20
net.sf.gridarta.gui.utils.GUIConstants.DIALOG_BORDER
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
Definition: GUIConstants.java:38
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils
Utility class to remove empty squares from a map's border.
Definition: ShrinkMapSizeUtils.java:30
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils.shrinkMap
static void shrinkMap(@NotNull final MapModel<?, ?, ?> mapModel, final int shrinkFlags)
Removes empty squares from a map's border.
Definition: ShrinkMapSizeUtils.java:53