jdk/test/java/awt/regtesthelpers/Sysout.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
   This class creates a dialog (with the instructions) and is the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   for sending text messages to the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
   To print the instructions, send an array of strings to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
   Sysout.createDialogWithInstructions() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
   Put one line of instructions per array entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
   To display a message for the tester to see, simply call Sysout.println()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
   with the string to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
   This mimics System.out.println but works within the test harness as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
   as standalone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
   @build TestDialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package test.java.awt.regtesthelpers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public final class Sysout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static TestDialog dialog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void createDefaultInstructionDialog()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        final String[] instructions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            "This is an AUTOMATIC test, simply wait until it is done.",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            "The result (passed or failed) will be shown in the",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            "message window below."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        Sysout.createDialogWithInstructions(instructions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static void createDialogWithInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        dialog = new TestDialog(null, "Instructions" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        dialog.printInstructions( instructions );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        dialog.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        println( "Any messages for the tester will display here." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static void createDialog( )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        String[] defInstr = { "Instructions will appear here. ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                              "",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                              "Any messages for the tester will display here." } ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        createDialogWithInstructions(defInstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static void printInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        dialog.printInstructions( instructions );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static void println( String messageIn )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (dialog != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            dialog.displayMessage( messageIn );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        System.out.println(messageIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}// Sysout  class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
  This class provides a place for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
  test instructions to be displayed, and a place for interactive messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
  to the user to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
  To have the test instructions displayed, see Sysout class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  To have a message to the user be displayed, see Sysout class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
final class TestDialog extends Dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private TextArea instructionsText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private TextArea messageText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private int maxStringLength = 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public TestDialog( Frame frame, String name )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        super( frame, name );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int scrollBoth = TextArea.SCROLLBARS_BOTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        add( "North", instructionsText );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        add("Center", messageText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }// TestDialog()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void printInstructions( String[] instructions )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        //Clear out any current instructions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        instructionsText.setText( "" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        //Go down array of instruction strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        String printStr, remainingStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        for( int i=0; i < instructions.length; i++ )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            //chop up each into pieces maxSringLength long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            remainingStr = instructions[ i ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            while( remainingStr.length() > 0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                //if longer than max then chop off first max chars to print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if( remainingStr.length() >= maxStringLength )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    //Try to chop on a word boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    int posOfSpace = remainingStr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        lastIndexOf( ' ', maxStringLength - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    printStr = remainingStr.substring( 0, posOfSpace + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    remainingStr = remainingStr.substring( posOfSpace + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                //else just print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    printStr = remainingStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    remainingStr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                instructionsText.append( printStr + "\n" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }// while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }// for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }//printInstructions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public void displayMessage( String messageIn )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        messageText.append( messageIn + "\n" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}// TestDialog  class