1 /* 2 * Copyright (c) 2016, 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 import java.awt.Component; 25 import java.awt.Container; 26 import java.awt.Robot; 27 import javax.swing.JComboBox; 28 import javax.swing.JFileChooser; 29 import javax.swing.JLabel; 30 import javax.swing.SwingUtilities; 31 import javax.swing.filechooser.FileFilter; 32 import javax.swing.filechooser.FileNameExtensionFilter; 33 34 /* 35 * @test 36 * @bug 8152677 37 * @requires (os.family == "mac") 38 * @summary [macosx] All files filter can't be selected in JFileChooser 39 * @run main SelectAllFilesFilterTest 40 */ 41 42 public class SelectAllFilesFilterTest { 43 44 private static final String LABEL_TEXT = "File Format:"; 45 private static volatile JFileChooser fileChooser; 46 private static JComboBox comboBox; 47 48 public static void main(String[] args) throws Exception { 49 50 SwingUtilities.invokeLater(SelectAllFilesFilterTest::createAndShowGUI); 51 52 while (fileChooser == null) { 53 Thread.sleep(100); 54 } 55 56 Robot robot = new Robot(); 57 robot.waitForIdle(); 58 59 SwingUtilities.invokeAndWait(() -> { 60 comboBox = findComboBox(fileChooser); 61 comboBox.setSelectedIndex(0); 62 }); 63 robot.waitForIdle(); 64 65 SwingUtilities.invokeAndWait(() -> { 66 int selectedIndex = comboBox.getSelectedIndex(); 67 fileChooser.setVisible(false); 68 69 if (selectedIndex != 0) { 70 throw new RuntimeException("Select All file filter is not selected!"); 71 } 72 }); 73 } 74 75 private static void createAndShowGUI() { 76 fileChooser = new JFileChooser(); 77 fileChooser.setAcceptAllFileFilterUsed(true); 78 fileChooser.setDialogType(JFileChooser.OPEN_DIALOG); 79 80 FileFilter txtFilter = new FileNameExtensionFilter("Text files", "txt"); 81 fileChooser.addChoosableFileFilter(txtFilter); 82 fileChooser.setFileFilter(txtFilter); 83 fileChooser.showOpenDialog(null); 84 } 85 86 private static JComboBox findComboBox(Component comp) { 87 88 if (comp instanceof JLabel) { 89 JLabel label = (JLabel) comp; 90 if (LABEL_TEXT.equals(label.getText())) { 91 return (JComboBox) label.getLabelFor(); 92 } 93 } 94 95 if (comp instanceof Container) { 96 Container cont = (Container) comp; 97 for (int i = 0; i < cont.getComponentCount(); i++) { 98 99 JComboBox result = findComboBox(cont.getComponent(i)); 100 if (result != null) { 101 return result; 102 } 103 } 104 } 105 106 return null; 107 } 108 }