1 package net.sf.openrocket.gui.util;
3 import net.sf.openrocket.l10n.L10N;
4 import net.sf.openrocket.l10n.Translator;
5 import net.sf.openrocket.logging.LogHelper;
6 import net.sf.openrocket.startup.Application;
8 import javax.imageio.ImageIO;
10 import javax.swing.filechooser.FileFilter;
13 import java.io.IOException;
14 import java.util.Arrays;
15 import java.util.Locale;
18 * Helper methods related to user-initiated file manipulation.
20 * These methods log the necessary information to the debug log.
22 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
24 public final class FileHelper {
25 private static final LogHelper log = Application.getLogger();
26 private static final Translator trans = Application.getTranslator();
29 // TODO: HIGH: Rename translation keys
31 /** File filter for any rocket designs (*.ork, *.rkt) */
32 public static final FileFilter ALL_DESIGNS_FILTER =
33 new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter1"),
34 ".ork", ".ork.gz", ".rkt", ".rkt.gz");
36 /** File filter for OpenRocket designs (*.ork) */
37 public static final FileFilter OPENROCKET_DESIGN_FILTER =
38 new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter2"), ".ork", ".ork.gz");
40 /** File filter for RockSim designs (*.rkt) */
41 public static final FileFilter ROCKSIM_DESIGN_FILTER =
42 new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter3"), ".rkt", ".rkt.gz");
44 /** File filter for OpenRocket components and presets (*.orc) */
45 public static final FileFilter OPEN_ROCKET_COMPONENT_FILTER =
46 new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter4"), ".orc", ".orc.gz");
48 /** File filter for PDF files (*.pdf) */
49 public static final FileFilter PDF_FILTER =
50 new SimpleFileFilter(trans.get("filetypes.pdf"), ".pdf");
52 /** File filter for CSV files (*.csv) */
53 public static final FileFilter CSV_FILE_FILTER =
54 new SimpleFileFilter(trans.get("SimExpPan.desc"), ".csv");
59 private FileHelper() {
60 // Prevent instantiation
64 public static FileFilter getImageFileFilter() {
65 String[] extensions = ImageIO.getReaderFileSuffixes();
66 for (int i = 0; i < extensions.length; i++) {
67 extensions[i] = extensions[i].toLowerCase(Locale.ENGLISH);
69 Arrays.sort(extensions);
71 StringBuilder sb = new StringBuilder();
72 sb.append(trans.get("filetypes.images"));
74 for (int i = 0; i < extensions.length; i++) {
75 sb.append("*.").append(extensions[i]);
76 if (i < extensions.length - 1) {
82 return new SimpleFileFilter(sb.toString(), extensions);
87 * Ensure that the provided file has a file extension. If the file does not have
88 * any extension, append the provided extension to it.
90 * @param original the original file
91 * @param extension the extension to append if none exists (without preceding dot)
92 * @return the resulting file
94 public static File ensureExtension(File original, String extension) {
96 if (original.getName().indexOf('.') < 0) {
97 log.debug(1, "File name does not contain extension, adding '" + extension + "'");
98 String name = original.getAbsolutePath();
99 name = name + "." + extension;
100 return new File(name);
107 * Ensure that the provided file has the given file extension. This differs from ensureExtension in that this
108 * method guarantees that the file will have the extension, whereas ensureExtension only treats the extension
111 * @param original the original file
112 * @param extension the extension to guarantee (without preceding dot)
113 * @return the resulting file
115 public static File forceExtension(File original, String extension) {
117 if (!original.getName().toLowerCase(Locale.ENGLISH).endsWith(extension.toLowerCase(Locale.ENGLISH))) {
118 log.debug(1, "File name does not contain extension, adding '" + extension + "'");
119 String name = original.getAbsolutePath();
120 if (extension.startsWith(".")) {
121 name = name + extension;
124 name = name + "." + extension;
126 return new File(name);
134 * Confirm that it is allowed to write to a file. If the file exists,
135 * a confirmation dialog will be presented to the user to ensure overwriting is ok.
137 * @param file the file that is going to be written.
138 * @param parent the parent component for the dialog.
139 * @return <code>true</code> to write, <code>false</code> to abort.
141 public static boolean confirmWrite(File file, Component parent) {
143 log.info(1, "File " + file + " exists, confirming overwrite from user");
144 int result = JOptionPane.showConfirmDialog(parent,
145 L10N.replace(trans.get("error.fileExists.desc"), "{filename}", file.getName()),
146 trans.get("error.fileExists.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
147 if (result != JOptionPane.YES_OPTION) {
148 log.user(1, "User decided not to overwrite the file");
151 log.user(1, "User decided to overwrite the file");
158 * Display an error message to the user that writing a file failed.
160 * @param e the I/O exception that caused the error.
161 * @param parent the parent component for the dialog.
163 public static void errorWriting(IOException e, Component parent) {
165 log.warn(1, "Error writing to file", e);
166 JOptionPane.showMessageDialog(parent,
168 trans.get("error.writing.desc"),
169 e.getLocalizedMessage()
170 }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);