001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 2004-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.openfile;
043:
044: import java.awt.GridLayout;
045: import java.io.File;
046: import java.util.ArrayList;
047: import java.util.List;
048: import javax.swing.JFileChooser;
049: import javax.swing.JLabel;
050: import javax.swing.JPanel;
051: import javax.swing.filechooser.FileFilter;
052: import org.openide.DialogDisplayer;
053: import org.openide.NotifyDescriptor;
054: import org.openide.util.NbBundle;
055:
056: /**
057: *
058: * @author Jesse Glick
059: * @author Marian Petras
060: */
061: class FileChooser extends JFileChooser {
062:
063: /** Creates a new instance of FileChooser */
064: FileChooser() {
065: setFileSelectionMode(JFileChooser.FILES_ONLY);
066: setMultiSelectionEnabled(true);
067:
068: /* initialize file filters */
069: FileFilter currentFilter = getFileFilter();
070: addChoosableFileFilter(new Filter(
071: new String[] { DefaultOpenFileImpl.JAVA_EXT },
072: NbBundle
073: .getMessage(FileChooser.class, "TXT_JavaFilter"))); //NOI18N
074: addChoosableFileFilter(new Filter(
075: new String[] { DefaultOpenFileImpl.TXT_EXT }, NbBundle
076: .getMessage(FileChooser.class, "TXT_TxtFilter"))); //NOI18N
077: setFileFilter(currentFilter);
078: }
079:
080: public void approveSelection() {
081: final File[] selectedFiles = getSelectedFiles();
082:
083: /* check the files: */
084: List<String> errorMsgs = null;
085: for (int i = 0; i < selectedFiles.length; i++) {
086: String msgPatternRef = null;
087: boolean isUNCPath = false;
088: File file = selectedFiles[i];
089:
090: if (!file.exists()) {
091: msgPatternRef = "MSG_FileDoesNotExist"; //NOI18N
092: } else if (OpenFile.isSpecifiedByUNCPath(file)) {
093: msgPatternRef = "MSG_FileSpecifiedByUNC"; //NOI18N
094: isUNCPath = true;
095: } else if (file.isDirectory()) {
096: msgPatternRef = "MSG_FileIsADirectory"; //NOI18N
097: } else if (!file.isFile()) {
098: msgPatternRef = "MSG_FileIsNotPlainFile"; //NOI18N
099: }
100: if (msgPatternRef == null) {
101: continue;
102: }
103:
104: if (errorMsgs == null) {
105: errorMsgs = new ArrayList<String>(selectedFiles.length
106: - i);
107: }
108: errorMsgs.add(NbBundle.getMessage(FileChooser.class,
109: msgPatternRef,
110: isUNCPath ? getUNCPathToDisplay(file) : file
111: .getName()));
112: }
113: if (errorMsgs == null) {
114: super .approveSelection();
115: } else {
116: JPanel panel = new JPanel(new GridLayout(errorMsgs.size(),
117: 0, 0, 2)); //gaps
118: for (String errMsg : errorMsgs) {
119: panel.add(new JLabel(errMsg));
120: }
121: DialogDisplayer.getDefault().notify(
122: new NotifyDescriptor.Message(panel,
123: NotifyDescriptor.WARNING_MESSAGE));
124: }
125: }
126:
127: /**
128: * Creates a file name to be displayed in an error message.
129: * If the file's full path is short, it is returned withouth any change.
130: * Otherwise part of the path is replaced with an ellipsis (...).
131: *
132: * @param file file whose name is to be displayed
133: * @return path of the file
134: */
135: private static String getUNCPathToDisplay(File file) {
136: String name = file.getName();
137: String path = file.getPath();
138: String pathToCheck = path.substring(2, path.length()
139: - name.length() - 1);
140:
141: final char sep = File.separatorChar;
142: int slashIndex = pathToCheck.indexOf(sep);
143: if (slashIndex != -1) {
144: slashIndex = pathToCheck.indexOf(sep, slashIndex + 1);
145: if ((slashIndex != -1)
146: && (pathToCheck.indexOf(sep, slashIndex + 1) != -1)) {
147:
148: /* too many separators in the path - shorten with an ellipsis */
149: return new StringBuffer(slashIndex + name.length() + 7)
150: .append(path.substring(0, 2 + slashIndex))
151: .append(sep).append("...") //NOI18N
152: .append(sep).append(name).toString();
153: }
154: }
155: return path;
156: }
157:
158: /** File chooser filter that filters files by their names' suffixes. */
159: private static class Filter extends FileFilter {
160:
161: /** suffixes accepted by this filter */
162: private String[] extensions;
163:
164: /** localized description of this filter */
165: private String description;
166:
167: /**
168: * Creates a new filter that accepts files having specified suffixes.
169: * The filter is case-insensitive.
170: * <p>
171: * The filter does not use file <em>extensions</em> but it just
172: * tests whether the file name ends with the specified string.
173: * So it is recommended to pass a file name extension including the
174: * preceding dot rather than just the extension.
175: *
176: * @param extensions list of accepted suffixes
177: * @param description name of the filter
178: */
179: public Filter(String[] extensions, String description) {
180:
181: this .extensions = new String[extensions.length];
182: for (int i = 0; i < extensions.length; i++) {
183: this .extensions[i] = extensions[i].toUpperCase();
184: }
185: this .description = description;
186: }
187:
188: /**
189: * @return <code>true</code> if the file's name ends with one of the
190: * strings specified by the constructor or if the file
191: * is a directory, <code>false</code> otherwise
192: */
193: public boolean accept(File file) {
194: if (file.isDirectory()) {
195: return true;
196: }
197: for (int i = 0; i < extensions.length; i++) {
198: if (file.getName().toUpperCase()
199: .endsWith(extensions[i])) {
200: return true;
201: }
202: }
203:
204: return false;
205: }
206:
207: /** */
208: public String getDescription() {
209: return description;
210: }
211: } // End of Filter class.
212:
213: }
|