From: <aki...@us...> - 2008-08-09 08:11:48
|
Revision: 4816 http://gridarta.svn.sourceforge.net/gridarta/?rev=4816&view=rev Author: akirschbaum Date: 2008-08-09 08:11:56 +0000 (Sat, 09 Aug 2008) Log Message: ----------- Extract FileControl from CMainControl. Modified Paths: -------------- trunk/crossfire/src/cfeditor/CMainControl.java trunk/daimonin/src/daieditor/CMainControl.java Added Paths: ----------- trunk/crossfire/src/cfeditor/FileControl.java trunk/daimonin/src/daieditor/FileControl.java Modified: trunk/crossfire/src/cfeditor/CMainControl.java =================================================================== --- trunk/crossfire/src/cfeditor/CMainControl.java 2008-08-08 22:40:40 UTC (rev 4815) +++ trunk/crossfire/src/cfeditor/CMainControl.java 2008-08-09 08:11:56 UTC (rev 4816) @@ -47,7 +47,6 @@ import java.util.Map; import java.util.MissingResourceException; import java.util.prefs.Preferences; -import javax.swing.JFileChooser; import javax.swing.JMenu; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; @@ -200,9 +199,6 @@ /** Actions used by this instance. */ private final MainActions<GameObject, MapArchObject, Archetype, CMapViewBasic> mainActions; - /** JFileChooser for opening a file. */ - private JFileChooser fileChooser; - /** The treasure lists. */ private final CFTreasureListTree<GameObject, MapArchObject, Archetype> treasureListTree; @@ -225,6 +221,12 @@ private final MapViewManager<GameObject, MapArchObject, Archetype, CMapViewBasic> mapViewManager; /** + * The file control instance. + */ + @NotNull + private final FileControl fileControl; + + /** * Constructs the main controller and its model and view. * @throws RuntimeException If the controller cannot be initialized. */ @@ -393,6 +395,7 @@ recentManager.initRecent(); validators = createMapValidators(); new AutoValidator<GameObject, MapArchObject, Archetype, CMapViewBasic>(this, mapManager, PREFS_VALIDATOR_AUTO_DEFAULT); + fileControl = new FileControl(globalSettings, archetypeSet, mapPreviewAccessory, mapManager, mainView, mapFileFilter, pythonFileFilter, newMapDialogFactory); } /** @@ -478,7 +481,7 @@ /** Edit an existing script. */ @ActionMethod public void editScript() { - openFileWanted(false); + fileControl.openFileWanted(false); } public void onlineHelp() { @@ -514,78 +517,10 @@ /** Invoked when user wants to open a file. */ public void open() { - openFileWanted(true); + fileControl.openFileWanted(true); } - /** Create the JFileChooser for opening a file. */ - private void createFileChooser() { - fileChooser = new JFileChooser(); - fileChooser.setDialogTitle(ACTION_FACTORY.getString("fileDialog.title")); - fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - fileChooser.setMultiSelectionEnabled(true); - fileChooser.addChoosableFileFilter(pythonFileFilter); - fileChooser.addChoosableFileFilter(mapFileFilter); - if (globalSettings.getMapDir().exists()) { - fileChooser.setCurrentDirectory(globalSettings.getMapDir()); - } - mapPreviewAccessory.attachTo(fileChooser); - } - /** - * The user wants to open a file. The filefilters and preselected filefilter - * are set accordingly to <var>mapFilter</var>. - * @param mapFilter set to <code>true</code> if the user probably wants to - * open a map, <code>false</code> otherwise - */ - public void openFileWanted(final boolean mapFilter) { - if (fileChooser == null) { - createFileChooser(); - } - if (mapFilter) { - fileChooser.setFileFilter(mapFileFilter); - } else { - fileChooser.setFileFilter(pythonFileFilter); - } - final int returnVal = fileChooser.showOpenDialog(mainView); - if (returnVal == JFileChooser.APPROVE_OPTION) { - if (archetypeSet.getLoadStatus() == ArchetypeSet.LoadStatus.EMPTY) { - // ArchStack is empty -> abort! - ACTION_FACTORY.showMessageDialog(mainView, "openFileWantedNoArches"); - return; - } - - globalSettings.setChangedDir(true); // user has chosen an active dir - openFiles(fileChooser.getCurrentDirectory(), fileChooser.getSelectedFiles()); - } - } - - /** - * Load an array of files. - * @param dir directory to load files from - * @param files array of files to load - */ - private void openFiles(final File dir, final File... files) { - for (final File file : files) { - final boolean isScriptFile = file.getName().toLowerCase().endsWith(".py"); - if (file.isFile()) { - if (isScriptFile) { - ScriptEditControlInstance.getInstance().openScriptFile(file.getAbsolutePath()); - } else { - globalSettings.setCurrentDir(dir); - mapManager.openMapFileWithView(file, null); - } - } else if (!file.exists()) { - if (isScriptFile) { - // TODO: pass filename - ScriptEditControlInstance.getInstance().openScriptNew(); - } else { - newLevelWanted(file.getName()); - } - } // If neither branch matches, it's a directory - what to do with directories? - } - } - - /** * Load a list of map files. * @param filenames collection of filenames to load */ @@ -612,42 +547,7 @@ /** {@inheritDoc} */ public boolean saveLevelAsWanted(@NotNull final MapControl<GameObject, MapArchObject, Archetype, CMapViewBasic> mapControl) { - final JFileChooser fileChooser = new JFileChooser(); - fileChooser.setDialogTitle("Save Map Or Script As"); - fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); - fileChooser.setMultiSelectionEnabled(false); - fileChooser.resetChoosableFileFilters(); - fileChooser.addChoosableFileFilter(pythonFileFilter); - fileChooser.setFileFilter(mapFileFilter); - - // default folder is the map-folder at first time, then the active folder - if (!globalSettings.hasChangedDir() && globalSettings.getMapDir().exists()) { - fileChooser.setCurrentDirectory(globalSettings.getMapDir()); - } else if (globalSettings.getCurrentDir().exists()) { - fileChooser.setCurrentDirectory(globalSettings.getCurrentDir()); - } - - // if file already exists, select it - final File mapFile = mapControl.getMapFile(); - if (mapFile != null && mapFile.exists()) { - fileChooser.setSelectedFile(mapFile); - } else { - fileChooser.setSelectedFile(new File(globalSettings.getMapDir(), mapControl.getMapFileName())); - } - - final int returnVal = fileChooser.showSaveDialog(mainView); - if (returnVal != JFileChooser.APPROVE_OPTION) { - return false; - } - - globalSettings.setChangedDir(true); // user has chosen an active dir - final File file = fileChooser.getSelectedFile(); - if (!file.exists() || ACTION_FACTORY.showConfirmDialog(mainView, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_CANCEL_OPTION, "overwriteOtherFile", file.getName()) == JOptionPane.OK_OPTION) { - mapControl.saveAs(file); - - globalSettings.setCurrentDir(fileChooser.getCurrentDirectory()); - } - return true; + return fileControl.saveLevelAsWanted(mapControl); } private PreferencesGroup prefsGroup; Added: trunk/crossfire/src/cfeditor/FileControl.java =================================================================== --- trunk/crossfire/src/cfeditor/FileControl.java (rev 0) +++ trunk/crossfire/src/cfeditor/FileControl.java 2008-08-09 08:11:56 UTC (rev 4816) @@ -0,0 +1,232 @@ +/* + * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. + * Copyright (C) 2000-2007 The Gridarta Developers. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package cfeditor; + +import cfeditor.gameobject.Archetype; +import cfeditor.gameobject.ArchetypeSet; +import cfeditor.gameobject.GameObject; +import cfeditor.gui.map.CMapViewBasic; +import cfeditor.map.MapArchObject; +import java.awt.Component; +import java.io.File; +import javax.swing.JFileChooser; +import javax.swing.JOptionPane; +import javax.swing.filechooser.FileFilter; +import net.sf.gridarta.GlobalSettings; +import net.sf.gridarta.MapManager; +import net.sf.gridarta.gui.map.MapPreviewAccessory; +import net.sf.gridarta.gui.newmap.NewMapDialogFactory; +import net.sf.gridarta.map.MapControl; +import net.sf.gridarta.textedit.scripteditor.ScriptEditControlInstance; +import net.sf.japi.swing.ActionFactory; +import org.jetbrains.annotations.NotNull; + +public class FileControl { + + /** Action Factory. */ + private static final ActionFactory ACTION_FACTORY = ActionFactory.getFactory("cfeditor"); + + /** + * The global settings instance. + */ + @NotNull + private final GlobalSettings globalSettings; + + /** + * The archetype set. + */ + @NotNull + private final ArchetypeSet archetypeSet; + + /** + * The map preview accessory. + */ + @NotNull + private final MapPreviewAccessory mapPreviewAccessory; + + /** + * The map manager. + */ + @NotNull + private final MapManager<GameObject, MapArchObject, Archetype, CMapViewBasic> mapManager; + + /** + * The parent component for showing dialog boxes. + */ + @NotNull + private final Component parent; + + /** + * The map file filter. + */ + @NotNull + private final FileFilter mapFileFilter; + + /** + * The script file filter. + */ + @NotNull + private final FileFilter scriptFileFilter; + + /** + * The new map dialog factory. + */ + @NotNull + private final NewMapDialogFactory<GameObject, MapArchObject, Archetype, CMapViewBasic> newMapDialogFactory; + + /** JFileChooser for opening a file. */ + private JFileChooser fileChooser; + + /** + * Creates a new instance. + * @param globalSettings the global settings instance + * @param archetypeSet the archetype set + * @param mapPreviewAccessory the map preview accessory + * @param mapManager the map manager + * @param parent the parent component for showing dialog boxes + * @param mapFileFilter the map file filter + * @param scriptFileFilter the script file filter + * @param newMapDialogFactory the new map dialog factory + */ + public FileControl(@NotNull final GlobalSettings globalSettings, @NotNull final ArchetypeSet archetypeSet, @NotNull final MapPreviewAccessory mapPreviewAccessory, @NotNull final MapManager<GameObject, MapArchObject, Archetype, CMapViewBasic> mapManager, @NotNull final Component parent, @NotNull final FileFilter mapFileFilter, @NotNull final FileFilter scriptFileFilter, @NotNull final NewMapDialogFactory<GameObject, MapArchObject, Archetype, CMapViewBasic> newMapDialogFactory) { + this.globalSettings = globalSettings; + this.archetypeSet = archetypeSet; + this.mapPreviewAccessory = mapPreviewAccessory; + this.mapManager = mapManager; + this.parent = parent; + this.mapFileFilter = mapFileFilter; + this.scriptFileFilter = scriptFileFilter; + this.newMapDialogFactory = newMapDialogFactory; + } + + /** Create the JFileChooser for opening a file. */ + private void createFileChooser() { + fileChooser = new JFileChooser(); + fileChooser.setDialogTitle(ACTION_FACTORY.getString("fileDialog.title")); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setMultiSelectionEnabled(true); + fileChooser.addChoosableFileFilter(scriptFileFilter); + fileChooser.addChoosableFileFilter(mapFileFilter); + if (globalSettings.getMapDir().exists()) { + fileChooser.setCurrentDirectory(globalSettings.getMapDir()); + } + mapPreviewAccessory.attachTo(fileChooser); + } + + /** + * The user wants to open a file. The filefilters and preselected filefilter + * are set accordingly to <var>mapFilter</var>. + * @param mapFilter set to <code>true</code> if the user probably wants to + * open a map, <code>false</code> otherwise + */ + public void openFileWanted(final boolean mapFilter) { + if (fileChooser == null) { + createFileChooser(); + } + if (mapFilter) { + fileChooser.setFileFilter(mapFileFilter); + } else { + fileChooser.setFileFilter(scriptFileFilter); + } + final int returnVal = fileChooser.showOpenDialog(parent); + if (returnVal == JFileChooser.APPROVE_OPTION) { + if (archetypeSet.getLoadStatus() == ArchetypeSet.LoadStatus.EMPTY) { + // ArchStack is empty -> abort! + ACTION_FACTORY.showMessageDialog(parent, "openFileWantedNoArches"); + return; + } + + globalSettings.setChangedDir(true); // user has chosen an active dir + openFiles(fileChooser.getCurrentDirectory(), fileChooser.getSelectedFiles()); + } + } + + /** + * Invoked when user wants to save a map to certain file. + * @param mapControl the map to be saved + * @return <code>true</code> if the user confirmed saving the map and the + * map was saved successfully, otherwise <code>false</code> + */ + public boolean saveLevelAsWanted(@NotNull final MapControl<GameObject, MapArchObject, Archetype, CMapViewBasic> mapControl) { + final JFileChooser fileChooser = new JFileChooser(); + fileChooser.setDialogTitle("Save Map Or Script As"); + fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); + fileChooser.setMultiSelectionEnabled(false); + fileChooser.resetChoosableFileFilters(); + fileChooser.addChoosableFileFilter(scriptFileFilter); + fileChooser.setFileFilter(mapFileFilter); + + // default folder is the map-folder at first time, then the active folder + if (!globalSettings.hasChangedDir() && globalSettings.getMapDir().exists()) { + fileChooser.setCurrentDirectory(globalSettings.getMapDir()); + } else if (globalSettings.getCurrentDir().exists()) { + fileChooser.setCurrentDirectory(globalSettings.getCurrentDir()); + } + + // if file already exists, select it + final File mapFile = mapControl.getMapFile(); + if (mapFile != null && mapFile.exists()) { + fileChooser.setSelectedFile(mapFile); + } else { + fileChooser.setSelectedFile(new File(globalSettings.getMapDir(), mapControl.getMapFileName())); + } + + final int returnVal = fileChooser.showSaveDialog(parent); + if (returnVal != JFileChooser.APPROVE_OPTION) { + return false; + } + + globalSettings.setChangedDir(true); // user has chosen an active dir + final File file = fileChooser.getSelectedFile(); + if (!file.exists() || ACTION_FACTORY.showConfirmDialog(parent, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_CANCEL_OPTION, "overwriteOtherFile", file.getName()) == JOptionPane.OK_OPTION) { + mapControl.saveAs(file); + + globalSettings.setCurrentDir(fileChooser.getCurrentDirectory()); + } + return true; + } + + /** + * Load an array of files. + * @param dir directory to load files from + * @param files array of files to load + */ + private void openFiles(final File dir, final File... files) { + for (final File file : files) { + final boolean isScriptFile = file.getName().toLowerCase().endsWith(".py"); + if (file.isFile()) { + if (isScriptFile) { + ScriptEditControlInstance.getInstance().openScriptFile(file.getAbsolutePath()); + } else { + globalSettings.setCurrentDir(dir); + mapManager.openMapFileWithView(file, null); + } + } else if (!file.exists()) { + if (isScriptFile) { + // TODO: pass filename + ScriptEditControlInstance.getInstance().openScriptNew(); + } else { + newMapDialogFactory.showNewMapDialog(file.getName()); + } + } // If neither branch matches, it's a directory - what to do with directories? + } + } + +} // class FileControl Property changes on: trunk/crossfire/src/cfeditor/FileControl.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF Modified: trunk/daimonin/src/daieditor/CMainControl.java =================================================================== --- trunk/daimonin/src/daieditor/CMainControl.java 2008-08-08 22:40:40 UTC (rev 4815) +++ trunk/daimonin/src/daieditor/CMainControl.java 2008-08-09 08:11:56 UTC (rev 4816) @@ -52,7 +52,6 @@ import java.util.Map; import java.util.MissingResourceException; import java.util.prefs.Preferences; -import javax.swing.JFileChooser; import javax.swing.JMenu; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; @@ -260,9 +259,6 @@ /** Actions used by this instance. */ private final MainActions<GameObject, MapArchObject, Archetype, CMapViewBasic> mainActions; - /** JFileChooser for opening a file. */ - private JFileChooser fileChooser; - /** The treasure lists. */ private final CFTreasureListTree<GameObject, MapArchObject, Archetype> treasureListTree; @@ -285,6 +281,12 @@ private final MapViewManager<GameObject, MapArchObject, Archetype, CMapViewBasic> mapViewManager; /** + * The file control instance. + */ + @NotNull + private final FileControl fileControl; + + /** * Constructs the main controller and its model and view. * @throws RuntimeException If the controller cannot be initialized. */ @@ -471,6 +473,7 @@ recentManager.initRecent(); validators = createMapValidators(); new AutoValidator<GameObject, MapArchObject, Archetype, CMapViewBasic>(this, mapManager, PREFS_VALIDATOR_AUTO_DEFAULT); + fileControl = new FileControl(globalSettings, archetypeSet, mapPreviewAccessory, mapManager, mainView, mapFileFilter, luaFileFilter, newMapDialogFactory); } /** @@ -621,7 +624,7 @@ /** Edit an existing script. */ @ActionMethod public void editScript() { - openFileWanted(false); + fileControl.openFileWanted(false); } /** @@ -699,78 +702,10 @@ /** Invoked when user wants to open a file. */ public void open() { - openFileWanted(true); + fileControl.openFileWanted(true); } - /** Create the JFileChooser for opening a file. */ - private void createFileChooser() { - fileChooser = new JFileChooser(); - fileChooser.setDialogTitle(ACTION_FACTORY.getString("fileDialog.title")); - fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - fileChooser.setMultiSelectionEnabled(true); - fileChooser.addChoosableFileFilter(luaFileFilter); - fileChooser.addChoosableFileFilter(mapFileFilter); - if (globalSettings.getMapDir().exists()) { - fileChooser.setCurrentDirectory(globalSettings.getMapDir()); - } - mapPreviewAccessory.attachTo(fileChooser); - } - /** - * The user wants to open a file. The filefilters and preselected filefilter - * are set accordingly to <var>mapFilter</var>. - * @param mapFilter set to <code>true</code> if the user probably wants to - * open a map, <code>false</code> otherwise - */ - public void openFileWanted(final boolean mapFilter) { - if (fileChooser == null) { - createFileChooser(); - } - if (mapFilter) { - fileChooser.setFileFilter(mapFileFilter); - } else { - fileChooser.setFileFilter(luaFileFilter); - } - final int returnVal = fileChooser.showOpenDialog(mainView); - if (returnVal == JFileChooser.APPROVE_OPTION) { - if (archetypeSet.getLoadStatus() == ArchetypeSet.LoadStatus.EMPTY) { - // ArchStack is empty -> abort! - ACTION_FACTORY.showMessageDialog(mainView, "openFileWantedNoArches"); - return; - } - - globalSettings.setChangedDir(true); // user has chosen an active dir - openFiles(fileChooser.getCurrentDirectory(), fileChooser.getSelectedFiles()); - } - } - - /** - * Load an array of files. - * @param dir directory to load files from - * @param files array of files to load - */ - private void openFiles(final File dir, final File... files) { - for (final File file : files) { - final boolean isScriptFile = file.getName().toLowerCase().endsWith(".lua"); - if (file.isFile()) { - if (isScriptFile) { - ScriptEditControlInstance.getInstance().openScriptFile(file.getAbsolutePath()); - } else { - globalSettings.setCurrentDir(dir); - mapManager.openMapFileWithView(file, null); - } - } else if (!file.exists()) { - if (isScriptFile) { - // TODO: pass filename - ScriptEditControlInstance.getInstance().openScriptNew(); - } else { - newLevelWanted(file.getName()); - } - } // If neither branch matches, it's a directory - what to do with directories? - } - } - - /** * Load a list of map files. * @param filenames collection of filenames to load */ @@ -797,42 +732,7 @@ /** {@inheritDoc} */ public boolean saveLevelAsWanted(@NotNull final MapControl<GameObject, MapArchObject, Archetype, CMapViewBasic> mapControl) { - final JFileChooser fileChooser = new JFileChooser(); - fileChooser.setDialogTitle("Save Map Or Script As"); - fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); - fileChooser.setMultiSelectionEnabled(false); - fileChooser.resetChoosableFileFilters(); - fileChooser.addChoosableFileFilter(luaFileFilter); - fileChooser.setFileFilter(mapFileFilter); - - // default folder is the map-folder at first time, then the active folder - if (!globalSettings.hasChangedDir() && globalSettings.getMapDir().exists()) { - fileChooser.setCurrentDirectory(globalSettings.getMapDir()); - } else if (globalSettings.getCurrentDir().exists()) { - fileChooser.setCurrentDirectory(globalSettings.getCurrentDir()); - } - - // if file already exists, select it - final File mapFile = mapControl.getMapFile(); - if (mapFile != null && mapFile.exists()) { - fileChooser.setSelectedFile(mapFile); - } else { - fileChooser.setSelectedFile(new File(globalSettings.getMapDir(), mapControl.getMapFileName())); - } - - final int returnVal = fileChooser.showSaveDialog(mainView); - if (returnVal != JFileChooser.APPROVE_OPTION) { - return false; - } - - globalSettings.setChangedDir(true); // user has chosen an active dir - final File file = fileChooser.getSelectedFile(); - if (!file.exists() || ACTION_FACTORY.showConfirmDialog(mainView, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_CANCEL_OPTION, "overwriteOtherFile", file.getName()) == JOptionPane.OK_OPTION) { - mapControl.saveAs(file); - - globalSettings.setCurrentDir(fileChooser.getCurrentDirectory()); - } - return true; + return fileControl.saveLevelAsWanted(mapControl); } private PreferencesGroup prefsGroup; Added: trunk/daimonin/src/daieditor/FileControl.java =================================================================== --- trunk/daimonin/src/daieditor/FileControl.java (rev 0) +++ trunk/daimonin/src/daieditor/FileControl.java 2008-08-09 08:11:56 UTC (rev 4816) @@ -0,0 +1,232 @@ +/* + * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. + * Copyright (C) 2000-2007 The Gridarta Developers. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package daieditor; + +import daieditor.gameobject.Archetype; +import daieditor.gameobject.ArchetypeSet; +import daieditor.gameobject.GameObject; +import daieditor.gui.map.CMapViewBasic; +import daieditor.map.MapArchObject; +import java.awt.Component; +import java.io.File; +import javax.swing.JFileChooser; +import javax.swing.JOptionPane; +import javax.swing.filechooser.FileFilter; +import net.sf.gridarta.GlobalSettings; +import net.sf.gridarta.MapManager; +import net.sf.gridarta.gui.map.MapPreviewAccessory; +import net.sf.gridarta.gui.newmap.NewMapDialogFactory; +import net.sf.gridarta.map.MapControl; +import net.sf.gridarta.textedit.scripteditor.ScriptEditControlInstance; +import net.sf.japi.swing.ActionFactory; +import org.jetbrains.annotations.NotNull; + +public class FileControl { + + /** Action Factory. */ + private static final ActionFactory ACTION_FACTORY = ActionFactory.getFactory("cfeditor"); + + /** + * The global settings instance. + */ + @NotNull + private final GlobalSettings globalSettings; + + /** + * The archetype set. + */ + @NotNull + private final ArchetypeSet archetypeSet; + + /** + * The map preview accessory. + */ + @NotNull + private final MapPreviewAccessory mapPreviewAccessory; + + /** + * The map manager. + */ + @NotNull + private final MapManager<GameObject, MapArchObject, Archetype, CMapViewBasic> mapManager; + + /** + * The parent component for showing dialog boxes. + */ + @NotNull + private final Component parent; + + /** + * The map file filter. + */ + @NotNull + private final FileFilter mapFileFilter; + + /** + * The script file filter. + */ + @NotNull + private final FileFilter scriptFileFilter; + + /** + * The new map dialog factory. + */ + @NotNull + private final NewMapDialogFactory<GameObject, MapArchObject, Archetype, CMapViewBasic> newMapDialogFactory; + + /** JFileChooser for opening a file. */ + private JFileChooser fileChooser; + + /** + * Creates a new instance. + * @param globalSettings the global settings instance + * @param archetypeSet the archetype set + * @param mapPreviewAccessory the map preview accessory + * @param mapManager the map manager + * @param parent the parent component for showing dialog boxes + * @param mapFileFilter the map file filter + * @param scriptFileFilter the script file filter + * @param newMapDialogFactory the new map dialog factory + */ + public FileControl(@NotNull final GlobalSettings globalSettings, @NotNull final ArchetypeSet archetypeSet, @NotNull final MapPreviewAccessory mapPreviewAccessory, @NotNull final MapManager<GameObject, MapArchObject, Archetype, CMapViewBasic> mapManager, @NotNull final Component parent, @NotNull final FileFilter mapFileFilter, @NotNull final FileFilter scriptFileFilter, @NotNull final NewMapDialogFactory<GameObject, MapArchObject, Archetype, CMapViewBasic> newMapDialogFactory) { + this.globalSettings = globalSettings; + this.archetypeSet = archetypeSet; + this.mapPreviewAccessory = mapPreviewAccessory; + this.mapManager = mapManager; + this.parent = parent; + this.mapFileFilter = mapFileFilter; + this.scriptFileFilter = scriptFileFilter; + this.newMapDialogFactory = newMapDialogFactory; + } + + /** Create the JFileChooser for opening a file. */ + private void createFileChooser() { + fileChooser = new JFileChooser(); + fileChooser.setDialogTitle(ACTION_FACTORY.getString("fileDialog.title")); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setMultiSelectionEnabled(true); + fileChooser.addChoosableFileFilter(scriptFileFilter); + fileChooser.addChoosableFileFilter(mapFileFilter); + if (globalSettings.getMapDir().exists()) { + fileChooser.setCurrentDirectory(globalSettings.getMapDir()); + } + mapPreviewAccessory.attachTo(fileChooser); + } + + /** + * The user wants to open a file. The filefilters and preselected filefilter + * are set accordingly to <var>mapFilter</var>. + * @param mapFilter set to <code>true</code> if the user probably wants to + * open a map, <code>false</code> otherwise + */ + public void openFileWanted(final boolean mapFilter) { + if (fileChooser == null) { + createFileChooser(); + } + if (mapFilter) { + fileChooser.setFileFilter(mapFileFilter); + } else { + fileChooser.setFileFilter(scriptFileFilter); + } + final int returnVal = fileChooser.showOpenDialog(parent); + if (returnVal == JFileChooser.APPROVE_OPTION) { + if (archetypeSet.getLoadStatus() == ArchetypeSet.LoadStatus.EMPTY) { + // ArchStack is empty -> abort! + ACTION_FACTORY.showMessageDialog(parent, "openFileWantedNoArches"); + return; + } + + globalSettings.setChangedDir(true); // user has chosen an active dir + openFiles(fileChooser.getCurrentDirectory(), fileChooser.getSelectedFiles()); + } + } + + /** + * Invoked when user wants to save a map to certain file. + * @param mapControl the map to be saved + * @return <code>true</code> if the user confirmed saving the map and the + * map was saved successfully, otherwise <code>false</code> + */ + public boolean saveLevelAsWanted(@NotNull final MapControl<GameObject, MapArchObject, Archetype, CMapViewBasic> mapControl) { + final JFileChooser fileChooser = new JFileChooser(); + fileChooser.setDialogTitle("Save Map Or Script As"); + fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); + fileChooser.setMultiSelectionEnabled(false); + fileChooser.resetChoosableFileFilters(); + fileChooser.addChoosableFileFilter(scriptFileFilter); + fileChooser.setFileFilter(mapFileFilter); + + // default folder is the map-folder at first time, then the active folder + if (!globalSettings.hasChangedDir() && globalSettings.getMapDir().exists()) { + fileChooser.setCurrentDirectory(globalSettings.getMapDir()); + } else if (globalSettings.getCurrentDir().exists()) { + fileChooser.setCurrentDirectory(globalSettings.getCurrentDir()); + } + + // if file already exists, select it + final File mapFile = mapControl.getMapFile(); + if (mapFile != null && mapFile.exists()) { + fileChooser.setSelectedFile(mapFile); + } else { + fileChooser.setSelectedFile(new File(globalSettings.getMapDir(), mapControl.getMapFileName())); + } + + final int returnVal = fileChooser.showSaveDialog(parent); + if (returnVal != JFileChooser.APPROVE_OPTION) { + return false; + } + + globalSettings.setChangedDir(true); // user has chosen an active dir + final File file = fileChooser.getSelectedFile(); + if (!file.exists() || ACTION_FACTORY.showConfirmDialog(parent, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_CANCEL_OPTION, "overwriteOtherFile", file.getName()) == JOptionPane.OK_OPTION) { + mapControl.saveAs(file); + + globalSettings.setCurrentDir(fileChooser.getCurrentDirectory()); + } + return true; + } + + /** + * Load an array of files. + * @param dir directory to load files from + * @param files array of files to load + */ + private void openFiles(final File dir, final File... files) { + for (final File file : files) { + final boolean isScriptFile = file.getName().toLowerCase().endsWith(".lua"); + if (file.isFile()) { + if (isScriptFile) { + ScriptEditControlInstance.getInstance().openScriptFile(file.getAbsolutePath()); + } else { + globalSettings.setCurrentDir(dir); + mapManager.openMapFileWithView(file, null); + } + } else if (!file.exists()) { + if (isScriptFile) { + // TODO: pass filename + ScriptEditControlInstance.getInstance().openScriptNew(); + } else { + newMapDialogFactory.showNewMapDialog(file.getName()); + } + } // If neither branch matches, it's a directory - what to do with directories? + } + } + +} // class FileControl Property changes on: trunk/daimonin/src/daieditor/FileControl.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |