8152677: [macosx] All files filter can't be selected in JFileChooser

Reviewed-by: serb
This commit is contained in:
Alexander Scherbatiy 2016-04-28 23:48:37 +04:00
parent 0a29c91962
commit 8499a7a0b3
2 changed files with 118 additions and 3 deletions

View File

@ -112,7 +112,7 @@ public class AquaFileChooserUI extends FileChooserUI {
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 @@ public class AquaFileChooserUI extends FileChooserUI {
// *****************************************
// ***** default AcceptAll file filter *****
// *****************************************
protected class AcceptAllFileFilter extends FileFilter {
private static class AcceptAllFileFilter extends FileFilter {
public AcceptAllFileFilter() {
}
@ -1305,6 +1305,8 @@ public class AquaFileChooserUI extends FileChooserUI {
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 class AquaFileChooserUI extends FileChooserUI {
}
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

View File

@ -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;
}
}