[tiled] r596 - in trunk/src/tiled: io mapeditor mapeditor/util

svn@biggeruniverse.com svn at biggeruniverse.com
Sat Apr 1 09:41:47 PST 2006


Author: aturk
Date: 2006-04-01 11:41:46 -0600 (Sat, 01 Apr 2006)
New Revision: 596

Modified:
   trunk/src/tiled/io/MapHelper.java
   trunk/src/tiled/mapeditor/MapEditor.java
   trunk/src/tiled/mapeditor/util/TiledFileFilter.java
Log:
Fixes for two tasks, #120 and #121. "Save As" dialog is now far superior, 
and accounts for many cases that were previously ignored or failed 
silently. It now correctly returns the user to the Save As dialog instead 
of closing it if they change their mind, and allows saving by extension 
explicitly.

Modified: trunk/src/tiled/io/MapHelper.java
===================================================================
--- trunk/src/tiled/io/MapHelper.java	2006-04-01 15:15:56 UTC (rev 595)
+++ trunk/src/tiled/io/MapHelper.java	2006-04-01 17:41:46 UTC (rev 596)
@@ -49,7 +49,7 @@
      * Saves the current map. Use the extension (.xxx) of the filename to
      * determine the plugin to use when writing the file. Throws an exception
      * when the extension is not supported by either the TMX writer or a
-     * plugin.
+     * plugin. (Unlikely)
      *
      * @param filename filename to save the current map to
      * @param currentMap {@link tiled.core.Map} instance to save to the file
@@ -111,6 +111,27 @@
     }
 
     /**
+     * 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 
+     */
+    public static void saveMap(Map currentMap, PluggableMapIO pmio, String filename) 
+    	throws Exception {
+    	MapWriter mw = (MapWriter)pmio;
+    	
+        Stack errors = new Stack();
+        mw.setErrorStack(errors);
+        mw.writeMap(currentMap, filename);
+        currentMap.setFilename(filename);
+        reportPluginMessages(errors);
+    }
+    
+    /**
      * 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
      * extension is not supported by either the TMX writer or a plugin.

Modified: trunk/src/tiled/mapeditor/MapEditor.java
===================================================================
--- trunk/src/tiled/mapeditor/MapEditor.java	2006-04-01 15:15:56 UTC (rev 595)
+++ trunk/src/tiled/mapeditor/MapEditor.java	2006-04-01 17:41:46 UTC (rev 596)
@@ -1796,65 +1796,102 @@
      *                 a "Save As" dialog.
      */
     public void saveMap(String filename, boolean bSaveAs) {
-        if (bSaveAs || filename == null) {
-            JFileChooser ch;
-
-            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].getFilter(), writers[i].getName()));
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-            }
-
-            ch.addChoosableFileFilter(
-                    new TiledFileFilter(TiledFileFilter.FILTER_TMX));
-
-            if (ch.showSaveDialog(appFrame) == JFileChooser.APPROVE_OPTION) {
-                filename = ch.getSelectedFile().getAbsolutePath();
-            } else {
-                // User cancelled operation, do nothing
-                return;
-            }
-        }
-
-        // Make sure that the file has an extension. If not, append .tmx
-        // NOTE: we can't know anything more than the filename has at least
-        //		 one '.' in it...
-        if (filename.lastIndexOf('.') == -1) {
-        	filename = filename.concat(".tmx");
-        }
-        
-        try {
-            // Check if file exists
-            File exist = new File(filename);
-            if (exist.exists() && bSaveAs) {
-                int result = JOptionPane.showConfirmDialog(appFrame,
-                        "The file already exists. Are you sure you want to " +
-                        "overwrite it?", "Overwrite file?",
-                        JOptionPane.YES_NO_OPTION);
-                if (result != JOptionPane.OK_OPTION) {
-                    return;
-                }
-            }
-
-            MapHelper.saveMap(currentMap, filename);
-            currentMap.setFilename(filename);
-            updateRecent(filename);
-            undoStack.commitSave();
-            updateTitle();
-        } catch (Exception e) {
+    	
+    	TiledFileFilter saver = new TiledFileFilter(TiledFileFilter.FILTER_EXT);
+    	boolean saveOk = false;
+    	JFileChooser ch = null;
+    	
+    	try {
+	    	while(!saveOk) {
+		        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;
+		            }
+		            
+		            // 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, "Save failed, unknown type");
+		            		continue;
+		            	}
+		            	filename = filename.concat("."+saver.getFirstExtention());
+		            }
+		        }
+		        
+		        
+	            // Check if file exists
+	            File exist = new File(filename);
+	            if (exist.exists() && bSaveAs) {
+	                int result = JOptionPane.showConfirmDialog(appFrame,
+	                        "The file already exists. Are you sure you want to " +
+	                        "overwrite it?", "Overwrite file?",
+	                        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,
+	                            "The file extension does not match the plugin."+
+	                            " Do you wish to continue?",
+	                            "Force save?",
+	                            JOptionPane.YES_NO_OPTION);
+	                	if (result != JOptionPane.OK_OPTION) {
+		                    continue;
+		                }
+	                }
+	                
+	                MapHelper.saveMap(currentMap, saver.getPlugin(), filename);
+	            }
+	            
+	            currentMap.setFilename(filename);
+	            updateRecent(filename);
+	            undoStack.commitSave();
+	            updateTitle();
+	            saveOk = true;
+	    	}
+    	} catch (Exception e) {
             e.printStackTrace();
             JOptionPane.showMessageDialog(appFrame,
-                    "Error while saving " + filename + ": " + e.toString(),
+                    "Error while attempting to save " + filename + ": " + e.toString(),
                     "Error while saving map",
                     JOptionPane.ERROR_MESSAGE);
         }

Modified: trunk/src/tiled/mapeditor/util/TiledFileFilter.java
===================================================================
--- trunk/src/tiled/mapeditor/util/TiledFileFilter.java	2006-04-01 15:15:56 UTC (rev 595)
+++ trunk/src/tiled/mapeditor/util/TiledFileFilter.java	2006-04-01 17:41:46 UTC (rev 596)
@@ -17,17 +17,23 @@
 import java.util.LinkedList;
 import javax.swing.filechooser.FileFilter;
 
+import tiled.io.PluggableMapIO;
+
 /**
  * @version $Id$
  */
 public class TiledFileFilter extends FileFilter
 {
+	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";
@@ -40,19 +46,36 @@
     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");
         }
+        
         if ((filter & FILTER_TSX) != 0) {
             desc += "Tiled Tileset files";
             exts.add("tsx");
         }
+        
+        if(filter == FILTER_EXT) {
+        	desc = "By Extension";
+        }
     }
 
+    public TiledFileFilter(PluggableMapIO p) throws Exception {
+    	exts = new LinkedList();
+    	pmio = p;
+    	buildFilter(p.getFilter(), p.getName());
+    }
+    
     public TiledFileFilter(String filter, String 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++) {
@@ -68,8 +91,20 @@
         exts.add(e);
     }
 
+    public PluggableMapIO getPlugin() {
+    	return pmio;
+    }
+    
+    public String getFirstExtention() {
+    	return (String) exts.getFirst();
+    }
+    
+    public int getType() {
+    	return type;
+    }
+    
     public boolean accept(File f) {
-        if (f.isFile()) {
+        if (f.isFile() || !f.exists()) {
             if (f.getAbsolutePath().lastIndexOf('.') == -1) {
                 return false;
             }
@@ -89,15 +124,22 @@
     }
 
     public String getDescription() {
-    	String filter = "(";
-    	Iterator itr = exts.iterator();
-        while (itr.hasNext()) {
-        	String ext = (String)itr.next();
-        	filter = filter + "*." + ext;
-        	if (itr.hasNext()) {
-                filter += ",";
-            }
-        }
-        return desc + " " + 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