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