1 package net.sf.openrocket.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: MEDIUM: 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");
49 private FileHelper() {
50 // Prevent instantiation
54 * Ensure that the provided file has a file extension. If the file does not have
55 * any extension, append the provided extension to it.
57 * @param original the original file
58 * @param extension the extension to append if none exists (without preceding dot)
59 * @return the resulting filen
61 public static File ensureExtension(File original, String extension) {
63 if (original.getName().indexOf('.') < 0) {
64 log.debug(1, "File name does not contain extension, adding '" + extension + "'");
65 String name = original.getAbsolutePath();
66 name = name + "." + extension;
67 return new File(name);
75 * Confirm that it is allowed to write to a file. If the file exists,
76 * a confirmation dialog will be presented to the user to ensure overwriting is ok.
78 * @param file the file that is going to be written.
79 * @param parent the parent component for the dialog.
80 * @return <code>true</code> to write, <code>false</code> to abort.
82 public static boolean confirmWrite(File file, Component parent) {
84 log.info(1, "File " + file + " exists, confirming overwrite from user");
85 int result = JOptionPane.showConfirmDialog(parent,
86 L10N.replace(trans.get("error.fileExists.desc"), "{filename}", file.getName()),
87 trans.get("error.fileExists.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
88 if (result != JOptionPane.YES_OPTION) {
89 log.user(1, "User decided not to overwrite the file");
92 log.user(1, "User decided to overwrite the file");
99 * Display an error message to the user that writing a file failed.
101 * @param e the I/O exception that caused the error.
102 * @param parent the parent component for the dialog.
104 public static void errorWriting(IOException e, Component parent) {
106 log.warn(1, "Error writing to file", e);
107 JOptionPane.showMessageDialog(parent,
109 trans.get("error.writing.desc"),
110 e.getLocalizedMessage()
111 }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);