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  * @test
26  * @bug 4399442
27  * @summary Brackets should be quoted in Postscript output
28  * @author prr
29  * @run main/manual PrintParenString
30  */
31 
32 
33 import java.awt.*;
34 import java.awt.event.*;
35 import java.awt.print.*;
36 import java.text.*;
37 
38 public class PrintParenString extends Frame implements ActionListener {
39 
40  private TextCanvas c;
41 
main(String args[])42  public static void main(String args[]) {
43 
44   String[] instructions =
45         {
46          "You must have a printer available to perform this test",
47          "This test should print a page which contains the same",
48          "text message as in the test window on the screen",
49          "You should also monitor the command line to see if any exceptions",
50          "were thrown",
51          "If an exception is thrown, or the page doesn't print properly",
52          "then the test fails",
53        };
54       Sysout.createDialog( );
55       Sysout.printInstructions( instructions );
56 
57     PrintParenString f = new PrintParenString();
58     f.show();
59  }
60 
PrintParenString()61  public PrintParenString() {
62     super("JDK 1.2 drawString Printing");
63 
64     c = new TextCanvas();
65     add("Center", c);
66 
67     Button printButton = new Button("Print");
68     printButton.addActionListener(this);
69     add("South", printButton);
70 
71     addWindowListener(new WindowAdapter() {
72        public void windowClosing(WindowEvent e) {
73              System.exit(0);
74             }
75     });
76 
77     pack();
78  }
79 
actionPerformed(ActionEvent e)80  public void actionPerformed(ActionEvent e) {
81 
82    PrinterJob pj = PrinterJob.getPrinterJob();
83 
84    if (pj != null && pj.printDialog()) {
85 
86        pj.setPrintable(c);
87        try {
88             pj.print();
89       } catch (PrinterException pe) {
90       } finally {
91          System.err.println("PRINT RETURNED");
92       }
93    }
94  }
95 
96  class TextCanvas extends Panel implements Printable {
97 
98     String nullStr = null;
99     String emptyStr = new String();
100     AttributedString nullAttStr = null;
101     AttributedString emptyAttStr = new AttributedString(emptyStr);
102     AttributedCharacterIterator nullIterator = null;
103     AttributedCharacterIterator emptyIterator = emptyAttStr.getIterator();
104 
print(Graphics g, PageFormat pgFmt, int pgIndex)105     public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
106 
107       if (pgIndex > 0)
108          return Printable.NO_SUCH_PAGE;
109 
110       Graphics2D g2d = (Graphics2D)g;
111       g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
112 
113       paint(g);
114 
115       return Printable.PAGE_EXISTS;
116     }
117 
paint(Graphics g1)118     public void paint(Graphics g1) {
119         Graphics2D g = (Graphics2D)g1;
120 
121           String str = "String containing unclosed parenthesis (.";
122           g.drawString(str, 20, 40);
123 
124     }
125 
getPreferredSize()126      public Dimension getPreferredSize() {
127         return new Dimension(450, 250);
128     }
129  }
130 
131 }
132 
133 class Sysout
134  {
135    private static TestDialog dialog;
136 
createDialogWithInstructions( String[] instructions )137    public static void createDialogWithInstructions( String[] instructions )
138     {
139       dialog = new TestDialog( new Frame(), "Instructions" );
140       dialog.printInstructions( instructions );
141       dialog.show();
142       println( "Any messages for the tester will display here." );
143     }
144 
createDialog( )145    public static void createDialog( )
146     {
147       dialog = new TestDialog( new Frame(), "Instructions" );
148       String[] defInstr = { "Instructions will appear here. ", "" } ;
149       dialog.printInstructions( defInstr );
150       dialog.show();
151       println( "Any messages for the tester will display here." );
152     }
153 
154 
printInstructions( String[] instructions )155    public static void printInstructions( String[] instructions )
156     {
157       dialog.printInstructions( instructions );
158     }
159 
160 
println( String messageIn )161    public static void println( String messageIn )
162     {
163       dialog.displayMessage( messageIn );
164     }
165 
166  }// Sysout  class
167 
168 /**
169   This is part of the standard test machinery.  It provides a place for the
170    test instructions to be displayed, and a place for interactive messages
171    to the user to be displayed.
172   To have the test instructions displayed, see Sysout.
173   To have a message to the user be displayed, see Sysout.
174   Do not call anything in this dialog directly.
175   */
176 class TestDialog extends Dialog {
177 
178    TextArea instructionsText;
179    TextArea messageText;
180    int maxStringLength = 80;
181 
182    //DO NOT call this directly, go through Sysout
TestDialog( Frame frame, String name )183    public TestDialog( Frame frame, String name )
184     {
185       super( frame, name );
186       int scrollBoth = TextArea.SCROLLBARS_BOTH;
187       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
188       add( "North", instructionsText );
189 
190       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
191       add("Center", messageText);
192 
193       pack();
194 
195       show();
196     }// TestDialog()
197 
198    //DO NOT call this directly, go through Sysout
printInstructions( String[] instructions )199    public void printInstructions( String[] instructions )
200     {
201       //Clear out any current instructions
202       instructionsText.setText( "" );
203 
204       //Go down array of instruction strings
205 
206       String printStr, remainingStr;
207       for( int i=0; i < instructions.length; i++ )
208        {
209          //chop up each into pieces maxSringLength long
210          remainingStr = instructions[ i ];
211          while( remainingStr.length() > 0 )
212           {
213             //if longer than max then chop off first max chars to print
214             if( remainingStr.length() >= maxStringLength )
215              {
216                //Try to chop on a word boundary
217                int posOfSpace = remainingStr.
218                   lastIndexOf( ' ', maxStringLength - 1 );
219 
220                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
221 
222                printStr = remainingStr.substring( 0, posOfSpace + 1 );
223                remainingStr = remainingStr.substring( posOfSpace + 1 );
224              }
225             //else just print
226             else
227              {
228                printStr = remainingStr;
229                remainingStr = "";
230              }
231 
232             instructionsText.append( printStr + "\n" );
233 
234           }// while
235 
236        }// for
237 
238     }//printInstructions()
239 
240    //DO NOT call this directly, go through Sysout
displayMessage( String messageIn )241    public void displayMessage( String messageIn )
242     {
243       messageText.append( messageIn + "\n" );
244     }
245 
246  }// TestDialog  class
247