This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 209998
Collapse All | Expand All

(-)a/cnd.utils/nbproject/project.xml (-9 lines)
Lines 14-28 Link Here
14
                    </run-dependency>
14
                    </run-dependency>
15
                </dependency>
15
                </dependency>
16
                <dependency>
16
                <dependency>
17
                    <code-name-base>org.netbeans.modules.utilities</code-name-base>
18
                    <build-prerequisite/>
19
                    <compile-dependency/>
20
                    <run-dependency>
21
                        <release-version>1</release-version>
22
                        <specification-version>1.34</specification-version>
23
                    </run-dependency>
24
                </dependency>
25
                <dependency>
26
                    <code-name-base>org.openide.awt</code-name-base>
17
                    <code-name-base>org.openide.awt</code-name-base>
27
                    <build-prerequisite/>
18
                    <build-prerequisite/>
28
                    <compile-dependency/>
19
                    <compile-dependency/>
(-)a/cnd.utils/src/org/netbeans/modules/cnd/utils/filters/CndOpenFileDialogFilter.java (-35 / +50 lines)
Lines 45-53 Link Here
45
import java.util.ArrayList;
45
import java.util.ArrayList;
46
import java.util.List;
46
import java.util.List;
47
import java.util.StringTokenizer;
47
import java.util.StringTokenizer;
48
import org.netbeans.modules.cnd.utils.FileFilterFactory;
48
import javax.swing.filechooser.FileFilter;
49
import org.netbeans.modules.cnd.utils.FileFilterFactory.AbstractFileAndFileObjectFilter;
49
import org.netbeans.modules.cnd.utils.FileFilterFactory.AbstractFileAndFileObjectFilter;
50
import org.netbeans.modules.openfile.OpenFileDialogFilter;
50
import org.openide.filesystems.FileFilterFactory;
51
import org.openide.util.lookup.ServiceProvider;
51
import org.openide.util.lookup.ServiceProvider;
52
52
53
/**
53
/**
Lines 77-92 Link Here
77
        return res.toArray(new String[res.size()]);
77
        return res.toArray(new String[res.size()]);
78
    }
78
    }
79
79
80
    private static class Adapter extends OpenFileDialogFilter {
80
    private static class Adapter extends FileFilter {
81
        private AbstractFileAndFileObjectFilter delegate;
81
        private AbstractFileAndFileObjectFilter delegate;
82
        public Adapter(AbstractFileAndFileObjectFilter delegate) {
82
        public Adapter(AbstractFileAndFileObjectFilter delegate) {
83
            this.delegate = delegate;
83
            this.delegate = delegate;
84
        }
84
        }        
85
86
        @Override
87
        public String getDescriptionString() {
88
            return CndOpenFileDialogFilter.convertDescription(delegate.getDescription());
89
        }
90
85
91
        @Override
86
        @Override
92
        public boolean accept(File file) {
87
        public boolean accept(File file) {
Lines 94-143 Link Here
94
        }
89
        }
95
90
96
        @Override
91
        @Override
97
        public String[] getSuffixes() {
92
        public String getDescription() {
98
            return CndOpenFileDialogFilter.convertSuffixes(delegate.getSuffixesAsString());
93
            return FileFilterFactory.Support.constructFilterDisplayName(
94
                    convertDescription(delegate.getDescription()),
95
                    convertSuffixes(delegate.getSuffixesAsString()));
99
        }
96
        }
100
    }
97
    }
101
98
102
    @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class)
99
    @ServiceProvider(service = org.openide.filesystems.FileFilterFactory.class)
103
    public static final class CFilter extends Adapter {
100
    public static final class CFilter
104
        public CFilter() {
101
            implements org.openide.filesystems.FileFilterFactory {
105
            super(CSourceFileFilter.getInstance());
102
103
        @Override
104
        public FileFilter createFileFilter() {
105
            return new Adapter(CSourceFileFilter.getInstance());
106
        }
106
        }
107
    }
107
    }
108
108
109
    @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class)
109
    @ServiceProvider(service = org.openide.filesystems.FileFilterFactory.class)
110
    public static final class CppFilter extends Adapter {
110
    public static final class CppFilter
111
        public CppFilter() {
111
            implements org.openide.filesystems.FileFilterFactory {
112
            super(CCSourceFileFilter.getInstance());
112
113
        @Override
114
        public FileFilter createFileFilter() {
115
            return new Adapter(CCSourceFileFilter.getInstance());
113
        }
116
        }
114
    }
117
    }
115
118
116
    @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class)
119
    @ServiceProvider(service = org.openide.filesystems.FileFilterFactory.class)
117
    public static final class HeaderFilter extends Adapter {
120
    public static final class HeaderFilter
118
        public HeaderFilter() {
121
            implements org.openide.filesystems.FileFilterFactory {
119
            super(HeaderSourceFileFilter.getInstance());
122
123
        @Override
124
        public FileFilter createFileFilter() {
125
            return new Adapter(HeaderSourceFileFilter.getInstance());
120
        }
126
        }
121
    }
127
    }
122
128
123
    @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class)
129
    @ServiceProvider(service = org.openide.filesystems.FileFilterFactory.class)
124
    public static final class FortranFilter extends Adapter {
130
    public static final class FortranFilter
125
        public FortranFilter() {
131
            implements org.openide.filesystems.FileFilterFactory {
126
            super(FortranSourceFileFilter.getInstance());
132
133
        @Override
134
        public FileFilter createFileFilter() {
135
            return new Adapter(FortranSourceFileFilter.getInstance());
127
        }
136
        }
128
    }
137
    }
129
138
130
    @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class)
139
    @ServiceProvider(service = org.openide.filesystems.FileFilterFactory.class)
131
    public static final class ResourceFilter extends Adapter {
140
    public static final class ResourceFilter
132
        public ResourceFilter() {
141
            implements org.openide.filesystems.FileFilterFactory {
133
            super(ResourceFileFilter.getInstance());
142
143
        @Override
144
        public FileFilter createFileFilter() {
145
            return new Adapter(ResourceFileFilter.getInstance());
134
        }
146
        }
135
    }
147
    }
136
148
137
    @ServiceProvider(service = org.netbeans.modules.openfile.OpenFileDialogFilter.class)
149
    @ServiceProvider(service = org.openide.filesystems.FileFilterFactory.class)
138
    public static final class QtFilter extends Adapter {
150
    public static final class QtFilter
139
        public QtFilter() {
151
            implements org.openide.filesystems.FileFilterFactory {
140
            super(QtFileFilter.getInstance());
152
153
        @Override
154
        public FileFilter createFileFilter() {
155
            return new Adapter(QtFileFilter.getInstance());
141
        }
156
        }
142
    }
157
    }
143
}
158
}
(-)a/java.source/nbproject/project.xml (-1 / +1 lines)
Lines 343-349 Link Here
343
                    <build-prerequisite/>
343
                    <build-prerequisite/>
344
                    <compile-dependency/>
344
                    <compile-dependency/>
345
                    <run-dependency>
345
                    <run-dependency>
346
                        <specification-version>7.58</specification-version>
346
                        <specification-version>7.64</specification-version>
347
                    </run-dependency>
347
                    </run-dependency>
348
                </dependency>
348
                </dependency>
349
                <dependency>
349
                <dependency>
(-)a/java.source/src/org/netbeans/modules/java/JavaFileFilterFactory.java (+60 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.java;
43
44
import javax.swing.filechooser.FileFilter;
45
import org.openide.filesystems.FileFilterFactory;
46
import org.openide.util.lookup.ServiceProvider;
47
48
/**
49
 * Open File Dialog filter for Java files.
50
 *
51
 * @author jhavlin
52
 */
53
@ServiceProvider(service = FileFilterFactory.class)
54
public class JavaFileFilterFactory implements FileFilterFactory {
55
56
    @Override
57
    public FileFilter createFileFilter() {
58
        return Support.createFilterForMimeType("text/x-java");          //NOI18N
59
    }
60
}
(-)a/openide.filesystems/apichanges.xml (+13 lines)
Lines 49-54 Link Here
49
        <apidef name="filesystems">Filesystems API</apidef>
49
        <apidef name="filesystems">Filesystems API</apidef>
50
    </apidefs>
50
    </apidefs>
51
    <changes>
51
    <changes>
52
        <change id="FileFilterFactory">
53
            <api name="filesystems"/>
54
            <summary>FileFilterFactory added.</summary>
55
            <date day="1" month="8" year="2012"/>
56
            <author login="jhavlin"/>
57
            <compatibility addition="yes" binary="compatible" source="compatible" deprecation="no" semantic="compatible" modification="no" deletion="no"/>
58
            <description>
59
                Added class FileFilterFactory, that is helpful when implementing
60
                custom file filters for Open File dialog.
61
            </description>
62
            <class package="org.openide.filesystems" name="FileFilterFactory"/>
63
            <issue number="209998"/>
64
        </change>
52
        <change id="recursive-listener-with-filter">
65
        <change id="recursive-listener-with-filter">
53
            <api name="filesystems"/>
66
            <api name="filesystems"/>
54
            <summary>addRecursiveListener with a filter</summary>
67
            <summary>addRecursiveListener with a filter</summary>
(-)a/openide.filesystems/manifest.mf (-1 / +1 lines)
Lines 2-6 Link Here
2
OpenIDE-Module: org.openide.filesystems
2
OpenIDE-Module: org.openide.filesystems
3
OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties
4
OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml
4
OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml
5
OpenIDE-Module-Specification-Version: 7.63
5
OpenIDE-Module-Specification-Version: 7.64
6
6
(-)a/openide.filesystems/src/org/openide/filesystems/FileFilterFactory.java (+294 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.openide.filesystems;
43
44
import java.io.File;
45
import java.util.Arrays;
46
import java.util.Collections;
47
import java.util.List;
48
import java.util.logging.Level;
49
import java.util.logging.Logger;
50
import javax.swing.filechooser.FileFilter;
51
52
/**
53
 * Class for registering custom {@link FileFilter file filters} that will be
54
 * used in Open File dialog.
55
 * <p>Example implementation:</p>
56
 * <pre>
57
 * {@literal @}{@code ServiceProvider(service=FileFilterFactory.class)
58
 * public class TXTFilterFactory extends FileFilterFactory {
59
 *
60
 *   public FileFilter createFileFilter() {
61
 *     return Support.createFilterForExtensions(<bundle-lookup>, ".txt"); //NOI18N
62
 *     // or return Support.createFilterForMimeType("text/plain");        //NOI18N
63
 *   }
64
 * }}
65
 * </pre>
66
 *
67
 * @author jhavlin, jlahoda
68
 * @since 7.64
69
 */
70
public interface FileFilterFactory {
71
72
    /**
73
     * Create a new instance of {@link FileFilter}. You should use support
74
     * method {@link Support#createFilterForMimeType(String)} or
75
     * {@link Support#createFilterForExtensions(String, String[])} to ensure
76
     * consistent behavior and appearance.
77
     */
78
    public FileFilter createFileFilter();
79
80
    /**
81
     * Support methods for creating custom file filters.
82
     */
83
    public static class Support {
84
85
        /**
86
         * The logger.
87
         */
88
        private static final Logger LOG = Logger.getLogger(
89
                FileFilterFactory.class.getName());
90
91
        /**
92
         * Hide the default constructor.
93
         */
94
        private Support() {
95
        }
96
97
        /**
98
         * Create file filter that accepts files of specified MIME type.
99
         *
100
         * @param mimeType MIME type of accepted files (e.g. "text/plain").
101
         */
102
        public static FileFilter createFilterForMimeType(String mimeType) {
103
            return new MimeFilter(mimeType);
104
        }
105
106
        /**
107
         * Create file filter that accepts files with specified extensions.
108
         *
109
         * @param displayName Display name of the filter (e.g. "Text Files").
110
         * @param allowedExtensions Array of allowed extensions (e.g. {".txt",
111
         * ".log"})
112
         */
113
        public static FileFilter createFilterForExtensions(String displayName,
114
                String... allowedExtensions) {
115
            return new ExtensionFilter(displayName, allowedExtensions);
116
        }
117
118
        /**
119
         * Construct description for {@link FileFilter} that accepts files with
120
         * specified extension.
121
         *
122
         * @param displayName Human readable display name (e.g. "HTML files")
123
         * @param extensions List of allowed extensions (e.g. {".htm",
124
         * ".html"}).
125
         *
126
         * @return Display name (description) for the filter.
127
         */
128
        public static String constructFilterDisplayName(String displayName,
129
                String... extensions) {
130
            StringBuilder sb = new StringBuilder(displayName);
131
            sb.append(" ");                                             //NOI18N
132
            sb.append(Arrays.asList(extensions).toString());
133
            return sb.toString();
134
        }
135
136
        /**
137
         * Construct description for {@link FileFilter} that accepts files of
138
         * specified MIME type.
139
         *
140
         * @param mimeType MIME type of the file filter.
141
         *
142
         * @return Display name (description) for the filter.
143
         *
144
         */
145
        public static String constructFilterDisplayName(String mimeType) {
146
            return constructFilterDisplayName(getMimeDisplayName(mimeType),
147
                    toExtArray(FileUtil.getMIMETypeExtensions(mimeType)));
148
        }
149
150
        /**
151
         * Check whether passed file is accepted by filter for specified list of
152
         * extensions.
153
         *
154
         * @param file File whose extension is checked.
155
         * @param extensions List of allowed extensions.
156
         *
157
         * @return True if the file ends with one of allowed extensions, false
158
         * otherwise.
159
         *
160
         * @see FileFilterSupport
161
         */
162
        public static boolean accept(File file, String... extensions) {
163
            if (file != null) {
164
                if (file.isDirectory()) {
165
                    return true;
166
                }
167
                for (String ext : extensions) {
168
                    if (compareSuffixes(file.getName(), ext)) {
169
                        return true;
170
                    }
171
                }
172
            }
173
            return false;
174
        }
175
176
        /**
177
         * Check whether passed file is accepted by filter for specified MIME
178
         * type.
179
         *
180
         * @param file File that is checked for a MIME type.
181
         * @param mimeType Accepted MIME type.
182
         *
183
         * @return True if file {@code file} is of MIME type {@code mimeType},
184
         * false otherwise.
185
         *
186
         * @see FileFilterSupport
187
         */
188
        public static boolean accept(File file, String mimeType) {
189
            List<String> exts = FileUtil.getMIMETypeExtensions(mimeType);
190
            return accept(file, toExtArray(exts));
191
        }
192
193
        /**
194
         * Get display name for a MIME type.
195
         *
196
         * @param mimeType MIME type (e.g. "java").
197
         * @return Display name for the MIME type (e.g. "Java Files"), or the
198
         * MIME type itself if no display name has been set.
199
         */
200
        private static String getMimeDisplayName(String mimeType) {
201
            try {
202
                FileObject factoriesFO = FileUtil.getConfigFile(
203
                        "Loaders/" + mimeType + "/Factories");          //NOI18N
204
                if (factoriesFO != null) {
205
                    FileObject[] children = factoriesFO.getChildren();
206
                    for (FileObject child : children) {
207
                        String childName = child.getNameExt();
208
                        String displayName = FileUtil.getConfigRoot().
209
                                getFileSystem().getStatus().
210
                                annotateName(childName,
211
                                Collections.singleton(child));
212
                        if (!childName.equals(displayName)) {
213
                            return displayName;
214
                        }
215
                    }
216
                }
217
            } catch (Exception e) {
218
                LOG.log(Level.WARNING, null, e);
219
            }
220
            return mimeType;
221
        }
222
223
        /**
224
         * Check whether the given filename has required suffex.
225
         */
226
        private static boolean compareSuffixes(String fileName, String suffix) {
227
            return fileName.toUpperCase().endsWith(suffix.toUpperCase());
228
        }
229
230
        /**
231
         * Convert a list of strings to a string array.
232
         *
233
         * @param listOfStrings List of extensions, without starting perios
234
         * (e.g. "txt", "java").
235
         * @return Array of extensions, with starting period (e.g. ".txt",
236
         * ".java").
237
         */
238
        private static String[] toExtArray(List<String> listOfStrings) {
239
            String[] array = new String[listOfStrings.size()];
240
            int index = 0;
241
            for (String ext : listOfStrings) {
242
                array[index++] = "." + ext;                             //NOI18N
243
            }
244
            return array;
245
        }
246
247
        /**
248
         * Type of filters returned by {@link #createFilterForMimeType(String)}.
249
         */
250
        private static class MimeFilter extends FileFilter {
251
252
            private String mimeType;
253
254
            public MimeFilter(String mimeType) {
255
                this.mimeType = mimeType;
256
            }
257
258
            @Override
259
            public boolean accept(File f) {
260
                return accept(f, mimeType);
261
            }
262
263
            @Override
264
            public String getDescription() {
265
                return constructFilterDisplayName(mimeType);
266
            }
267
        }
268
269
        /**
270
         * Type of filters returned by
271
         * {@link #createFilterForExtensions(String, String[])}.
272
         */
273
        private static class ExtensionFilter extends FileFilter {
274
275
            private String displayName;
276
            private String[] extensions;
277
278
            public ExtensionFilter(String displayName, String[] extensions) {
279
                this.displayName = displayName;
280
                this.extensions = extensions;
281
            }
282
283
            @Override
284
            public boolean accept(File f) {
285
                return accept(f, extensions);
286
            }
287
288
            @Override
289
            public String getDescription() {
290
                return constructFilterDisplayName(displayName, extensions);
291
            }
292
        }
293
    }
294
}
(-)a/openide.filesystems/test/unit/src/org/openide/filesystems/FileFilterFactoryTest.java (+78 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.openide.filesystems;
43
44
import java.io.File;
45
import javax.swing.filechooser.FileFilter;
46
import static org.junit.Assert.*;
47
import org.junit.Test;
48
49
/**
50
 * Test default implementation of OpenFileDialogFilter.
51
 *
52
 * @author jhavlin
53
 */
54
public class FileFilterFactoryTest {
55
56
    private final FileFilter filter =
57
            FileFilterFactory.Support.createFilterForExtensions(
58
            "Custom Example Files", new String[]{".xyz", ".uvw"});
59
60
    public FileFilterFactoryTest() {
61
    }
62
63
    @Test
64
    public void testAccept() {
65
        assertTrue(filter.accept(new File("test.xyz")));
66
        assertTrue(filter.accept(new File("test.XYZ")));
67
        assertTrue(filter.accept(new File("test.uvw")));
68
        assertTrue(filter.accept(new File("test.UVW")));
69
        assertFalse(filter.accept(new File("test.java")));
70
    }
71
72
    @Test
73
    public void testGetDescription() {
74
        assertEquals(
75
                "Custom Example Files [.xyz, .uvw]",
76
                filter.getDescription());
77
    }
78
}
(-)a/utilities/nbproject/project.xml (-3 / +1 lines)
Lines 96-102 Link Here
96
                    <build-prerequisite/>
96
                    <build-prerequisite/>
97
                    <compile-dependency/>
97
                    <compile-dependency/>
98
                    <run-dependency>
98
                    <run-dependency>
99
                        <specification-version>7.58</specification-version>
99
                        <specification-version>7.64</specification-version>
100
                    </run-dependency>
100
                    </run-dependency>
101
                </dependency>
101
                </dependency>
102
                <dependency>
102
                <dependency>
Lines 182-192 Link Here
182
                </test-type>
182
                </test-type>
183
            </test-dependencies>
183
            </test-dependencies>
184
            <friend-packages>
184
            <friend-packages>
185
                <friend>org.netbeans.modules.cnd.utils</friend>
186
                <friend>org.netbeans.modules.utilities.project</friend>
185
                <friend>org.netbeans.modules.utilities.project</friend>
187
                <friend>org.netbeans.modules.visualweb.gravy</friend>
186
                <friend>org.netbeans.modules.visualweb.gravy</friend>
188
                <package>org.netbeans.modules.openfile</package>
187
                <package>org.netbeans.modules.openfile</package>
189
                <package>org.netbeans.modules.search</package>
190
            </friend-packages>
188
            </friend-packages>
191
        </data>
189
        </data>
192
    </configuration>
190
    </configuration>
(-)a/utilities/src/org/netbeans/modules/openfile/Bundle.properties (-1 lines)
Lines 41-47 Link Here
41
# made subject to such option by the copyright holder.
41
# made subject to such option by the copyright holder.
42
42
43
# OpenFileDialogFilter
43
# OpenFileDialogFilter
44
OFDFD_Java=Java Files
45
OFDFD_Txt=Text Files
44
OFDFD_Txt=Text Files
46
45
47
# OpenFileAction
46
# OpenFileAction
(-)a/utilities/src/org/netbeans/modules/openfile/FileChooser.java (-29 / +15 lines)
Lines 52-60 Link Here
52
import javax.swing.JLabel;
52
import javax.swing.JLabel;
53
import javax.swing.JPanel;
53
import javax.swing.JPanel;
54
import javax.swing.filechooser.FileFilter;
54
import javax.swing.filechooser.FileFilter;
55
import javax.swing.filechooser.FileNameExtensionFilter;
56
import org.openide.DialogDisplayer;
55
import org.openide.DialogDisplayer;
57
import org.openide.NotifyDescriptor;
56
import org.openide.NotifyDescriptor;
57
import org.openide.filesystems.FileFilterFactory;
58
import org.openide.util.Lookup;
58
import org.openide.util.Lookup;
59
import org.openide.util.NbBundle;
59
import org.openide.util.NbBundle;
60
import org.openide.util.lookup.ServiceProvider;
60
import org.openide.util.lookup.ServiceProvider;
Lines 83-92 Link Here
83
     * @see javax.swing.JFileChooser
83
     * @see javax.swing.JFileChooser
84
     */
84
     */
85
    public void addChoosableFileFilters() {
85
    public void addChoosableFileFilters() {
86
        for (OpenFileDialogFilter f :
86
        // Add legacy filters
87
                    Lookup.getDefault().lookupAll(OpenFileDialogFilter.class)) {
87
        for (org.netbeans.modules.openfile.OpenFileDialogFilter f :
88
                Lookup.getDefault().lookupAll(
89
                org.netbeans.modules.openfile.OpenFileDialogFilter.class)) {
88
            addChoosableFileFilter(f);
90
            addChoosableFileFilter(f);
89
        }
91
        }
92
        for (FileFilterFactory f :
93
                Lookup.getDefault().lookupAll(FileFilterFactory.class)) {
94
            addChoosableFileFilter(f.createFileFilter());
95
        }
90
    }
96
    }
91
    
97
    
92
    @Override
98
    @Override
Lines 130-162 Link Here
130
        }
136
        }
131
    }
137
    }
132
138
133
139
    @ServiceProvider(service = FileFilterFactory.class)
134
    @ServiceProvider(service=org.netbeans.modules.openfile.OpenFileDialogFilter.class)
140
    public static class TxtFileFilterFactory implements FileFilterFactory {
135
    public static class JavaFilesFilter extends OpenFileDialogFilter {
136
141
137
        @Override
142
        @Override
138
        public String getDescriptionString() {
143
        public FileFilter createFileFilter() {
139
            return NbBundle.getMessage(getClass(), "OFDFD_Java"); // NOI18N
144
            String name = NbBundle.getMessage(getClass(), "OFDFD_Txt"); //NOI18N
145
            return Support.createFilterForExtensions(name, ".txt");     //NOI18N
140
        }
146
        }
141
147
    } // End of TxtFileFilterFactory
142
        @Override
143
        public String[] getSuffixes() {
144
            return new String[] {".java"};
145
        }
146
147
    }
148
149
    @ServiceProvider(service=OpenFileDialogFilter.class)
150
    public static class TxtFileFilter
151
            extends OpenFileDialogFilter.ExtensionFilter {
152
153
        @Override
154
        public FileNameExtensionFilter getFilter() {
155
            return new FileNameExtensionFilter(
156
                            NbBundle.getMessage(getClass(), "OFDFD_Txt"),
157
                            "txt"); // NOI18N
158
        }
159
160
    } // End of TxtFileFilter
161
162
}
148
}
(-)a/utilities/src/org/netbeans/modules/openfile/OpenFileDialogFilter.java (+6 lines)
Lines 47-52 Link Here
47
import java.util.Arrays;
47
import java.util.Arrays;
48
import javax.swing.filechooser.FileFilter;
48
import javax.swing.filechooser.FileFilter;
49
import javax.swing.filechooser.FileNameExtensionFilter;
49
import javax.swing.filechooser.FileNameExtensionFilter;
50
import org.openide.filesystems.FileFilterFactory;
50
51
51
/**
52
/**
52
 * {@code OpenFileDialogFilter} is an abstract class used by {@link FileChooser}
53
 * {@code OpenFileDialogFilter} is an abstract class used by {@link FileChooser}
Lines 107-113 Link Here
107
 * @see OpenFileDialogFilter.ExtensionFilter
108
 * @see OpenFileDialogFilter.ExtensionFilter
108
 *
109
 *
109
 * @author Victor G. Vasilyev <vvg@netbeans.org>
110
 * @author Victor G. Vasilyev <vvg@netbeans.org>
111
 *
112
 * @deprecated Use {@link FileFilter} and {@link FileFilterFactory}.
110
 */
113
 */
114
@Deprecated
111
public abstract class OpenFileDialogFilter extends FileFilter {
115
public abstract class OpenFileDialogFilter extends FileFilter {
112
116
113
    /**
117
    /**
Lines 258-264 Link Here
258
     * @see OpenFileDialogFilter
262
     * @see OpenFileDialogFilter
259
     * @see FileNameExtensionFilter
263
     * @see FileNameExtensionFilter
260
     *
264
     *
265
     * @deprecated Use {@link FileFilter} and {@link FileFilterFactory}.
261
     */
266
     */
267
    @Deprecated
262
    public static abstract class ExtensionFilter extends OpenFileDialogFilter {
268
    public static abstract class ExtensionFilter extends OpenFileDialogFilter {
263
269
264
        private static final char EXTENSION_SEPARATOR = '.';
270
        private static final char EXTENSION_SEPARATOR = '.';

Return to bug 209998