enable the localization on some strings
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / bridge / filechooser / SwingScilabExportFileChooser.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2008 - INRIA - Sylvestre Koumar
4  * 
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at    
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12 package org.scilab.modules.gui.bridge.filechooser;
13
14 import java.io.File;
15
16 import javax.swing.BorderFactory;
17 import javax.swing.ImageIcon;
18 import javax.swing.JFileChooser;
19 import javax.swing.JFrame;
20 import javax.swing.JOptionPane;
21 import javax.swing.JPanel;
22 import javax.swing.border.TitledBorder;
23 import javax.swing.filechooser.FileFilter;
24
25 import org.scilab.modules.localization.Messages;
26
27 import org.scilab.modules.action_binding.InterpreterManagement;
28
29 /**
30  * This is the son of the usual Scilab file chooser,
31  * it have been customized for the graphic export
32  * @author Sylvestre Koumar
33  *
34  */
35 public class SwingScilabExportFileChooser extends SwingScilabFileChooser {
36         
37         private final String bmpDesc = Messages.gettext("Windows BMP image");
38         private final String gifDesc = Messages.gettext("GIF image");
39         private final String jpgDesc = Messages.gettext("JPEG image");
40         private final String pngDesc = Messages.gettext("PNG image");
41         private final String ppmDesc = Messages.gettext("PPM image");
42         private final String emfDesc = Messages.gettext("Enhanced Metafile image (EMF)");
43         private final String epsDesc = Messages.gettext("Encapsulated PostScript image (EPS)");
44         private final String figDesc = Messages.gettext("FIG image");
45         private final String pdfDesc = Messages.gettext("PDF image");
46         private final String svgDesc = Messages.gettext("SVG image");
47         private final String allFilesDesc = Messages.gettext("All files");
48         
49         private final String bmp = "bmp";
50         private final String gif = "gif";
51         private final String jpg = "jpg";
52         private final String png = "png";
53         private final String ppm = "ppm";
54         private final String emf = "emf";
55         private final String eps = "eps";
56         private final String fig = "fig";
57         private final String pdf = "pdf";
58         private final String svg = "svg";
59         private final String byExtension = "*";
60         
61         private String exportName;      
62         private String extensionSelected;
63         private int figureId;
64         
65         //Format description & format name      
66         private String [][] data = {
67                         {bmpDesc, bmp},
68                         {gifDesc, gif},
69                         {jpgDesc, jpg},
70                         {pngDesc, png},
71                         {ppmDesc, ppm},
72                         {emfDesc, emf},
73                         {epsDesc, eps},
74                         {figDesc, fig},
75                         {pdfDesc, pdf},
76                         {svgDesc, svg},
77                         {byExtensionDesc, byExtension} // should always be at the last position
78         };
79         
80         /**
81          * Default constructor
82          * @param figureId id of the exported figure
83          */
84         public SwingScilabExportFileChooser(int figureId) {
85                 super();
86                 this.figureId = figureId;
87                 exportCustomFileChooser(figureId);
88         }
89         
90         /**
91          * We customize the file chooser for the graphic export 
92          * by adding format selection
93          * @param figureId exported figure number
94          */
95         public void exportCustomFileChooser(int figureId) {             
96
97                 super.setDialogTitle(Messages.gettext("Export"));
98                 super.setApproveButtonText(Messages.gettext("Export"));
99                 File exportFile = new File(Messages.gettext("Untitled-export"));
100                 super.setSelectedFile(exportFile);
101                 super.setAcceptAllFileFilterUsed(false);                
102                 
103                 //image filter
104                 FileMask[] fm = new FileMask[data.length];
105                 
106                 this.figureId = figureId;
107                 
108                 for (int i = 0; i < data.length; i++) {
109                         fm[i] = new FileMask(data[i][1].toString(), data[i][0].toString());
110                 }
111                 
112                 for (int i = 0; i < fm.length; i++) {
113                         super.addChoosableFileFilter(fm[i]);
114                 }                               
115                                 
116                 fm[fm.length-1].clearExtensions();              
117                         
118
119         //Preview panel 
120                 JPanel panelPreview = new JPanel();
121                 
122         //Title for preview panel
123         TitledBorder titlePreview;
124         titlePreview = BorderFactory.createTitledBorder(Messages.gettext("Preview"));
125         panelPreview.setBorder(titlePreview);
126         
127         //add preview image
128                 panelPreview.add(new ImagePreview(this));               
129                 
130                 //Main panel contains extensionTable panel & preview panel
131                 JPanel accessoryPanel = new JPanel();
132                 
133                 //accessoryPanel.add(scrollPane);               
134                 accessoryPanel.add(panelPreview);
135                 accessoryPanel.setVisible(true);        
136                 super.setAccessory(accessoryPanel);
137                 
138                 
139                 JFrame frame = new JFrame();    
140                 frame.setIconImage(new ImageIcon(System.getenv("SCI") + "/modules/gui/images/icons/scilab.png").getImage());
141                 int selection = super.showSaveDialog(frame);            
142                 
143                 if (selection == JFileChooser.APPROVE_OPTION) {         
144                         javax.swing.filechooser.FileFilter ft = super.getFileFilter();
145                         exportName = super.getSelectedFile().getAbsolutePath();
146                         
147                         //Test if there is a file with the same name
148                         if (new File(this.exportName).exists()) {
149                                 int actionDialog = JOptionPane.showConfirmDialog(this, Messages.gettext("Replace existing file?"), Messages.gettext("File already exists"), JOptionPane.YES_NO_OPTION);
150
151                                 if(actionDialog == JOptionPane.YES_OPTION) {
152
153                                 } else {
154                                         return;
155                                 }
156                         }
157                         
158                         
159                         extensionSelected = ft.getDescription();
160                         
161                         //get the extension from the description of the combobox
162                         String extensionCombo = extensionSelected.subSequence(extensionSelected.indexOf(".") + 1, extensionSelected.length() - 1).toString().toLowerCase();
163                         
164                         if (extensionCombo.equals(byExtension)) {                               
165                                 exportManager();        
166                                 
167                         } else if(extensionCombo.equals(emf) || extensionCombo.equals(eps) || extensionCombo.equals(fig) 
168                                         || extensionCombo.equals(pdf) || extensionCombo.equals(svg)){           
169                                 vectorialExport(extensionCombo);
170                                 
171                         } else {                                
172                                 bitmapExport(extensionCombo);                           
173                         }
174                 } else ; // no file chosen              
175                 
176         }
177         
178         
179         /**
180          * Return the file extension
181          * @param fileName Name of the file
182          * @return the extension
183          */
184         public String getExtension(String fileName) {
185                 if(fileName != null) {
186                         int i = fileName.lastIndexOf('.');
187                         if(i > 0 && i < fileName.length() - 1) {
188                                 return fileName.substring(i + 1).toLowerCase();
189                         };
190                 }
191                 return null;
192         }
193         
194         /**
195          * Manage the export (bitmap/vectorial format) and export errors 
196          */
197         public void exportManager() {
198
199                 //get the extension of the fileName chosen by the user (can be 'null')
200                 String userExtension = getExtension(this.exportName);                   
201                 
202                 if (userExtension == null) {
203                         //fileName without extension + "by extension (.*)" selected
204                         JOptionPane.showMessageDialog(this, Messages.gettext("Please specify a file format"), Messages.gettext("Error on export"), JOptionPane.ERROR_MESSAGE);
205                         return;
206                 } else if (userExtension.equals(bmp)) {
207                         bitmapExport(userExtension);
208
209                 } else if (userExtension.equals(gif)) {                 
210                         bitmapExport(userExtension);                    
211                         
212                 } else if (userExtension.equals(jpg)) {
213                         bitmapExport(userExtension);
214                         
215                 } else if (userExtension.equals(png)) {
216                         bitmapExport(userExtension);
217                         
218                 } else if (userExtension.equals(ppm)) {
219                         bitmapExport(userExtension);
220                 
221                 } else if (userExtension.equals(emf)) {
222                         vectorialExport(userExtension);
223                         
224                 } else if (userExtension.equals(eps)) {
225                         vectorialExport(userExtension);
226                                 
227                 } else if (userExtension.equals(fig)) {
228                         vectorialExport(userExtension);
229                 
230                 } else if (userExtension.equals(pdf)) {
231                         vectorialExport(userExtension);
232                 
233                 } else if (userExtension.equals(svg)) {
234                         vectorialExport(userExtension);
235                         
236                 } else {
237                         //fileName with a wrong extension + "by extension (.*)" selected
238                         JOptionPane.showMessageDialog(this, Messages.gettext("Unrecognized extension '") + userExtension + Messages.gettext("'.\n Please specify a valid file format."), Messages.gettext("Error on export"), JOptionPane.ERROR_MESSAGE);
239                         return;
240                 }               
241         }
242         
243         /**
244          * Export an bitmap file
245          * @param userExtension extension caught by the user
246          */
247         public void bitmapExport(String userExtension) {
248                 ExportData exportData;
249                 String exportcmd;
250                 
251                 exportData = new ExportData(figureId, exportName, userExtension, null);
252                 
253                 //the export instruction for the selected format
254                 exportcmd = "xs2" + exportData.getExportExtension() 
255                 + "(" + figureId + ", '" + exportData.getExportName() + "');";
256                 InterpreterManagement.putCommandInScilabQueue(exportcmd);               
257         }
258         
259         /**
260          * Export an bitmap file
261          * @param userExtension extension caught by the user
262          */
263         public void vectorialExport(String userExtension) {
264                 ExportData exportData = new ExportData(figureId, this.exportName, userExtension, null);
265                 ExportOptionWindow exportOptionWindow = new ExportOptionWindow(exportData);             
266         exportOptionWindow.displayOptionWindow();
267                 exportOptionWindow.landscapePortraitOption();
268         }
269
270 }