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;
9 import javax.swing.filechooser.FileFilter;
12 import java.io.IOException;
15 * Helper methods related to user-initiated file manipulation.
17 * These methods log the necessary information to the debug log.
19 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
21 public final class FileHelper {
22 private static final LogHelper log = Application.getLogger();
23 private static final Translator trans = Application.getTranslator();
26 // TODO: HIGH: Rename translation keys
28 /** File filter for any rocket designs (*.ork, *.rkt) */
29 public static final FileFilter ALL_DESIGNS_FILTER =
30 new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter1"),
31 ".ork", ".ork.gz", ".rkt", ".rkt.gz");
33 /** File filter for OpenRocket designs (*.ork) */
34 public static final FileFilter OPENROCKET_DESIGN_FILTER =
35 new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter2"), ".ork", ".ork.gz");
37 /** File filter for RockSim designs (*.rkt) */
38 public static final FileFilter ROCKSIM_DESIGN_FILTER =
39 new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter3"), ".rkt", ".rkt.gz");
41 /** File filter for PDF files (*.pdf) */
42 public static final FileFilter PDF_FILTER =
43 new SimpleFileFilter(trans.get("filetypes.pdf"), ".pdf");
45 /** File filter for CSV files (*.csv) */
46 public static final FileFilter CSV_FILE_FILTER =
47 new SimpleFileFilter(trans.get("SimExpPan.desc"), ".csv");
53 private FileHelper() {
54 // Prevent instantiation
58 * Ensure that the provided file has a file extension. If the file does not have
59 * any extension, append the provided extension to it.
61 * @param original the original file
62 * @param extension the extension to append if none exists (without preceding dot)
63 * @return the resulting file
65 public static File ensureExtension(File original, String extension) {
67 if (original.getName().indexOf('.') < 0) {
68 log.debug(1, "File name does not contain extension, adding '" + extension + "'");
69 String name = original.getAbsolutePath();
70 name = name + "." + extension;
71 return new File(name);
78 * Ensure that the provided file has the given file extension. This differs from ensureExtension in that this
79 * method guarantees that the file will have the extension, whereas ensureExtension only treats the extension
82 * @param original the original file
83 * @param extension the extension to guarantee (without preceding dot)
84 * @return the resulting file
86 public static File forceExtension(File original, String extension) {
88 if (!original.getName().toLowerCase().endsWith(extension.toLowerCase())) {
89 log.debug(1, "File name does not contain extension, adding '" + extension + "'");
90 String name = original.getAbsolutePath();
91 if (extension.startsWith(".")) {
92 name = name + extension;
95 name = name + "." + extension;
97 return new File(name);
105 * Confirm that it is allowed to write to a file. If the file exists,
106 * a confirmation dialog will be presented to the user to ensure overwriting is ok.
108 * @param file the file that is going to be written.
109 * @param parent the parent component for the dialog.
110 * @return <code>true</code> to write, <code>false</code> to abort.
112 public static boolean confirmWrite(File file, Component parent) {
114 log.info(1, "File " + file + " exists, confirming overwrite from user");
115 int result = JOptionPane.showConfirmDialog(parent,
116 L10N.replace(trans.get("error.fileExists.desc"), "{filename}", file.getName()),
117 trans.get("error.fileExists.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
118 if (result != JOptionPane.YES_OPTION) {
119 log.user(1, "User decided not to overwrite the file");
122 log.user(1, "User decided to overwrite the file");
129 * Display an error message to the user that writing a file failed.
131 * @param e the I/O exception that caused the error.
132 * @param parent the parent component for the dialog.
134 public static void errorWriting(IOException e, Component parent) {
136 log.warn(1, "Error writing to file", e);
137 JOptionPane.showMessageDialog(parent,
139 trans.get("error.writing.desc"),
140 e.getLocalizedMessage()
141 }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);