Gridarta Editor
NewPickmapFolderDialog.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 java.io.File;
26 import javax.swing.JPanel;
27 import javax.swing.JTextField;
28 import javax.swing.text.JTextComponent;
38 import net.sf.japi.swing.action.ActionBuilder;
39 import net.sf.japi.swing.action.ActionBuilderFactory;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
42 
47 public class NewPickmapFolderDialog<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractNewMapDialog<G, A, R> {
48 
52  private static final long serialVersionUID = 1L;
53 
57  @NotNull
58  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
59 
63  @NotNull
65 
69  @Nullable
70  private final MapFolder<G, A, R> parent;
71 
75  @NotNull
77 
82  @NotNull
83  private final JTextComponent folderNameField = new JTextField(16);
84 
92  public NewPickmapFolderDialog(@NotNull final Component parentComponent, @NotNull final MapFolderTree<G, A, R> mapFolderTree, @Nullable final MapFolder<G, A, R> parent, @NotNull final MapViewsManager<G, A, R> mapViewsManager) {
93  this.mapFolderTree = mapFolderTree;
94  this.parent = parent;
95  this.mapViewsManager = mapViewsManager;
96  init1(parentComponent, ActionBuilderUtils.getString(ACTION_BUILDER, "newPickmapFolder.title"));
97  init2();
98  addDocumentListener(folderNameField);
100  }
101 
102  @NotNull
103  @Override
104  protected JPanel createMapNamePanel() {
105  final JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
106  panel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "newMapPickmapFolderName"));
107  panel.add(folderNameField);
108  return panel;
109  }
110 
111  @Nullable
112  @Override
113  protected JPanel createMapParametersPanel() {
114  return null;
115  }
116 
117  @Override
118  protected void addFields(@NotNull final JPanel panel, @NotNull final GridBagConstraints gbcLabel, @NotNull final GridBagConstraints gbcField) {
119  throw new AssertionError();
120 
121  }
122 
123  @Override
124  protected boolean createNew() {
125  final String folderName = getFolderName();
126  if (folderName == null) {
127  return false;
128  }
129 
130  final MapFolder<G, A, R> mapFolder;
131  try {
132  mapFolder = new MapFolder<>(parent, folderName, mapFolderTree.getBaseDir(), mapViewsManager);
133  } catch (final InvalidNameException ex) {
134  ACTION_BUILDER.showMessageDialog(this, "newPickmapFolderInvalidName", folderName);
135  folderNameField.requestFocus();
136  return false;
137  }
138 
139  final File folderDir = mapFolder.getDir();
140  if (folderDir.exists()) {
141  ACTION_BUILDER.showMessageDialog(this, "newPickmapFolderExists", folderName);
142  folderNameField.requestFocus();
143  return false;
144  }
145  if (!folderDir.mkdir()) {
146  ACTION_BUILDER.showMessageDialog(this, "mkdirIOError", folderDir);
147  folderNameField.requestFocus();
148  return false;
149  }
150 
151  try {
152  mapFolderTree.addMapFolder(mapFolder);
153  } catch (final DuplicateMapFolderException ex) {
154  ACTION_BUILDER.showMessageDialog(this, "newPickmapFolderExists", folderName);
155  folderNameField.requestFocus();
156  return false;
157  }
158 
159  mapFolderTree.setActiveMapFolder(mapFolder);
160 
161  return true;
162  }
163 
164  @Override
165  protected boolean isOkButtonEnabled() {
166  return super.isOkButtonEnabled() && getFolderName() != null;
167  }
168 
173  @Nullable
174  private String getFolderName() {
175  final String folderName = folderNameField.getText();
176  if (folderName.length() <= 0) {
177  return null;
178  }
179 
180  return folderName;
181  }
182 
183 }
File getBaseDir()
Returns the base directory for creating new map folders.
Graphical User Interface of Gridarta.
final MapFolder< G, A, R > parent
The parent folder to add the pickmap folder to.
final JTextComponent folderNameField
Textfield for the name of the map.
final MapFolderTree< G, A, R > mapFolderTree
The model to add the pickmap folder to.
File getDir()
Returns the base directory.
Definition: MapFolder.java:143
static final ActionBuilder ACTION_BUILDER
Action Builder.
void addFields(@NotNull final JPanel panel, @NotNull final GridBagConstraints gbcLabel, @NotNull final GridBagConstraints gbcField)
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.
NewPickmapFolderDialog(@NotNull final Component parentComponent, @NotNull final MapFolderTree< G, A, R > mapFolderTree, @Nullable final MapFolder< G, A, R > parent, @NotNull final MapViewsManager< G, A, R > mapViewsManager)
Creates a "new pickmap folder" dialog.
synchronized void addMapFolder(@NotNull final MapFolder< G, A, R > mapFolder)
Adds a map folder to this model.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
GameObjects are the objects based on Archetypes found on maps.
Base classes for rendering maps.
Utility class for ActionBuilder related functions.
synchronized void setActiveMapFolder(@NotNull final MapFolder< G, A, R > mapFolder)
Sets the active map folder.
void addDocumentListener( @NotNull final JTextComponent textComponent)
Watches for text changes in a text component and enables the "OK" button accordingly.
Dialog used to ask the user the properties for the new level.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager.
String getFolderName()
Returns the current folder name value.
Indicates that a folder or pickmap name is invalid.
Model class representing a folder of MapFiles.
Definition: MapFolder.java:47
void init1( @NotNull final Component parentComponent, @NotNull final String dialogTitle)
Initializes the dialog.