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
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.1-en.txt
14 package org.scilab.modules.gui.bridge.filechooser;
17 import java.util.StringTokenizer;
19 import javax.swing.ImageIcon;
20 import javax.swing.JFileChooser;
21 import javax.swing.JFrame;
22 import javax.swing.JOptionPane;
24 import org.scilab.modules.gui.filechooser.FileChooserInfos;
25 import org.scilab.modules.gui.filechooser.SimpleFileChooser;
26 import org.scilab.modules.gui.utils.ConfigManager;
27 import org.scilab.modules.gui.utils.SciFileFilter;
28 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
29 import org.scilab.modules.localization.Messages;
32 * Swing implementation of a Scilab File ChooserS
33 * @author Vincent COUVERT
34 * @author Sylvestre KOUMAR
36 public class SwingScilabFileChooser extends JFileChooser implements SimpleFileChooser {
38 private static final long serialVersionUID = 1L;
40 private String[] selection; // Path + filenames
41 private String selectionPath; // Path
42 private String[] selectionFileNames; // Filenames
43 private int selectionSize;
44 private int filterIndex;
46 private int dialogType;
48 private String[] maskDescription;
49 private JFrame parent;
54 public SwingScilabFileChooser() {
57 //System.out.println("[Constructor] SwingScilabFileChooser");
58 /** Bug 3231 fixed: do not explore all zip files on desktop under Windows */
59 //putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);
61 * Bug 4187 fixed: uigetdir() opens on "Desktop" and not on "Computer" on windows
62 * No need to use 'putClientProperty' anymore (bug 3231)
64 /* Bug 5111 : The Current directory have to be set before */
65 super.setCurrentDirectory(new File(ConfigManager.getLastOpenedDirectory()));
69 * Set the title of the file chooser
70 * @param title the title to set
73 public void setTitle(String title) {
74 super.setDialogTitle(title);
78 * Set the mask & the mask description for the filechooser
79 * @param mask the mask to set
80 * @param fileMaskDescription the maskDescription to set
82 public void addMask(String[] mask, String[] fileMaskDescription) {
84 //size of the mask list
85 maskSize = mask.length;
87 //If the mask description is empty we allocate description
88 //according to the extensions given
89 if (fileMaskDescription == null || fileMaskDescription.length == 0) {
90 for (int i = 0; i < mask.length; i++) {
91 super.addChoosableFileFilter(new SciFileFilter(mask[i], null, i/*, maskSize*/));
94 //If the mask description is filled
95 //we use those descriptions given by the user
96 this.maskDescription = fileMaskDescription;
97 for (int i = 0; i < mask.length; i++) {
98 super.addChoosableFileFilter(new SciFileFilter(mask[i], maskDescription[i], i/*, maskSize*/));
104 * Set the initial directory used for file search
105 * @param path the default path
108 public void setInitialDirectory(String path) {
109 // When empty string given
110 if (path.length() == 0) {
113 // Replace beginning of the path if is an environment variable
114 String newPath = path;
115 StringTokenizer tok = new StringTokenizer(path, File.separator);
116 if (tok.hasMoreTokens()) {
117 /* It is possible that we don't have any more token here when
118 Scilab is started from / for example */
119 String firstToken = tok.nextToken();
120 if (firstToken != null && System.getenv(firstToken) != null) {
121 newPath = newPath.replaceFirst(firstToken, System.getenv(firstToken));
124 super.setCurrentDirectory(new File(newPath));
128 * Set the parent frame
129 * @param parent the parent frame
131 public void setParentFrame(JFrame parent) {
132 this.parent = parent;
136 * Display this chooser and wait for user selection
139 public void displayAndWait() {
141 if (parent == null) {
142 parentFrame = new JFrame();
143 parentFrame.setIconImage(new ImageIcon(ScilabSwingUtilities.findIcon("scilab", "256x256")).getImage());
145 parentFrame = parent;
150 setFileFilter(getChoosableFileFilters()[maskSize]);
152 if (this.dialogType == JFileChooser.SAVE_DIALOG) {
153 returnValue = this.showSaveDialog(parentFrame);
155 returnValue = this.showOpenDialog(parentFrame);
158 //User validate the uigetfile
159 if (returnValue == APPROVE_OPTION) {
160 if (this.isMultiSelectionEnabled()) {
161 File[] files = this.getSelectedFiles();
162 selection = new String[files.length];
163 selectionFileNames = new String[files.length];
164 selectionSize = files.length;
165 for (int i = 0; i < files.length; i++) {
166 selection[i] = files[i].getAbsolutePath();
167 selectionPath = files[i].getParentFile().getPath();
168 selectionFileNames[i] = files[i].getName();
171 File file = this.getSelectedFile();
173 if (this.dialogType == JFileChooser.SAVE_DIALOG) {
174 //Test if there is a file with the same name
176 int actionDialog = JOptionPane.showConfirmDialog(this,
177 Messages.gettext("Replace existing file?"),
178 Messages.gettext("File already exists"),
179 JOptionPane.YES_NO_OPTION);
181 if (actionDialog != JOptionPane.YES_OPTION) {
182 // Same as cancel case
183 selection = new String[1];
187 selectionFileNames = new String[1];
188 selectionFileNames[0] = "";
189 //set the filter index of the JFileChooser at 0 if "cancel" button was selected
192 //return the filechooser's information
193 //they are stocked into FileChooserInfos
194 FileChooserInfos.getInstance().setSelection(selection);
195 FileChooserInfos.getInstance().setSelectionPathName(selectionPath);
196 FileChooserInfos.getInstance().setSelectionFileNames(selectionFileNames);
197 FileChooserInfos.getInstance().setSelectionSize(selectionSize);
198 FileChooserInfos.getInstance().setFilterIndex(filterIndex);
204 selection = new String[1];
205 selection[0] = file.getAbsolutePath();
206 if (getFileSelectionMode() == DIRECTORIES_ONLY) {
207 selectionPath = file.getPath();
209 selectionPath = file.getParentFile().getPath();
211 selectionFileNames = new String[1];
212 selectionFileNames[0] = file.getName();
216 //return the filechooser's information
217 //they are stocked into FileChooserInfos
218 FileChooserInfos.getInstance().setSelection(selection);
219 FileChooserInfos.getInstance().setSelectionPathName(selectionPath);
220 FileChooserInfos.getInstance().setSelectionFileNames(selectionFileNames);
221 FileChooserInfos.getInstance().setSelectionSize(selectionSize);
223 //set the filter index at the last index
224 //of the list box if the mask "All files" is selected
225 javax.swing.filechooser.FileFilter allFilesSelected = getFileFilter();
226 if (allFilesSelected.getDescription().equals("All Files")) {
227 FileChooserInfos.getInstance().setFilterIndex(maskSize + 1);
230 ConfigManager.saveLastOpenedDirectory(selectionPath);
231 //User cancel the uigetfile
233 selection = new String[1];
237 selectionFileNames = new String[1];
238 selectionFileNames[0] = "";
239 //set the filter index of the JFileChooser at 0 if "cancel" button was selected
242 //return the filechooser's information
243 //they are stocked into FileChooserInfos
244 FileChooserInfos.getInstance().setSelection(selection);
245 FileChooserInfos.getInstance().setSelectionPathName(selectionPath);
246 FileChooserInfos.getInstance().setSelectionFileNames(selectionFileNames);
247 FileChooserInfos.getInstance().setSelectionSize(selectionSize);
248 FileChooserInfos.getInstance().setFilterIndex(filterIndex);
256 * Get the number of files selected
257 * @return the number of files selected
260 public int getSelectionSize() {
261 return selectionSize;
265 * Get the names of selected files
266 * @return the names of selected files
269 public String[] getSelection() {
274 * Set the flag indicating that we want only select directories
277 public void setDirectorySelectionOnly() {
278 setFileSelectionMode(DIRECTORIES_ONLY);
282 * Set the flag indicating that we can select multiple files
283 * @param multipleSelection is enable or not
286 public void setMultipleSelection(boolean multipleSelection) {
287 setMultiSelectionEnabled(multipleSelection);
291 * Get the path of selected files
292 * @return selectionPath selected file(s) path
295 public String getSelectionPathName() {
296 return selectionPath;
300 * Get the names of selected files
301 * @return selectionFileNnames selected file(s) path
304 public String[] getSelectionFileNames() {
305 return selectionFileNames;
309 * Get the filter index
310 * @return this.getFilterIndex() filter index
313 public int getFilterIndex() {
314 return getFilterIndex();
318 * Set the flag indicating the filter index
319 * @param filterIndex index of the filter
321 public void setFilterIndex(int filterIndex) {
322 setFilterIndex(filterIndex);
326 * Set the dialog type (save or open a file ?)
327 * @param dialogType the dialog type
330 public void setUiDialogType(int dialogType) {
331 this.dialogType = dialogType;