1 /* 2 * Copyright (c) 2010, 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 /* 25 * @test 26 * @bug 4847375 27 * @summary JFileChooser Create New Folder button is disabled incorrectly 28 * @author Pavel Porvatov 29 */ 30 31 import sun.awt.OSInfo; 32 import sun.awt.shell.ShellFolder; 33 34 import javax.swing.*; 35 import java.awt.*; 36 import java.lang.reflect.Method; 37 38 public class bug4847375 { 39 private final String newFolderToolTipText; 40 41 private final String lookAndFeel; 42 43 public static void main(String[] args) throws Exception { 44 if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) { 45 System.out.println("The test is suitable only for Windows OS. Skipped."); 46 47 return; 48 } 49 50 SwingUtilities.invokeAndWait(new Runnable() { 51 public void run() { 52 new bug4847375("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 53 54 new bug4847375("javax.swing.plaf.metal.MetalLookAndFeel"); 55 } 56 }); 57 } 58 59 private static Object[][] DIRECTORIES = new Object[][]{ 60 {"getDesktop", Boolean.TRUE}, 61 {"getDrives", Boolean.FALSE}, // My computer 62 {"getRecent", Boolean.TRUE}, 63 {"getNetwork", Boolean.FALSE}, 64 {"getPersonal", Boolean.TRUE}, 65 }; 66 67 private bug4847375(String lookAndFeel) { 68 this.lookAndFeel = lookAndFeel; 69 70 try { 71 UIManager.setLookAndFeel(lookAndFeel); 72 } catch (Exception e) { 73 fail("Cannot set LookAndFeel", e); 74 } 75 76 JFileChooser fileChooser = new JFileChooser(); 77 78 // Find button NewFolder 79 newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText", fileChooser.getLocale()); 80 81 if (newFolderToolTipText == null || newFolderToolTipText.length() == 0) { 82 fail("Cannot find NewFolderButton in FileChooser (tooltip doesn't exist)"); 83 84 return; 85 } 86 87 JButton newFolderButton = findNewFolderButton(fileChooser); 88 89 if (newFolderButton == null) { 90 fail("Cannot find NewFolderButton in FileChooser"); 91 92 return; 93 } 94 95 for (Object[] objects : DIRECTORIES) { 96 String getterName = (String) objects[0]; 97 Boolean enabledNewFolder = (Boolean) objects[1]; 98 99 fileChooser.setCurrentDirectory(getWin32Folder(getterName)); 100 101 if (newFolderButton.isEnabled() != enabledNewFolder) { 102 fail("Enabled state of NewFolderButton should be " + enabledNewFolder + 103 " for Win32ShellFolderManager2." + getterName + "()"); 104 } 105 } 106 } 107 108 private JButton findNewFolderButton(Container container) { 109 JButton result = null; 110 111 for (int i = 0; i < container.getComponentCount(); i++) { 112 Component c = container.getComponent(i); 113 114 if (c instanceof JButton && newFolderToolTipText.equals(((JButton) c).getToolTipText())) { 115 if (result != null) { 116 fail("Two or more NewFolderButton found in FileChooser"); 117 } 118 119 result = (JButton) c; 120 } 121 122 if (c instanceof Container) { 123 JButton button = findNewFolderButton((Container) c); 124 125 if (result == null) { 126 result = button; 127 } else { 128 if (button != null) { 129 fail("Two or more NewFolderButton found in FileChooser"); 130 } 131 } 132 } 133 } 134 135 return result; 136 } 137 138 private ShellFolder getWin32Folder(String getterName) { 139 try { 140 Class win32ShellFolderManager2 = Class.forName("sun.awt.shell.Win32ShellFolderManager2"); 141 142 Method method = win32ShellFolderManager2.getDeclaredMethod(getterName); 143 method.setAccessible(true); 144 145 return (ShellFolder) method.invoke(null); 146 } catch (Exception e) { 147 fail("Cannot call '" + getterName + "' in the Win32ShellFolderManager2 class", e); 148 149 return null; 150 } 151 } 152 153 private void fail(String s) { 154 throw new RuntimeException("Test failed: " + s); 155 } 156 157 private void fail(String s, Throwable e) { 158 throw new RuntimeException("Test failed for LookAndFeel " + lookAndFeel + ": " + s, e); 159 } 160 }