1 package net.sf.openrocket.gui.util;
3 import java.awt.Component;
5 import java.io.IOException;
6 import java.util.Arrays;
8 import javax.imageio.ImageIO;
9 import javax.swing.JOptionPane;
10 import javax.swing.filechooser.FileFilter;
12 import net.sf.openrocket.l10n.L10N;
13 import net.sf.openrocket.l10n.Translator;
14 import net.sf.openrocket.logging.LogHelper;
15 import net.sf.openrocket.startup.Application;
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 PDF files (*.pdf) */
45 public static final FileFilter PDF_FILTER =
46 new SimpleFileFilter(trans.get("filetypes.pdf"), ".pdf");
48 /** File filter for CSV files (*.csv) */
49 public static final FileFilter CSV_FILE_FILTER =
50 new SimpleFileFilter(trans.get("SimExpPan.desc"), ".csv");
55 private FileHelper() {
56 // Prevent instantiation
60 public static FileFilter getImageFileFilter() {
61 String[] extensions = ImageIO.getReaderFileSuffixes();
62 for (int i = 0; i < extensions.length; i++) {
63 extensions[i] = extensions[i].toLowerCase();
65 Arrays.sort(extensions);
67 StringBuilder sb = new StringBuilder();
68 sb.append(trans.get("filetypes.images"));
70 for (int i = 0; i < extensions.length; i++) {
71 sb.append("*.").append(extensions[i]);
72 if (i < extensions.length - 1) {
78 return new SimpleFileFilter(sb.toString(), extensions);
83 * Ensure that the provided file has a file extension. If the file does not have
84 * any extension, append the provided extension to it.
86 * @param original the original file
87 * @param extension the extension to append if none exists (without preceding dot)
88 * @return the resulting file
90 public static File ensureExtension(File original, String extension) {
92 if (original.getName().indexOf('.') < 0) {
93 log.debug(1, "File name does not contain extension, adding '" + extension + "'");
94 String name = original.getAbsolutePath();
95 name = name + "." + extension;
96 return new File(name);
103 * Ensure that the provided file has the given file extension. This differs from ensureExtension in that this
104 * method guarantees that the file will have the extension, whereas ensureExtension only treats the extension
107 * @param original the original file
108 * @param extension the extension to guarantee (without preceding dot)
109 * @return the resulting file
111 public static File forceExtension(File original, String extension) {
113 if (!original.getName().toLowerCase().endsWith(extension.toLowerCase())) {
114 log.debug(1, "File name does not contain extension, adding '" + extension + "'");
115 String name = original.getAbsolutePath();
116 if (extension.startsWith(".")) {
117 name = name + extension;
120 name = name + "." + extension;
122 return new File(name);
130 * Confirm that it is allowed to write to a file. If the file exists,
131 * a confirmation dialog will be presented to the user to ensure overwriting is ok.
133 * @param file the file that is going to be written.
134 * @param parent the parent component for the dialog.
135 * @return <code>true</code> to write, <code>false</code> to abort.
137 public static boolean confirmWrite(File file, Component parent) {
139 log.info(1, "File " + file + " exists, confirming overwrite from user");
140 int result = JOptionPane.showConfirmDialog(parent,
141 L10N.replace(trans.get("error.fileExists.desc"), "{filename}", file.getName()),
142 trans.get("error.fileExists.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
143 if (result != JOptionPane.YES_OPTION) {
144 log.user(1, "User decided not to overwrite the file");
147 log.user(1, "User decided to overwrite the file");
154 * Display an error message to the user that writing a file failed.
156 * @param e the I/O exception that caused the error.
157 * @param parent the parent component for the dialog.
159 public static void errorWriting(IOException e, Component parent) {
161 log.warn(1, "Error writing to file", e);
162 JOptionPane.showMessageDialog(parent,
164 trans.get("error.writing.desc"),
165 e.getLocalizedMessage()
166 }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);