8010718: javax/swing/JFileChooser/8013442/Test8013442.java fails
authormalenkov
Fri, 24 Jan 2014 16:50:40 +0400
changeset 23271 99648f1e831a
parent 23270 b5073d208bbf
child 23272 0e0103e73d3c
8010718: javax/swing/JFileChooser/8013442/Test8013442.java fails Reviewed-by: alexsch
jdk/src/macosx/classes/com/apple/laf/AquaFileChooserUI.java
jdk/test/javax/swing/JFileChooser/8010718/bug8010718.java
--- a/jdk/src/macosx/classes/com/apple/laf/AquaFileChooserUI.java	Fri Jan 24 16:23:22 2014 +0400
+++ b/jdk/src/macosx/classes/com/apple/laf/AquaFileChooserUI.java	Fri Jan 24 16:50:40 2014 +0400
@@ -1266,64 +1266,70 @@
     /**
      * Data model for a type-face selection combo-box.
      */
-    protected class FilterComboBoxModel extends DefaultListModel implements ComboBoxModel, PropertyChangeListener {
-        int selectedIndex = -1;
-
+    protected class FilterComboBoxModel extends AbstractListModel<FileFilter> implements ComboBoxModel<FileFilter>,
+            PropertyChangeListener {
+        protected FileFilter[] filters;
         protected FilterComboBoxModel() {
             super();
-            final FileFilter filters[] = getFileChooser().getChoosableFileFilters();
-            for (int i = 0; i < filters.length; i++) {
-                this.add(i, filters[i]);
+            filters = getFileChooser().getChoosableFileFilters();
+        }
+
+        public void propertyChange(PropertyChangeEvent e) {
+            String prop = e.getPropertyName();
+            if(prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) {
+                filters = (FileFilter[]) e.getNewValue();
+                fireContentsChanged(this, -1, -1);
+            } else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) {
+                fireContentsChanged(this, -1, -1);
             }
         }
 
-        public void propertyChange(final PropertyChangeEvent e) {
-            final String prop = e.getPropertyName();
-            if (prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) {
-                this.clear();
-                final FileFilter filters[] = (FileFilter[])e.getNewValue();
-
-                for (int i = 0; i < filters.length; i++) {
-                    this.add(i, filters[i]);
-                }
-
-                fireContentsChanged(this, -1, -1);
-            } else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) {
-                final FileFilter currentFilter = (FileFilter)e.getNewValue();
-                FileFilter filters[] = getFileChooser().getChoosableFileFilters();
-
-                boolean found = false;
-                if (currentFilter != null) {
-                    for (final FileFilter element : filters) {
-                        if (element == currentFilter) {
-                            found = true;
-                        }
-                    }
-                    if (found == false) {
-                        getFileChooser().addChoosableFileFilter(currentFilter);
-                    }
-                }
-
-                filters = getFileChooser().getChoosableFileFilters();
-                setSelectedItem(e.getNewValue());
-            }
-        }
-
-        public void setSelectedItem(final Object filter) {
-            if (filter != null) {
-                selectedIndex = this.indexOf(filter);
+        public void setSelectedItem(Object filter) {
+            if(filter != null) {
+                getFileChooser().setFileFilter((FileFilter) filter);
                 fireContentsChanged(this, -1, -1);
             }
         }
 
         public Object getSelectedItem() {
-            final Object returnValue = null;
+            // Ensure that the current filter is in the list.
+            // NOTE: we shouldnt' have to do this, since JFileChooser adds
+            // the filter to the choosable filters list when the filter
+            // is set. Lets be paranoid just in case someone overrides
+            // setFileFilter in JFileChooser.
+            FileFilter currentFilter = getFileChooser().getFileFilter();
+            boolean found = false;
+            if(currentFilter != null) {
+                for (FileFilter filter : filters) {
+                    if (filter == currentFilter) {
+                        found = true;
+                    }
+                }
+                if(found == false) {
+                    getFileChooser().addChoosableFileFilter(currentFilter);
+                }
+            }
+            return getFileChooser().getFileFilter();
+        }
 
-            if (this.size() > 0) {
-                if ((selectedIndex != -1) && (selectedIndex < size())) { return this.get(selectedIndex); }
+        public int getSize() {
+            if(filters != null) {
+                return filters.length;
+            } else {
+                return 0;
             }
+        }
 
-            return returnValue;
+        public FileFilter getElementAt(int index) {
+            if(index > getSize() - 1) {
+                // This shouldn't happen. Try to recover gracefully.
+                return getFileChooser().getFileFilter();
+            }
+            if(filters != null) {
+                return filters[index];
+            } else {
+                return null;
+            }
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JFileChooser/8010718/bug8010718.java	Fri Jan 24 16:50:40 2014 +0400
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2013, 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.
+ */
+
+/*
+ * @test
+ * @bug 8010718
+ * @summary [macosx] JFileChooser current filter nullified by addChoosableFileFilter
+ * @author Tomas Hurka
+ */
+import java.io.File;
+import javax.swing.*;
+import javax.swing.filechooser.FileFilter;
+
+public class bug8010718 {
+
+    public static void main(String[] args) throws Exception {
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                JFileChooser fileChooser = new JFileChooser();
+                fileChooser.addChoosableFileFilter(new FileFilter() {
+                    @Override
+                    public boolean accept(File f) {
+                        return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml");
+                    }
+
+                    @Override
+                    public String getDescription() {
+                        return "XML";
+                    }
+                });
+                fileChooser.addChoosableFileFilter(new FileFilter() {
+                    @Override
+                    public boolean accept(File f) {
+                        return f.isDirectory() || f.getName().toLowerCase().endsWith(".txt");
+                    }
+
+                    @Override
+                    public String getDescription() {
+                        return "TXT";
+                    }
+                });
+                if (fileChooser.getFileFilter() == null) {
+                    throw new RuntimeException("getFileFilter() should not return null");
+                }
+            }
+        });
+    }
+}