1 /*
   2  * Copyright (c) 1999, 2010, 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 4118621
  27   @summary tests that selected text isn't scrolled when there is enough room.
  28   @author prs: area=TextField
  29   @run applet/manual=yesno ScrollSelectionTest.html
  30 */
  31 
  32 /**
  33  * ScrollSelectionTest.java
  34  *
  35  * summary: tests that selected text isn't scrolled when there is enough room.
  36  */
  37 
  38 import java.applet.Applet;
  39 import java.awt.Dialog;
  40 import java.awt.Frame;
  41 import java.awt.TextField;
  42 import java.awt.TextArea;
  43 
  44 public class ScrollSelectionTest extends Applet
  45  {
  46 
  47    Frame frame = new Frame("ScrollSelectionTest frame");
  48    TextField tf = new TextField(40);
  49 
  50    public void init()
  51     {
  52       tf.setText("abcdefghijklmnopqrstuvwxyz");
  53       frame.add(tf);
  54       tf.select(0, 20);
  55 
  56       String[] instructions = {
  57           "INSTRUCTIONS:",
  58           "This is a test for a win32 specific problem",
  59          "If you see all the letters from 'a' to 'z' and",
  60           "letters from 'a' to 't' are selected then test passes.",
  61           "You may have to activate the frame to see the selection"
  62           + " highlighted (e.g. by clicking on frame's title)."
  63       };
  64       Sysout.createDialogWithInstructions( instructions );
  65 
  66     }// init()
  67 
  68    public void start ()
  69     {
  70       setSize (300,300);
  71       setVisible(true);
  72 
  73       frame.setVisible(true);
  74       frame.setBounds (400, 0, 300, 300);
  75 
  76     }// start()
  77 
  78  }// class ScrollSelectionTest
  79 
  80 /****************************************************
  81  Standard Test Machinery
  82  DO NOT modify anything below -- it's a standard
  83   chunk of code whose purpose is to make user
  84   interaction uniform, and thereby make it simpler
  85   to read and understand someone else's test.
  86  ****************************************************/
  87 
  88 /**
  89  This is part of the standard test machinery.
  90  It creates a dialog (with the instructions), and is the interface
  91   for sending text messages to the user.
  92  To print the instructions, send an array of strings to Sysout.createDialog
  93   WithInstructions method.  Put one line of instructions per array entry.
  94  To display a message for the tester to see, simply call Sysout.println
  95   with the string to be displayed.
  96  This mimics System.out.println but works within the test harness as well
  97   as standalone.
  98  */
  99 
 100 class Sysout
 101  {
 102    private static TestDialog dialog;
 103 
 104    public static void createDialogWithInstructions( String[] instructions )
 105     {
 106       dialog = new TestDialog( new Frame(), "Instructions" );
 107       dialog.printInstructions( instructions );
 108       dialog.show();
 109       println( "Any messages for the tester will display here." );
 110     }
 111 
 112    public static void createDialog( )
 113     {
 114       dialog = new TestDialog( new Frame(), "Instructions" );
 115       String[] defInstr = { "Instructions will appear here. ", "" } ;
 116       dialog.printInstructions( defInstr );
 117       dialog.show();
 118       println( "Any messages for the tester will display here." );
 119     }
 120 
 121 
 122    public static void printInstructions( String[] instructions )
 123     {
 124       dialog.printInstructions( instructions );
 125     }
 126 
 127 
 128    public static void println( String messageIn )
 129     {
 130       dialog.displayMessage( messageIn );
 131     }
 132 
 133  }// Sysout  class
 134 
 135 /**
 136   This is part of the standard test machinery.  It provides a place for the
 137    test instructions to be displayed, and a place for interactive messages
 138    to the user to be displayed.
 139   To have the test instructions displayed, see Sysout.
 140   To have a message to the user be displayed, see Sysout.
 141   Do not call anything in this dialog directly.
 142   */
 143 class TestDialog extends Dialog
 144  {
 145 
 146    TextArea instructionsText;
 147    TextArea messageText;
 148    int maxStringLength = 80;
 149 
 150    //DO NOT call this directly, go through Sysout
 151    public TestDialog( Frame frame, String name )
 152     {
 153       super( frame, name );
 154       int scrollBoth = TextArea.SCROLLBARS_BOTH;
 155       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 156       add( "North", instructionsText );
 157 
 158       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 159       add("South", messageText);
 160 
 161       pack();
 162 
 163       show();
 164     }// TestDialog()
 165 
 166    //DO NOT call this directly, go through Sysout
 167    public void printInstructions( String[] instructions )
 168     {
 169       //Clear out any current instructions
 170       instructionsText.setText( "" );
 171 
 172       //Go down array of instruction strings
 173 
 174       String printStr, remainingStr;
 175       for( int i=0; i < instructions.length; i++ )
 176        {
 177          //chop up each into pieces maxSringLength long
 178          remainingStr = instructions[ i ];
 179          while( remainingStr.length() > 0 )
 180           {
 181             //if longer than max then chop off first max chars to print
 182             if( remainingStr.length() >= maxStringLength )
 183              {
 184                //Try to chop on a word boundary
 185                int posOfSpace = remainingStr.
 186                   lastIndexOf( ' ', maxStringLength - 1 );
 187 
 188                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 189 
 190                printStr = remainingStr.substring( 0, posOfSpace + 1 );
 191                remainingStr = remainingStr.substring( posOfSpace + 1 );
 192              }
 193             //else just print
 194             else
 195              {
 196                printStr = remainingStr;
 197                remainingStr = "";
 198              }
 199 
 200             instructionsText.append( printStr + "\n" );
 201 
 202           }// while
 203 
 204        }// for
 205 
 206     }//printInstructions()
 207 
 208    //DO NOT call this directly, go through Sysout
 209    public void displayMessage( String messageIn )
 210     {
 211       messageText.append( messageIn + "\n" );
 212     }
 213 
 214  }// TestDialog  class