enable the localization on some strings
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / bridge / filechooser / SwingScilabFileChooser.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Vincent Couvert
4  * Copyright (C) 2008 - DIGITEO - Sylvestre KOUMAR
5  * 
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at    
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13
14 package org.scilab.modules.gui.bridge.filechooser;
15
16 import java.io.File;
17 import java.util.StringTokenizer;
18
19 import javax.swing.ImageIcon;
20 import javax.swing.JFileChooser;
21 import javax.swing.JFrame;
22 import javax.swing.JOptionPane;
23
24 import org.scilab.modules.localization.Messages;
25
26 import org.scilab.modules.gui.filechooser.FileChooserInfos;
27 import org.scilab.modules.gui.filechooser.SimpleFileChooser;
28 import org.scilab.modules.gui.utils.SciFileFilter;
29
30 /**
31  * Swing implementation of a Scilab File ChooserS
32  * @author Vincent COUVERT
33  * @author Sylvestre KOUMAR
34  */
35 public class SwingScilabFileChooser extends JFileChooser implements SimpleFileChooser {
36         
37         private static final long serialVersionUID = 1L;
38         
39         private String[] selection;     // Path + filenames
40         private String selectionPath; // Path
41         private String[] selectionFileNames; // Filenames       
42         private int selectionSize;      
43         private int filterIndex;
44         private int maskSize;
45         private int dialogType;
46         
47         private String[] maskDescription;       
48
49         /**
50          * Default constructor
51          */
52         public SwingScilabFileChooser() {
53                 super();
54                 //System.out.println("[Constructor] SwingScilabFileChooser");
55                 /** Bug 3231 fixed: do not explore all zip files on desktop under Windows */
56                 //putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);
57                 /**
58                  * Bug 4187 fixed: uigetdir() opens on "Desktop" and not on "Computer" on windows
59                  * No need to use 'putClientProperty' anymore (bug 3231)
60                  */
61         }       
62
63         /**
64          * Set the title of the file chooser
65          * @param title the title to set
66          */
67         public void setTitle(String title) {
68                 super.setDialogTitle(title);
69         }
70         
71         /**
72          * Set the mask & the mask description for the filechooser
73          * @param mask the mask to set
74          * @param fileMaskDescription the maskDescription to set
75          */
76         public void addMask(String[] mask, String[] fileMaskDescription) {      
77                 
78                 //size of the mask list
79                 maskSize = mask.length;
80                 
81                 //If the mask description is empty we allocate description 
82                 //according to the extensions given
83                 if (fileMaskDescription.length == 0) {
84                         maskDescription = new String[mask.length];                      
85                         for (int i = 0; i < mask.length; i++) {
86                                 super.addChoosableFileFilter(new SciFileFilter(mask[i], maskDescription[i], i/*, maskSize*/));
87                         }
88                 } else {
89                         //If the mask description is filled
90                         //we use those descriptions given by the user
91                         this.maskDescription = fileMaskDescription;
92                         for (int i = 0; i < mask.length; i++) {
93                                 super.addChoosableFileFilter(new SciFileFilter(mask[i], maskDescription[i],i/*, maskSize*/));
94                         }
95                 }
96         }
97         
98         /**
99          * Set the initial directory used for file search
100          * @param path the default path
101          */
102         public void setInitialDirectory(String path) {
103                 // When empty string given
104                 if (path.length() == 0) {
105                         return;
106                 }
107                 // Replace beginning of the path if is an environment variable
108                 String newPath = path;
109                 StringTokenizer tok = new StringTokenizer(path, File.separator);
110                 if (tok.hasMoreTokens()) { /* It is possible that we don't have any more token here when
111                                               Scilab is started from / for example */
112                     String firstToken = tok.nextToken();
113                     if (firstToken != null && System.getenv(firstToken) != null)  {
114                         newPath = newPath.replaceFirst(firstToken, System.getenv(firstToken));
115                     }           
116                 }
117                 super.setCurrentDirectory(new File(newPath));
118         }
119         
120         /**
121          * Display this chooser and wait for user selection 
122          */
123         public void displayAndWait() {
124                 
125                 JFrame parentFrame = new JFrame();
126                 parentFrame.setIconImage(new ImageIcon(System.getenv("SCI") + "/modules/gui/images/icons/scilab.png").getImage());
127                 int returnValue = 0;
128                 if (this.dialogType == JFileChooser.SAVE_DIALOG) {
129                         returnValue = this.showSaveDialog(parentFrame);
130                 } else {
131                         returnValue = this.showOpenDialog(parentFrame);
132                 }
133                 
134                 //User validate the uigetfile
135                 if (returnValue == APPROVE_OPTION) {
136                         if (this.isMultiSelectionEnabled()) {
137                                 File[] files = this.getSelectedFiles();
138                                 selection = new String[files.length];
139                                 selectionFileNames = new String[files.length];
140                                 selectionSize = files.length;
141                                 for (int i = 0; i < files.length; i++) {
142                                         selection[i] = files[i].getAbsolutePath();
143                                         selectionPath = files[i].getParentFile().getPath();
144                                         selectionFileNames[i] = files[i].getName();
145                                 }                                       
146                         } else {
147                                 File file = this.getSelectedFile();
148                                 
149                                 if (this.dialogType == JFileChooser.SAVE_DIALOG) {
150                                         //Test if there is a file with the same name    
151                                         if (file.exists()) {
152                                                 int actionDialog = JOptionPane.showConfirmDialog(this, Messages.gettext("Replace existing file?"), Messages.gettext("File already exist"), JOptionPane.YES_NO_OPTION);
153
154                                                 if (actionDialog == JOptionPane.YES_OPTION) {
155
156                                                 } else {
157                                                         // Same as cancel case
158                                                         selection = new String[1];
159                                                         selection[0] = "";
160                                                         selectionSize = 0;
161                                                         selectionPath = "";     
162                                                         selectionFileNames = new String[1];
163                                                         selectionFileNames[0] = "";
164                                                         //set the filter index of the JFileChooser at 0 if "cancel" button was selected
165                                                         filterIndex = 0;
166                                                 
167                                                         //return the filechooser's informations 
168                                                         //they are stocked into FileChooserInfos
169                                                         FileChooserInfos.getInstance().setSelection(selection);
170                                                         FileChooserInfos.getInstance().setSelectionPathName(selectionPath);
171                                                         FileChooserInfos.getInstance().setSelectionFileNames(selectionFileNames);
172                                                         FileChooserInfos.getInstance().setSelectionSize(selectionSize);                 
173                                                         FileChooserInfos.getInstance().setFilterIndex(filterIndex);                     
174                                                         return;
175                                                 }
176                                         }
177                                 }
178
179                                 selection = new String[1];
180                                 selection[0] = file.getAbsolutePath();
181                                 selectionPath = file.getParentFile().getPath();
182                                 selectionFileNames = new String[1];
183                                 selectionFileNames[0] = file.getName();
184                                 selectionSize = 1;                                              
185                         }
186                         
187                         //return the filechooser's informations 
188                         //they are stocked into FileChooserInfos
189                         FileChooserInfos.getInstance().setSelection(selection);
190                         FileChooserInfos.getInstance().setSelectionPathName(selectionPath);
191                         FileChooserInfos.getInstance().setSelectionFileNames(selectionFileNames);
192                         FileChooserInfos.getInstance().setSelectionSize(selectionSize);         
193                         
194                         //set the filter index at the last index 
195                         //of the list box if the mask "All files" is selected  
196                         javax.swing.filechooser.FileFilter AllFilesSelected = getFileFilter();
197                         if (AllFilesSelected.getDescription().equals("All Files")){
198                                 FileChooserInfos.getInstance().setFilterIndex(maskSize + 1);
199                         }
200                         
201                 //User cancel the uigetfile     
202                 } else {                        
203                         selection = new String[1];
204                         selection[0] = "";
205                         selectionSize = 0;
206                         selectionPath = "";     
207                         selectionFileNames = new String[1];
208                         selectionFileNames[0] = "";
209                         //set the filter index of the JFileChooser at 0 if "cancel" button was selected
210                         filterIndex = 0;
211                         
212                         //return the filechooser's informations 
213                         //they are stocked into FileChooserInfos
214                         FileChooserInfos.getInstance().setSelection(selection);
215                         FileChooserInfos.getInstance().setSelectionPathName(selectionPath);
216                         FileChooserInfos.getInstance().setSelectionFileNames(selectionFileNames);
217                         FileChooserInfos.getInstance().setSelectionSize(selectionSize);                 
218                         FileChooserInfos.getInstance().setFilterIndex(filterIndex);                     
219                 }                               
220                 
221         
222
223         }
224         
225         /**
226          * Get the number of files selected
227          * @return the number of files selected
228          */
229         public int getSelectionSize() {
230                 return selectionSize;
231         }
232         
233         /**
234          * Get the names of selected files
235          * @return the names of selected files
236          */
237         public String[] getSelection() {
238                 return selection;
239         }
240         
241         /**
242          * Set the flag indicating that we want only select directories
243          */
244         public void setDirectorySelectionOnly() {
245                 setFileSelectionMode(DIRECTORIES_ONLY);
246         }
247         
248         /**
249          * Set the flag indicating that we can select multiple files
250          * @param multipleSelection is enable or not
251          */
252         public void setMultipleSelection(boolean multipleSelection) {
253                 setMultiSelectionEnabled(multipleSelection);
254         }
255         
256         /**
257          * Get the path of selected files
258          * @return selectionPath selected file(s) path
259          */
260         public String getSelectionPathName() {
261                 return selectionPath;
262         }
263         
264         /**
265          * Get the names of selected files
266          * @return selectionFileNnames selected file(s) path
267          */
268         public String[] getSelectionFileNames() {
269                 return selectionFileNames;
270         }
271         
272         /**
273          * Get the filter index
274          * @return this.getFilterIndex() filter index
275          */
276         public int getFilterIndex() {
277                 return getFilterIndex();
278         }
279         
280         /**
281          * Set the flag indicating the filter index
282          * @param filterIndex index of the filter
283          */
284         public void setFilterIndex(int filterIndex) {
285                 setFilterIndex(filterIndex);
286         }
287         
288         /**
289          * Set the dialog type (save or open a file ?)
290          * @param dialogType the dialog type
291          */
292         public void setUiDialogType(int dialogType) {
293                 this.dialogType = dialogType;
294         }
295 }