1 package net.sf.openrocket.gui.util;
3 import java.awt.Component;
5 import java.io.IOException;
7 import javax.swing.JOptionPane;
8 import javax.swing.filechooser.FileFilter;
10 import net.sf.openrocket.l10n.L10N;
11 import net.sf.openrocket.l10n.Translator;
12 import net.sf.openrocket.logging.LogHelper;
13 import net.sf.openrocket.startup.Application;
16 * Helper methods related to user-initiated file manipulation.
18 * These methods log the necessary information to the debug log.
20 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
22 public final class FileHelper {
23 private static final LogHelper log = Application.getLogger();
24 private static final Translator trans = Application.getTranslator();
27 // TODO: HIGH: Rename translation keys
29 /** File filter for any rocket designs (*.ork, *.rkt) */
30 public static final FileFilter ALL_DESIGNS_FILTER =
31 new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter1"),
32 ".ork", ".ork.gz", ".rkt", ".rkt.gz");
34 /** File filter for OpenRocket designs (*.ork) */
35 public static final FileFilter OPENROCKET_DESIGN_FILTER =
36 new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter2"), ".ork", ".ork.gz");
38 /** File filter for RockSim designs (*.rkt) */
39 public static final FileFilter ROCKSIM_DESIGN_FILTER =
40 new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter3"), ".rkt", ".rkt.gz");
42 /** File filter for PDF files (*.pdf) */
43 public static final FileFilter PDF_FILTER =
44 new SimpleFileFilter(trans.get("filetypes.pdf"), ".pdf");
46 /** File filter for CSV files (*.csv) */
47 public static final FileFilter CSV_FILE_FILTER =
48 new SimpleFileFilter(trans.get("SimExpPan.desc"), ".csv");
50 /** File filter for BMP files (*.bmp) */
51 public static final FileFilter BMP_FILE_FILTER =
52 new SimpleFileFilter(trans.get("CustomFinImport.filter"), ".bmp");
58 private FileHelper() {
59 // Prevent instantiation
63 // public FileFilter getImageFileFilter() {
64 // String[] extensions = ImageIO.getReaderFileSuffixes();
70 * Ensure that the provided file has a file extension. If the file does not have
71 * any extension, append the provided extension to it.
73 * @param original the original file
74 * @param extension the extension to append if none exists (without preceding dot)
75 * @return the resulting file
77 public static File ensureExtension(File original, String extension) {
79 if (original.getName().indexOf('.') < 0) {
80 log.debug(1, "File name does not contain extension, adding '" + extension + "'");
81 String name = original.getAbsolutePath();
82 name = name + "." + extension;
83 return new File(name);
90 * Ensure that the provided file has the given file extension. This differs from ensureExtension in that this
91 * method guarantees that the file will have the extension, whereas ensureExtension only treats the extension
94 * @param original the original file
95 * @param extension the extension to guarantee (without preceding dot)
96 * @return the resulting file
98 public static File forceExtension(File original, String extension) {
100 if (!original.getName().toLowerCase().endsWith(extension.toLowerCase())) {
101 log.debug(1, "File name does not contain extension, adding '" + extension + "'");
102 String name = original.getAbsolutePath();
103 if (extension.startsWith(".")) {
104 name = name + extension;
107 name = name + "." + extension;
109 return new File(name);
117 * Confirm that it is allowed to write to a file. If the file exists,
118 * a confirmation dialog will be presented to the user to ensure overwriting is ok.
120 * @param file the file that is going to be written.
121 * @param parent the parent component for the dialog.
122 * @return <code>true</code> to write, <code>false</code> to abort.
124 public static boolean confirmWrite(File file, Component parent) {
126 log.info(1, "File " + file + " exists, confirming overwrite from user");
127 int result = JOptionPane.showConfirmDialog(parent,
128 L10N.replace(trans.get("error.fileExists.desc"), "{filename}", file.getName()),
129 trans.get("error.fileExists.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
130 if (result != JOptionPane.YES_OPTION) {
131 log.user(1, "User decided not to overwrite the file");
134 log.user(1, "User decided to overwrite the file");
141 * Display an error message to the user that writing a file failed.
143 * @param e the I/O exception that caused the error.
144 * @param parent the parent component for the dialog.
146 public static void errorWriting(IOException e, Component parent) {
148 log.warn(1, "Error writing to file", e);
149 JOptionPane.showMessageDialog(parent,
151 trans.get("error.writing.desc"),
152 e.getLocalizedMessage()
153 }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);