Importing of image to freeform fin set
[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
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         /** File filter for BMP files (*.bmp) */
51         public static final FileFilter BMP_FILE_FILTER =
52                         new SimpleFileFilter(trans.get("CustomFinImport.filter"), ".bmp");
53         
54         
55         
56         
57         
58         private FileHelper() {
59                 // Prevent instantiation
60         }
61         
62         
63         //      public FileFilter getImageFileFilter() {
64         //              String[] extensions = ImageIO.getReaderFileSuffixes();
65         //              
66         //      }
67         
68         
69         /**
70          * Ensure that the provided file has a file extension.  If the file does not have
71          * any extension, append the provided extension to it.
72          * 
73          * @param original              the original file
74          * @param extension             the extension to append if none exists (without preceding dot)
75          * @return                              the resulting file
76          */
77         public static File ensureExtension(File original, String extension) {
78                 
79                 if (original.getName().indexOf('.') < 0) {
80                         log.debug(1, "File name does not contain extension, adding '" + extension + "'");
81                         String name = original.getAbsolutePath();
82                         name = name + "." + extension;
83                         return new File(name);
84                 }
85                 
86                 return original;
87         }
88         
89         /**
90          * Ensure that the provided file has the given file extension.  This differs from ensureExtension in that this
91          * method guarantees that the file will have the extension, whereas ensureExtension only treats the extension
92          * as a default.
93          *
94          * @param original              the original file
95          * @param extension             the extension to guarantee (without preceding dot)
96          * @return                              the resulting file
97          */
98         public static File forceExtension(File original, String extension) {
99                 
100                 if (!original.getName().toLowerCase().endsWith(extension.toLowerCase())) {
101                         log.debug(1, "File name does not contain extension, adding '" + extension + "'");
102                         String name = original.getAbsolutePath();
103                         if (extension.startsWith(".")) {
104                                 name = name + extension;
105                         }
106                         else {
107                                 name = name + "." + extension;
108                         }
109                         return new File(name);
110                 }
111                 
112                 return original;
113         }
114         
115         
116         /**
117          * Confirm that it is allowed to write to a file.  If the file exists,
118          * a confirmation dialog will be presented to the user to ensure overwriting is ok.
119          * 
120          * @param file          the file that is going to be written.
121          * @param parent        the parent component for the dialog.
122          * @return                      <code>true</code> to write, <code>false</code> to abort.
123          */
124         public static boolean confirmWrite(File file, Component parent) {
125                 if (file.exists()) {
126                         log.info(1, "File " + file + " exists, confirming overwrite from user");
127                         int result = JOptionPane.showConfirmDialog(parent,
128                                         L10N.replace(trans.get("error.fileExists.desc"), "{filename}", file.getName()),
129                                         trans.get("error.fileExists.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
130                         if (result != JOptionPane.YES_OPTION) {
131                                 log.user(1, "User decided not to overwrite the file");
132                                 return false;
133                         }
134                         log.user(1, "User decided to overwrite the file");
135                 }
136                 return true;
137         }
138         
139         
140         /**
141          * Display an error message to the user that writing a file failed.
142          * 
143          * @param e                     the I/O exception that caused the error.
144          * @param parent        the parent component for the dialog.
145          */
146         public static void errorWriting(IOException e, Component parent) {
147                 
148                 log.warn(1, "Error writing to file", e);
149                 JOptionPane.showMessageDialog(parent,
150                                 new Object[] {
151                                                 trans.get("error.writing.desc"),
152                                                 e.getLocalizedMessage()
153                                 }, trans.get("error.writing.title"), JOptionPane.ERROR_MESSAGE);
154                 
155         }
156         
157 }