6a8216ff0863497dc2a2deea5f1bf9ea00545a54
[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: MEDIUM: 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         
47
48
49         private FileHelper() {
50                 // Prevent instantiation
51         }
52         
53         /**
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.
56          * 
57          * @param original              the original file
58          * @param extension             the extension to append if none exists (without preceding dot)
59          * @return                              the resulting filen
60          */
61         public static File ensureExtension(File original, String extension) {
62                 
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);
68                 }
69                 
70                 return original;
71         }
72         
73         
74         /**
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.
77          * 
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.
81          */
82         public static boolean confirmWrite(File file, Component parent) {
83                 if (file.exists()) {
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");
90                                 return false;
91                         }
92                         log.user(1, "User decided to overwrite the file");
93                 }
94                 return true;
95         }
96         
97         
98         /**
99          * Display an error message to the user that writing a file failed.
100          * 
101          * @param e                     the I/O exception that caused the error.
102          * @param parent        the parent component for the dialog.
103          */
104         public static void errorWriting(IOException e, Component parent) {
105                 
106                 log.warn(1, "Error writing to file", e);
107                 JOptionPane.showMessageDialog(parent,
108                                 new Object[] {
109                                                 trans.get("error.writing.desc"),
110                                                 e.getLocalizedMessage()
111                                 }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);
112                 
113         }
114         
115 }