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