20 package net.sf.gridarta.gui.misc;
22 import java.awt.Component;
24 import java.io.IOException;
25 import javax.swing.JFileChooser;
26 import javax.swing.JOptionPane;
27 import javax.swing.filechooser.FileFilter;
46 import net.
sf.japi.swing.action.ActionBuilder;
47 import net.
sf.japi.swing.action.ActionBuilderFactory;
48 import org.jetbrains.annotations.NotNull;
49 import org.jetbrains.annotations.Nullable;
61 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
157 public DefaultFileControl(@NotNull
final ProjectSettings projectSettings, @NotNull
final VolatileSettings volatileSettings, @NotNull
final MapImageCache<G, A, R> mapImageCache, @NotNull
final MapManager<G, A, R> mapManager, @NotNull
final MapViewsManager<G, A, R> mapViewsManager, @NotNull
final Component parent, @NotNull
final FileFilter mapFileFilter, @NotNull
final FileFilter scriptFileFilter, @NotNull
final NewMapDialogFactory<G, A, R> newMapDialogFactory, @NotNull
final String scriptExtension, @NotNull
final ScriptEditControl scriptEditControl, @NotNull
final PathManager pathManager) {
178 final JFileChooser tmpFileChooser =
new JFileChooser();
180 tmpFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
181 tmpFileChooser.setMultiSelectionEnabled(
true);
182 tmpFileChooser.addChoosableFileFilter(scriptFileFilter);
183 tmpFileChooser.addChoosableFileFilter(mapFileFilter);
188 return tmpFileChooser;
193 if (fileChooser == null) {
198 tmpFileChooser.setFileFilter(mapFileFilter);
200 tmpFileChooser.setFileFilter(scriptFileFilter);
203 final int returnVal = tmpFileChooser.showOpenDialog(parent);
204 if (returnVal != JFileChooser.APPROVE_OPTION) {
208 final File[] files = tmpFileChooser.getSelectedFiles();
209 for (
final File file : files) {
210 final boolean isScriptFile = file.getName().toLowerCase().endsWith(scriptExtension);
217 }
catch (
final IOException ex) {
221 }
else if (!file.exists()) {
233 final MapFile mapFile = mapControl.getMapModel().getMapFile();
234 if (mapFile != null) {
237 }
catch (
final IOException ex) {
244 return saveAs(mapControl);
250 if (mapControl.getMapModel().isModified() && !
save(mapControl)) {
260 if (mapControl == null) {
282 mapControl.saveAs(pathManager.
getMapFile(file));
283 }
catch (
final IOException ex) {
298 final JFileChooser saveFileChooser =
new JFileChooser();
299 saveFileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
300 saveFileChooser.setDialogTitle(
"Save Map Or Script As");
301 saveFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
302 saveFileChooser.setMultiSelectionEnabled(
false);
303 saveFileChooser.resetChoosableFileFilters();
304 saveFileChooser.addChoosableFileFilter(scriptFileFilter);
305 saveFileChooser.setFileFilter(mapFileFilter);
307 final MapFile mapFile = mapControl.getMapModel().getMapFile();
308 final File defaultDir = mapFile == null ? null : mapFile.
getFile().getParentFile();
309 if (defaultDir != null && defaultDir.exists()) {
311 saveFileChooser.setSelectedFile(mapFile.
getFile());
318 final int returnVal = saveFileChooser.showSaveDialog(parent);
319 if (returnVal != JFileChooser.APPROVE_OPTION) {
324 final File file = saveFileChooser.getSelectedFile();
325 if (file.exists() && ACTION_BUILDER.showConfirmDialog(parent, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_CANCEL_OPTION,
"overwriteOtherFile", file.getName()) != JOptionPane.OK_OPTION) {
334 if (mapControl.getMapModel().isModified()) {
335 final int result = ACTION_BUILDER.showConfirmDialog(parent, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE,
"confirmSaveChanges", mapControl.getMapModel().getMapArchObject().getMapName());
336 if (result == JOptionPane.YES_OPTION) {
337 if (!
save(mapControl)) {
340 }
else if (result == JOptionPane.CANCEL_OPTION || result == JOptionPane.CLOSED_OPTION) {
350 final MapFile mapFile = mapControl.getMapModel().getMapFile();
351 assert mapFile != null;
356 public void reportSaveError(@NotNull
final File mapFile, @NotNull
final String message) {
357 ACTION_BUILDER.showMessageDialog(parent,
"encodeMapFile", mapFile, message);
361 public void reportLoadError(@Nullable
final File file, @NotNull
final String message) {
362 ACTION_BUILDER.showMessageDialog(parent,
"openFileLoadMap", file == null ?
"<unknown>" : file, message);
366 public void reportOutOfMapBoundsDeleted(@NotNull
final File file,
final int outOfMapBoundsDeleted, @NotNull
final StringBuilder outOfMapBoundsObjects) {
367 ACTION_BUILDER.showMessageDialog(parent,
"openFileOutOfMapBoundsDeleted", file, outOfMapBoundsDeleted, outOfMapBoundsObjects);
372 ACTION_BUILDER.showMessageDialog(parent,
"mapOutOfMemory", file);
377 ACTION_BUILDER.showMessageDialog(parent,
"teleportCharacterError", mapPath, message);
final FileFilter mapFileFilter
The FileFilter for selecting map files.
void reportSaveError(@NotNull final MapControl< G, A, R > mapControl, @NotNull final String message)
Reports an error while saving a map file to the user.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
A MapManager manages all opened maps.
void reportLoadError(@Nullable final File file, @NotNull final String message)
Graphical User Interface of Gridarta.
Reading and writing of maps, handling of paths.
Settings that apply to a project.
void reportTeleportCharacterError(@NotNull final String mapPath, @NotNull final String message)
Reports an error while teleporting a character to the current map.
Utility class for JFileChooser related functions.
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
void reportSaveError(@NotNull final File mapFile, @NotNull final String message)
Reports an error while saving a map file to the user.
final MapManager< G, A, R > mapManager
The MapManager.
File saveMapAs(@NotNull final MapControl< G, A, R > mapControl)
Displays a "save as" dialog.
void setCurrentSaveMapDirectory(@NotNull File currentSaveMapDirectory)
Sets the default directory for saving maps.
final MapImageCache< G, A, R > mapImageCache
The MapImageCache.
Stores all existing MapViews.
final Component parent
The parent component for showing dialog boxes.
void showNewMapDialog()
Shows a dialog for creating a new map.
Class with constants used in Gridarta and derivates.
Default FileControl implementation.
JFileChooser createFileChooser()
Creates the JFileChooser for opening a file.
File getCurrentSaveMapDirectory()
Returns the default directory for saving maps.
void openScriptFile(@NotNull final String pathName)
Open a new empty script document.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager.
void reportOutOfMemory(@NotNull final File file)
Base package of all Gridarta classes.
static void setCurrentDirectory(@NotNull final JFileChooser fileChooser, @Nullable final File dir)
Calls JFileChooser#setCurrentDirectory(File).
This package contains the classes for the script editor used within the editor to create and modify P...
Reflects a game object (object on a map).
MapView< G, A, R > openMapFileWithView(@NotNull final MapFile mapFile, @Nullable final Point viewPosition, @Nullable final Point centerSquare)
Load a map file and create a map view.
final NewMapDialogFactory< G, A, R > newMapDialogFactory
The NewMapDialogFactory.
static final String DEFAULT_MAP_FILENAME
Default file name for new maps.
void reportOutOfMapBoundsDeleted(@NotNull final File file, final int outOfMapBoundsDeleted, @NotNull final StringBuilder outOfMapBoundsObjects)
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
GameObjects are the objects based on Archetypes found on maps.
Caches icon and preview images for map files.
void openFile(final boolean mapFile)
The user wants to edit a file.
DefaultFileControl(@NotNull final ProjectSettings projectSettings, @NotNull final VolatileSettings volatileSettings, @NotNull final MapImageCache< G, A, R > mapImageCache, @NotNull final MapManager< G, A, R > mapManager, @NotNull final MapViewsManager< G, A, R > mapViewsManager, @NotNull final Component parent, @NotNull final FileFilter mapFileFilter, @NotNull final FileFilter scriptFileFilter, @NotNull final NewMapDialogFactory< G, A, R > newMapDialogFactory, @NotNull final String scriptExtension, @NotNull final ScriptEditControl scriptEditControl, @NotNull final PathManager pathManager)
Creates a new instance.
final String scriptExtension
The file extension for script files.
Base classes for rendering maps.
JFileChooser fileChooser
The JFileChooser for opening a file.
final VolatileSettings volatileSettings
The VolatileSettings.
ScriptEditControl - Manages events and data flow for the script editor entity.
Utility class to create NewMapDialog instances.
Utility class for ActionBuilder related functions.
void saveAllMaps()
Saves all maps.
final FileFilter scriptFileFilter
The FileFilter for selecting script files.
Currently nothing more than a marker interface for unification.
final ProjectSettings projectSettings
The ProjectSettings.
void closeMap(@NotNull MapControl< G, A, R > mapControl)
Closes the given map, ignoring modified status.
boolean confirmSaveChanges(@NotNull final MapControl< G, A, R > mapControl)
Asks the user whether to save changes of a map.
File getFile()
Returns a File for this map file.
boolean save(@NotNull final MapControl< G, A, R > mapControl)
Save one map.
Abstract base class for MapPreviewAccessories which are used for previewing maps in JFileChoosers...
MapControl< G, A, R > getOpenMap()
Returns one open map.
static void sanitizeCurrentDirectory(@NotNull final JFileChooser fileChooser)
Makes sure the current directory of a JFileChooser is valid.
File getMapsDirectory()
Returns the default maps directory.
final ScriptEditControl scriptEditControl
The ScriptEditControl to forward to.
void newScript()
Open a new empty script document.
boolean closeAllMaps()
Invoked when the user wants to close all maps.
boolean saveAs(@NotNull final MapControl< G, A, R > mapControl)
Asks the user for a filename, then saves the map.
List< MapControl< G, A, R > > getOpenedMaps()
Returns all opened maps.
The location of a map file with a map directory.
Interface for MapArchObjects.
final PathManager pathManager
The PathManager for creating MapFile instances.
Settings that are not retained across editor restarts.