1 /*
2  * Copyright (c) 2007, 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    This class creates a dialog (with the instructions) and is the interface
26    for sending text messages to the user.
27    To print the instructions, send an array of strings to
28    Sysout.createDialogWithInstructions() method.
29    Put one line of instructions per array entry.
30    To display a message for the tester to see, simply call Sysout.println()
31    with the string to be displayed.
32    This mimics System.out.println but works within the test harness as well
33    as standalone.
34    @build TestDialog
35  */
36 package test.java.awt.regtesthelpers;
37 
38 import java.awt.*;
39 
40 public final class Sysout
41 {
42     private static TestDialog dialog;
43 
createDefaultInstructionDialog()44     public static void createDefaultInstructionDialog()
45     {
46         final String[] instructions =
47         {
48             "This is an AUTOMATIC test, simply wait until it is done.",
49             "The result (passed or failed) will be shown in the",
50             "message window below."
51         };
52         Sysout.createDialogWithInstructions(instructions);
53     }
54 
createDialogWithInstructions( String[] instructions )55     public static void createDialogWithInstructions( String[] instructions )
56     {
57         dialog = new TestDialog(null, "Instructions" );
58         dialog.printInstructions( instructions );
59         dialog.setVisible(true);
60         println( "Any messages for the tester will display here." );
61     }
62 
createDialog( )63     public static void createDialog( )
64     {
65         String[] defInstr = { "Instructions will appear here. ",
66                               "",
67                               "Any messages for the tester will display here." } ;
68         createDialogWithInstructions(defInstr);
69     }
70 
71 
printInstructions( String[] instructions )72     public static void printInstructions( String[] instructions )
73     {
74         dialog.printInstructions( instructions );
75     }
76 
77 
println( String messageIn )78     public static void println( String messageIn )
79     {
80         if (dialog != null){
81             dialog.displayMessage( messageIn );
82         }
83         System.out.println(messageIn);
84     }
85 }// Sysout  class
86 
87 /**
88   This class provides a place for the
89   test instructions to be displayed, and a place for interactive messages
90   to the user to be displayed.
91   To have the test instructions displayed, see Sysout class.
92   To have a message to the user be displayed, see Sysout class.
93 */
94 
95 final class TestDialog extends Dialog
96 {
97 
98     private TextArea instructionsText;
99     private TextArea messageText;
100     private int maxStringLength = 80;
101 
TestDialog( Frame frame, String name )102     public TestDialog( Frame frame, String name )
103     {
104         super( frame, name );
105         int scrollBoth = TextArea.SCROLLBARS_BOTH;
106         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
107         add( "North", instructionsText );
108 
109         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
110         add("Center", messageText);
111 
112         pack();
113 
114         setVisible(true);
115     }// TestDialog()
116 
printInstructions( String[] instructions )117     public void printInstructions( String[] instructions )
118     {
119         //Clear out any current instructions
120         instructionsText.setText( "" );
121 
122         //Go down array of instruction strings
123 
124         String printStr, remainingStr;
125         for( int i=0; i < instructions.length; i++ )
126         {
127             //chop up each into pieces maxSringLength long
128             remainingStr = instructions[ i ];
129             while( remainingStr.length() > 0 )
130             {
131                 //if longer than max then chop off first max chars to print
132                 if( remainingStr.length() >= maxStringLength )
133                 {
134                     //Try to chop on a word boundary
135                     int posOfSpace = remainingStr.
136                         lastIndexOf( ' ', maxStringLength - 1 );
137 
138                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
139 
140                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
141                     remainingStr = remainingStr.substring( posOfSpace + 1 );
142                 }
143                 //else just print
144                 else
145                 {
146                     printStr = remainingStr;
147                     remainingStr = "";
148                 }
149 
150                 instructionsText.append( printStr + "\n" );
151 
152             }// while
153 
154         }// for
155 
156     }//printInstructions()
157 
displayMessage( String messageIn )158     public void displayMessage( String messageIn )
159     {
160         messageText.append( messageIn + "\n" );
161     }
162 }// TestDialog  class
163