DGP - Changes to file filtering, extensions, etc. for Save and Save As.
[debian/openrocket] / core / src / net / sf / openrocket / gui / util / FileHelper.java
1 package net.sf.openrocket.gui.util;
2
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;
7
8 import javax.swing.*;
9 import javax.swing.filechooser.FileFilter;
10 import java.awt.*;
11 import java.io.File;
12 import java.io.IOException;
13
14 /**
15  * Helper methods related to user-initiated file manipulation.
16  * <p>
17  * These methods log the necessary information to the debug log.
18
19  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
20  */
21 public final class FileHelper {
22         private static final LogHelper log = Application.getLogger();
23         private static final Translator trans = Application.getTranslator();
24         
25
26         // TODO: HIGH: Rename translation keys
27         
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");
32         
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");
36         
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");
40         
41         /** File filter for PDF files (*.pdf) */
42         public static final FileFilter PDF_FILTER =
43                         new SimpleFileFilter(trans.get("filetypes.pdf"), ".pdf");
44         
45         /** File filter for CSV files (*.csv) */
46         public static final FileFilter CSV_FILE_FILTER =
47                         new SimpleFileFilter(trans.get("SimExpPan.desc"), ".csv");
48         
49         
50
51
52
53         private FileHelper() {
54                 // Prevent instantiation
55         }
56         
57         /**
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.
60          * 
61          * @param original              the original file
62          * @param extension             the extension to append if none exists (without preceding dot)
63          * @return                              the resulting file
64          */
65         public static File ensureExtension(File original, String extension) {
66                 
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);
72                 }
73                 
74                 return original;
75         }
76         
77         /**
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
80      * as a default.
81          *
82          * @param original              the original file
83          * @param extension             the extension to guarantee (without preceding dot)
84          * @return                              the resulting file
85          */
86         public static File forceExtension(File original, String extension) {
87
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;
93             }
94             else {
95                         name = name + "." + extension;
96             }
97             return new File(name);
98                 }
99
100                 return original;
101         }
102
103
104         /**
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.
107          * 
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.
111          */
112         public static boolean confirmWrite(File file, Component parent) {
113                 if (file.exists()) {
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");
120                                 return false;
121                         }
122                         log.user(1, "User decided to overwrite the file");
123                 }
124                 return true;
125         }
126         
127         
128         /**
129          * Display an error message to the user that writing a file failed.
130          * 
131          * @param e                     the I/O exception that caused the error.
132          * @param parent        the parent component for the dialog.
133          */
134         public static void errorWriting(IOException e, Component parent) {
135                 
136                 log.warn(1, "Error writing to file", e);
137                 JOptionPane.showMessageDialog(parent,
138                                 new Object[] {
139                                                 trans.get("error.writing.desc"),
140                                                 e.getLocalizedMessage()
141                                 }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);
142                 
143         }
144         
145 }