1 /* 2 * Copyright (c) 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 /* @test 25 * @bug 7124218 26 * @summary verifies different behaviour of SPACE and ENTER in JTable 27 * @library ../../regtesthelpers 28 * @build Util 29 * @run main SelectEditTableCell 30 */ 31 import java.awt.Point; 32 import java.awt.Robot; 33 import java.awt.event.InputEvent; 34 import java.awt.event.KeyEvent; 35 import javax.swing.DefaultListSelectionModel; 36 import javax.swing.JFrame; 37 import javax.swing.JTable; 38 import javax.swing.LookAndFeel; 39 import javax.swing.SwingUtilities; 40 import javax.swing.UIManager; 41 import javax.swing.UnsupportedLookAndFeelException; 42 43 public class SelectEditTableCell { 44 45 private static JFrame frame; 46 private static JTable table; 47 private static Robot robot; 48 49 public static void main(String[] args) throws Exception { 50 robot = new Robot(); 51 robot.delay(2000); 52 UIManager.LookAndFeelInfo[] lookAndFeelArray 53 = UIManager.getInstalledLookAndFeels(); 54 for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) { 55 executeCase(lookAndFeelItem.getClassName()); 56 } 57 58 } 59 60 private static void executeCase(String lookAndFeelString) throws Exception { 61 if (tryLookAndFeel(lookAndFeelString)) { 62 createUI(lookAndFeelString); 63 robot.delay(2000); 64 runTestCase(); 65 robot.delay(2000); 66 cleanUp(); 67 robot.delay(2000); 68 } 69 70 } 71 72 private static void createUI(final String lookAndFeelString) 73 throws Exception { 74 SwingUtilities.invokeAndWait(new Runnable() { 75 @Override 76 public void run() { 77 String[][] data = {{"Foo"}}; 78 String[] cols = {"One"}; 79 table = new JTable(data, cols); 80 table.setSelectionMode( 81 DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 82 frame = new JFrame(lookAndFeelString); 83 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 84 frame.getContentPane().add(table); 85 frame.pack(); 86 frame.setSize(500, frame.getSize().height); 87 frame.setLocationRelativeTo(null); 88 frame.setVisible(true); 89 frame.toFront(); 90 } 91 }); 92 } 93 94 private static void runTestCase() throws Exception { 95 Point centerPoint; 96 centerPoint = Util.getCenterPoint(table); 97 LookAndFeel lookAndFeel = UIManager.getLookAndFeel(); 98 robot.mouseMove(centerPoint.x, centerPoint.y); 99 robot.mousePress(InputEvent.BUTTON1_MASK); 100 robot.mouseRelease(InputEvent.BUTTON1_MASK); 101 SwingUtilities.invokeAndWait(new Runnable() { 102 @Override 103 public void run() { 104 table.clearSelection(); 105 if (table.isEditing() || table.isCellSelected(0, 0)) { 106 // assumption is bad, bail 107 frame.dispose(); 108 throw new AssertionError("Failed assumption: assumed no" 109 + "editing and no selection."); 110 } 111 } 112 }); 113 robot.waitForIdle(); 114 int fetchKeyCode; 115 keyTap(fetchKeyCode = isMac(lookAndFeel) 116 ? KeyEvent.VK_ENTER : KeyEvent.VK_SPACE); 117 final int keyCode = fetchKeyCode; 118 robot.waitForIdle(); 119 SwingUtilities.invokeAndWait(new Runnable() { 120 @Override 121 public void run() { 122 if (!table.isCellSelected(0, 0)) { 123 frame.dispose(); 124 throw new RuntimeException(((keyCode == KeyEvent.VK_ENTER) 125 ? "Enter" : "Space") 126 + " should select cell"); 127 } 128 } 129 }); 130 robot.waitForIdle(); 131 keyTap(KeyEvent.VK_SPACE); 132 robot.waitForIdle(); 133 SwingUtilities.invokeAndWait(new Runnable() { 134 @Override 135 public void run() { 136 if (!table.isEditing()) { 137 frame.dispose(); 138 throw new RuntimeException("Space should start editing"); 139 } 140 table.getCellEditor().cancelCellEditing(); 141 table.clearSelection(); 142 if (table.isEditing() || table.isCellSelected(0, 0)) { 143 // assumption is bad, bail 144 frame.dispose(); 145 throw new AssertionError("Failed assumption: assumed no " 146 + "editing and no selection."); 147 } 148 } 149 }); 150 robot.waitForIdle(); 151 // hitting a letter key will start editing 152 keyTap(KeyEvent.VK_A); 153 keyTap(KeyEvent.VK_SPACE); 154 keyTap(KeyEvent.VK_A); 155 robot.waitForIdle(); 156 SwingUtilities.invokeAndWait(new Runnable() { 157 @Override 158 public void run() { 159 if (table.isCellSelected(0, 0)) { 160 frame.dispose(); 161 throw new RuntimeException("Space should not select when " 162 + "already editing."); 163 } 164 } 165 }); 166 } 167 168 private static void cleanUp() throws Exception { 169 SwingUtilities.invokeAndWait(new Runnable() { 170 @Override 171 public void run() { 172 frame.dispose(); 173 } 174 }); 175 } 176 177 private static boolean isMac(LookAndFeel lookAndFeel) { 178 179 return lookAndFeel.toString().toLowerCase().contains("mac"); 180 } 181 182 private static void keyTap(int keyCode) { 183 robot.keyPress(keyCode); 184 robot.keyRelease(keyCode); 185 } 186 187 private static boolean tryLookAndFeel(String lookAndFeelString) 188 throws Exception { 189 try { 190 UIManager.setLookAndFeel( 191 lookAndFeelString); 192 193 } catch (UnsupportedLookAndFeelException 194 | ClassNotFoundException 195 | InstantiationException 196 | IllegalAccessException e) { 197 return false; 198 } 199 return true; 200 } 201 }