diff --git a/cnd.utils/nbproject/project.xml b/cnd.utils/nbproject/project.xml --- a/cnd.utils/nbproject/project.xml +++ b/cnd.utils/nbproject/project.xml @@ -14,15 +14,6 @@ - org.netbeans.modules.utilities - - - - 1 - 1.34 - - - org.openide.awt diff --git a/cnd.utils/src/org/netbeans/modules/cnd/utils/filters/CndOpenFileDialogFilter.java b/cnd.utils/src/org/netbeans/modules/cnd/utils/filters/CndOpenFileDialogFilter.java --- a/cnd.utils/src/org/netbeans/modules/cnd/utils/filters/CndOpenFileDialogFilter.java +++ b/cnd.utils/src/org/netbeans/modules/cnd/utils/filters/CndOpenFileDialogFilter.java @@ -45,9 +45,9 @@ import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; -import org.netbeans.modules.cnd.utils.FileFilterFactory; +import javax.swing.filechooser.FileFilter; import org.netbeans.modules.cnd.utils.FileFilterFactory.AbstractFileAndFileObjectFilter; -import org.netbeans.modules.openfile.OpenFileDialogFilter; +import org.openide.filesystems.FileFilterSupport; import org.openide.util.lookup.ServiceProvider; /** @@ -77,64 +77,61 @@ return res.toArray(new String[res.size()]); } - private static class Adapter extends OpenFileDialogFilter { + private static class Adapter extends FileFilter { private AbstractFileAndFileObjectFilter delegate; public Adapter(AbstractFileAndFileObjectFilter delegate) { this.delegate = delegate; } @Override - public String getDescriptionString() { - return CndOpenFileDialogFilter.convertDescription(delegate.getDescription()); - } - - @Override public boolean accept(File file) { return delegate.accept(file); } @Override - public String[] getSuffixes() { - return CndOpenFileDialogFilter.convertSuffixes(delegate.getSuffixesAsString()); + public String getDescription() { + return FileFilterSupport.constructFilterDisplayName( + convertDescription(delegate.getDescription()), + convertSuffixes(delegate.getSuffixesAsString())); } } - @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class) + @ServiceProvider(service = FileFilter.class, path=FileFilterSupport.FILE_FILTER_LOOKUP_PATH) public static final class CFilter extends Adapter { public CFilter() { super(CSourceFileFilter.getInstance()); } } - @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class) + @ServiceProvider(service = FileFilter.class, path=FileFilterSupport.FILE_FILTER_LOOKUP_PATH) public static final class CppFilter extends Adapter { public CppFilter() { super(CCSourceFileFilter.getInstance()); } } - @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class) + @ServiceProvider(service = FileFilter.class, path=FileFilterSupport.FILE_FILTER_LOOKUP_PATH) public static final class HeaderFilter extends Adapter { public HeaderFilter() { super(HeaderSourceFileFilter.getInstance()); } } - @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class) + @ServiceProvider(service = FileFilter.class, path=FileFilterSupport.FILE_FILTER_LOOKUP_PATH) public static final class FortranFilter extends Adapter { public FortranFilter() { super(FortranSourceFileFilter.getInstance()); } } - @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class) + @ServiceProvider(service = FileFilter.class, path=FileFilterSupport.FILE_FILTER_LOOKUP_PATH) public static final class ResourceFilter extends Adapter { public ResourceFilter() { super(ResourceFileFilter.getInstance()); } } - @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class) + @ServiceProvider(service = FileFilter.class, path=FileFilterSupport.FILE_FILTER_LOOKUP_PATH) public static final class QtFilter extends Adapter { public QtFilter() { super(QtFileFilter.getInstance()); diff --git a/java.source/nbproject/project.xml b/java.source/nbproject/project.xml --- a/java.source/nbproject/project.xml +++ b/java.source/nbproject/project.xml @@ -343,7 +343,7 @@ - 7.58 + 7.64 diff --git a/java.source/src/org/netbeans/modules/java/JavaOpenFileDialogFilter.java b/java.source/src/org/netbeans/modules/java/JavaOpenFileDialogFilter.java new file mode 100644 --- /dev/null +++ b/java.source/src/org/netbeans/modules/java/JavaOpenFileDialogFilter.java @@ -0,0 +1,68 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java; + +import java.io.File; +import javax.swing.filechooser.FileFilter; +import org.openide.filesystems.FileFilterSupport; +import static org.openide.filesystems.FileFilterSupport.FILE_FILTER_LOOKUP_PATH; +import org.openide.util.lookup.ServiceProvider; + +/** + * Open File Dialog filter for Java files. + * + * @author jhavlin + */ +@ServiceProvider(service = FileFilter.class, path = FILE_FILTER_LOOKUP_PATH) +public class JavaOpenFileDialogFilter extends FileFilter { + + @Override + public String getDescription() { + return FileFilterSupport.constructFilterDisplayName( + "text/x-java"); //NOI18N + } + + @Override + public boolean accept(File file) { + return FileFilterSupport.accept(file, "text/x-java"); //NOI18N + } +} diff --git a/openide.filesystems/apichanges.xml b/openide.filesystems/apichanges.xml --- a/openide.filesystems/apichanges.xml +++ b/openide.filesystems/apichanges.xml @@ -49,6 +49,19 @@ Filesystems API + + + FileFilterSupport added. + + + + + Added class FileFilterSupport, that is helpful when implementing + custom file filters for Open File dialog. + + + + addRecursiveListener with a filter diff --git a/openide.filesystems/manifest.mf b/openide.filesystems/manifest.mf --- a/openide.filesystems/manifest.mf +++ b/openide.filesystems/manifest.mf @@ -2,5 +2,5 @@ OpenIDE-Module: org.openide.filesystems OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml -OpenIDE-Module-Specification-Version: 7.63 +OpenIDE-Module-Specification-Version: 7.64 diff --git a/openide.filesystems/src/org/openide/filesystems/FileFilterSupport.java b/openide.filesystems/src/org/openide/filesystems/FileFilterSupport.java new file mode 100644 --- /dev/null +++ b/openide.filesystems/src/org/openide/filesystems/FileFilterSupport.java @@ -0,0 +1,220 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.openide.filesystems; + +import java.io.File; +import java.io.FileFilter; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Support methods for {@link FileFilter file filters} that can be used, for + * example, in Open File dialog.

See issue #209998.

The following + * example shows how to implement and register a custom filter.

+ *
+ * {@literal
+ *
+ * @}{@code ServiceProvider(service=FileFilter.class, path=FileFilterSupport.FILE_FILTER_LOOKUP_PATH)
+ * public class TXTFilter extends FileFilter {
+ *   private static final String[] EXTENSIONS = new String[] {".txt"};
+ *
+ *   public boolean accept(File f) {
+ *     return FileFilterSupport.accept(f, EXTENSIONS);
+ *   }
+ *
+ *   public String getDescription() {
+ *     return FileFilterSupport.constructFilterDisplayName(, EXTENSIONS);
+ *   }
+ * }}
+ * 
+ * + * @author jhavlin, jlahoda + * @since 7.49 + */ +public final class FileFilterSupport { + + /** + * Path to use when registering custom filters. + */ + public static final String FILE_FILTER_LOOKUP_PATH = + "OpenFileDialogFilter"; //NOI18N + /** + * The logger. + */ + private static final Logger LOG = Logger.getLogger( + FileFilterSupport.class.getName()); + + /** + * Hide the default constructor. + */ + private FileFilterSupport() { + } + + /** + * Construct description for {@link FileFilter} that accepts files with + * specified extension. + * + * @param displayName Human readable display name (e.g. "HTML files") + * @param extensions List of allowed extensions (e.g. {".htm", ".html"}). + * + * @return Display name (description) for the filter. + */ + public static String constructFilterDisplayName(String displayName, + String... extensions) { + StringBuilder sb = new StringBuilder(displayName); + sb.append(" "); //NOI18N + sb.append(Arrays.asList(extensions).toString()); + return sb.toString(); + } + + /** + * Construct description for {@link FileFilter} that accepts files of + * specified MIME type. + * + * @param mimeType MIME type of the file filter. + * + * @return Display name (description) for the filter. + * + */ + public static String constructFilterDisplayName(String mimeType) { + return constructFilterDisplayName(getMimeDisplayName(mimeType), + toExtArray(FileUtil.getMIMETypeExtensions(mimeType))); + } + + /** + * Check whether passed file is accepted by filter for specified list of + * extensions. + * + * @param file File whose extension is checked. + * @param extensions List of allowed extensions. + * + * @return True if the file ends with one of allowed extensions, false + * otherwise. + * + * @see FileFilterSupport + */ + public static boolean accept(File file, String... extensions) { + if (file != null) { + if (file.isDirectory()) { + return true; + } + for (String ext : extensions) { + if (compareSuffixes(file.getName(), ext)) { + return true; + } + } + } + return false; + } + + /** + * Check whether passed file is accepted by filter for specified MIME type. + * + * @param file File that is checked for a MIME type. + * @param mimeType Accepted MIME type. + * + * @return True if file {@code file} is of MIME type {@code mimeType}, false + * otherwise. + * + * @see FileFilterSupport + */ + public static boolean accept(File file, String mimeType) { + List exts = FileUtil.getMIMETypeExtensions(mimeType); + return accept(file, toExtArray(exts)); + } + + /** + * Get display name for a MIME type. + * + * @param mimeType MIME type (e.g. "java"). + * @return Display name for the MIME type (e.g. "Java Files"), or the MIME + * type itself if no display name has been set. + */ + private static String getMimeDisplayName(String mimeType) { + try { + FileObject factoriesFO = FileUtil.getConfigFile( + "Loaders/" + mimeType + "/Factories"); //NOI18N + if (factoriesFO != null) { + FileObject[] children = factoriesFO.getChildren(); + for (FileObject child : children) { + String childName = child.getNameExt(); + String displayName = FileUtil.getConfigRoot(). + getFileSystem().getStatus(). + annotateName(childName, + Collections.singleton(child)); + if (!childName.equals(displayName)) { + return displayName; + } + } + } + } catch (Exception e) { + LOG.log(Level.WARNING, null, e); + } + return mimeType; + } + + /** + * Check whether the given filename has required suffex. + */ + private static boolean compareSuffixes(String fileName, String suffix) { + return fileName.toUpperCase().endsWith(suffix.toUpperCase()); + } + + /** + * Convert a list of strings to a string array. + * + * @param listOfStrings List of extensions, without starting perios (e.g. + * "txt", "java"). + * @return Array of extensions, with starting period (e.g. ".txt", ".java"). + */ + private static String[] toExtArray(List listOfStrings) { + String[] array = new String[listOfStrings.size()]; + int index = 0; + for (String ext : listOfStrings) { + array[index++] = "." + ext; //NOI18N + } + return array; + } +} diff --git a/openide.filesystems/test/unit/src/org/openide/filesystems/FileFilterSupportTest.java b/openide.filesystems/test/unit/src/org/openide/filesystems/FileFilterSupportTest.java new file mode 100644 --- /dev/null +++ b/openide.filesystems/test/unit/src/org/openide/filesystems/FileFilterSupportTest.java @@ -0,0 +1,92 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.openide.filesystems; + +import java.io.File; +import javax.swing.filechooser.FileFilter; +import static org.junit.Assert.*; +import org.junit.Test; + +/** + * Test default implementation of OpenFileDialogFilter. + * + * @author jhavlin + */ +public class FileFilterSupportTest { + + private final FileFilter filter = new FileFilterImpl(); + + public FileFilterSupportTest() { + } + + @Test + public void testAccept() { + assertTrue(filter.accept(new File("test.xyz"))); + assertTrue(filter.accept(new File("test.XYZ"))); + assertTrue(filter.accept(new File("test.uvw"))); + assertTrue(filter.accept(new File("test.UVW"))); + assertFalse(filter.accept(new File("test.java"))); + } + + @Test + public void testGetDescription() { + assertEquals( + "Custom Example Files [.xyz, .uvw]", + filter.getDescription()); + } + + private static class FileFilterImpl extends FileFilter { + + private static final String[] EXTENSIONS = new String[]{".xyz", ".uvw"}; + + @Override + public String getDescription() { + return FileFilterSupport.constructFilterDisplayName( + "Custom Example Files", EXTENSIONS); + } + + @Override + public boolean accept(File f) { + return FileFilterSupport.accept(f, EXTENSIONS); + } + } +} diff --git a/utilities/nbproject/project.xml b/utilities/nbproject/project.xml --- a/utilities/nbproject/project.xml +++ b/utilities/nbproject/project.xml @@ -96,7 +96,7 @@ - 7.58 + 7.64
@@ -182,11 +182,9 @@ - org.netbeans.modules.cnd.utils org.netbeans.modules.utilities.project org.netbeans.modules.visualweb.gravy org.netbeans.modules.openfile - org.netbeans.modules.search diff --git a/utilities/src/org/netbeans/modules/openfile/Bundle.properties b/utilities/src/org/netbeans/modules/openfile/Bundle.properties --- a/utilities/src/org/netbeans/modules/openfile/Bundle.properties +++ b/utilities/src/org/netbeans/modules/openfile/Bundle.properties @@ -41,7 +41,6 @@ # made subject to such option by the copyright holder. # OpenFileDialogFilter -OFDFD_Java=Java Files OFDFD_Txt=Text Files # OpenFileAction diff --git a/utilities/src/org/netbeans/modules/openfile/FileChooser.java b/utilities/src/org/netbeans/modules/openfile/FileChooser.java --- a/utilities/src/org/netbeans/modules/openfile/FileChooser.java +++ b/utilities/src/org/netbeans/modules/openfile/FileChooser.java @@ -52,11 +52,13 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.filechooser.FileFilter; -import javax.swing.filechooser.FileNameExtensionFilter; import org.openide.DialogDisplayer; import org.openide.NotifyDescriptor; +import org.openide.filesystems.FileFilterSupport; +import static org.openide.filesystems.FileFilterSupport.FILE_FILTER_LOOKUP_PATH; import org.openide.util.Lookup; import org.openide.util.NbBundle; +import org.openide.util.lookup.Lookups; import org.openide.util.lookup.ServiceProvider; /** @@ -83,8 +85,14 @@ * @see javax.swing.JFileChooser */ public void addChoosableFileFilters() { - for (OpenFileDialogFilter f : - Lookup.getDefault().lookupAll(OpenFileDialogFilter.class)) { + // Add legacy filters + for (org.netbeans.modules.openfile.OpenFileDialogFilter f : + Lookup.getDefault().lookupAll( + org.netbeans.modules.openfile.OpenFileDialogFilter.class)) { + addChoosableFileFilter(f); + } + Lookup lkp = Lookups.forPath(FileFilterSupport.FILE_FILTER_LOOKUP_PATH); + for (FileFilter f : lkp.lookupAll(FileFilter.class)) { addChoosableFileFilter(f); } } @@ -130,33 +138,22 @@ } } + @ServiceProvider(service = FileFilter.class, path = FILE_FILTER_LOOKUP_PATH) + public static class TxtFileFilter extends FileFilter { - @ServiceProvider(service=org.netbeans.modules.openfile.OpenFileDialogFilter.class) - public static class JavaFilesFilter extends OpenFileDialogFilter { + private static final String[] EXTENSIONS = new String[]{".txt"};//NOI18N @Override - public String getDescriptionString() { - return NbBundle.getMessage(getClass(), "OFDFD_Java"); // NOI18N + public boolean accept(File f) { + return FileFilterSupport.accept(f, EXTENSIONS); } @Override - public String[] getSuffixes() { - return new String[] {".java"}; + public String getDescription() { + return FileFilterSupport.constructFilterDisplayName( + NbBundle.getMessage(getClass(), "OFDFD_Txt"), //NOI18N + EXTENSIONS); } - - } - - @ServiceProvider(service=OpenFileDialogFilter.class) - public static class TxtFileFilter - extends OpenFileDialogFilter.ExtensionFilter { - - @Override - public FileNameExtensionFilter getFilter() { - return new FileNameExtensionFilter( - NbBundle.getMessage(getClass(), "OFDFD_Txt"), - "txt"); // NOI18N - } - } // End of TxtFileFilter } diff --git a/utilities/src/org/netbeans/modules/openfile/OpenFileDialogFilter.java b/utilities/src/org/netbeans/modules/openfile/OpenFileDialogFilter.java --- a/utilities/src/org/netbeans/modules/openfile/OpenFileDialogFilter.java +++ b/utilities/src/org/netbeans/modules/openfile/OpenFileDialogFilter.java @@ -47,6 +47,7 @@ import java.util.Arrays; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; +import org.openide.filesystems.FileFilterSupport; /** * {@code OpenFileDialogFilter} is an abstract class used by {@link FileChooser} @@ -107,7 +108,10 @@ * @see OpenFileDialogFilter.ExtensionFilter * * @author Victor G. Vasilyev + * + * @deprecated Use {@link FileFilter} and {@link FileFilterSupport}. */ +@Deprecated public abstract class OpenFileDialogFilter extends FileFilter { /** @@ -258,7 +262,9 @@ * @see OpenFileDialogFilter * @see FileNameExtensionFilter * + * @deprecated Use {@link FileFilter} and {@link FileFilterSupport}. */ + @Deprecated public static abstract class ExtensionFilter extends OpenFileDialogFilter { private static final char EXTENSION_SEPARATOR = '.';