Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JFileChooser/8013442/Test8013442.java
38854 views
/*1* Copyright (c) 2013, 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* @bug 801344226* @summary Tests that at least one file filter is selected27* @author Sergey Malenkov28*/2930import java.io.File;31import java.util.concurrent.CountDownLatch;32import javax.swing.JFileChooser;33import javax.swing.JFrame;34import javax.swing.SwingUtilities;35import javax.swing.UIManager;36import javax.swing.UIManager.LookAndFeelInfo;37import javax.swing.filechooser.FileFilter;3839public class Test8013442 extends FileFilter implements Runnable, Thread.UncaughtExceptionHandler {40private static final CountDownLatch LATCH = new CountDownLatch(1);4142public static void main(String[] args) throws InterruptedException {43SwingUtilities.invokeLater(new Test8013442());44LATCH.await(); // workaround for jtreg45}4647private int index;48private LookAndFeelInfo[] infos;49private JFileChooser chooser;5051@Override52public boolean accept(File file) {53return !file.isFile() || file.getName().toLowerCase().endsWith(".txt");54}5556@Override57public String getDescription() {58return "Text files";59}6061@Override62public void run() {63if (this.infos == null) {64this.infos = UIManager.getInstalledLookAndFeels();65Thread.currentThread().setUncaughtExceptionHandler(this);66}67if (this.infos.length == this.index) {68LATCH.countDown(); // release main thread69} else if (this.chooser == null) {70// change LaF before creation of Swing components71LookAndFeelInfo info = this.infos[this.index];72System.out.println(info.getName());73try {74UIManager.setLookAndFeel(info.getClassName());75}76catch (Exception exception) {77throw new Error("could not change look and feel", exception);78}79// create and show new file chooser80JFrame frame = new JFrame(getClass().getSimpleName());81frame.add(this.chooser = new JFileChooser());82frame.setSize(800, 600);83frame.setLocationRelativeTo(null);84frame.setVisible(true);85SwingUtilities.invokeLater(this);86}87else {88int count = this.chooser.getChoosableFileFilters().length;89System.out.println("count = " + count + "; " + this.chooser.isAcceptAllFileFilterUsed());90if (count == 0) {91if (null != this.chooser.getFileFilter()) {92throw new Error("file filter is selected");93}94// close window and stop testing file chooser for current LaF95SwingUtilities.getWindowAncestor(this.chooser).dispose();96this.chooser = null;97this.index++;98} else {99if (null == this.chooser.getFileFilter()) {100throw new Error("file filter is not selected");101}102if (count == 2) {103// remove default file filter104this.chooser.setAcceptAllFileFilterUsed(false);105} else if (this.chooser.isAcceptAllFileFilterUsed()) {106// remove add file filter107this.chooser.addChoosableFileFilter(this);108} else {109// remove custom file filter110this.chooser.removeChoosableFileFilter(this);111}112}113SwingUtilities.invokeLater(this);114}115}116117public void uncaughtException(Thread thread, Throwable throwable) {118throwable.printStackTrace();119System.exit(1);120}121}122123124