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/FileFilterFactory.java b/openide.filesystems/src/org/openide/filesystems/FileFilterFactory.java
new file mode 100644
--- /dev/null
+++ b/openide.filesystems/src/org/openide/filesystems/FileFilterFactory.java
@@ -0,0 +1,294 @@
+/*
+ * 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.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.filechooser.FileFilter;
+
+/**
+ * Class for registering custom {@link FileFilter file filters} that will be
+ * used in Open File dialog.
+ * Example implementation:
+ *
+ * {@literal @}{@code ServiceProvider(service=FileFilterFactory.class)
+ * public class TXTFilterFactory extends FileFilterFactory {
+ *
+ * public FileFilter createFileFilter() {
+ * return Support.createFilterForExtensions(, ".txt"); //NOI18N
+ * // or return Support.createFilterForMimeType("text/plain"); //NOI18N
+ * }
+ * }}
+ *
+ *
+ * @author jhavlin, jlahoda
+ * @since 7.64
+ */
+public interface FileFilterFactory {
+
+ /**
+ * Create a new instance of {@link FileFilter}. You should use support
+ * method {@link Support#createFilterForMimeType(String)} or
+ * {@link Support#createFilterForExtensions(String, String[])} to ensure
+ * consistent behavior and appearance.
+ */
+ public FileFilter createFileFilter();
+
+ /**
+ * Support methods for creating custom file filters.
+ */
+ public static class Support {
+
+ /**
+ * The logger.
+ */
+ private static final Logger LOG = Logger.getLogger(
+ FileFilterFactory.class.getName());
+
+ /**
+ * Hide the default constructor.
+ */
+ private Support() {
+ }
+
+ /**
+ * Create file filter that accepts files of specified MIME type.
+ *
+ * @param mimeType MIME type of accepted files (e.g. "text/plain").
+ */
+ public static FileFilter createFilterForMimeType(String mimeType) {
+ return new MimeFilter(mimeType);
+ }
+
+ /**
+ * Create file filter that accepts files with specified extensions.
+ *
+ * @param displayName Display name of the filter (e.g. "Text Files").
+ * @param allowedExtensions Array of allowed extensions (e.g. {".txt",
+ * ".log"})
+ */
+ public static FileFilter createFilterForExtensions(String displayName,
+ String... allowedExtensions) {
+ return new ExtensionFilter(displayName, allowedExtensions);
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * Type of filters returned by {@link #createFilterForMimeType(String)}.
+ */
+ private static class MimeFilter extends FileFilter {
+
+ private String mimeType;
+
+ public MimeFilter(String mimeType) {
+ this.mimeType = mimeType;
+ }
+
+ @Override
+ public boolean accept(File f) {
+ return accept(f, mimeType);
+ }
+
+ @Override
+ public String getDescription() {
+ return constructFilterDisplayName(mimeType);
+ }
+ }
+
+ /**
+ * Type of filters returned by
+ * {@link #createFilterForExtensions(String, String[])}.
+ */
+ private static class ExtensionFilter extends FileFilter {
+
+ private String displayName;
+ private String[] extensions;
+
+ public ExtensionFilter(String displayName, String[] extensions) {
+ this.displayName = displayName;
+ this.extensions = extensions;
+ }
+
+ @Override
+ public boolean accept(File f) {
+ return accept(f, extensions);
+ }
+
+ @Override
+ public String getDescription() {
+ return constructFilterDisplayName(displayName, extensions);
+ }
+ }
+ }
+}
diff --git a/openide.filesystems/test/unit/src/org/openide/filesystems/FileFilterFactoryTest.java b/openide.filesystems/test/unit/src/org/openide/filesystems/FileFilterFactoryTest.java
new file mode 100644
--- /dev/null
+++ b/openide.filesystems/test/unit/src/org/openide/filesystems/FileFilterFactoryTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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 FileFilterFactoryTest {
+
+ private final FileFilter filter =
+ FileFilterFactory.Support.createFilterForExtensions(
+ "Custom Example Files", new String[]{".xyz", ".uvw"});
+
+ public FileFilterFactoryTest() {
+ }
+
+ @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());
+ }
+}
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