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-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.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();
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 
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  return folderName.length() <= 0 ? null : folderName;
177  }
178 
179 }
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Action Builder.
Definition: NewPickmapFolderDialog.java:58
net.sf.gridarta.gui.dialog.newmap.AbstractNewMapDialog< G, A, R >::init2
void init2()
Initializes the dialog.
Definition: AbstractNewMapDialog.java:148
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.mapfiles.MapFolderTree
Stores all known MapFiles.
Definition: MapFolderTree.java:42
net.sf
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.parent
final MapFolder< G, A, R > parent
The parent folder to add the pickmap folder to.
Definition: NewPickmapFolderDialog.java:70
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.gui.mapfiles.MapFolderTree.addMapFolder
synchronized void addMapFolder(@NotNull final MapFolder< G, A, R > mapFolder)
Adds a map folder to this model.
Definition: MapFolderTree.java:90
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.NewPickmapFolderDialog
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.
Definition: NewPickmapFolderDialog.java:92
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.getFolderName
String getFolderName()
Returns the current folder name value.
Definition: NewPickmapFolderDialog.java:174
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
Graphical User Interface of Gridarta.
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.serialVersionUID
static final long serialVersionUID
Serial Version UID.
Definition: NewPickmapFolderDialog.java:52
net.sf.gridarta.gui.mapfiles.MapFolder.getDir
File getDir()
Returns the base directory.
Definition: MapFolder.java:143
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.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.gui.dialog.newmap.AbstractNewMapDialog< G, A, R >::updateOkButton
void updateOkButton()
Updates the enabled state of the "OK" button depending on the dialog's contents.
Definition: AbstractNewMapDialog.java:232
net.sf.gridarta.gui.mapfiles.InvalidNameException
Indicates that a folder or pickmap name is invalid.
Definition: InvalidNameException.java:28
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.isOkButtonEnabled
boolean isOkButtonEnabled()
Definition: NewPickmapFolderDialog.java:165
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.addFields
void addFields(@NotNull final JPanel panel, @NotNull final GridBagConstraints gbcLabel, @NotNull final GridBagConstraints gbcField)
Definition: NewPickmapFolderDialog.java:118
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.mapfiles.MapFolder
Model class representing a folder of MapFiles.
Definition: MapFolder.java:47
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.createNew
boolean createNew()
Definition: NewPickmapFolderDialog.java:124
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog
Dialog to create a new pickmap folder.
Definition: NewPickmapFolderDialog.java:47
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.newmap.NewPickmapFolderDialog.mapFolderTree
final MapFolderTree< G, A, R > mapFolderTree
The model to add the pickmap folder to.
Definition: NewPickmapFolderDialog.java:64
net.sf.gridarta.gui.dialog.newmap.AbstractNewMapDialog< G, A, R >::init1
void init1( @NotNull final Component parentComponent, @NotNull final String dialogTitle)
Initializes the dialog.
Definition: AbstractNewMapDialog.java:131
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.folderNameField
final JTextComponent folderNameField
Textfield for the name of the map.
Definition: NewPickmapFolderDialog.java:83
net.sf.gridarta.gui.mapfiles.DuplicateMapFolderException
Indicates that a folder name is not unique.
Definition: DuplicateMapFolderException.java:28
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.gui.mapfiles.MapFolderTree.getBaseDir
File getBaseDir()
Returns the base directory for creating new map folders.
Definition: MapFolderTree.java:79
net.sf.gridarta.utils.ActionBuilderUtils
Utility class for ActionBuilder related functions.
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.mapViewsManager
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager.
Definition: NewPickmapFolderDialog.java:76
net.sf.gridarta.gui.mapfiles.MapFolderTree.setActiveMapFolder
synchronized void setActiveMapFolder(@NotNull final MapFolder< G, A, R > mapFolder)
Sets the active map folder.
Definition: MapFolderTree.java:157
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.createMapNamePanel
JPanel createMapNamePanel()
Definition: NewPickmapFolderDialog.java:104
net.sf.gridarta.gui.map.mapview.MapViewsManager
Stores all existing MapViews.
Definition: MapViewsManager.java:47
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.gui.dialog.newmap.NewPickmapFolderDialog.createMapParametersPanel
JPanel createMapParametersPanel()
Definition: NewPickmapFolderDialog.java:113
net.sf.gridarta.gui.mapfiles
Definition: DuplicateMapFolderException.java:20