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