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 /* @test 25 @bug 6302514 26 @run main/manual=yesno PageDialogTest 27 @summary A toolkit modal dialog should not be blocked by Page/Print dialog. 28 */ 29 import java.awt.*; 30 import java.awt.event.*; 31 import java.awt.print.*; 32 33 import javax.swing.*; 34 35 public class PageDialogTest { 36 main(String[] args)37 public static void main(String[] args) { 38 String[] instructions = 39 { 40 "The test shows a Toolkit modal dialog. ", 41 "Click the 'Open' button. It opens a page dialog.", 42 "The test fails if the page dialog blocks the toolkit", 43 "modal dialog, otherwise it passes." 44 }; 45 46 Sysout.createDialog( ); 47 Sysout.printInstructions( instructions ); 48 49 Dialog td = new Dialog((Frame) null, "Toolkit modal dialog", 50 Dialog.ModalityType.TOOLKIT_MODAL); 51 td.setLayout(new FlowLayout()); 52 td.add(new Button("Dummy")); 53 Button tdb = new Button("Open"); 54 tdb.addActionListener(new ActionListener() { 55 public void actionPerformed(ActionEvent event) { 56 PrinterJob.getPrinterJob().pageDialog(new PageFormat()); 57 } 58 }); 59 td.add(tdb); 60 td.setSize(250, 150); 61 td.setVisible(true); 62 } 63 } 64 65 class Sysout { 66 private static TestDialog dialog; 67 createDialogWithInstructions( String[] instructions )68 public static void createDialogWithInstructions( String[] instructions ) 69 { 70 dialog = new TestDialog( new Frame(), "Instructions" ); 71 dialog.printInstructions( instructions ); 72 dialog.show(); 73 println( "Any messages for the tester will display here." ); 74 } 75 createDialog( )76 public static void createDialog( ) 77 { 78 dialog = new TestDialog( new Frame(), "Instructions" ); 79 String[] defInstr = { "Instructions will appear here. ", "" } ; 80 dialog.printInstructions( defInstr ); 81 dialog.show(); 82 println( "Any messages for the tester will display here." ); 83 } 84 85 printInstructions( String[] instructions )86 public static void printInstructions( String[] instructions ) 87 { 88 dialog.printInstructions( instructions ); 89 } 90 91 println( String messageIn )92 public static void println( String messageIn ) 93 { 94 dialog.displayMessage( messageIn ); 95 } 96 97 }// Sysout class 98 99 /** 100 This is part of the standard test machinery. It provides a place for the 101 test instructions to be displayed, and a place for interactive messages 102 to the user to be displayed. 103 To have the test instructions displayed, see Sysout. 104 To have a message to the user be displayed, see Sysout. 105 Do not call anything in this dialog directly. 106 */ 107 class TestDialog extends Dialog { 108 109 TextArea instructionsText; 110 TextArea messageText; 111 int maxStringLength = 80; 112 113 //DO NOT call this directly, go through Sysout TestDialog( Frame frame, String name )114 public TestDialog( Frame frame, String name ) 115 { 116 super( frame, name ); 117 int scrollBoth = TextArea.SCROLLBARS_BOTH; 118 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 119 add( "North", instructionsText ); 120 121 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 122 add("Center", messageText); 123 124 pack(); 125 126 show(); 127 }// TestDialog() 128 129 //DO NOT call this directly, go through Sysout printInstructions( String[] instructions )130 public void printInstructions( String[] instructions ) 131 { 132 //Clear out any current instructions 133 instructionsText.setText( "" ); 134 135 //Go down array of instruction strings 136 137 String printStr, remainingStr; 138 for( int i=0; i < instructions.length; i++ ) 139 { 140 //chop up each into pieces maxSringLength long 141 remainingStr = instructions[ i ]; 142 while( remainingStr.length() > 0 ) 143 { 144 //if longer than max then chop off first max chars to print 145 if( remainingStr.length() >= maxStringLength ) 146 { 147 //Try to chop on a word boundary 148 int posOfSpace = remainingStr. 149 lastIndexOf( ' ', maxStringLength - 1 ); 150 151 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 152 153 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 154 remainingStr = remainingStr.substring( posOfSpace + 1 ); 155 } 156 //else just print 157 else 158 { 159 printStr = remainingStr; 160 remainingStr = ""; 161 } 162 163 instructionsText.append( printStr + "\n" ); 164 165 }// while 166 167 }// for 168 169 }//printInstructions() 170 171 //DO NOT call this directly, go through Sysout displayMessage( String messageIn )172 public void displayMessage( String messageIn ) 173 { 174 messageText.append( messageIn + "\n" ); 175 } 176 177 }// TestDialog class 178