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