create changelog entry
[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.imageio.ImageIO;
9 import javax.swing.*;
10 import javax.swing.filechooser.FileFilter;
11 import java.awt.*;
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.Arrays;
15 import java.util.Locale;
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 OpenRocket components and presets (*.orc) */
45         public static final FileFilter OPEN_ROCKET_COMPONENT_FILTER =
46                         new SimpleFileFilter(trans.get("BasicFrame.SimpleFileFilter4"), ".orc", ".orc.gz");
47
48         /** File filter for PDF files (*.pdf) */
49         public static final FileFilter PDF_FILTER =
50                         new SimpleFileFilter(trans.get("filetypes.pdf"), ".pdf");
51
52         /** File filter for CSV files (*.csv) */
53         public static final FileFilter CSV_FILE_FILTER =
54                         new SimpleFileFilter(trans.get("SimExpPan.desc"), ".csv");
55
56
57
58
59         private FileHelper() {
60                 // Prevent instantiation
61         }
62
63
64         public static FileFilter getImageFileFilter() {
65                 String[] extensions = ImageIO.getReaderFileSuffixes();
66                 for (int i = 0; i < extensions.length; i++) {
67                         extensions[i] = extensions[i].toLowerCase(Locale.ENGLISH);
68                 }
69                 Arrays.sort(extensions);
70
71                 StringBuilder sb = new StringBuilder();
72                 sb.append(trans.get("filetypes.images"));
73                 sb.append(" (");
74                 for (int i = 0; i < extensions.length; i++) {
75                         sb.append("*.").append(extensions[i]);
76                         if (i < extensions.length - 1) {
77                                 sb.append("; ");
78                         }
79                 }
80                 sb.append(")");
81
82                 return new SimpleFileFilter(sb.toString(), extensions);
83         }
84
85
86         /**
87          * Ensure that the provided file has a file extension.  If the file does not have
88          * any extension, append the provided extension to it.
89          *
90          * @param original              the original file
91          * @param extension             the extension to append if none exists (without preceding dot)
92          * @return                              the resulting file
93          */
94         public static File ensureExtension(File original, String extension) {
95
96                 if (original.getName().indexOf('.') < 0) {
97                         log.debug(1, "File name does not contain extension, adding '" + extension + "'");
98                         String name = original.getAbsolutePath();
99                         name = name + "." + extension;
100                         return new File(name);
101                 }
102
103                 return original;
104         }
105
106         /**
107          * Ensure that the provided file has the given file extension.  This differs from ensureExtension in that this
108          * method guarantees that the file will have the extension, whereas ensureExtension only treats the extension
109          * as a default.
110          *
111          * @param original              the original file
112          * @param extension             the extension to guarantee (without preceding dot)
113          * @return                              the resulting file
114          */
115         public static File forceExtension(File original, String extension) {
116
117                 if (!original.getName().toLowerCase(Locale.ENGLISH).endsWith(extension.toLowerCase(Locale.ENGLISH))) {
118                         log.debug(1, "File name does not contain extension, adding '" + extension + "'");
119                         String name = original.getAbsolutePath();
120                         if (extension.startsWith(".")) {
121                                 name = name + extension;
122                         }
123                         else {
124                                 name = name + "." + extension;
125                         }
126                         return new File(name);
127                 }
128
129                 return original;
130         }
131
132
133         /**
134          * Confirm that it is allowed to write to a file.  If the file exists,
135          * a confirmation dialog will be presented to the user to ensure overwriting is ok.
136          *
137          * @param file          the file that is going to be written.
138          * @param parent        the parent component for the dialog.
139          * @return                      <code>true</code> to write, <code>false</code> to abort.
140          */
141         public static boolean confirmWrite(File file, Component parent) {
142                 if (file.exists()) {
143                         log.info(1, "File " + file + " exists, confirming overwrite from user");
144                         int result = JOptionPane.showConfirmDialog(parent,
145                                         L10N.replace(trans.get("error.fileExists.desc"), "{filename}", file.getName()),
146                                         trans.get("error.fileExists.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
147                         if (result != JOptionPane.YES_OPTION) {
148                                 log.user(1, "User decided not to overwrite the file");
149                                 return false;
150                         }
151                         log.user(1, "User decided to overwrite the file");
152                 }
153                 return true;
154         }
155
156
157         /**
158          * Display an error message to the user that writing a file failed.
159          *
160          * @param e                     the I/O exception that caused the error.
161          * @param parent        the parent component for the dialog.
162          */
163         public static void errorWriting(IOException e, Component parent) {
164
165                 log.warn(1, "Error writing to file", e);
166                 JOptionPane.showMessageDialog(parent,
167                                 new Object[] {
168                                                 trans.get("error.writing.desc"),
169                                                 e.getLocalizedMessage()
170                                 }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);
171
172         }
173
174 }