8152677: [macosx] All files filter can't be selected in JFileChooser
authoralexsch
Thu, 28 Apr 2016 23:48:37 +0400
changeset 38387 ea8cc6a2fef2
parent 38386 d7e7d592d396
child 38388 84ab1f133f16
8152677: [macosx] All files filter can't be selected in JFileChooser Reviewed-by: serb
jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaFileChooserUI.java
jdk/test/javax/swing/JFileChooser/8152677/SelectAllFilesFilterTest.java
--- a/jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaFileChooserUI.java	Thu Apr 28 19:35:09 2016 +0300
+++ b/jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaFileChooserUI.java	Thu Apr 28 23:48:37 2016 +0400
@@ -112,7 +112,7 @@
     private AncestorListener ancestorListener = null;
     private DropTarget dragAndDropTarget = null;
 
-    private final AcceptAllFileFilter acceptAllFileFilter = new AcceptAllFileFilter();
+    private static final AcceptAllFileFilter acceptAllFileFilter = new AcceptAllFileFilter();
 
     private AquaFileSystemModel model;
 
@@ -997,7 +997,7 @@
     // *****************************************
     // ***** default AcceptAll file filter *****
     // *****************************************
-    protected class AcceptAllFileFilter extends FileFilter {
+    private static class AcceptAllFileFilter extends FileFilter {
         public AcceptAllFileFilter() {
         }
 
@@ -1305,6 +1305,8 @@
     protected class FilterComboBoxModel extends AbstractListModel<FileFilter> implements ComboBoxModel<FileFilter>,
             PropertyChangeListener {
         protected FileFilter[] filters;
+        Object oldFileFilter = getFileChooser().getFileFilter();
+
         protected FilterComboBoxModel() {
             super();
             filters = getFileChooser().getChoosableFileFilters();
@@ -1321,12 +1323,17 @@
         }
 
         public void setSelectedItem(Object filter) {
-            if (filter != null && !containsFileFilter(filter)) {
+            if (filter != null && !isSelectedFileFilterInModel(filter)) {
+                oldFileFilter = filter;
                 getFileChooser().setFileFilter((FileFilter) filter);
                 fireContentsChanged(this, -1, -1);
             }
         }
 
+        private boolean isSelectedFileFilterInModel(Object filter) {
+            return Objects.equals(filter, oldFileFilter);
+        }
+
         public Object getSelectedItem() {
             // Ensure that the current filter is in the list.
             // NOTE: we shouldnt' have to do this, since JFileChooser adds
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JFileChooser/8152677/SelectAllFilesFilterTest.java	Thu Apr 28 23:48:37 2016 +0400
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.Component;
+import java.awt.Container;
+import java.awt.Robot;
+import javax.swing.JComboBox;
+import javax.swing.JFileChooser;
+import javax.swing.JLabel;
+import javax.swing.SwingUtilities;
+import javax.swing.filechooser.FileFilter;
+import javax.swing.filechooser.FileNameExtensionFilter;
+
+/*
+ * @test
+ * @bug 8152677
+ * @requires (os.family == "mac")
+ * @summary [macosx] All files filter can't be selected in JFileChooser
+ * @run main SelectAllFilesFilterTest
+ */
+
+public class SelectAllFilesFilterTest {
+
+    private static final String LABEL_TEXT = "File Format:";
+    private static volatile JFileChooser fileChooser;
+    private static JComboBox comboBox;
+
+    public static void main(String[] args) throws Exception {
+
+        SwingUtilities.invokeLater(SelectAllFilesFilterTest::createAndShowGUI);
+
+        while (fileChooser == null) {
+            Thread.sleep(100);
+        }
+
+        Robot robot = new Robot();
+        robot.waitForIdle();
+
+        SwingUtilities.invokeAndWait(() -> {
+            comboBox = findComboBox(fileChooser);
+            comboBox.setSelectedIndex(0);
+        });
+        robot.waitForIdle();
+
+        SwingUtilities.invokeAndWait(() -> {
+            int selectedIndex = comboBox.getSelectedIndex();
+            fileChooser.setVisible(false);
+
+            if (selectedIndex != 0) {
+                throw new RuntimeException("Select All file filter is not selected!");
+            }
+        });
+    }
+
+    private static void createAndShowGUI() {
+        fileChooser = new JFileChooser();
+        fileChooser.setAcceptAllFileFilterUsed(true);
+        fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
+
+        FileFilter txtFilter = new FileNameExtensionFilter("Text files", "txt");
+        fileChooser.addChoosableFileFilter(txtFilter);
+        fileChooser.setFileFilter(txtFilter);
+        fileChooser.showOpenDialog(null);
+    }
+
+    private static JComboBox findComboBox(Component comp) {
+
+        if (comp instanceof JLabel) {
+            JLabel label = (JLabel) comp;
+            if (LABEL_TEXT.equals(label.getText())) {
+                return (JComboBox) label.getLabelFor();
+            }
+        }
+
+        if (comp instanceof Container) {
+            Container cont = (Container) comp;
+            for (int i = 0; i < cont.getComponentCount(); i++) {
+
+                JComboBox result = findComboBox(cont.getComponent(i));
+                if (result != null) {
+                    return result;
+                }
+            }
+        }
+
+        return null;
+    }
+}