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