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-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.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 
118  public ShrinkMapSizeDialog(@NotNull final ShrinkMapSizeDialogManager<G, A, R> shrinkMapSizeDialogManager, @NotNull final MapView<G, A, R> mapView) {
119  this.shrinkMapSizeDialogManager = shrinkMapSizeDialogManager;
120 
121  okButton.setDefaultCapable(true);
122  setOptions(new Object[] { okButton, cancelButton });
123 
124  this.mapView = mapView;
125  setMessage(createPanel());
126 
127  dialog = createDialog(mapView.getComponent(), ActionBuilderUtils.getString(ACTION_BUILDER, "shrinkMapSizeDialogTitle"));
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));
163  final int shrinkFlags = ShrinkMapSizeUtils.getShrinkFlags(mapView.getMapControl().getMapModel());
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 {
181  final MapArchObject<A> mapArchObject = mapView.getMapControl().getMapModel().getMapArchObject();
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();
219  setValue(okButton);
220  }
221 
225  @ActionMethod
227  setValue(cancelButton);
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  }
241  ShrinkMapSizeUtils.shrinkMap(mapView.getMapControl().getMapModel(), shrinkFlags);
242  }
243 
244  @Override
245  public void setValue(@Nullable final Object newValue) {
246  super.setValue(newValue);
247  if (newValue != UNINITIALIZED_VALUE) {
248  shrinkMapSizeDialogManager.disposeDialog(mapView);
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 }
final AbstractButton southCheckBox
Checkbox to remove empty squares at the south edge.
Graphical User Interface of Gridarta.
Utility class to remove empty squares from a map&#39;s border.
MapControl< G, A, R > getMapControl()
Return the controller of this view.
void setShrinkMapSizeDialogEast(final boolean flag)
Action method for "shrinkMapSizeDialogEast".
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.
final JDialog dialog
The associated JDialog instance.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
static final ActionBuilder ACTION_BUILDER
Action Builder.
Dialog to ask which empty borders to remove from a map.
void disposeDialog(@NotNull final MapView< G, A, R > mapView)
Disposes a dialog.
GameObjects are the objects based on Archetypes found on maps.
static void shrinkMap(@NotNull final MapModel<?, ?, ?> mapModel, final int shrinkFlags)
Removes empty squares from a map&#39;s border.
boolean isShrinkMapSizeDialogSouth()
Action method for "shrinkMapSizeDialogSouth".
static final int SHRINK_EAST
Flag value: remove empty space from east border.
Base classes for rendering maps.
final JLabel warnings
Text area for displaying warning messages.
Window getDialog()
Returns the JDialog for this instance.
final JDialog createDialog(@NotNull final Component parentComponent, @NotNull final String title)
boolean isShrinkMapSizeDialogEast()
Action method for "shrinkMapSizeDialogEast".
Utility class for ActionBuilder related functions.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
final MapView< G, A, R > mapView
The affected map view of this dialog.
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
final ShrinkMapSizeDialogManager< G, A, R > shrinkMapSizeDialogManager
The manager for this dialog.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
static int getShrinkFlags(@NotNull final MapModel<?, ?, ?> mapModel)
Returns which borders contain empty squares.
static final int SHRINK_SOUTH
Flag value: remove empty space from south border.
ShrinkMapSizeDialog(@NotNull final ShrinkMapSizeDialogManager< G, A, R > shrinkMapSizeDialogManager, @NotNull final MapView< G, A, R > mapView)
Creates a new instance.
String getTilePath(@NotNull Direction direction)
Returns a tile path.
void setShrinkMapSizeDialogSouth(final boolean flag)
Action method for "shrinkMapSizeDialogSouth".
Defines common UI constants used in different dialogs.
final AbstractButton eastCheckBox
Checkbox to remove empty squares at the east edge.