KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > FileChooser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.project.ui;
21
22 import org.openide.filesystems.FileUtil;
23
24 import javax.swing.*;
25 import javax.swing.filechooser.FileFilter JavaDoc;
26 import java.io.File JavaDoc;
27 import java.awt.*;
28
29 public class FileChooser extends JFileChooser {
30
31     private String JavaDoc key;
32     protected String JavaDoc initialPath;
33
34     public FileChooser(String JavaDoc key, String JavaDoc currentDirectoryPath) {
35         super(getInitialDirectory(key, currentDirectoryPath));
36         this.key = key;
37     }
38
39     public FileChooser(String JavaDoc key) {
40         this(key, null);
41         this.key = key;
42     }
43
44     private static File JavaDoc getInitialDirectory(String JavaDoc key, String JavaDoc currentDirectoryPath) {
45         return getInitialDirectory(key, currentDirectoryPath == null ? null : new File JavaDoc(currentDirectoryPath));
46     }
47
48     private static File JavaDoc getInitialDirectory(String JavaDoc key, File JavaDoc f) {
49         while (f != null) {
50             if (f.exists() && f.isDirectory()) {
51                 return f;
52             }
53             f = f.getParentFile();
54         }
55         File JavaDoc lastChooserLocation = getLastChooserLocation(key);
56         if (lastChooserLocation != null && lastChooserLocation.exists()) {
57             return lastChooserLocation;
58         } else {
59             String JavaDoc pathname = System.getProperty("user.home"); //NOI18N
60
if(pathname != null) {
61                 File JavaDoc file = new File JavaDoc(pathname).getAbsoluteFile();
62                 if(file.exists()) {
63                     return file;
64                 }
65             }
66             File JavaDoc file = new File JavaDoc("").getAbsoluteFile(); //NOI18N
67
assert file.exists() : "Default directory '" + file.getAbsolutePath() + "' does not exist"; //NOI18N
68
return f;
69         }
70     }
71
72     public int showDialog(Component parent, String JavaDoc approveButtonText) throws HeadlessException {
73         FileUtil.preventFileChooserSymlinkTraversal(this, getInitialDirectory(key, getCurrentDirectory()));
74         return super.showDialog(parent, approveButtonText);
75     }
76
77     protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
78         if (SELECTED_FILE_CHANGED_PROPERTY.equals(propertyName)) {
79             newValue = correctFile((File JavaDoc) newValue);
80         }
81         super.firePropertyChange(propertyName, oldValue, newValue);
82     }
83
84     public void approveSelection() {
85         saveCurrentLocation();
86         super.approveSelection();
87     }
88
89     private static File JavaDoc correctFile(File JavaDoc f) {
90         while(f != null && ".".equals(f.getName())) { //NOI18N
91
f = f.getParentFile();
92         }
93         return f;
94     }
95
96     private void saveCurrentLocation() {
97         if (!isMultiSelectionEnabled() && isDirectorySelectionEnabled()) {
98             // Try to save selected file (if it is an existing single directory
99
if (saveLocation(getSelectedFile())) {
100                 return;
101             }
102         }
103         saveLocation(getCurrentDirectory());
104     }
105
106     private boolean saveLocation(File JavaDoc f) {
107         if (f != null && f.isDirectory()) {
108             setLastChooserLocation(key, f);
109             return true;
110         } else {
111             return false;
112         }
113     }
114
115     public static File JavaDoc getLastChooserLocation(String JavaDoc key) {
116         String JavaDoc path = FoldersListSettings.getPreferences().get(FoldersListSettings.LAST_USED_CHOOSER_LOCATIONS+key, null);
117         return path != null ? new File JavaDoc(path) : null;
118     }
119
120     public static void setLastChooserLocation(String JavaDoc key, File JavaDoc folder) {
121         FoldersListSettings.getPreferences().put(FoldersListSettings.LAST_USED_CHOOSER_LOCATIONS+key, folder.getPath());
122     }
123
124     public static FileChooser createDirectoryChooser(String JavaDoc key) {
125         return createDirectoryChooser(key, null);
126     }
127
128     public static FileChooser createDirectoryChooser(String JavaDoc key, String JavaDoc initialPath) {
129         FileChooser chooser = new FileChooser(key, initialPath);
130         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
131         chooser.setMultiSelectionEnabled(false);
132         chooser.setAcceptAllFileFilterUsed(false);
133         return chooser;
134     }
135
136     public static FileChooser createFileChooser(String JavaDoc key, String JavaDoc dialogTitle, FileFilter JavaDoc fileFilter) {
137         FileChooser chooser = new FileChooser(key);
138         chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
139         chooser.setMultiSelectionEnabled(true);
140         chooser.setDialogTitle(dialogTitle);
141         //#61789 on old macosx (jdk 1.4.1) these two method need to be called in this order.
142
chooser.setAcceptAllFileFilterUsed( false );
143         chooser.setFileFilter(fileFilter);
144         return chooser;
145     }
146 }
147
Popular Tags