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