Fin import updates
[debian/openrocket] / core / src / net / sf / openrocket / gui / util / FileHelper.java
index 2f7623d7b42b4164d83b99ebace51d83f6a9c87d..860379ed48bdb4f962cb973e7bdc9f33929eaac4 100644 (file)
@@ -1,16 +1,19 @@
 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;
+
 import net.sf.openrocket.l10n.L10N;
 import net.sf.openrocket.l10n.Translator;
 import net.sf.openrocket.logging.LogHelper;
 import net.sf.openrocket.startup.Application;
 
-import javax.swing.*;
-import javax.swing.filechooser.FileFilter;
-import java.awt.*;
-import java.io.File;
-import java.io.IOException;
-
 /**
  * Helper methods related to user-initiated file manipulation.
  * <p>
@@ -22,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) */
@@ -47,13 +50,35 @@ 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.
@@ -76,31 +101,31 @@ public final class FileHelper {
        
        /**
         * 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.
+        * 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);
+                       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,
         * a confirmation dialog will be presented to the user to ensure overwriting is ok.