Fin import updates
[debian/openrocket] / core / src / net / sf / openrocket / gui / util / FileHelper.java
1 package net.sf.openrocket.gui.util;
2
3 import java.awt.Component;
4 import java.io.File;
5 import java.io.IOException;
6 import java.util.Arrays;
7
8 import javax.imageio.ImageIO;
9 import javax.swing.JOptionPane;
10 import javax.swing.filechooser.FileFilter;
11
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;
16
17 /**
18  * Helper methods related to user-initiated file manipulation.
19  * <p>
20  * These methods log the necessary information to the debug log.
21
22  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
23  */
24 public final class FileHelper {
25         private static final LogHelper log = Application.getLogger();
26         private static final Translator trans = Application.getTranslator();
27         
28         
29         // TODO: HIGH: Rename translation keys
30         
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");
35         
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");
39         
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");
43         
44         /** File filter for PDF files (*.pdf) */
45         public static final FileFilter PDF_FILTER =
46                         new SimpleFileFilter(trans.get("filetypes.pdf"), ".pdf");
47         
48         /** File filter for CSV files (*.csv) */
49         public static final FileFilter CSV_FILE_FILTER =
50                         new SimpleFileFilter(trans.get("SimExpPan.desc"), ".csv");
51         
52         
53         
54         
55         private FileHelper() {
56                 // Prevent instantiation
57         }
58         
59         
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();
64                 }
65                 Arrays.sort(extensions);
66                 
67                 StringBuilder sb = new StringBuilder();
68                 sb.append(trans.get("filetypes.images"));
69                 sb.append(" (");
70                 for (int i = 0; i < extensions.length; i++) {
71                         sb.append("*.").append(extensions[i]);
72                         if (i < extensions.length - 1) {
73                                 sb.append("; ");
74                         }
75                 }
76                 sb.append(")");
77                 
78                 return new SimpleFileFilter(sb.toString(), extensions);
79         }
80         
81         
82         /**
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.
85          * 
86          * @param original              the original file
87          * @param extension             the extension to append if none exists (without preceding dot)
88          * @return                              the resulting file
89          */
90         public static File ensureExtension(File original, String extension) {
91                 
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);
97                 }
98                 
99                 return original;
100         }
101         
102         /**
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
105          * as a default.
106          *
107          * @param original              the original file
108          * @param extension             the extension to guarantee (without preceding dot)
109          * @return                              the resulting file
110          */
111         public static File forceExtension(File original, String extension) {
112                 
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;
118                         }
119                         else {
120                                 name = name + "." + extension;
121                         }
122                         return new File(name);
123                 }
124                 
125                 return original;
126         }
127         
128         
129         /**
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.
132          * 
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.
136          */
137         public static boolean confirmWrite(File file, Component parent) {
138                 if (file.exists()) {
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");
145                                 return false;
146                         }
147                         log.user(1, "User decided to overwrite the file");
148                 }
149                 return true;
150         }
151         
152         
153         /**
154          * Display an error message to the user that writing a file failed.
155          * 
156          * @param e                     the I/O exception that caused the error.
157          * @param parent        the parent component for the dialog.
158          */
159         public static void errorWriting(IOException e, Component parent) {
160                 
161                 log.warn(1, "Error writing to file", e);
162                 JOptionPane.showMessageDialog(parent,
163                                 new Object[] {
164                                                 trans.get("error.writing.desc"),
165                                                 e.getLocalizedMessage()
166                                 }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);
167                 
168         }
169         
170 }