1/*
2 * Copyright (c) 2013, 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
24/*
25 * @test
26 * @bug 8013442
27 * @summary Tests that at least one file filter is selected
28 * @author Sergey Malenkov
29 */
30
31import java.io.File;
32import java.util.concurrent.CountDownLatch;
33import javax.swing.JFileChooser;
34import javax.swing.JFrame;
35import javax.swing.SwingUtilities;
36import javax.swing.UIManager;
37import javax.swing.UIManager.LookAndFeelInfo;
38import javax.swing.filechooser.FileFilter;
39
40public class Test8013442 extends FileFilter implements Runnable, Thread.UncaughtExceptionHandler {
41    private static final CountDownLatch LATCH = new CountDownLatch(1);
42
43    public static void main(String[] args) throws InterruptedException {
44        SwingUtilities.invokeLater(new Test8013442());
45        LATCH.await(); // workaround for jtreg
46    }
47
48    private int index;
49    private LookAndFeelInfo[] infos;
50    private JFileChooser chooser;
51
52    @Override
53    public boolean accept(File file) {
54        return !file.isFile() || file.getName().toLowerCase().endsWith(".txt");
55    }
56
57    @Override
58    public String getDescription() {
59        return "Text files";
60    }
61
62    @Override
63    public void run() {
64        if (this.infos == null) {
65            this.infos = UIManager.getInstalledLookAndFeels();
66            Thread.currentThread().setUncaughtExceptionHandler(this);
67        }
68        if (this.infos.length == this.index) {
69            LATCH.countDown(); // release main thread
70        } else if (this.chooser == null) {
71            // change LaF before creation of Swing components
72            LookAndFeelInfo info = this.infos[this.index];
73            System.out.println(info.getName());
74            try {
75                UIManager.setLookAndFeel(info.getClassName());
76            }
77            catch (Exception exception) {
78                throw new Error("could not change look and feel", exception);
79            }
80            // create and show new file chooser
81            JFrame frame = new JFrame(getClass().getSimpleName());
82            frame.add(this.chooser = new JFileChooser());
83            frame.setSize(800, 600);
84            frame.setLocationRelativeTo(null);
85            frame.setVisible(true);
86            SwingUtilities.invokeLater(this);
87        }
88        else {
89            int count = this.chooser.getChoosableFileFilters().length;
90            System.out.println("count = " + count + "; " + this.chooser.isAcceptAllFileFilterUsed());
91            if (count == 0) {
92                if (null != this.chooser.getFileFilter()) {
93                    throw new Error("file filter is selected");
94                }
95                // close window and stop testing file chooser for current LaF
96                SwingUtilities.getWindowAncestor(this.chooser).dispose();
97                this.chooser = null;
98                this.index++;
99            } else {
100                if (null == this.chooser.getFileFilter()) {
101                    throw new Error("file filter is not selected");
102                }
103                if (count == 2) {
104                    // remove default file filter
105                    this.chooser.setAcceptAllFileFilterUsed(false);
106                } else if (this.chooser.isAcceptAllFileFilterUsed()) {
107                    // remove add file filter
108                    this.chooser.addChoosableFileFilter(this);
109                } else {
110                    // remove custom file filter
111                    this.chooser.removeChoosableFileFilter(this);
112                }
113            }
114            SwingUtilities.invokeLater(this);
115        }
116    }
117
118    public void uncaughtException(Thread thread, Throwable throwable) {
119        throwable.printStackTrace();
120        System.exit(1);
121    }
122}
123