1 /* 2 * Copyright (c) 2013, 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 import java.awt.Component; 25 import java.awt.Container; 26 import java.awt.Point; 27 import java.awt.Robot; 28 import java.awt.Toolkit; 29 import java.awt.event.InputEvent; 30 import java.awt.event.KeyEvent; 31 import java.io.File; 32 import java.io.IOException; 33 import javax.swing.JFileChooser; 34 import javax.swing.SwingUtilities; 35 36 import java.nio.file.Files; 37 import javax.swing.AbstractButton; 38 import javax.swing.JTable; 39 import javax.swing.UIManager; 40 import sun.awt.SunToolkit; 41 42 /** 43 * @test 44 * @key headful 45 * @bug 7199708 46 * @author Alexander Scherbatiy 47 * @summary FileChooser crashs when opening large folder 48 * @run main bug7199708 49 */ 50 public class bug7199708 { 51 52 private static int FILE_NUMBER = 30000; 53 private static volatile JFileChooser fileChooser; 54 private static volatile int locationX; 55 private static volatile int locationY; 56 private static volatile int width; 57 58 public static void main(String[] args) throws Exception { 59 60 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 61 Robot robot = new Robot(); 62 robot.setAutoDelay(50); 63 64 final File folder = createLargeFolder(); 65 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 66 67 SwingUtilities.invokeLater(new Runnable() { 68 public void run() { 69 fileChooser = new JFileChooser(folder); 70 fileChooser.showSaveDialog(null); 71 } 72 }); 73 74 toolkit.realSync(); 75 76 SwingUtilities.invokeLater(new Runnable() { 77 public void run() { 78 final String detailsTooltip = UIManager.getString("FileChooser." 79 + "detailsViewButtonToolTipText", fileChooser.getLocale()); 80 81 doAction(fileChooser, new ComponentAction() { 82 @Override 83 public boolean accept(Component component) { 84 return (component instanceof AbstractButton) 85 && detailsTooltip.equals( 86 ((AbstractButton) component).getToolTipText()); 87 } 88 89 @Override 90 public void perform(Component component) { 91 ((AbstractButton) component).doClick(); 92 } 93 }); 94 95 doAction(fileChooser, new ComponentAction() { 96 @Override 97 public boolean accept(Component component) { 98 return (component instanceof JTable); 99 } 100 101 @Override 102 public void perform(Component component) { 103 Point tableLocation = component.getLocationOnScreen(); 104 locationX = (int) tableLocation.getX(); 105 locationY = (int) tableLocation.getY(); 106 width = (int) fileChooser.getBounds().getWidth(); 107 } 108 }); 109 } 110 }); 111 112 toolkit.realSync(); 113 114 int d = 25; 115 for (int i = 0; i < width / d; i++) { 116 robot.mouseMove(locationX + i * d, locationY + 5); 117 robot.mousePress(InputEvent.BUTTON1_MASK); 118 robot.mouseRelease(InputEvent.BUTTON1_MASK); 119 toolkit.realSync(); 120 } 121 122 robot.keyPress(KeyEvent.VK_ESCAPE); 123 robot.keyRelease(KeyEvent.VK_ESCAPE); 124 } 125 126 static void doAction(Component component, ComponentAction action) { 127 if (action.accept(component)) { 128 action.perform(component); 129 } else if (component instanceof Container) { 130 for (Component comp : ((Container) component).getComponents()) { 131 doAction(comp, action); 132 } 133 } 134 } 135 136 private static File createLargeFolder() { 137 try { 138 139 File largeFolder = Files.createTempDirectory("large_folder").toFile(); 140 largeFolder.deleteOnExit(); 141 142 for (int i = 0; i < FILE_NUMBER; i++) { 143 File file = new File(largeFolder, "File_" + i + ".txt"); 144 file.createNewFile(); 145 file.deleteOnExit(); 146 } 147 return largeFolder; 148 } catch (IOException ex) { 149 throw new RuntimeException(ex); 150 } 151 } 152 153 interface ComponentAction { 154 155 boolean accept(Component component); 156 157 void perform(Component component); 158 } 159 }