Path: blob/master/test/jdk/javax/swing/JFileChooser/8013442/Test8013442.java
41154 views
/*1* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 801344227* @summary Tests that at least one file filter is selected28* @author Sergey Malenkov29*/3031import 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;3940public class Test8013442 extends FileFilter implements Runnable, Thread.UncaughtExceptionHandler {41private static final CountDownLatch LATCH = new CountDownLatch(1);4243public static void main(String[] args) throws InterruptedException {44SwingUtilities.invokeLater(new Test8013442());45LATCH.await(); // workaround for jtreg46}4748private int index;49private LookAndFeelInfo[] infos;50private JFileChooser chooser;5152@Override53public boolean accept(File file) {54return !file.isFile() || file.getName().toLowerCase().endsWith(".txt");55}5657@Override58public String getDescription() {59return "Text files";60}6162@Override63public void run() {64if (this.infos == null) {65this.infos = UIManager.getInstalledLookAndFeels();66Thread.currentThread().setUncaughtExceptionHandler(this);67}68if (this.infos.length == this.index) {69LATCH.countDown(); // release main thread70} else if (this.chooser == null) {71// change LaF before creation of Swing components72LookAndFeelInfo info = this.infos[this.index];73System.out.println(info.getName());74try {75UIManager.setLookAndFeel(info.getClassName());76}77catch (Exception exception) {78throw new Error("could not change look and feel", exception);79}80// create and show new file chooser81JFrame frame = new JFrame(getClass().getSimpleName());82frame.add(this.chooser = new JFileChooser());83frame.setSize(800, 600);84frame.setLocationRelativeTo(null);85frame.setVisible(true);86SwingUtilities.invokeLater(this);87}88else {89int count = this.chooser.getChoosableFileFilters().length;90System.out.println("count = " + count + "; " + this.chooser.isAcceptAllFileFilterUsed());91if (count == 0) {92if (null != this.chooser.getFileFilter()) {93throw new Error("file filter is selected");94}95// close window and stop testing file chooser for current LaF96SwingUtilities.getWindowAncestor(this.chooser).dispose();97this.chooser = null;98this.index++;99} else {100if (null == this.chooser.getFileFilter()) {101throw new Error("file filter is not selected");102}103if (count == 2) {104// remove default file filter105this.chooser.setAcceptAllFileFilterUsed(false);106} else if (this.chooser.isAcceptAllFileFilterUsed()) {107// remove add file filter108this.chooser.addChoosableFileFilter(this);109} else {110// remove custom file filter111this.chooser.removeChoosableFileFilter(this);112}113}114SwingUtilities.invokeLater(this);115}116}117118public void uncaughtException(Thread thread, Throwable throwable) {119throwable.printStackTrace();120System.exit(1);121}122}123124125