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/java.source/nbproject/project.xml (-1 / +1 lines)
Lines 327-333 Link Here
327
                    <build-prerequisite/>
327
                    <build-prerequisite/>
328
                    <compile-dependency/>
328
                    <compile-dependency/>
329
                    <run-dependency>
329
                    <run-dependency>
330
                        <specification-version>6.2</specification-version>
330
                        <specification-version>7.49</specification-version>
331
                    </run-dependency>
331
                    </run-dependency>
332
                </dependency>
332
                </dependency>
333
                <dependency>
333
                <dependency>
(-)a/java.source/src/org/netbeans/modules/java/JavaOpenFileDialogFilter.java (+68 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 java.io.File;
45
import javax.swing.filechooser.FileFilter;
46
import org.openide.awt.FileFilterSupport;
47
import static org.openide.awt.FileFilterSupport.FILE_FILTER_LOOKUP_PATH;
48
import org.openide.util.lookup.ServiceProvider;
49
50
/**
51
 * Open File Dialog filter for Java files.
52
 *
53
 * @author jhavlin
54
 */
55
@ServiceProvider(service = FileFilter.class, path = FILE_FILTER_LOOKUP_PATH)
56
public class JavaOpenFileDialogFilter extends FileFilter {
57
58
    @Override
59
    public String getDescription() {
60
        return FileFilterSupport.constructFilterDisplayName(
61
                "text/x-java");                                         //NOI18N
62
    }
63
64
    @Override
65
    public boolean accept(File file) {
66
        return FileFilterSupport.accept(file, "text/x-java");           //NOI18N
67
    }
68
}
(-)a/openide.awt/apichanges.xml (+13 lines)
Lines 50-55 Link Here
50
<apidef name="awt">AWT API</apidef>
50
<apidef name="awt">AWT API</apidef>
51
</apidefs>
51
</apidefs>
52
<changes>
52
<changes>
53
    <change id="FileFilterSupport">
54
      <api name="awt"/>
55
      <summary>FileFilterSupport added.</summary>
56
      <date day="30" month="7" year="2012"/>
57
      <author login="jhavlin"/>
58
      <compatibility addition="yes" binary="compatible" source="compatible" deprecation="no" semantic="compatible" modification="no" deletion="no"/>
59
      <description>
60
          Added class FileFilterSupport, that is helpful when implementing
61
          custom file filters to Open File dialog.
62
      </description>
63
      <class package="org.openide.awt" name="FileFilterSupport"/>
64
      <issue number="209998"/>
65
    </change>
53
    <change id="TabbedPaneFactorySubClass">
66
    <change id="TabbedPaneFactorySubClass">
54
      <api name="awt"/>
67
      <api name="awt"/>
55
      <summary>TabbedPaneFactory can be subclassed.</summary>
68
      <summary>TabbedPaneFactory can be subclassed.</summary>
(-)a/openide.awt/manifest.mf (-1 / +1 lines)
Lines 2-6 Link Here
2
OpenIDE-Module: org.openide.awt
2
OpenIDE-Module: org.openide.awt
3
OpenIDE-Module-Localizing-Bundle: org/openide/awt/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/openide/awt/Bundle.properties
4
AutoUpdate-Essential-Module: true
4
AutoUpdate-Essential-Module: true
5
OpenIDE-Module-Specification-Version: 7.48
5
OpenIDE-Module-Specification-Version: 7.49
6
6
(-)a/openide.awt/src/org/openide/awt/FileFilterSupport.java (+222 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.awt;
43
44
import java.io.File;
45
import java.io.FileFilter;
46
import java.util.Arrays;
47
import java.util.Collections;
48
import java.util.List;
49
import java.util.logging.Level;
50
import java.util.logging.Logger;
51
import org.openide.filesystems.FileObject;
52
import org.openide.filesystems.FileUtil;
53
54
/**
55
 * Support methods for {@link FileFilter file filters} that can be used, for
56
 * example, in Open File dialog. <p> See issue #209998. </p> <p> The following
57
 * example shows how to implement and register a custom filter. </p>
58
 * <pre>
59
 * {@literal
60
 *
61
 * @}{@code ServiceProvider(service=FileFilter.class, path=FileFilterSupport.FILE_FILTER_LOOKUP_PATH)
62
 * public class TXTFilter extends FileFilter {
63
 *   private static final String[] EXTENSIONS = new String[] {".txt"};
64
 *
65
 *   public boolean accept(File f) {
66
 *     return FileFilterSupport.accept(f, EXTENSIONS);
67
 *   }
68
 *
69
 *   public String getDescription() {
70
 *     return FileFilterSupport.constructFilterDisplayName(<bundle-lookup>, EXTENSIONS);
71
 *   }
72
 * }}
73
 * </pre>
74
 *
75
 * @author jhavlin, jlahoda
76
 * @since 7.49
77
 */
78
public final class FileFilterSupport {
79
80
    /**
81
     * Path to use when registering custom filters.
82
     */
83
    public static final String FILE_FILTER_LOOKUP_PATH =
84
            "OpenFileDialogFilter";                                     //NOI18N
85
    /**
86
     * The logger.
87
     */
88
    private static final Logger LOG = Logger.getLogger(
89
            FileFilterSupport.class.getName());
90
91
    /**
92
     * Hide the default constructor.
93
     */
94
    private FileFilterSupport() {
95
    }
96
97
    /**
98
     * Construct description for {@link FileFilter} that accepts files with
99
     * specified extension.
100
     *
101
     * @param displayName Human readable display name (e.g. "HTML files")
102
     * @param extensions List of allowed extensions (e.g. {".htm", ".html"}).
103
     *
104
     * @return Display name (description) for the filter.
105
     */
106
    public static String constructFilterDisplayName(String displayName,
107
            String... extensions) {
108
        StringBuilder sb = new StringBuilder(displayName);
109
        sb.append(" ");                                                 //NOI18N
110
        sb.append(Arrays.asList(extensions).toString());
111
        return sb.toString();
112
    }
113
114
    /**
115
     * Construct description for {@link FileFilter} that accepts files of
116
     * specified MIME type.
117
     *
118
     * @param mimeType MIME type of the file filter.
119
     *
120
     * @return Display name (description) for the filter.
121
     *
122
     */
123
    public static String constructFilterDisplayName(String mimeType) {
124
        return constructFilterDisplayName(getMimeDisplayName(mimeType),
125
                toExtArray(FileUtil.getMIMETypeExtensions(mimeType)));
126
    }
127
128
    /**
129
     * Check whether passed file is accepted by filter for specified list of
130
     * extensions.
131
     *
132
     * @param file File whose extension is checked.
133
     * @param extensions List of allowed extensions.
134
     *
135
     * @return True if the file ends with one of allowed extensions, false
136
     * otherwise.
137
     *
138
     * @see FileFilterSupport
139
     */
140
    public static boolean accept(File file, String... extensions) {
141
        if (file != null) {
142
            if (file.isDirectory()) {
143
                return true;
144
            }
145
            for (String ext : extensions) {
146
                if (compareSuffixes(file.getName(), ext)) {
147
                    return true;
148
                }
149
            }
150
        }
151
        return false;
152
    }
153
154
    /**
155
     * Check whether passed file is accepted by filter for specified MIME type.
156
     *
157
     * @param file File that is checked for a MIME type.
158
     * @param mimeType Accepted MIME type.
159
     *
160
     * @return True if file {@code file} is of MIME type {@code mimeType}, false
161
     * otherwise.
162
     *
163
     * @see FileFilterSupport
164
     */
165
    public static boolean accept(File file, String mimeType) {
166
        List<String> exts = FileUtil.getMIMETypeExtensions(mimeType);
167
        return accept(file, toExtArray(exts));
168
    }
169
170
    /**
171
     * Get display name for a MIME type.
172
     *
173
     * @param mimeType MIME type (e.g. "java").
174
     * @return Display name for the MIME type (e.g. "Java Files"), or the MIME
175
     * type itself if no display name has been set.
176
     */
177
    private static String getMimeDisplayName(String mimeType) {
178
        try {
179
            FileObject factoriesFO = FileUtil.getConfigFile(
180
                    "Loaders/" + mimeType + "/Factories");               //NOI18N
181
            if (factoriesFO != null) {
182
                FileObject[] children = factoriesFO.getChildren();
183
                for (FileObject child : children) {
184
                    String childName = child.getNameExt();
185
                    String displayName = FileUtil.getConfigRoot().
186
                            getFileSystem().getStatus().
187
                            annotateName(childName,
188
                            Collections.singleton(child));
189
                    if (!childName.equals(displayName)) {
190
                        return displayName;
191
                    }
192
                }
193
            }
194
        } catch (Exception e) {
195
            LOG.log(Level.WARNING, null, e);
196
        }
197
        return mimeType;
198
    }
199
200
    /**
201
     * Check whether the given filename has required suffex.
202
     */
203
    private static boolean compareSuffixes(String fileName, String suffix) {
204
        return fileName.toUpperCase().endsWith(suffix.toUpperCase());
205
    }
206
207
    /**
208
     * Convert a list of strings to a string array.
209
     *
210
     * @param listOfStrings List of extensions, without starting perios (e.g.
211
     * "txt", "java").
212
     * @return Array of extensions, with starting period (e.g. ".txt", ".java").
213
     */
214
    private static String[] toExtArray(List<String> listOfStrings) {
215
        String[] array = new String[listOfStrings.size()];
216
        int index = 0;
217
        for (String ext : listOfStrings) {
218
            array[index++] = "." + ext;                                //NOI18N
219
        }
220
        return array;
221
    }
222
}
(-)a/openide.awt/test/unit/src/org/openide/awt/FileFilterSupportTest.java (+92 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.awt;
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 FileFilterSupportTest {
55
56
    private final FileFilter filter = new FileFilterImpl();
57
58
    public FileFilterSupportTest() {
59
    }
60
61
    @Test
62
    public void testAccept() {
63
        assertTrue(filter.accept(new File("test.xyz")));
64
        assertTrue(filter.accept(new File("test.XYZ")));
65
        assertTrue(filter.accept(new File("test.uvw")));
66
        assertTrue(filter.accept(new File("test.UVW")));
67
        assertFalse(filter.accept(new File("test.java")));
68
    }
69
70
    @Test
71
    public void testGetDescription() {
72
        assertEquals(
73
                "Custom Example Files [.xyz, .uvw]",
74
                filter.getDescription());
75
    }
76
77
    private static class FileFilterImpl extends FileFilter {
78
79
        private static final String[] EXTENSIONS = new String[]{".xyz", ".uvw"};
80
81
        @Override
82
        public String getDescription() {
83
            return FileFilterSupport.constructFilterDisplayName(
84
                    "Custom Example Files", EXTENSIONS);
85
        }
86
87
        @Override
88
        public boolean accept(File f) {
89
            return FileFilterSupport.accept(f, EXTENSIONS);
90
        }
91
    }
92
}
(-)a/utilities/nbproject/project.xml (-1 / +1 lines)
Lines 80-86 Link Here
80
                    <build-prerequisite/>
80
                    <build-prerequisite/>
81
                    <compile-dependency/>
81
                    <compile-dependency/>
82
                    <run-dependency>
82
                    <run-dependency>
83
                        <specification-version>7.27</specification-version>
83
                        <specification-version>7.49</specification-version>
84
                    </run-dependency>
84
                    </run-dependency>
85
                </dependency>
85
                </dependency>
86
                <dependency>
86
                <dependency>
(-)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 (-23 / +20 lines)
Lines 52-62 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.awt.FileFilterSupport;
58
import static org.openide.awt.FileFilterSupport.FILE_FILTER_LOOKUP_PATH;
58
import org.openide.util.Lookup;
59
import org.openide.util.Lookup;
59
import org.openide.util.NbBundle;
60
import org.openide.util.NbBundle;
61
import org.openide.util.lookup.Lookups;
60
import org.openide.util.lookup.ServiceProvider;
62
import org.openide.util.lookup.ServiceProvider;
61
63
62
/**
64
/**
Lines 83-90 Link Here
83
     * @see javax.swing.JFileChooser
85
     * @see javax.swing.JFileChooser
84
     */
86
     */
85
    public void addChoosableFileFilters() {
87
    public void addChoosableFileFilters() {
86
        for (OpenFileDialogFilter f :
88
        // Add legacy filters
87
                    Lookup.getDefault().lookupAll(OpenFileDialogFilter.class)) {
89
        for (org.netbeans.modules.openfile.OpenFileDialogFilter f :
90
                Lookup.getDefault().lookupAll(
91
                org.netbeans.modules.openfile.OpenFileDialogFilter.class)) {
92
            addChoosableFileFilter(f);
93
        }
94
        Lookup lkp = Lookups.forPath(FileFilterSupport.FILE_FILTER_LOOKUP_PATH);
95
        for (FileFilter f : lkp.lookupAll(FileFilter.class)) {
88
            addChoosableFileFilter(f);
96
            addChoosableFileFilter(f);
89
        }
97
        }
90
    }
98
    }
Lines 130-162 Link Here
130
        }
138
        }
131
    }
139
    }
132
140
141
    @ServiceProvider(service = FileFilter.class, path = FILE_FILTER_LOOKUP_PATH)
142
    public static class TxtFileFilter extends FileFilter {
133
143
134
    @ServiceProvider(service=org.netbeans.modules.openfile.OpenFileDialogFilter.class)
144
        private static final String[] EXTENSIONS = new String[]{".txt"};//NOI18N
135
    public static class JavaFilesFilter extends OpenFileDialogFilter {
136
145
137
        @Override
146
        @Override
138
        public String getDescriptionString() {
147
        public boolean accept(File f) {
139
            return NbBundle.getMessage(getClass(), "OFDFD_Java"); // NOI18N
148
            return FileFilterSupport.accept(f, EXTENSIONS);
140
        }
149
        }
141
150
142
        @Override
151
        @Override
143
        public String[] getSuffixes() {
152
        public String getDescription() {
144
            return new String[] {".java"};
153
            return FileFilterSupport.constructFilterDisplayName(
154
                    NbBundle.getMessage(getClass(), "OFDFD_Txt"), //NOI18N
155
                    EXTENSIONS);
145
        }
156
        }
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
157
    } // End of TxtFileFilter
161
158
162
}
159
}
(-)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.awt.FileFilterSupport;
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 FileFilterSupport}.
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 FileFilterSupport}.
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