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 6260676 27 @summary FileDialog.setDirectory() does not work properly, XToolkit 28 @author Dmitry.Cherepanov area=awt.filedialog 29 @run applet/manual=yesno FileDialogReturnTest.html 30*/ 31 32import java.applet.Applet; 33import java.awt.*; 34import java.awt.event.*; 35 36/* 37 * Current implementation of the FileDialog class doesn't provide 38 * any explicit method to get the return value after the user closes 39 * the dialog. The only way to detect whether the user cancels the 40 * dialog or the user selects any file is to use the getFile() method. 41 * The getFile() method should return null value if the user cancels 42 * the dialog or non-null value if the user selects any file. 43 */ 44public class FileDialogReturnTest extends Applet 45{ 46 47 public static void main(String[] args) { 48 Applet a = new FileDialogReturnTest(); 49 a.init(); 50 a.start(); 51 } 52 53 public void init() 54 { 55 this.setLayout (new BorderLayout ()); 56 57 String[] instructions = 58 { 59 " 1. The test shows the 'FileDialogReturnTest' applet which contains two text fields and one button, ", 60 " 2. Input something into the 'File:' text field or just keep the field empty, ", 61 " 3. Input something into the 'Dir:' text field or just keep the field empty, ", 62 " 4. Press the 'Show' button and a file dialog will appear, ", 63 " 5-1. Cancel the file dialog, e.g. by selecting the 'close' menu item, ", 64 " If the output window shows that 'file'/'dir' values is null then the test passes, otherwise the test fails, ", 65 " 5-2. Select any file, e.g. by pressing the 'OK' button, ", 66 " If the output window shows that 'file'/'dir' values is not-null then the test passes, otherwise the test fails. " 67 }; 68 Sysout.createDialogWithInstructions( instructions ); 69 70 }//End init() 71 72 final TextField fileField = new TextField("", 20); 73 final TextField dirField = new TextField("", 20); 74 final Button button = new Button("Show"); 75 76 public void start () 77 { 78 setLayout(new FlowLayout()); 79 80 add(new Label("File:")); 81 add(fileField); 82 add(new Label("Dir:")); 83 add(dirField); 84 add(button); 85 86 button.addActionListener(new ActionListener() { 87 public void actionPerformed(ActionEvent e) { 88 showDialog(); 89 } 90 }); 91 92 setSize (200,200); 93 setVisible(true); 94 validate(); 95 } 96 97 void showDialog() 98 { 99 FileDialog fd = new FileDialog(new Frame()); 100 fd.setFile(fileField.getText()); 101 fd.setDirectory(dirField.getText()); 102 fd.setVisible(true); 103 104 Sysout.println("[file=" + fd.getFile()+"]"); 105 Sysout.println("[dir=" + fd.getDirectory()+"]"); 106 } 107 108} 109 110 111/**************************************************** 112 Standard Test Machinery 113 DO NOT modify anything below -- it's a standard 114 chunk of code whose purpose is to make user 115 interaction uniform, and thereby make it simpler 116 to read and understand someone else's test. 117 ****************************************************/ 118 119/** 120 This is part of the standard test machinery. 121 It creates a dialog (with the instructions), and is the interface 122 for sending text messages to the user. 123 To print the instructions, send an array of strings to Sysout.createDialog 124 WithInstructions method. Put one line of instructions per array entry. 125 To display a message for the tester to see, simply call Sysout.println 126 with the string to be displayed. 127 This mimics System.out.println but works within the test harness as well 128 as standalone. 129 */ 130 131class Sysout 132{ 133 private static TestDialog dialog; 134 135 public static void createDialogWithInstructions( String[] instructions ) 136 { 137 dialog = new TestDialog( new Frame(), "Instructions" ); 138 dialog.printInstructions( instructions ); 139 dialog.setVisible(true); 140 println( "Any messages for the tester will display here." ); 141 } 142 143 public static void createDialog( ) 144 { 145 dialog = new TestDialog( new Frame(), "Instructions" ); 146 String[] defInstr = { "Instructions will appear here. ", "" } ; 147 dialog.printInstructions( defInstr ); 148 dialog.setVisible(true); 149 println( "Any messages for the tester will display here." ); 150 } 151 152 153 public static void printInstructions( String[] instructions ) 154 { 155 dialog.printInstructions( instructions ); 156 } 157 158 159 public static void println( String messageIn ) 160 { 161 dialog.displayMessage( messageIn ); 162 } 163 164}// Sysout class 165 166/** 167 This is part of the standard test machinery. It provides a place for the 168 test instructions to be displayed, and a place for interactive messages 169 to the user to be displayed. 170 To have the test instructions displayed, see Sysout. 171 To have a message to the user be displayed, see Sysout. 172 Do not call anything in this dialog directly. 173 */ 174class TestDialog extends Dialog 175{ 176 177 TextArea instructionsText; 178 TextArea messageText; 179 int maxStringLength = 100; 180 181 //DO NOT call this directly, go through Sysout 182 public TestDialog( Frame frame, String name ) 183 { 184 super( frame, name ); 185 int scrollBoth = TextArea.SCROLLBARS_BOTH; 186 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 187 add( "North", instructionsText ); 188 189 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 190 add("Center", messageText); 191 192 pack(); 193 194 setVisible(true); 195 }// TestDialog() 196 197 //DO NOT call this directly, go through Sysout 198 public void printInstructions( String[] instructions ) 199 { 200 //Clear out any current instructions 201 instructionsText.setText( "" ); 202 203 //Go down array of instruction strings 204 205 String printStr, remainingStr; 206 for( int i=0; i < instructions.length; i++ ) 207 { 208 //chop up each into pieces maxSringLength long 209 remainingStr = instructions[ i ]; 210 while( remainingStr.length() > 0 ) 211 { 212 //if longer than max then chop off first max chars to print 213 if( remainingStr.length() >= maxStringLength ) 214 { 215 //Try to chop on a word boundary 216 int posOfSpace = remainingStr. 217 lastIndexOf( ' ', maxStringLength - 1 ); 218 219 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 220 221 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 222 remainingStr = remainingStr.substring( posOfSpace + 1 ); 223 } 224 //else just print 225 else 226 { 227 printStr = remainingStr; 228 remainingStr = ""; 229 } 230 231 instructionsText.append( printStr + "\n" ); 232 233 }// while 234 235 }// for 236 237 }//printInstructions() 238 239 //DO NOT call this directly, go through Sysout 240 public void displayMessage( String messageIn ) 241 { 242 messageText.append( messageIn + "\n" ); 243 System.out.println(messageIn); 244 } 245 246}// TestDialog class 247