1 /*
   2   test %W% %E%  %I%, %G%
   3   @bug 6315717
   4   @summary  manual control over the Robot
   5   @author Andrei Dmitriev : area=awt.robot
   6   @run applet/manual=yesno ManualInstructions.html
   7 */
   8 
   9 import java.applet.Applet;
  10 import java.awt.*;
  11 import java.awt.event.*;
  12 import java.util.Timer;
  13 import java.util.TimerTask;
  14 
  15 public class ManualInstructions extends Applet
  16 {
  17     final static long SEND_DELAY = 1000;
  18 
  19     public static void main(String s[]){
  20         ManualInstructions mi = new ManualInstructions();
  21         mi.init();
  22         mi.start();
  23     }
  24 
  25     static Robot robot;
  26     Point mouseLocation; //where mouse should be pressed each time
  27     Panel target = new Panel();
  28     Button pressOn = new Button("press on ...");
  29     Button releaseOn = new Button("release on ...");
  30     Button clickOn = new Button("click on ...");
  31     Choice buttonNumber = new Choice();
  32 
  33     public void init()
  34     {
  35         try {
  36             robot = new Robot();
  37         } catch (AWTException ex) {
  38             ex.printStackTrace();
  39             throw new RuntimeException(ex);
  40         }
  41         this.setLayout (new BorderLayout ());
  42 
  43         target.setBackground(Color.green);
  44         target.setName("GreenBox");//for the ease of debug
  45         target.setPreferredSize(new Dimension(100, 100));
  46         String toolkit = Toolkit.getDefaultToolkit().getClass().getName();
  47 
  48         // on X systems two buttons are reserved for wheel though they are countable by MouseInfo.
  49         int buttonsNumber = toolkit.equals("sun.awt.windows.WToolkit")?MouseInfo.getNumberOfButtons():MouseInfo.getNumberOfButtons()-2;
  50 
  51         for (int i = 0; i < 8; i++){
  52             buttonNumber.add("BUTTON"+(i+1)+"_MASK");
  53         }
  54 
  55         pressOn.addActionListener(new ActionListener(){
  56                 public void actionPerformed(ActionEvent e){
  57                     System.out.println("Now pressing : " + (buttonNumber.getSelectedIndex()+1));
  58 
  59                     Timer timer = new Timer();
  60                     TimerTask robotInteraction = new TimerTask(){
  61                             public void run(){
  62                                 robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y);
  63                                 robot.mousePress(getMask(buttonNumber.getSelectedIndex()+1));
  64                             }
  65                         };
  66                     timer.schedule(robotInteraction, SEND_DELAY);
  67                 }
  68             });
  69 
  70         releaseOn.addActionListener(new ActionListener(){
  71             public void actionPerformed(ActionEvent e){
  72                 System.out.println("Now releasing : " + (buttonNumber.getSelectedIndex()+1));
  73                 Timer timer = new Timer();
  74                 TimerTask robotInteraction = new TimerTask(){
  75                         public void run(){
  76                             robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y);
  77                             robot.mouseRelease(getMask(buttonNumber.getSelectedIndex()+1));
  78                         }
  79                     };
  80                 timer.schedule(robotInteraction, SEND_DELAY);
  81             }
  82         });
  83 
  84         clickOn.addActionListener(new ActionListener(){
  85             public void actionPerformed(ActionEvent e){
  86                 System.out.println("Now clicking : " + (buttonNumber.getSelectedIndex()+1));
  87                 Timer timer = new Timer();
  88                 TimerTask robotInteraction = new TimerTask(){
  89                         public void run(){
  90                             robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y);
  91                             robot.mousePress(getMask(buttonNumber.getSelectedIndex()+1));
  92                             robot.mouseRelease(getMask(buttonNumber.getSelectedIndex()+1));
  93                         }
  94                     };
  95                 timer.schedule(robotInteraction, SEND_DELAY);
  96             }
  97 
  98         });
  99         target.addMouseListener(new MouseAdapter(){
 100            public void mousePressed(MouseEvent e){
 101                 Sysout.println(""+e);
 102            }
 103            public void mouseReleased(MouseEvent e){
 104                 Sysout.println(""+e);
 105            }
 106            public void mouseClicked(MouseEvent e){
 107                 Sysout.println(""+e);
 108            }
 109         });
 110 
 111         String[] instructions =
 112         {
 113             "Do provide an instruction to the robot by",
 114             "choosing the button number to act and ",
 115             "pressing appropriate java.awt.Button on the left.",
 116             "Inspect an output in the TextArea below.",
 117             "Please don't generate non-natural sequences like Release-Release, etc.",
 118             "If you use keyboard be sure that you released the keyboard shortly.",
 119             "If events are generated well press Pass, otherwise Fail."
 120         };
 121         Sysout.createDialogWithInstructions( instructions );
 122 
 123     }//End  init()
 124 
 125     private int getMask(int button){
 126         return InputEvent.getMaskForButton(button);
 127 
 128         /*
 129             //this only works for standard buttons and for old JDK builds
 130         int mask = 0;
 131         switch (button){
 132         case 1: {
 133             mask = InputEvent.BUTTON1_MASK;
 134             break;
 135         }
 136         case 2: {
 137             mask = InputEvent.BUTTON2_MASK;
 138             break;
 139         }
 140         case 3: {
 141             mask = InputEvent.BUTTON3_MASK;
 142             break;
 143         }
 144         }
 145         return mask;
 146         */
 147     }
 148 
 149     private Point updateTargetLocation() {
 150         return new Point(target.getLocationOnScreen().x + target.getWidth()/2, target.getLocationOnScreen().y + target.getHeight()/2);
 151     }
 152 
 153     public void start ()
 154     {
 155         //Get things going.  Request focus, set size, et cetera
 156         setSize (200,200);
 157         setVisible(true);
 158         validate();
 159         Frame f = new Frame ("Set action for Robot here.");
 160         f.setLayout(new FlowLayout());
 161         f.add(buttonNumber);
 162         f.add(pressOn);
 163         f.add(releaseOn);
 164         f.add(clickOn);
 165         f.add(target);
 166         f.pack();
 167         f.setVisible(true);
 168      }// start()
 169 }// class
 170 
 171 /* Place other classes related to the test after this line */
 172 
 173 
 174 /****************************************************
 175  Standard Test Machinery
 176  DO NOT modify anything below -- it's a standard
 177   chunk of code whose purpose is to make user
 178   interaction uniform, and thereby make it simpler
 179   to read and understand someone else's test.
 180  ****************************************************/
 181 
 182 /**
 183  This is part of the standard test machinery.
 184  It creates a dialog (with the instructions), and is the interface
 185   for sending text messages to the user.
 186  To print the instructions, send an array of strings to Sysout.createDialog
 187   WithInstructions method.  Put one line of instructions per array entry.
 188  To display a message for the tester to see, simply call Sysout.println
 189   with the string to be displayed.
 190  This mimics System.out.println but works within the test harness as well
 191   as standalone.
 192  */
 193 
 194 class Sysout
 195 {
 196     private static TestDialog dialog;
 197 
 198     public static void createDialogWithInstructions( String[] instructions )
 199     {
 200         dialog = new TestDialog( new Frame(), "Instructions" );
 201         dialog.printInstructions( instructions );
 202         dialog.setVisible(true);
 203         println( "Any messages for the tester will display here." );
 204     }
 205 
 206     public static void createDialog( )
 207     {
 208         dialog = new TestDialog( new Frame(), "Instructions" );
 209         String[] defInstr = { "Instructions will appear here. ", "" } ;
 210         dialog.printInstructions( defInstr );
 211         dialog.setVisible(true);
 212         println( "Any messages for the tester will display here." );
 213     }
 214 
 215     public static void printInstructions( String[] instructions )
 216     {
 217         dialog.printInstructions( instructions );
 218     }
 219 
 220 
 221     public static void println( String messageIn )
 222     {
 223         dialog.displayMessage( messageIn );
 224     }
 225 
 226 }// Sysout  class
 227 
 228 /**
 229   This is part of the standard test machinery.  It provides a place for the
 230    test instructions to be displayed, and a place for interactive messages
 231    to the user to be displayed.
 232   To have the test instructions displayed, see Sysout.
 233   To have a message to the user be displayed, see Sysout.
 234   Do not call anything in this dialog directly.
 235   */
 236 class TestDialog extends Dialog
 237 {
 238 
 239     TextArea instructionsText;
 240     TextArea messageText;
 241     int maxStringLength = 120;
 242 
 243     //DO NOT call this directly, go through Sysout
 244     public TestDialog( Frame frame, String name )
 245     {
 246         super( frame, name );
 247         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 248         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 249         add( "North", instructionsText );
 250 
 251         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 252         add("Center", messageText);
 253 
 254         pack();
 255 
 256         setVisible(true);
 257     }// TestDialog()
 258 
 259     //DO NOT call this directly, go through Sysout
 260     public void printInstructions( String[] instructions )
 261     {
 262         //Clear out any current instructions
 263         instructionsText.setText( "" );
 264 
 265         //Go down array of instruction strings
 266 
 267         String printStr, remainingStr;
 268         for( int i=0; i < instructions.length; i++ )
 269         {
 270             //chop up each into pieces maxSringLength long
 271             remainingStr = instructions[ i ];
 272             while( remainingStr.length() > 0 )
 273             {
 274                 //if longer than max then chop off first max chars to print
 275                 if( remainingStr.length() >= maxStringLength )
 276                 {
 277                     //Try to chop on a word boundary
 278                     int posOfSpace = remainingStr.
 279                         lastIndexOf( ' ', maxStringLength - 1 );
 280 
 281                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 282 
 283                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 284                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 285                 }
 286                 //else just print
 287                 else
 288                 {
 289                     printStr = remainingStr;
 290                     remainingStr = "";
 291                 }
 292 
 293                 instructionsText.append( printStr + "\n" );
 294             }// while
 295         }// for
 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