1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.applet.Applet;
25import java.io.File;
26
27import javax.swing.JFileChooser;
28import javax.swing.SwingUtilities;
29import javax.swing.UIManager;
30import javax.swing.UnsupportedLookAndFeelException;
31import javax.swing.filechooser.FileFilter;
32
33public final class FileFilterDescription extends Applet {
34
35    @Override
36    public void init() {
37    }
38
39    @Override
40    public void start() {
41        try {
42            test();
43        } catch (Exception e) {
44            throw new RuntimeException(e);
45        }
46    }
47
48
49    public static void test() throws Exception {
50        final UIManager.LookAndFeelInfo[] infos = UIManager
51                .getInstalledLookAndFeels();
52        for (final UIManager.LookAndFeelInfo info : infos) {
53            SwingUtilities.invokeAndWait(() -> {
54                final JFileChooser chooser = new JFileChooser();
55                setLookAndFeel(info);
56                chooser.setAcceptAllFileFilterUsed(false);
57                chooser.setFileFilter(new CustomFileFilter());
58                SwingUtilities.updateComponentTreeUI(chooser);
59                chooser.showDialog(null, "Open");
60            });
61        }
62    }
63
64    private static void setLookAndFeel(final UIManager.LookAndFeelInfo info) {
65        try {
66            UIManager.setLookAndFeel(info.getClassName());
67        } catch (ClassNotFoundException | InstantiationException |
68                UnsupportedLookAndFeelException | IllegalAccessException e) {
69            throw new RuntimeException(e);
70        }
71    }
72
73    private static class CustomFileFilter extends FileFilter {
74
75        @Override
76        public boolean accept(final File f) {
77            return false;
78        }
79
80        @Override
81        public String getDescription() {
82            return "CustomFileFilter";
83        }
84    }
85}
86