1 /* 2 test 3 @bug 6680988 4 @summary verify that various shortcuts and accelerators work 5 @author yuri.nesterenko : area=awt.keyboard 6 @run applet/manual=yesno AcceleratorTest.html 7 */ 8 9 /** 10 * AcceleratorTest.java 11 * 12 * summary: 13 */ 14 15 //import java.applet.Applet; 16 import javax.swing.*; 17 import java.awt.*; 18 import java.awt.event.*; 19 import java.util.Hashtable; 20 21 22 public class AcceleratorTest extends JApplet 23 { 24 //Declare things used in the test, like buttons and labels here 25 static int pressed = 0; 26 Hashtable<String, Integer> cmdHash = new Hashtable<String, Integer>(); 27 String[] CMD = { 28 "\u042E, keep me in focus", 29 "Item Cyrl Be", 30 "Item English Period", 31 "Item English N", 32 "\u0436" 33 }; 34 35 JFrame jfr; 36 37 public void init() 38 { 39 //Create instructions for the user here, as well as set up 40 // the environment -- set the layout manager, add buttons, 41 // etc. 42 this.setLayout (new BorderLayout ()); 43 44 String[] instructions = 45 { 46 " Ensure you have Russian keyboard layout as a currently active.", 47 "(1) Press Ctrl+\u0411 (a key with \",<\" on it) ", 48 "(2) Find a . (period) in this layout (perhaps \"/?\" or \"7&\" key).", 49 "Press Ctrl+.", 50 "(3) Press Crtl+ regular English . (period) key (on \".>\" )", 51 "(4) Press Ctrl+ key with English N.", 52 "(5) Press Alt+\u042E (key with \".>\")", 53 "(6) Press Alt+\u0436 (key with \";:\")", 54 "If all expected commands will be fired, look for message", 55 "\"All tests passed\"" 56 }; 57 Sysout.createDialogWithInstructions( instructions ); 58 for(int i = 0; i < CMD.length; i++) { 59 cmdHash.put(CMD[i], 0); 60 } 61 62 jfr = new JFrame(); 63 JButton jbu; 64 jfr.add((jbu = new JButton(CMD[0]))); 65 jbu.setMnemonic(java.awt.event.KeyEvent.getExtendedKeyCodeForChar('\u042E')); 66 jbu.addActionListener( new ALi(CMD[0])); 67 68 69 JMenuBar menuBar = new JMenuBar(); 70 jfr.setJMenuBar(menuBar); 71 JMenu menu = new JMenu("Menu"); 72 menuBar.add(menu); 73 74 JMenuItem menuItem = new JMenuItem(CMD[1]); 75 menuItem.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.getExtendedKeyCodeForChar('\u0431'), 76 InputEvent.CTRL_DOWN_MASK)); 77 78 JMenuItem menuItemEnglish = new JMenuItem(CMD[2]); 79 menuItemEnglish.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 80 InputEvent.CTRL_DOWN_MASK)); 81 JMenuItem menuItemE1 = new JMenuItem(CMD[3]); 82 menuItemE1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, 83 InputEvent.CTRL_DOWN_MASK)); 84 menuItem.addActionListener( new ALi(CMD[1])); 85 menuItemEnglish.addActionListener( new ALi(CMD[2])); 86 menuItemE1.addActionListener( new ALi(CMD[3])); 87 menu.add(menuItem); 88 menu.add(menuItemEnglish); 89 menu.add(menuItemE1); 90 91 KeyStroke ks; 92 InputMap im = new InputMap(); 93 ks = KeyStroke.getKeyStroke(KeyEvent.getExtendedKeyCodeForChar('\u0436'), java.awt.event.InputEvent.ALT_DOWN_MASK); 94 im.put(ks, "pushAction"); 95 im.setParent(jbu.getInputMap(JComponent.WHEN_FOCUSED)); 96 jbu.setInputMap(JComponent.WHEN_FOCUSED, im); 97 98 jbu.getActionMap().put("pushAction", 99 new AbstractAction("pushAction") { 100 public void actionPerformed(ActionEvent evt) { 101 if( evt.getActionCommand().equals(CMD[4])) { 102 cmdHash.put(CMD[4], 1); 103 } 104 boolean notYet = false; 105 for(int i = 0; i < CMD.length; i++) { 106 if(cmdHash.get(CMD[i]) == 0 ) notYet = true; 107 } 108 Sysout.println("Fired"); 109 if( !notYet ) { 110 Sysout.println("All tests passed."); 111 } 112 } 113 } 114 ); 115 116 117 jfr.setBounds(650,0,200,200); 118 jfr.setVisible(true); 119 120 }//End init() 121 122 public void start () 123 { 124 //Get things going. Request focus, set size, et cetera 125 setSize (200,200); 126 setVisible(true); 127 validate(); 128 129 }// start() 130 public class ALi implements ActionListener { 131 String expectedCmd; 132 public ALi( String eCmd ) { 133 expectedCmd = eCmd; 134 } 135 public void actionPerformed(ActionEvent ae) { 136 if( cmdHash.containsKey(ae.getActionCommand()) ) { 137 cmdHash.put(expectedCmd, 1); 138 } 139 boolean notYet = false; 140 for(int i = 0; i < CMD.length; i++) { 141 if(cmdHash.get(CMD[i]) == 0 ) notYet = true; 142 //Sysout.println(CMD[i]+":"+cmdHash.get(CMD[i])); 143 } 144 Sysout.println("FIRED"); 145 if( !notYet ) { 146 Sysout.println("All tests passed."); 147 } 148 } 149 } 150 151 152 }// class AcceleratorTest 153 154 /* Place other classes related to the test after this line */ 155 156 157 158 159 160 /**************************************************** 161 Standard Test Machinery 162 DO NOT modify anything below -- it's a standard 163 chunk of code whose purpose is to make user 164 interaction uniform, and thereby make it simpler 165 to read and understand someone else's test. 166 ****************************************************/ 167 168 /** 169 This is part of the standard test machinery. 170 It creates a dialog (with the instructions), and is the interface 171 for sending text messages to the user. 172 To print the instructions, send an array of strings to Sysout.createDialog 173 WithInstructions method. Put one line of instructions per array entry. 174 To display a message for the tester to see, simply call Sysout.println 175 with the string to be displayed. 176 This mimics System.out.println but works within the test harness as well 177 as standalone. 178 */ 179 180 class Sysout 181 { 182 private static TestDialog dialog; 183 private static boolean numbering = false; 184 private static int messageNumber = 0; 185 186 public static void createDialogWithInstructions( String[] instructions ) 187 { 188 dialog = new TestDialog( new Frame(), "Instructions" ); 189 dialog.printInstructions( instructions ); 190 dialog.setVisible(true); 191 println( "Any messages for the tester will display here." ); 192 } 193 194 public static void createDialog( ) 195 { 196 dialog = new TestDialog( new Frame(), "Instructions" ); 197 String[] defInstr = { "Instructions will appear here. ", "" } ; 198 dialog.printInstructions( defInstr ); 199 dialog.setVisible(true); 200 println( "Any messages for the tester will display here." ); 201 } 202 203 /* Enables message counting for the tester. */ 204 public static void enableNumbering(boolean enable){ 205 numbering = enable; 206 } 207 208 public static void printInstructions( String[] instructions ) 209 { 210 dialog.printInstructions( instructions ); 211 } 212 213 214 public static void println( String messageIn ) 215 { 216 if (numbering) { 217 messageIn = "" + messageNumber + " " + messageIn; 218 messageNumber++; 219 } 220 dialog.displayMessage( messageIn ); 221 } 222 223 }// Sysout class 224 225 /** 226 This is part of the standard test machinery. It provides a place for the 227 test instructions to be displayed, and a place for interactive messages 228 to the user to be displayed. 229 To have the test instructions displayed, see Sysout. 230 To have a message to the user be displayed, see Sysout. 231 Do not call anything in this dialog directly. 232 */ 233 class TestDialog extends Dialog 234 { 235 236 TextArea instructionsText; 237 TextArea messageText; 238 int maxStringLength = 80; 239 240 //DO NOT call this directly, go through Sysout 241 public TestDialog( Frame frame, String name ) 242 { 243 super( frame, name ); 244 int scrollBoth = TextArea.SCROLLBARS_BOTH; 245 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 246 add( "North", instructionsText ); 247 248 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 249 add("Center", messageText); 250 251 pack(); 252 253 setVisible(true); 254 }// TestDialog() 255 256 //DO NOT call this directly, go through Sysout 257 public void printInstructions( String[] instructions ) 258 { 259 //Clear out any current instructions 260 instructionsText.setText( "" ); 261 262 //Go down array of instruction strings 263 264 String printStr, remainingStr; 265 for( int i=0; i < instructions.length; i++ ) 266 { 267 //chop up each into pieces maxSringLength long 268 remainingStr = instructions[ i ]; 269 while( remainingStr.length() > 0 ) 270 { 271 //if longer than max then chop off first max chars to print 272 if( remainingStr.length() >= maxStringLength ) 273 { 274 //Try to chop on a word boundary 275 int posOfSpace = remainingStr. 276 lastIndexOf( ' ', maxStringLength - 1 ); 277 278 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 279 280 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 281 remainingStr = remainingStr.substring( posOfSpace + 1 ); 282 } 283 //else just print 284 else 285 { 286 printStr = remainingStr; 287 remainingStr = ""; 288 } 289 290 instructionsText.append( printStr + "\n" ); 291 292 }// while 293 294 }// for 295 296 }//printInstructions() 297 298 //DO NOT call this directly, go through Sysout 299 public void displayMessage( String messageIn ) 300 { 301 messageText.append( messageIn + "\n" ); 302 System.out.println(messageIn); 303 } 304 305 }// TestDialog class