Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / core / src / net / sf / openrocket / gui / util / FileHelper.java
index 860379ed48bdb4f962cb973e7bdc9f33929eaac4..7fcdc2a4c0c337d42a80f9cb4ef4b5ad7e344372 100644 (file)
@@ -1,69 +1,73 @@
 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.imageio.ImageIO;
+import javax.swing.*;
+import javax.swing.filechooser.FileFilter;
+import java.awt.*;
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Locale;
+
 /**
  * Helper methods related to user-initiated file manipulation.
  * <p>
  * These methods log the necessary information to the debug log.
-* 
+*
  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
  */
 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) */
        public static final FileFilter ALL_DESIGNS_FILTER =
                        new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter1"),
                                        ".ork", ".ork.gz", ".rkt", ".rkt.gz");
-       
+
        /** File filter for OpenRocket designs (*.ork) */
        public static final FileFilter OPENROCKET_DESIGN_FILTER =
                        new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter2"), ".ork", ".ork.gz");
-       
+
        /** File filter for RockSim designs (*.rkt) */
        public static final FileFilter ROCKSIM_DESIGN_FILTER =
                        new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter3"), ".rkt", ".rkt.gz");
-       
+
+       /** File filter for OpenRocket components and presets (*.orc) */
+       public static final FileFilter OPEN_ROCKET_COMPONENT_FILTER =
+                       new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter4"), ".orc", ".orc.gz");
+
        /** File filter for PDF files (*.pdf) */
        public static final FileFilter PDF_FILTER =
                        new SimpleFileFilter(trans.get("filetypes.pdf"), ".pdf");
-       
+
        /** File filter for CSV files (*.csv) */
        public static final FileFilter CSV_FILE_FILTER =
                        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();
+                       extensions[i] = extensions[i].toLowerCase(Locale.ENGLISH);
                }
                Arrays.sort(extensions);
-               
+
                StringBuilder sb = new StringBuilder();
                sb.append(trans.get("filetypes.images"));
                sb.append(" (");
@@ -74,31 +78,31 @@ public final class FileHelper {
                        }
                }
                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 file
         */
        public static File ensureExtension(File original, String extension) {
-               
+
                if (original.getName().indexOf('.') < 0) {
                        log.debug(1, "File name does not contain extension, adding '" + extension + "'");
                        String name = original.getAbsolutePath();
                        name = name + "." + extension;
                        return new File(name);
                }
-               
+
                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
@@ -109,8 +113,8 @@ public final class FileHelper {
         * @return                              the resulting file
         */
        public static File forceExtension(File original, String extension) {
-               
-               if (!original.getName().toLowerCase().endsWith(extension.toLowerCase())) {
+
+               if (!original.getName().toLowerCase(Locale.ENGLISH).endsWith(extension.toLowerCase(Locale.ENGLISH))) {
                        log.debug(1, "File name does not contain extension, adding '" + extension + "'");
                        String name = original.getAbsolutePath();
                        if (extension.startsWith(".")) {
@@ -121,15 +125,15 @@ public final class FileHelper {
                        }
                        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.
-        * 
+        *
         * @param file          the file that is going to be written.
         * @param parent        the parent component for the dialog.
         * @return                      <code>true</code> to write, <code>false</code> to abort.
@@ -148,23 +152,23 @@ public final class FileHelper {
                }
                return true;
        }
-       
-       
+
+
        /**
         * Display an error message to the user that writing a file failed.
-        * 
+        *
         * @param e                     the I/O exception that caused the error.
         * @param parent        the parent component for the dialog.
         */
        public static void errorWriting(IOException e, Component parent) {
-               
+
                log.warn(1, "Error writing to file", e);
                JOptionPane.showMessageDialog(parent,
                                new Object[] {
                                                trans.get("error.writing.desc"),
                                                e.getLocalizedMessage()
                                }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);
-               
+
        }
-       
+
 }