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 6488219 6560738 7158350 8017469
27  * @summary Test that text printed in Swing UI measures and looks OK.
28  * @run main/manual=yesno PrintTextTest
29  */
30 
31 import java.awt.*;
32 import javax.swing.*;
33 import java.awt.print.*;
34 
35 public class SwingUIText implements Printable {
36 
37     static String[] instructions = {
38         "This tests that when a Swing UI is printed, that the text",
39         "in each component properly matches the length of the component",
40         "as seen on-screen, and that the spacing of the text is of",
41         "reasonable even-ness. This latter part is very subjective and",
42         "the comparison has to be with JDK1.5 GA, or JDK 1.6 GA",
43     };
44 
45     static JFrame frame;
46 
main(String args[])47     public static void main(String args[]) {
48         SwingUtilities.invokeLater(new Runnable() {
49           public void run() {
50               createUI();
51           }
52       });
53     }
54 
createUI()55     public static void createUI() {
56 
57         Sysout.createDialogWithInstructions(instructions);
58 
59         JPanel panel = new JPanel();
60         panel.setLayout(new GridLayout(4,1));
61 
62         String text = "marvelous suspicious solving";
63         displayText(panel, text);
64 
65         String itext = "\u0641\u0642\u0643 \u0644\u0627\u064b";
66         itext = itext+itext+itext+itext+itext+itext+itext;
67         displayText(panel, itext);
68 
69         String itext2 = "\u0641"+text;
70         displayText(panel, itext2);
71 
72         JEditorPane editor = new JEditorPane();
73         editor.setContentType("text/html");
74         String CELL = "<TD align=\"center\"><font style=\"font-size: 18;\">Text</font></TD>";
75         String TABLE_BEGIN = "<TABLE BORDER=1 cellpadding=1 cellspacing=0 width=100%>";
76         String TABLE_END = "</TABLE>";
77         StringBuffer buffer = new StringBuffer();
78         buffer.append("<html><body>").append(TABLE_BEGIN);
79         for (int j = 0; j < 15; j++) {
80             buffer.append(CELL);
81         }
82         buffer.append("</tr>");
83         buffer.append(TABLE_END).append("</body></html>");
84         editor.setText(buffer.toString());
85 
86         panel.add(editor);
87 
88         frame = new JFrame("Swing UI Text Printing Test");
89         frame.getContentPane().add(panel);
90         frame.pack();
91         frame.setVisible(true);
92 
93         PrinterJob job = PrinterJob.getPrinterJob();
94         PageFormat pf = job.defaultPage();
95         job.setPrintable(new SwingUIText(), pf);
96         if (job.printDialog()) {
97             try { job.print(); }
98             catch (Exception e) {
99               e.printStackTrace();
100               throw new RuntimeException(e);
101             }
102         }
103     }
104 
105 
displayText(JPanel p, String text)106     static void displayText(JPanel p, String text) {
107         JPanel panel = new JPanel();
108         panel.setLayout(new GridLayout(2,1));
109         JPanel row = new JPanel();
110         Font font = new Font("Dialog", Font.PLAIN, 12);
111 
112         JLabel label = new JLabel(text);
113         label.setFont(font);
114         row.add(label);
115 
116         JButton button = new JButton("Print "+text);
117         button.setMnemonic('P');
118         button.setFont(font);
119         row.add(button);
120 
121         panel.add(row);
122 
123         row = new JPanel();
124         JTextField textField = new JTextField(text);
125         row.add(textField);
126 
127         JTextArea textArea = new JTextArea();
128         textArea.setText(text);
129         row.add(textArea);
130 
131         panel.add(row);
132         p.add(panel);
133     }
134 
print(Graphics g, PageFormat pf, int pageIndex)135     public int print(Graphics g, PageFormat pf, int pageIndex)
136         throws PrinterException {
137 
138         if (pageIndex >= 1) {
139             return Printable.NO_SUCH_PAGE;
140         }
141         g.translate((int)pf.getImageableX(), (int)pf.getImageableY());
142         frame.printAll(g);
143 
144         return Printable.PAGE_EXISTS;
145     }
146 
147 }
148 
149 class Sysout
150  {
151    private static TestDialog dialog;
152 
createDialogWithInstructions( String[] instructions )153    public static void createDialogWithInstructions( String[] instructions )
154     {
155       dialog = new TestDialog( new Frame(), "Instructions" );
156       dialog.printInstructions( instructions );
157       dialog.show();
158       println( "Any messages for the tester will display here." );
159     }
160 
createDialog( )161    public static void createDialog( )
162     {
163       dialog = new TestDialog( new Frame(), "Instructions" );
164       String[] defInstr = { "Instructions will appear here. ", "" } ;
165       dialog.printInstructions( defInstr );
166       dialog.show();
167       println( "Any messages for the tester will display here." );
168     }
169 
170 
printInstructions( String[] instructions )171    public static void printInstructions( String[] instructions )
172     {
173       dialog.printInstructions( instructions );
174     }
175 
176 
println( String messageIn )177    public static void println( String messageIn )
178     {
179       dialog.displayMessage( messageIn );
180     }
181 
182  }// Sysout  class
183 
184 /**
185   This is part of the standard test machinery.  It provides a place for the
186    test instructions to be displayed, and a place for interactive messages
187    to the user to be displayed.
188   To have the test instructions displayed, see Sysout.
189   To have a message to the user be displayed, see Sysout.
190   Do not call anything in this dialog directly.
191   */
192 class TestDialog extends Dialog
193  {
194 
195    TextArea instructionsText;
196    TextArea messageText;
197    int maxStringLength = 80;
198 
199    //DO NOT call this directly, go through Sysout
TestDialog( Frame frame, String name )200    public TestDialog( Frame frame, String name )
201     {
202       super( frame, name );
203       int scrollBoth = TextArea.SCROLLBARS_BOTH;
204       instructionsText = new TextArea( "", 10, maxStringLength, scrollBoth );
205       add( "North", instructionsText );
206 
207       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
208       add("South", messageText);
209 
210       pack();
211 
212       show();
213     }// TestDialog()
214 
215    //DO NOT call this directly, go through Sysout
printInstructions( String[] instructions )216    public void printInstructions( String[] instructions )
217     {
218       //Clear out any current instructions
219       instructionsText.setText( "" );
220 
221       //Go down array of instruction strings
222 
223       String printStr, remainingStr;
224       for( int i=0; i < instructions.length; i++ )
225        {
226      //chop up each into pieces maxSringLength long
227      remainingStr = instructions[ i ];
228      while( remainingStr.length() > 0 )
229       {
230         //if longer than max then chop off first max chars to print
231         if( remainingStr.length() >= maxStringLength )
232          {
233            //Try to chop on a word boundary
234            int posOfSpace = remainingStr.
235           lastIndexOf( ' ', maxStringLength - 1 );
236 
237            if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
238 
239            printStr = remainingStr.substring( 0, posOfSpace + 1 );
240            remainingStr = remainingStr.substring( posOfSpace + 1 );
241          }
242         //else just print
243         else
244          {
245            printStr = remainingStr;
246            remainingStr = "";
247          }
248 
249             instructionsText.append( printStr + "\n" );
250 
251       }// while
252 
253        }// for
254 
255     }//printInstructions()
256 
257    //DO NOT call this directly, go through Sysout
displayMessage( String messageIn )258    public void displayMessage( String messageIn )
259     {
260       messageText.append( messageIn + "\n" );
261     }
262 
263 }// TestDialog  class
264