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