0929c3280a16bf847553a0a3341c9b377aa921b3
[debian/openrocket] / src / net / sf / openrocket / util / FileHelper.java
1 package net.sf.openrocket.util;
2
3 import java.awt.Component;
4 import java.io.File;
5 import java.io.IOException;
6
7 import javax.swing.JOptionPane;
8 import javax.swing.filechooser.FileFilter;
9
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;
14
15 /**
16  * Helper methods related to user-initiated file manipulation.
17  * <p>
18  * These methods log the necessary information to the debug log.
19
20  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
21  */
22 public final class FileHelper {
23         private static final LogHelper log = Application.getLogger();
24         private static final Translator trans = Application.getTranslator();
25         
26
27         // TODO: HIGH: Rename translation keys
28         
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");
33         
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");
37         
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");
41         
42         /** File filter for PDF files (*.pdf) */
43         public static final FileFilter PDF_FILTER =
44                         new SimpleFileFilter(trans.get("filetypes.pdf"), ".pdf");
45         
46         /** File filter for CSV files (*.csv) */
47         public static final FileFilter CSV_FILE_FILTER =
48                         new SimpleFileFilter(trans.get("SimExpPan.desc"), ".csv");
49         
50         
51
52
53
54         private FileHelper() {
55                 // Prevent instantiation
56         }
57         
58         /**
59          * Ensure that the provided file has a file extension.  If the file does not have
60          * any extension, append the provided extension to it.
61          * 
62          * @param original              the original file
63          * @param extension             the extension to append if none exists (without preceding dot)
64          * @return                              the resulting filen
65          */
66         public static File ensureExtension(File original, String extension) {
67                 
68                 if (original.getName().indexOf('.') < 0) {
69                         log.debug(1, "File name does not contain extension, adding '" + extension + "'");
70                         String name = original.getAbsolutePath();
71                         name = name + "." + extension;
72                         return new File(name);
73                 }
74                 
75                 return original;
76         }
77         
78         
79         /**
80          * Confirm that it is allowed to write to a file.  If the file exists,
81          * a confirmation dialog will be presented to the user to ensure overwriting is ok.
82          * 
83          * @param file          the file that is going to be written.
84          * @param parent        the parent component for the dialog.
85          * @return                      <code>true</code> to write, <code>false</code> to abort.
86          */
87         public static boolean confirmWrite(File file, Component parent) {
88                 if (file.exists()) {
89                         log.info(1, "File " + file + " exists, confirming overwrite from user");
90                         int result = JOptionPane.showConfirmDialog(parent,
91                                         L10N.replace(trans.get("error.fileExists.desc"), "{filename}", file.getName()),
92                                         trans.get("error.fileExists.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
93                         if (result != JOptionPane.YES_OPTION) {
94                                 log.user(1, "User decided not to overwrite the file");
95                                 return false;
96                         }
97                         log.user(1, "User decided to overwrite the file");
98                 }
99                 return true;
100         }
101         
102         
103         /**
104          * Display an error message to the user that writing a file failed.
105          * 
106          * @param e                     the I/O exception that caused the error.
107          * @param parent        the parent component for the dialog.
108          */
109         public static void errorWriting(IOException e, Component parent) {
110                 
111                 log.warn(1, "Error writing to file", e);
112                 JOptionPane.showMessageDialog(parent,
113                                 new Object[] {
114                                                 trans.get("error.writing.desc"),
115                                                 e.getLocalizedMessage()
116                                 }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);
117                 
118         }
119         
120 }