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