Fin import updates
[debian/openrocket] / core / src / net / sf / openrocket / gui / util / FileHelper.java
index 8c496393eeb183f11ccade0560000bb0cff91e28..860379ed48bdb4f962cb973e7bdc9f33929eaac4 100644 (file)
@@ -3,7 +3,9 @@ package net.sf.openrocket.gui.util;
 import java.awt.Component;
 import java.io.File;
 import java.io.IOException;
+import java.util.Arrays;
 
+import javax.imageio.ImageIO;
 import javax.swing.JOptionPane;
 import javax.swing.filechooser.FileFilter;
 
@@ -23,7 +25,7 @@ public final class FileHelper {
        private static final LogHelper log = Application.getLogger();
        private static final Translator trans = Application.getTranslator();
        
-
+       
        // TODO: HIGH: Rename translation keys
        
        /** File filter for any rocket designs (*.ork, *.rkt) */
@@ -48,20 +50,42 @@ public final class FileHelper {
                        new SimpleFileFilter(trans.get("SimExpPan.desc"), ".csv");
        
        
-
-
-
+       
+       
        private FileHelper() {
                // Prevent instantiation
        }
        
+       
+       public static FileFilter getImageFileFilter() {
+               String[] extensions = ImageIO.getReaderFileSuffixes();
+               for (int i = 0; i < extensions.length; i++) {
+                       extensions[i] = extensions[i].toLowerCase();
+               }
+               Arrays.sort(extensions);
+               
+               StringBuilder sb = new StringBuilder();
+               sb.append(trans.get("filetypes.images"));
+               sb.append(" (");
+               for (int i = 0; i < extensions.length; i++) {
+                       sb.append("*.").append(extensions[i]);
+                       if (i < extensions.length - 1) {
+                               sb.append("; ");
+                       }
+               }
+               sb.append(")");
+               
+               return new SimpleFileFilter(sb.toString(), extensions);
+       }
+       
+       
        /**
         * Ensure that the provided file has a file extension.  If the file does not have
         * any extension, append the provided extension to it.
         * 
         * @param original              the original file
         * @param extension             the extension to append if none exists (without preceding dot)
-        * @return                              the resulting filen
+        * @return                              the resulting file
         */
        public static File ensureExtension(File original, String extension) {
                
@@ -75,6 +99,32 @@ public final class FileHelper {
                return original;
        }
        
+       /**
+        * Ensure that the provided file has the given file extension.  This differs from ensureExtension in that this
+        * method guarantees that the file will have the extension, whereas ensureExtension only treats the extension
+        * as a default.
+        *
+        * @param original              the original file
+        * @param extension             the extension to guarantee (without preceding dot)
+        * @return                              the resulting file
+        */
+       public static File forceExtension(File original, String extension) {
+               
+               if (!original.getName().toLowerCase().endsWith(extension.toLowerCase())) {
+                       log.debug(1, "File name does not contain extension, adding '" + extension + "'");
+                       String name = original.getAbsolutePath();
+                       if (extension.startsWith(".")) {
+                               name = name + extension;
+                       }
+                       else {
+                               name = name + "." + extension;
+                       }
+                       return new File(name);
+               }
+               
+               return original;
+       }
+       
        
        /**
         * Confirm that it is allowed to write to a file.  If the file exists,