Gridarta Editor
AbstractNewMapDialog.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.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Insets;
26 import java.awt.event.FocusEvent;
27 import java.awt.event.FocusListener;
28 import java.util.prefs.Preferences;
29 import javax.swing.BoxLayout;
30 import javax.swing.JButton;
31 import javax.swing.JDialog;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34 import javax.swing.WindowConstants;
35 import javax.swing.border.CompoundBorder;
36 import javax.swing.border.EtchedBorder;
37 import javax.swing.border.TitledBorder;
38 import javax.swing.event.DocumentEvent;
39 import javax.swing.event.DocumentListener;
40 import javax.swing.text.JTextComponent;
41 import net.sf.gridarta.MainControl;
47 import net.sf.japi.swing.action.ActionBuilder;
48 import net.sf.japi.swing.action.ActionBuilderFactory;
49 import net.sf.japi.swing.action.ActionMethod;
50 import org.jetbrains.annotations.NotNull;
51 import org.jetbrains.annotations.Nullable;
52 
62 public abstract class AbstractNewMapDialog<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends JOptionPane {
63 
67  private static final long serialVersionUID = 1L;
68 
72  @NotNull
73  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
74 
78  @NotNull
79  protected static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainControl.class);
80 
84  @NotNull
85  private final JButton okButton = new JButton(ACTION_BUILDER.createAction(false, "mapOkay", this));
86 
90  @NotNull
91  private final JButton cancelButton = new JButton(ACTION_BUILDER.createAction(false, "mapCancel", this));
92 
93  @Nullable
94  private JDialog dialog;
95 
100  @NotNull
101  private final DocumentListener documentListener = new DocumentListener() {
102 
103  @Override
104  public void insertUpdate(@NotNull final DocumentEvent e) {
105  updateOkButton();
106  }
107 
108  @Override
109  public void removeUpdate(@NotNull final DocumentEvent e) {
110  updateOkButton();
111  }
112 
113  @Override
114  public void changedUpdate(@NotNull final DocumentEvent e) {
115  updateOkButton();
116  }
117 
118  };
119 
123  protected AbstractNewMapDialog() {
124  }
125 
131  protected void init1(@NotNull final Component parentComponent, @NotNull final String dialogTitle) {
132  okButton.setDefaultCapable(true);
133  setOptions(new Object[] { okButton, cancelButton });
134  setMessage(createPanel());
135 
136  dialog = createDialog(parentComponent, dialogTitle);
137  assert dialog != null;
138  dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
139  assert dialog != null;
140  dialog.getRootPane().setDefaultButton(okButton);
141  assert dialog != null;
142  dialog.setModal(false);
143  }
144 
148  protected void init2() {
149  assert dialog != null;
150  dialog.setVisible(true);
151  }
152 
153  @NotNull
154  private JPanel createPanel() {
155  final JPanel panel = new JPanel();
156  panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
157  panel.setBorder(GUIConstants.DIALOG_BORDER);
158  panel.add(createMapNamePanel());
159  final Component parametersPanel = createMapParametersPanel();
160  if (parametersPanel != null) {
161  panel.add(parametersPanel);
162  }
163  return panel;
164  }
165 
166  @NotNull
167  protected abstract JPanel createMapNamePanel();
168 
169  @Nullable
170  protected JPanel createMapParametersPanel() {
171  final JPanel panel = new JPanel(new GridBagLayout());
172 
173  final GridBagConstraints gbcLabel = new GridBagConstraints();
174  gbcLabel.insets = new Insets(2, 2, 2, 2);
175  gbcLabel.anchor = GridBagConstraints.EAST;
176 
177  final GridBagConstraints gbcField = new GridBagConstraints();
178  gbcField.insets = new Insets(2, 2, 2, 2);
179  gbcField.gridwidth = GridBagConstraints.REMAINDER;
180 
181  panel.setBorder(new CompoundBorder(new TitledBorder(new EtchedBorder(), ActionBuilderUtils.getString(ACTION_BUILDER, "newMapParameters")), GUIConstants.DIALOG_BORDER));
182  addFields(panel, gbcLabel, gbcField);
183  return panel;
184  }
185 
193  protected abstract void addFields(@NotNull JPanel panel, @NotNull GridBagConstraints gbcLabel, @NotNull GridBagConstraints gbcField);
194 
198  @ActionMethod
199  public void mapOkay() {
200  if (isOkButtonEnabled() && createNew()) {
201  setValue(okButton);
202  }
203  }
204 
208  @ActionMethod
209  public void mapCancel() {
210  setValue(cancelButton);
211  }
212 
213  @Override
214  public void setValue(@Nullable final Object newValue) {
215  super.setValue(newValue);
216  if (dialog != null && newValue != UNINITIALIZED_VALUE) {
217  dialog.dispose();
218  }
219  }
220 
226  protected abstract boolean createNew();
227 
232  protected void updateOkButton() {
233  okButton.setEnabled(isOkButtonEnabled());
234  }
235 
241  protected boolean isOkButtonEnabled() {
242  return true;
243  }
244 
250  protected void addDocumentListener(@NotNull final JTextComponent textComponent) {
251  textComponent.getDocument().addDocumentListener(documentListener);
252  textComponent.addFocusListener(new FocusListener() {
253 
254  @Override
255  public void focusGained(@NotNull final FocusEvent e) {
256  textComponent.selectAll();
257  }
258 
259  @Override
260  public void focusLost(@NotNull final FocusEvent e) {
261  textComponent.select(0, 0);
262  }
263 
264  });
265  textComponent.selectAll();
266  }
267 
268 }
Graphical User Interface of Gridarta.
final DocumentListener documentListener
The DocumentListener attached to input fields for detecting changes.
abstract boolean createNew()
Checks the given values and creates a new map.
static final ActionBuilder ACTION_BUILDER
Action Builder.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
void updateOkButton()
Updates the enabled state of the "OK" button depending on the dialog&#39;s contents.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
boolean isOkButtonEnabled()
Returns whether the "OK" button is enabled depending on the dialog&#39;s contents.
GameObjects are the objects based on Archetypes found on maps.
Interface used as preferences location.
Utility class for ActionBuilder related functions.
void addDocumentListener(@NotNull final JTextComponent textComponent)
Watches for text changes in a text component and enables the "OK" button accordingly.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
Dialog used to ask the user the properties for the new level.
static final long serialVersionUID
Serial Version UID.
abstract void addFields(@NotNull JPanel panel, @NotNull GridBagConstraints gbcLabel, @NotNull GridBagConstraints gbcField)
Adds additional fields to the dialog.
Defines common UI constants used in different dialogs.
void init1(@NotNull final Component parentComponent, @NotNull final String dialogTitle)
Initializes the dialog.