[tiled] r613 - in trunk: . src/tiled/core src/tiled/io src/tiled/io/xml src/tiled/mapeditor src/tiled/mapeditor/actions src/tiled/mapeditor/resources src/tiled/mapeditor/util
svn@biggeruniverse.com
svn at biggeruniverse.com
Sun Apr 23 14:38:06 PDT 2006
Author: bjorn
Date: 2006-04-23 16:38:06 -0500 (Sun, 23 Apr 2006)
New Revision: 613
Added:
trunk/src/tiled/mapeditor/actions/AbstractFileAction.java
trunk/src/tiled/mapeditor/actions/CloseMapAction.java
trunk/src/tiled/mapeditor/actions/ExitAction.java
trunk/src/tiled/mapeditor/actions/NewMapAction.java
trunk/src/tiled/mapeditor/actions/OpenMapAction.java
trunk/src/tiled/mapeditor/actions/SaveAction.java
trunk/src/tiled/mapeditor/actions/SaveAsAction.java
Modified:
trunk/CHANGES
trunk/src/tiled/core/TileSet.java
trunk/src/tiled/io/MapHelper.java
trunk/src/tiled/io/xml/XMLMapWriter.java
trunk/src/tiled/mapeditor/MapEditor.java
trunk/src/tiled/mapeditor/actions/AbstractLayerAction.java
trunk/src/tiled/mapeditor/actions/AddLayerAction.java
trunk/src/tiled/mapeditor/actions/CloneLayerAction.java
trunk/src/tiled/mapeditor/actions/DeleteLayerAction.java
trunk/src/tiled/mapeditor/actions/MergeAllLayersAction.java
trunk/src/tiled/mapeditor/actions/MergeLayerDownAction.java
trunk/src/tiled/mapeditor/actions/MoveLayerDownAction.java
trunk/src/tiled/mapeditor/actions/MoveLayerUpAction.java
trunk/src/tiled/mapeditor/resources/gui.properties
trunk/src/tiled/mapeditor/util/TiledFileFilter.java
Log:
Introduced the AbstractFileAction class for any action that requires the current map to be checked for changes to save. Closing a map, exiting Tiled, starting a new map and opening a map are now implemented in subclasses of this action. The huge saving method has been splitted up in SaveAsAction, SaveAction and a ConfirmingFileChooser.
Modified: trunk/CHANGES
===================================================================
--- trunk/CHANGES 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/CHANGES 2006-04-23 21:38:06 UTC (rev 613)
@@ -31,6 +31,7 @@
* Fixed bug of adding a new tile even if cancelling out of new tile dialog
* Fixed NPE when saving a map with no extension - default to .tmx
* Fixed the zooming so that it keeps the center in place
+* Fixed exiting even when a request to save the changes failed
* Rewrote configuration based on the Preferences class, no more tiled.conf
* Cleaned up TileSet (removed checksumming and rotation/orienation code)
Modified: trunk/src/tiled/core/TileSet.java
===================================================================
--- trunk/src/tiled/core/TileSet.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/core/TileSet.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -368,12 +368,14 @@
* tileset doesn't reference a tile bitmap
*/
public String getTilebmpFile() {
- try {
- return tilebmpFile.getCanonicalPath();
- } catch (IOException e) {
- }
+ if (tilebmpFile != null) {
+ try {
+ return tilebmpFile.getCanonicalPath();
+ } catch (IOException e) {
+ }
+ }
- return "";
+ return null;
}
/**
@@ -538,7 +540,8 @@
while (itr.hasNext()) {
Tile t = (Tile)itr.next();
- if (t.getImageId() == id) {
+ // todo: move the null check back into the iterator?
+ if (t != null && t.getImageId() == id) {
relations++;
}
}
Modified: trunk/src/tiled/io/MapHelper.java
===================================================================
--- trunk/src/tiled/io/MapHelper.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/io/MapHelper.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -36,9 +36,8 @@
private static PluginClassLoader pluginLoader;
/**
- * Called to tell the MapHelper which
- * {@link tiled.mapeditor.plugin.PluginClassLoader} to use when finding a
- * suitable plugin for a filename.
+ * Called to tell the MapHelper which {@link PluginClassLoader} to use when
+ * finding a suitable plugin for a filename.
*
* @param p the PluginClassLoader instance to use
*/
@@ -61,7 +60,7 @@
throws Exception
{
MapWriter mw;
- if (filename.endsWith("tmx") || filename.endsWith("tmx.gz")) {
+ if (filename.endsWith(".tmx") || filename.endsWith(".tmx.gz")) {
// Override, so people can't overtake our format
mw = new XMLMapWriter();
} else {
@@ -115,23 +114,23 @@
* Saves a map. Ignores the extension of the filename, and instead uses the
* passed plugin to write the file. Plugins can still refuse to save the file
* based on the extension, but this is not recommended practice.
- *
+ *
* @param currentMap
* @param pmio
* @param filename
- * @throws Exception
+ * @throws Exception
*/
- public static void saveMap(Map currentMap, PluggableMapIO pmio, String filename)
+ public static void saveMap(Map currentMap, PluggableMapIO pmio, String filename)
throws Exception {
MapWriter mw = (MapWriter)pmio;
-
+
PluginLogger logger = new PluginLogger();
mw.setLogger(logger);
mw.writeMap(currentMap, filename);
currentMap.setFilename(filename);
reportPluginMessages(logger);
}
-
+
/**
* Loads a map. Use the extension (.xxx) of the filename to determine
* the plugin to use when reading the file. Throws an exception when the
Modified: trunk/src/tiled/io/xml/XMLMapWriter.java
===================================================================
--- trunk/src/tiled/io/xml/XMLMapWriter.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/io/xml/XMLMapWriter.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -19,7 +19,6 @@
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
-import java.util.Stack;
import java.util.Vector;
import java.util.prefs.Preferences;
import java.util.zip.GZIPOutputStream;
@@ -289,7 +288,10 @@
tileIterator = set.iterator();
while (tileIterator.hasNext()) {
Tile tile = (Tile)tileIterator.next();
- writeTile(tile, w);
+ // todo: move this check back into the iterator?
+ if (tile != null) {
+ writeTile(tile, w);
+ }
}
}
}
Modified: trunk/src/tiled/mapeditor/MapEditor.java
===================================================================
--- trunk/src/tiled/mapeditor/MapEditor.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/MapEditor.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -36,11 +36,9 @@
import tiled.mapeditor.widget.*;
import tiled.mapeditor.undo.*;
import tiled.mapeditor.actions.*;
-import tiled.util.TileMergeHelper;
import tiled.util.TiledConfiguration;
import tiled.io.MapHelper;
import tiled.io.MapReader;
-import tiled.io.MapWriter;
/**
* The main class for the Tiled Map Editor.
@@ -129,6 +127,9 @@
private Brush eraserBrush;
// Actions
+ private final SaveAction saveAction;
+ private final SaveAsAction saveAsAction;
+ private final Action exitAction;
private final Action zoomInAction, zoomOutAction, zoomNormalAction;
private final Action undoAction, redoAction;
private final Action rot90Action, rot180Action, rot270Action;
@@ -176,6 +177,9 @@
setBrush(sb);
// Create the actions
+ saveAction = new SaveAction(this);
+ saveAsAction = new SaveAsAction(this);
+ exitAction = new ExitAction(this, saveAction);
zoomInAction = new ZoomInAction();
zoomOutAction = new ZoomOutAction();
zoomNormalAction = new ZoomNormalAction();
@@ -202,7 +206,7 @@
appFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
appFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
- exit();
+ exitAction.actionPerformed(null);
}
});
appFrame.setContentPane(createContentPane());
@@ -249,25 +253,16 @@
return mainPanel;
}
- private void exit() {
- if (checkSave()) {
- System.exit(0);
- }
- }
-
/**
* Creates all the menus and submenus of the top menu bar. Handles
* assigning listeners and tooltips as well.
*/
private void createMenuBar() {
- JMenuItem save = createMenuItem("Save", null, "Save current map",
- "control S");
- JMenuItem saveAs = createMenuItem("Save as...", null,
- "Save current map as new file", "control shift S");
+ JMenuItem save = new TMenuItem(saveAction);
+ JMenuItem saveAs = new TMenuItem(saveAsAction);
JMenuItem saveAsImage = createMenuItem("Save as Image...", null,
"Save current map as an image", "control shift I");
- JMenuItem close = createMenuItem("Close", null, "Close this map",
- "control W");
+ JMenuItem close = new TMenuItem(new CloseMapAction(this, saveAction));
recentMenu = new JMenu("Open Recent");
@@ -277,18 +272,15 @@
mapEventAdapter.addListener(close);
JMenu fileMenu = new JMenu("File");
- fileMenu.add(createMenuItem("New...", null, "Start a new map",
- "control N"));
- fileMenu.add(createMenuItem("Open...", null, "Open a map",
- "control O"));
+ fileMenu.add(new TMenuItem(new NewMapAction(this, saveAction)));
+ fileMenu.add(new TMenuItem(new OpenMapAction(this, saveAction)));
fileMenu.add(recentMenu);
fileMenu.add(save);
fileMenu.add(saveAs);
fileMenu.add(saveAsImage);
fileMenu.addSeparator();
fileMenu.add(close);
- fileMenu.add(createMenuItem("Exit", null, "Exit the map editor",
- "control Q"));
+ fileMenu.add(new TMenuItem(exitAction));
undoMenuItem = new TMenuItem(undoAction);
redoMenuItem = new TMenuItem(redoAction);
@@ -665,21 +657,8 @@
private AbstractButton createToggleButton(Icon icon, String command,
String tt) {
- return createButton(icon, command, true, tt);
- }
-
- private AbstractButton createButton(Icon icon, String command, String tt) {
- return createButton(icon, command, false, tt);
- }
-
- private AbstractButton createButton(Icon icon, String command,
- boolean toggleButton, String tt) {
AbstractButton button;
- if (toggleButton) {
- button = new JToggleButton("", icon);
- } else {
- button = new JButton("", icon);
- }
+ button = new JToggleButton("", icon);
button.setMargin(new Insets(0, 0, 0, 0));
button.setActionCommand(command);
button.addActionListener(this);
@@ -734,6 +713,22 @@
}
/**
+ * Returns the {@link UndoStack} instance.
+ * @return the undo stack
+ */
+ public UndoStack getUndoStack() {
+ return undoStack;
+ }
+
+ /**
+ * Returns the {@link PluginClassLoader} instance.
+ * @return the plugin class loader
+ */
+ public PluginClassLoader getPluginLoader() {
+ return pluginLoader;
+ }
+
+ /**
* Returns the main application frame.
*
* @return the frame of the main application
@@ -1034,21 +1029,7 @@
private void handleEvent(ActionEvent event) {
String command = event.getActionCommand();
- if (command.equals("Open...")) {
- if (checkSave()) {
- openMap();
- }
- } else if (command.equals("Exit")) {
- exit();
- } else if (command.equals("Close")) {
- if (checkSave()) {
- setCurrentMap(null);
- }
- } else if (command.equals("New...")) {
- if (checkSave()) {
- newMap();
- }
- } else if (command.equals("Brush...")) {
+ if (command.equals("Brush...")) {
BrushDialog bd = new BrushDialog(this, appFrame, currentBrush);
bd.setVisible(true);
} else if (command.equals("New Tileset...")) {
@@ -1093,14 +1074,6 @@
TilesetManager manager = new TilesetManager(appFrame, currentMap);
manager.setVisible(true);
}
- } else if (command.equals("Save")) {
- if (currentMap != null) {
- saveMap(currentMap.getFilename(), false);
- }
- } else if (command.equals("Save as...")) {
- if (currentMap != null) {
- saveMap(currentMap.getFilename(), true);
- }
} else if (command.equals("Save as Image...")) {
if (currentMap != null) {
saveMapImage(null);
@@ -1222,7 +1195,7 @@
*/
}
}
- else if (e.getSource() == mapViewport) {
+ else if (e.getSource() == mapViewport && mapView != null) {
// Store the point in the middle for zooming purposes
Rectangle viewRect = mapViewport.getViewRect();
relativeMidX = Math.min(1, (viewRect.x + viewRect.width / 2) / (float)mapView.getWidth());
@@ -1594,8 +1567,8 @@
} else {
if (marqueeSelection.getSelectedArea().contains(x, y)) {
area = marqueeSelection.getSelectedAreaBounds();
- for (int i = area.y; i < area.height+area.y; i++) {
- for (int j = area.x;j<area.width+area.x;j++){
+ for (int i = area.y; i < area.height + area.y; i++) {
+ for (int j = area.x; j < area.width + area.x; j++) {
if (marqueeSelection.getSelectedArea().contains(j, i)){
layer.setTileAt(j, i, newTile);
}
@@ -1620,7 +1593,7 @@
currentBrush = b;
}
- private void updateTitle() {
+ public void updateTitle() {
String title = Resources.getString("dialog.main.title");
if (currentMap != null) {
@@ -1639,23 +1612,7 @@
appFrame.setTitle(title);
}
- private boolean checkSave() {
- if (unsavedChanges()) {
- int ret = JOptionPane.showConfirmDialog(appFrame,
- "There are unsaved changes for the current map. " +
- "Save changes?",
- "Save Changes?", JOptionPane.YES_NO_CANCEL_OPTION);
-
- if (ret == JOptionPane.YES_OPTION) {
- saveMap(currentMap.getFilename(), true);
- } else if (ret == JOptionPane.CANCEL_OPTION){
- return false;
- }
- }
- return true;
- }
-
- private boolean unsavedChanges() {
+ public boolean unsavedChanges() {
return currentMap != null && undoStack.canUndo() &&
!undoStack.isAllSaved();
}
@@ -1706,125 +1663,6 @@
}
/**
- * Saves the current map, optionally with a "Save As" dialog. If
- * <code>filename</code> is <code>null</code> or <code>bSaveAs</code> is
- * passed <code>true</code>, a "Save As" dialog is opened.
- *
- * @see MapHelper#saveMap(Map, String)
- * @param filename Filename to save the current map to.
- * @param bSaveAs Pass <code>true</code> to ask for a new filename using
- * a "Save As" dialog.
- */
- public void saveMap(String filename, boolean bSaveAs) {
- TiledFileFilter saver = new TiledFileFilter(TiledFileFilter.FILTER_EXT);
- JFileChooser ch = null;
-
- try {
- while(true) {
- if (bSaveAs || filename == null) {
-
- if(ch == null) {
- if (filename == null) {
- ch = new JFileChooser();
- } else {
- ch = new JFileChooser(filename);
- }
-
- MapWriter[] writers = pluginLoader.getWriters();
- for(int i = 0; i < writers.length; i++) {
- try {
- ch.addChoosableFileFilter(new TiledFileFilter(writers[i]));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- ch.addChoosableFileFilter(
- new TiledFileFilter(TiledFileFilter.FILTER_TMX));
-
- ch.addChoosableFileFilter(saver);
- }
-
- if (ch.showSaveDialog(appFrame) == JFileChooser.APPROVE_OPTION) {
- filename = ch.getSelectedFile().getAbsolutePath();
- saver = (TiledFileFilter) ch.getFileFilter();
- } else {
- // User cancelled operation, do nothing
- return;
- }
-
- // Don't let users be tricky (no foo. files)
- if(filename.substring(filename.lastIndexOf('.')+1).length() == 0) {
- filename = filename.substring(0,filename.lastIndexOf('.'));
- }
-
- // Make sure that the file has an extension. If not, append extension
- // chosen from dropdown.
- // NOTE: we can't know anything more than the filename has at least
- // one '.' in it, or at least we won't go to the trouble...
- if (filename.lastIndexOf('.') == -1) {
- if(saver.getType() == TiledFileFilter.FILTER_EXT) {
- //impossible to tell
- JOptionPane.showMessageDialog(appFrame, Resources.getString("dialog.saveas.unknown-type.message"));
- continue;
- }
-
- //we will also be lazy about picking a valid extention...
- filename = filename.concat("."+saver.getFirstExtention());
- }
- }
-
-
- // Check if file exists
- File exist = new File(filename);
- if (exist.exists() && bSaveAs) {
- int result = JOptionPane.showConfirmDialog(appFrame,
- Resources.getString("general.file.exists.message"),
- Resources.getString("general.file.exists.title"),
- JOptionPane.YES_NO_OPTION);
- if (result != JOptionPane.OK_OPTION) {
- continue;
- }
- }
-
- // Do we want to just go by extention?
- if(saver.getType() == TiledFileFilter.FILTER_EXT) {
- MapHelper.saveMap(currentMap, filename);
- } else {
- // Check that chosen plugin and extension match.
- // If they don't, ask the user if they want to shoot themselves in the foot
- if(!saver.accept(exist)) {
- int result = JOptionPane.showConfirmDialog(appFrame,
- Resources.getString("dialog.saveas.confirm.mismatch"),
- "Force save?",
- JOptionPane.YES_NO_OPTION);
- if (result != JOptionPane.OK_OPTION) {
- continue;
- }
- }
-
- MapHelper.saveMap(currentMap, saver.getPlugin(), filename);
- }
-
- // If we make it to the bottom, the user and Tiled have agreed on something,
- // and the file was saved successfully. Update UI.
- currentMap.setFilename(filename);
- updateRecent(filename);
- undoStack.commitSave();
- updateTitle();
- break;
- }
- } catch (Exception e) {
- //e.printStackTrace();
- JOptionPane.showMessageDialog(appFrame,
- Resources.getString("dialog.saveas.error.message") +
- " " + filename + ": " + e.getLocalizedMessage(),
- Resources.getString("dialog.saveas.error.title"),
- JOptionPane.ERROR_MESSAGE);
- }
- }
-
- /**
* Attempts to draw the entire map to an image file
* of the format of the extension. (filename.ext)
*
@@ -1848,7 +1686,7 @@
myView.setZoom(mapView.getZoom());
Dimension d = myView.getPreferredSize();
BufferedImage i = new BufferedImage(d.width, d.height,
- BufferedImage.TYPE_INT_ARGB);
+ BufferedImage.TYPE_INT_ARGB);
Graphics2D g = i.createGraphics();
g.setClip(0, 0, d.width, d.height);
myView.paint(g);
@@ -1860,14 +1698,14 @@
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(appFrame,
- "Error while saving " + filename + ": " + e.toString(),
- "Error while saving map image",
- JOptionPane.ERROR_MESSAGE);
+ "Error while saving " + filename + ": " + e.toString(),
+ "Error while saving map image",
+ JOptionPane.ERROR_MESSAGE);
}
}
}
- private void openMap() {
+ public void openMap() {
// Start at the location of the most recently loaded map file
String startLocation = prefs.node("recent").get("file0", "");
@@ -1896,7 +1734,10 @@
}
}
- private void newMap() {
+ /**
+ * Opens the new map dialog, allowing the user to start editing a new map.
+ */
+ public void newMap() {
NewMapDialog nmd = new NewMapDialog(appFrame);
Map newMap = nmd.create();
if (newMap != null) {
@@ -1913,7 +1754,7 @@
return null;
}
- private void updateRecent(String filename) {
+ public void updateRecent(String filename) {
// If a filename is given, add it to the recent files
if (filename != null) {
TiledConfiguration.addToRecentFiles(filename);
@@ -1934,7 +1775,7 @@
}
}
- private void setCurrentMap(Map newMap) {
+ public void setCurrentMap(Map newMap) {
currentMap = newMap;
boolean mapLoaded = currentMap != null;
Added: trunk/src/tiled/mapeditor/actions/AbstractFileAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/AbstractFileAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/AbstractFileAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -0,0 +1,75 @@
+/*
+ * Tiled Map Editor, (c) 2004-2006
+ *
+ * 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.
+ *
+ * Adam Turk <aturk at biggeruniverse.com>
+ * Bjorn Lindeijer <b.lindeijer at xs4all.nl>
+ */
+
+package tiled.mapeditor.actions;
+
+import java.awt.event.ActionEvent;
+import javax.swing.AbstractAction;
+import javax.swing.JOptionPane;
+
+import tiled.mapeditor.MapEditor;
+
+/**
+ * This abstract file action asks to save the map if it has been modified. It
+ * should be subclassed by any action that would discard the currently loaded
+ * map.
+ *
+ * @version $Id$
+ */
+public abstract class AbstractFileAction extends AbstractAction
+{
+ protected final MapEditor editor;
+ private final SaveAsAction saveAction;
+
+ protected AbstractFileAction(MapEditor editor,
+ SaveAction saveAction,
+ String name, String description)
+ {
+ super(name);
+ this.editor = editor;
+ this.saveAction = saveAction;
+ putValue(SHORT_DESCRIPTION, description);
+ }
+
+ public final void actionPerformed(ActionEvent e) {
+ if (editor.unsavedChanges()) {
+ // todo: internationalize
+ int ret = JOptionPane.showConfirmDialog(editor.getAppFrame(),
+ "There are unsaved changes for the current map. " +
+ "Save changes?",
+ "Save Changes?", JOptionPane.YES_NO_CANCEL_OPTION);
+
+ if (ret == JOptionPane.YES_OPTION) {
+ saveAction.actionPerformed(e);
+
+ // If saving was not cancelled and there are not still unsaved
+ // changes (which would indicate an error occured), continue
+ // to perform the action.
+ if (!saveAction.isSavingCancelled() && !editor.unsavedChanges())
+ {
+ doPerformAction();
+ }
+ } else if (ret == JOptionPane.NO_OPTION){
+ doPerformAction();
+ }
+ }
+ else {
+ doPerformAction();
+ }
+ }
+
+ /**
+ * Actually performs the action, in the confidence that any existing map
+ * has been either saved or discarded.
+ */
+ protected abstract void doPerformAction();
+}
Property changes on: trunk/src/tiled/mapeditor/actions/AbstractFileAction.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: trunk/src/tiled/mapeditor/actions/AbstractLayerAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/AbstractLayerAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/AbstractLayerAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -76,5 +76,5 @@
/**
* Actually performs the action that modifies the layer configuration.
*/
- public abstract void doPerformAction();
+ protected abstract void doPerformAction();
}
Property changes on: trunk/src/tiled/mapeditor/actions/AbstractLayerAction.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: trunk/src/tiled/mapeditor/actions/AddLayerAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/AddLayerAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/AddLayerAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -30,7 +30,7 @@
Resources.getIcon("gnome-new.png"));
}
- public void doPerformAction() {
+ protected void doPerformAction() {
Map currentMap = editor.getCurrentMap();
currentMap.addLayer();
editor.setCurrentLayer(currentMap.getTotalLayers() - 1);
Modified: trunk/src/tiled/mapeditor/actions/CloneLayerAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/CloneLayerAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/CloneLayerAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -34,7 +34,7 @@
Resources.getIcon("gimp-duplicate-16.png"));
}
- public void doPerformAction() {
+ protected void doPerformAction() {
MapLayer currentLayer = editor.getCurrentLayer();
Map currentMap = editor.getCurrentMap();
Added: trunk/src/tiled/mapeditor/actions/CloseMapAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/CloseMapAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/CloseMapAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -0,0 +1,38 @@
+/*
+ * Tiled Map Editor, (c) 2004-2006
+ *
+ * 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.
+ *
+ * Adam Turk <aturk at biggeruniverse.com>
+ * Bjorn Lindeijer <b.lindeijer at xs4all.nl>
+ */
+
+package tiled.mapeditor.actions;
+
+import javax.swing.KeyStroke;
+
+import tiled.mapeditor.MapEditor;
+import tiled.mapeditor.Resources;
+
+/**
+ * Closes the currently opened map.
+ *
+ * @version $Id$
+ */
+public class CloseMapAction extends AbstractFileAction
+{
+ public CloseMapAction(MapEditor editor, SaveAction saveAction) {
+ super(editor, saveAction,
+ Resources.getString("action.map.close.name"),
+ Resources.getString("action.map.close.tooltip"));
+
+ putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control W"));
+ }
+
+ protected void doPerformAction() {
+ editor.setCurrentMap(null);
+ }
+}
Property changes on: trunk/src/tiled/mapeditor/actions/CloseMapAction.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: trunk/src/tiled/mapeditor/actions/DeleteLayerAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/DeleteLayerAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/DeleteLayerAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -30,7 +30,7 @@
Resources.getIcon("gnome-delete.png"));
}
- public void doPerformAction() {
+ protected void doPerformAction() {
Map map = editor.getCurrentMap();
int layerIndex = editor.getCurrentLayerIndex();
int totalLayers = map.getTotalLayers();
Property changes on: trunk/src/tiled/mapeditor/actions/DeleteLayerAction.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: trunk/src/tiled/mapeditor/actions/ExitAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/ExitAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/ExitAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -0,0 +1,38 @@
+/*
+ * Tiled Map Editor, (c) 2004-2006
+ *
+ * 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.
+ *
+ * Adam Turk <aturk at biggeruniverse.com>
+ * Bjorn Lindeijer <b.lindeijer at xs4all.nl>
+ */
+
+package tiled.mapeditor.actions;
+
+import javax.swing.KeyStroke;
+
+import tiled.mapeditor.MapEditor;
+import tiled.mapeditor.Resources;
+
+/**
+ * Exits the map editor.
+ *
+ * @version $Id$
+ */
+public class ExitAction extends AbstractFileAction
+{
+ public ExitAction(MapEditor editor, SaveAction saveAction) {
+ super(editor, saveAction,
+ Resources.getString("action.main.exit.name"),
+ Resources.getString("action.main.exit.tooltip"));
+
+ putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control Q"));
+ }
+
+ protected void doPerformAction() {
+ System.exit(0);
+ }
+}
Property changes on: trunk/src/tiled/mapeditor/actions/ExitAction.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: trunk/src/tiled/mapeditor/actions/MergeAllLayersAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/MergeAllLayersAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/MergeAllLayersAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -34,7 +34,7 @@
Resources.getString("action.layer.mergeall.tooltip"));
}
- public void doPerformAction() {
+ protected void doPerformAction() {
Map map = editor.getCurrentMap();
if (JOptionPane.showConfirmDialog(editor.getAppFrame(),
Modified: trunk/src/tiled/mapeditor/actions/MergeLayerDownAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/MergeLayerDownAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/MergeLayerDownAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -33,7 +33,7 @@
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("shift control M"));
}
- public void doPerformAction() {
+ protected void doPerformAction() {
Map map = editor.getCurrentMap();
int layerIndex = editor.getCurrentLayerIndex();
Modified: trunk/src/tiled/mapeditor/actions/MoveLayerDownAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/MoveLayerDownAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/MoveLayerDownAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -34,7 +34,7 @@
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("shift PAGE_DOWN"));
}
- public void doPerformAction() {
+ protected void doPerformAction() {
Map map = editor.getCurrentMap();
int layerIndex = editor.getCurrentLayerIndex();
Modified: trunk/src/tiled/mapeditor/actions/MoveLayerUpAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/MoveLayerUpAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/MoveLayerUpAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -34,7 +34,7 @@
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("shift PAGE_UP"));
}
- public void doPerformAction() {
+ protected void doPerformAction() {
Map map = editor.getCurrentMap();
int layerIndex = editor.getCurrentLayerIndex();
int totalLayers = map.getTotalLayers();
Added: trunk/src/tiled/mapeditor/actions/NewMapAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/NewMapAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/NewMapAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -0,0 +1,38 @@
+/*
+ * Tiled Map Editor, (c) 2004-2006
+ *
+ * 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.
+ *
+ * Adam Turk <aturk at biggeruniverse.com>
+ * Bjorn Lindeijer <b.lindeijer at xs4all.nl>
+ */
+
+package tiled.mapeditor.actions;
+
+import javax.swing.KeyStroke;
+
+import tiled.mapeditor.MapEditor;
+import tiled.mapeditor.Resources;
+
+/**
+ * Creates a new map.
+ *
+ * @version $Id$
+ */
+public class NewMapAction extends AbstractFileAction
+{
+ public NewMapAction(MapEditor editor, SaveAction saveAction) {
+ super(editor, saveAction,
+ Resources.getString("action.map.new.name"),
+ Resources.getString("action.map.new.tooltip"));
+
+ putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control N"));
+ }
+
+ protected void doPerformAction() {
+ editor.newMap();
+ }
+}
Property changes on: trunk/src/tiled/mapeditor/actions/NewMapAction.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: trunk/src/tiled/mapeditor/actions/OpenMapAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/OpenMapAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/OpenMapAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -0,0 +1,38 @@
+/*
+ * Tiled Map Editor, (c) 2004-2006
+ *
+ * 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.
+ *
+ * Adam Turk <aturk at biggeruniverse.com>
+ * Bjorn Lindeijer <b.lindeijer at xs4all.nl>
+ */
+
+package tiled.mapeditor.actions;
+
+import javax.swing.KeyStroke;
+
+import tiled.mapeditor.MapEditor;
+import tiled.mapeditor.Resources;
+
+/**
+ * Opens the map open dialog.
+ *
+ * @version $Id$
+ */
+public class OpenMapAction extends AbstractFileAction
+{
+ public OpenMapAction(MapEditor editor, SaveAction saveAction) {
+ super(editor, saveAction,
+ Resources.getString("action.map.open.name"),
+ Resources.getString("action.map.open.tooltip"));
+
+ putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control O"));
+ }
+
+ protected void doPerformAction() {
+ editor.openMap();
+ }
+}
Property changes on: trunk/src/tiled/mapeditor/actions/OpenMapAction.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: trunk/src/tiled/mapeditor/actions/SaveAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/SaveAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/SaveAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -0,0 +1,56 @@
+/*
+ * Tiled Map Editor, (c) 2004-2006
+ *
+ * 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.
+ *
+ * Adam Turk <aturk at biggeruniverse.com>
+ * Bjorn Lindeijer <b.lindeijer at xs4all.nl>
+ */
+
+package tiled.mapeditor.actions;
+
+import java.awt.event.ActionEvent;
+
+import javax.swing.KeyStroke;
+
+import tiled.mapeditor.MapEditor;
+import tiled.mapeditor.Resources;
+import tiled.mapeditor.util.TiledFileFilter;
+import tiled.core.Map;
+
+/**
+ * Tries to save the file if a filepath is already set in the main app,
+ * otherwise prompts for a filename.
+ *
+ * @version $Id$
+ */
+public class SaveAction extends SaveAsAction
+{
+ public SaveAction(MapEditor editor) {
+ super(editor);
+ putValue(NAME, Resources.getString("action.map.save.name"));
+ putValue(SHORT_DESCRIPTION, Resources.getString("action.map.save.tooltip"));
+ putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control S"));
+ }
+
+ public void actionPerformed (ActionEvent e)
+ {
+ Map currentMap = editor.getCurrentMap();
+ String filePath = currentMap.getFilename();
+
+ // todo: Fix the case where the plugin cannot be determined by the
+ // todo: current filename. This can happen when the user has used
+ // todo: "Save As" to save the file using a non-standard extension.
+ if (filePath != null) {
+ // The plugin is determined by the extention.
+ saveFile(new TiledFileFilter(TiledFileFilter.FILTER_EXT),
+ filePath);
+ }
+ else {
+ super.actionPerformed(e);
+ }
+ }
+}
Property changes on: trunk/src/tiled/mapeditor/actions/SaveAction.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: trunk/src/tiled/mapeditor/actions/SaveAsAction.java
===================================================================
--- trunk/src/tiled/mapeditor/actions/SaveAsAction.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/actions/SaveAsAction.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -0,0 +1,230 @@
+/*
+ * Tiled Map Editor, (c) 2004-2006
+ *
+ * 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.
+ *
+ * Adam Turk <aturk at biggeruniverse.com>
+ * Bjorn Lindeijer <b.lindeijer at xs4all.nl>
+ */
+
+package tiled.mapeditor.actions;
+
+import java.awt.event.ActionEvent;
+import java.io.File;
+import javax.swing.AbstractAction;
+import javax.swing.KeyStroke;
+import javax.swing.JFileChooser;
+import javax.swing.JOptionPane;
+
+import tiled.mapeditor.MapEditor;
+import tiled.mapeditor.Resources;
+import tiled.mapeditor.util.TiledFileFilter;
+import tiled.io.MapWriter;
+import tiled.io.MapHelper;
+import tiled.util.TiledConfiguration;
+
+/**
+ * A save action that always shows a file chooser.
+ *
+ * @version $Id$
+ */
+public class SaveAsAction extends AbstractAction
+{
+ protected MapEditor editor;
+ private boolean savingCancelled;
+
+ private static final String ACTION_NAME = Resources.getString("action.map.saveas.name");
+ private static final String ACTION_TOOLTIP = Resources.getString("action.map.saveas.tooltip");
+ private static final String UNKNOWN_TYPE_MESSAGE = Resources.getString("dialog.saveas.unknown-type.message");
+ private static final String CONFIRM_MISMATCH = Resources.getString("dialog.saveas.confirm.mismatch");
+ private static final String CONFIRM_MISMATCH_TITLE = Resources.getString("dialog.saveas.confirm.mismatch.title");
+ private static final String FILE_EXISTS_MESSAGE = Resources.getString("general.file.exists.message");
+ private static final String FILE_EXISTS_TITLE = Resources.getString("general.file.exists.title");
+ private static final String SAVEAS_ERROR_MESSAGE = Resources.getString("dialog.saveas.error.message");
+ private static final String SAVEAS_ERROR_TITLE = Resources.getString("dialog.saveas.error.title");
+
+ public SaveAsAction(MapEditor editor) {
+ super(ACTION_NAME);
+ putValue(SHORT_DESCRIPTION, ACTION_TOOLTIP);
+ putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control shift S"));
+ this.editor = editor;
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ showFileChooser();
+ }
+
+ /**
+ * Shows the confirming file chooser and proceeds with saving the map when
+ * a filename was approved.
+ */
+ protected void showFileChooser()
+ {
+ // Start at the location of the most recently loaded map file
+ String startLocation = TiledConfiguration.node("recent").get("file0", "");
+ JFileChooser chooser = new ConfirmingFileChooser(startLocation);
+ chooser.setDialogTitle(Resources.getString("dialog.saveas.title"));
+
+ MapWriter[] writers = editor.getPluginLoader().getWriters();
+ for (int i = 0; i < writers.length; i++) {
+ try {
+ chooser.addChoosableFileFilter(new TiledFileFilter(writers[i]));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ chooser.addChoosableFileFilter(
+ new TiledFileFilter(TiledFileFilter.FILTER_TMX));
+ chooser.addChoosableFileFilter(
+ new TiledFileFilter(TiledFileFilter.FILTER_EXT));
+
+ int result = chooser.showSaveDialog(editor.getAppFrame());
+ if (result == JFileChooser.APPROVE_OPTION)
+ {
+ TiledFileFilter saver = (TiledFileFilter) chooser.getFileFilter();
+ String selectedFile = chooser.getSelectedFile().getAbsolutePath();
+ saveFile(saver, selectedFile);
+ }
+ }
+
+ /**
+ * Actually saves the map.
+ *
+ * @param saver the file filter selected when the filename was chosen
+ * @param filename the filename to save the map to
+ */
+ protected void saveFile(TiledFileFilter saver, String filename)
+ {
+ try {
+ // Either select the format by extension or use a specific format
+ // when selected.
+ if (saver.getType() == TiledFileFilter.FILTER_EXT) {
+ MapHelper.saveMap(editor.getCurrentMap(), filename);
+ } else {
+ MapHelper.saveMap(editor.getCurrentMap(), saver.getPlugin(),
+ filename);
+ }
+
+ // The file was saved successfully, update some things.
+ // todo: this could probably be done a bit neater
+ editor.getCurrentMap().setFilename(filename);
+ editor.updateRecent(filename);
+ editor.getUndoStack().commitSave();
+ editor.updateTitle();
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ JOptionPane.showMessageDialog(editor.getAppFrame(),
+ SAVEAS_ERROR_MESSAGE + " " +
+ filename + ": " +
+ e.getLocalizedMessage(),
+ SAVEAS_ERROR_TITLE,
+ JOptionPane.ERROR_MESSAGE);
+ }
+ }
+
+ public boolean isSavingCancelled ()
+ {
+ return savingCancelled;
+ }
+
+ public void resetSavingCancelled ()
+ {
+ savingCancelled = false;
+ }
+
+ /**
+ * This file chooser extends the {@link JFileChooser} in a number of ways.
+ * <ul>
+ * <li>Adds an extention to the filename based on the file filter, when
+ * the user didn't specify any.</li>
+ * <li>If the file to be saved is not accepted by the chosen file filter,
+ * it confirms that the user really wants to do this. This is done
+ * because the same file filter is used to determine with which
+ * plugin to load the file.</li>
+ * <li>Confirms before overwriting an existing file.</li>
+ * </ul>
+ * This file chooser can only be used with {@link TiledFileFilter}.
+ */
+ private final class ConfirmingFileChooser extends JFileChooser
+ {
+ public ConfirmingFileChooser(String currentDirectoryPath) {
+ super(currentDirectoryPath);
+ }
+
+ public void approveSelection ()
+ {
+ File file = new File(getSelectedFile().getAbsolutePath());
+ TiledFileFilter saver = (TiledFileFilter) getFileFilter();
+
+ // If the file does not have an extention, append the first
+ // extension specified by the file filter.
+ String filename = file.getName();
+ int lastDot = filename.lastIndexOf('.');
+
+ if (lastDot == -1 || lastDot == filename.length() - 1) {
+ if (saver.getType() == TiledFileFilter.FILTER_EXT) {
+ // Impossible to determine extension with this filter
+ JOptionPane.showMessageDialog(this,
+ UNKNOWN_TYPE_MESSAGE);
+ return;
+ }
+
+ String newFilePath = file.getAbsolutePath();
+
+ // Add a dot if it wasn't at the end already
+ if (lastDot != filename.length() - 1) {
+ newFilePath = newFilePath + ".";
+ }
+
+ file = new File(newFilePath + saver.getFirstExtention());
+ }
+
+ // Check that chosen plugin accepts the file. It is a good idea to
+ // warn the user when this is not the case, because loading the map
+ // becomes a problem.
+ if (saver.getType() != TiledFileFilter.FILTER_EXT) {
+ if (!saver.accept(file)) {
+ int result = JOptionPane.showConfirmDialog(
+ editor.getAppFrame(),
+ CONFIRM_MISMATCH, CONFIRM_MISMATCH_TITLE,
+ JOptionPane.YES_NO_OPTION);
+
+ if (result != JOptionPane.OK_OPTION) {
+ return;
+ }
+ }
+ }
+
+ // Confirm overwrite if the file happens to exist already
+ if (file.exists())
+ {
+
+ int answer = JOptionPane.showConfirmDialog(
+ editor.getAppFrame(),
+ FILE_EXISTS_MESSAGE, FILE_EXISTS_TITLE,
+ JOptionPane.YES_NO_OPTION);
+
+ if (answer == JOptionPane.YES_OPTION)
+ {
+ savingCancelled = false;
+ super.approveSelection();
+ }
+ }
+ else {
+ savingCancelled = false;
+ super.approveSelection();
+ }
+ }
+
+ public void cancelSelection ()
+ {
+ savingCancelled = true;
+ super.cancelSelection();
+ }
+ }
+}
Property changes on: trunk/src/tiled/mapeditor/actions/SaveAsAction.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: trunk/src/tiled/mapeditor/resources/gui.properties
===================================================================
--- trunk/src/tiled/mapeditor/resources/gui.properties 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/resources/gui.properties 2006-04-23 21:38:06 UTC (rev 613)
@@ -17,6 +17,18 @@
action.layer.movedown.tooltip=Move layer down one in layer stack
action.layer.moveup.name=Move Layer Up
action.layer.moveup.tooltip=Move layer up one in layer stack
+action.main.exit.name=Exit
+action.main.exit.tooltip=Exit the map editor
+action.map.close.name=Close
+action.map.close.tooltip=Close this map
+action.map.new.name=New...
+action.map.new.tooltip=Start a new map
+action.map.open.name=Open...
+action.map.open.tooltip=Open a map
+action.map.saveas.name=Save As...
+action.map.saveas.tooltip=Save current map as new file
+action.map.save.name=Save
+action.map.save.tooltip=Save current map
action.paste.name=Paste
action.paste.tooltip=Paste from clipboard
action.select.all.name=All
@@ -81,8 +93,10 @@
dialog.resizemap.x.label=X:
dialog.resizemap.y.label=Y:
dialog.saveas.confirm.mismatch=The file extension does not match the plugin. Do you wish to continue?
+dialog.saveas.confirm.mismatch.title=Force save?
dialog.saveas.error.message=Error while attempting to save
dialog.saveas.error.title=Error while saving map
+dialog.saveas.title=Save As
dialog.saveas.unknown-type.message=Save failed, unknown type
dialog.tile.button.animation=Animation
dialog.tile.button.changeimage=Change Image
Modified: trunk/src/tiled/mapeditor/util/TiledFileFilter.java
===================================================================
--- trunk/src/tiled/mapeditor/util/TiledFileFilter.java 2006-04-22 20:36:51 UTC (rev 612)
+++ trunk/src/tiled/mapeditor/util/TiledFileFilter.java 2006-04-23 21:38:06 UTC (rev 613)
@@ -5,7 +5,7 @@
* 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.
- *
+ *
* Adam Turk <aturk at biggeruniverse.com>
* Bjorn Lindeijer <b.lindeijer at xs4all.nl>
*/
@@ -25,17 +25,17 @@
*/
public class TiledFileFilter extends FileFilter
{
- public static final int FILTER_EXT = 0;
+ public static final int FILTER_EXT = 0;
public static final int FILTER_TMX = 1;
public static final int FILTER_TSX = 2;
public static final int FILTER_BOTH = 3;
public static final int FILTER_PLUG = 4;
-
+
private String desc;
private LinkedList exts;
private PluggableMapIO pmio;
private int type = FILTER_EXT;
-
+
public TiledFileFilter() {
desc = "Tiled files";
exts = new LinkedList();
@@ -45,48 +45,50 @@
pmio = new XMLMapWriter();
}
- public TiledFileFilter(int filter) {
+ public TiledFileFilter(int filter) {
exts = new LinkedList();
desc = "";
type = filter;
-
+
if ((filter & FILTER_TMX) != 0) {
desc = "Tiled Maps files ";
exts.add("tmx");
exts.add("tmx.gz");
pmio = new XMLMapWriter();
}
-
+
if ((filter & FILTER_TSX) != 0) {
desc += "Tiled Tileset files";
exts.add("tsx");
- if(pmio == null) pmio = new XMLMapWriter();
+ if (pmio == null) {
+ pmio = new XMLMapWriter();
+ }
}
-
- if(filter == FILTER_EXT) {
- desc = "By Extension";
+
+ if (filter == FILTER_EXT) {
+ desc = "By Extension";
}
}
public TiledFileFilter(PluggableMapIO p) throws Exception {
- exts = new LinkedList();
- pmio = p;
- buildFilter(p.getFilter(), p.getName());
+ exts = new LinkedList();
+ pmio = p;
+ buildFilter(p.getFilter(), p.getName());
}
-
+
public TiledFileFilter(String filter, String desc) {
- exts = new LinkedList();
- buildFilter(filter, desc);
+ exts = new LinkedList();
+ buildFilter(filter, desc);
}
-
+
private void buildFilter(String filter, String desc) {
- this.desc = desc;
- String[] ext = filter.split(",");
- for (int i = 0; i < ext.length; i++) {
- exts.add(ext[i].substring(ext[i].indexOf('.') + 1));
- }
+ this.desc = desc;
+ String[] ext = filter.split(",");
+ for (int i = 0; i < ext.length; i++) {
+ exts.add(ext[i].substring(ext[i].indexOf('.') + 1));
+ }
}
-
+
public void setDescription(String d) {
desc = d;
}
@@ -96,29 +98,26 @@
}
public PluggableMapIO getPlugin() {
- return pmio;
+ return pmio;
}
-
+
public String getFirstExtention() {
- return (String) exts.getFirst();
+ return (String) exts.getFirst();
}
-
+
public int getType() {
- return type;
+ return type;
}
-
+
public boolean accept(File f) {
- if (f.isFile() || !f.exists()) {
- if (f.getAbsolutePath().lastIndexOf('.') == -1) {
- return false;
- }
+ // todo: Verify that the "!f.exists()" check is rather weird.
+ if (type != FILTER_EXT && (f.isFile() || !f.exists())) {
+ String fileName = f.getPath().toLowerCase();
- String fileName = f.getAbsolutePath().toLowerCase();
-
Iterator itr = exts.iterator();
while (itr.hasNext()) {
- String ext = (String)itr.next();
- if (fileName.endsWith(ext)) {
+ String ext = (String) itr.next();
+ if (fileName.endsWith("." + ext)) {
return true;
}
}
@@ -128,22 +127,22 @@
}
public String getDescription() {
- String filter = "";
-
- if(!exts.isEmpty()) {
- filter = filter + " (";
- Iterator itr = exts.iterator();
- while (itr.hasNext()) {
- String ext = (String)itr.next();
- filter = filter + "*." + ext;
- if (itr.hasNext()) {
- filter += ",";
- }
- }
-
- filter = filter+")";
- }
-
+ String filter = "";
+
+ if (!exts.isEmpty()) {
+ filter = filter + " (";
+ Iterator itr = exts.iterator();
+ while (itr.hasNext()) {
+ String ext = (String) itr.next();
+ filter = filter + "*." + ext;
+ if (itr.hasNext()) {
+ filter += ",";
+ }
+ }
+
+ filter = filter + ")";
+ }
+
return desc + filter;
}
}
More information about the tiled-commit
mailing list