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