1 /*
   2  test
   3  @bug 4974135
   4  @summary FileDialog should open current directory by default.
   5  @author tav@sparc.spb.su area=awt.filedialog
   6  @run applet/manual=yesno FileDialogOpenDirTest.html
   7 */
   8 
   9 import java.awt.*;
  10 import java.awt.event.*;
  11 import java.applet.*;
  12 
  13 public class FileDialogOpenDirTest extends Applet {
  14 
  15     public static void main(String[] args) {
  16         Applet a = new FileDialogOpenDirTest();
  17         a.init();
  18         a.start();
  19     }
  20 
  21     public void init()
  22     {
  23         System.setProperty("sun.awt.disableGtkFileDialogs","true");
  24         //Create instructions for the user here, as well as set up
  25         // the environment -- set the layout manager, add buttons,
  26         // etc.
  27         this.setLayout (new BorderLayout ());
  28 
  29         String curdir = System.getProperty("user.dir");
  30 
  31         String[] instructions1 =
  32         {
  33             "After test started you will see 'Test Frame' with a button inside.",
  34             "Click the button to open FileDialog.",
  35             "Verify that the directory opened is current directory, that is:",
  36             curdir,
  37             "If so press PASSED, otherwise FAILED."
  38         };
  39 
  40         String[] instructions2 =
  41         {
  42             "Not for Windows. Press PASSED."
  43         };
  44 
  45         Sysout.createDialogWithInstructions(Toolkit.getDefaultToolkit().getClass().getName().
  46                                             equals("sun.awt.windows.WToolkit") ?
  47                                             instructions2 : instructions1);
  48     }
  49 
  50     public void start() {
  51         Frame frame = new Frame("Test Frame");
  52         Button open = new Button("Open File Dialog");
  53 
  54         open.addActionListener(new ActionListener() {
  55                 public void actionPerformed(ActionEvent e) {
  56                     new FileDialog(new Frame()).show();
  57                 }
  58             });
  59 
  60         frame.setLayout(new FlowLayout());
  61         frame.add(open);
  62 
  63         int x = 0;
  64         int y = 0;
  65         Component dlg = null;
  66 
  67         if ((dlg = Sysout.getDialog()) != null) {
  68             x = dlg.getBounds().x + dlg.getBounds().width;
  69             y = dlg.getBounds().y;
  70         }
  71         frame.setBounds(x, y, 150, 70);
  72         frame.setVisible(true);
  73     }
  74 }
  75 
  76 
  77 /****************************************************
  78  Standard Test Machinery
  79  DO NOT modify anything below -- it's a standard
  80   chunk of code whose purpose is to make user
  81   interaction uniform, and thereby make it simpler
  82   to read and understand someone else's test.
  83  ****************************************************/
  84 
  85 /**
  86  This is part of the standard test machinery.
  87  It creates a dialog (with the instructions), and is the interface
  88   for sending text messages to the user.
  89  To print the instructions, send an array of strings to Sysout.createDialog
  90   WithInstructions method.  Put one line of instructions per array entry.
  91  To display a message for the tester to see, simply call Sysout.println
  92   with the string to be displayed.
  93  This mimics System.out.println but works within the test harness as well
  94   as standalone.
  95  */
  96 
  97 class Sysout
  98 {
  99     private static TestDialog dialog;
 100 
 101     public static void createDialogWithInstructions( String[] instructions )
 102     {
 103         dialog = new TestDialog( new Frame(), "Instructions" );
 104         dialog.printInstructions( instructions );
 105         dialog.setVisible(true);
 106         println( "Any messages for the tester will display here." );
 107     }
 108 
 109     public static void createDialog( )
 110     {
 111         dialog = new TestDialog( new Frame(), "Instructions" );
 112         String[] defInstr = { "Instructions will appear here. ", "" } ;
 113         dialog.printInstructions( defInstr );
 114         dialog.setVisible(true);
 115         println( "Any messages for the tester will display here." );
 116     }
 117 
 118 
 119     public static void printInstructions( String[] instructions )
 120     {
 121         dialog.printInstructions( instructions );
 122     }
 123 
 124 
 125     public static void println( String messageIn )
 126     {
 127         dialog.displayMessage( messageIn );
 128     }
 129 
 130     public static Component getDialog() {
 131         return dialog;
 132     }
 133 
 134 }// Sysout  class
 135 
 136 /**
 137   This is part of the standard test machinery.  It provides a place for the
 138    test instructions to be displayed, and a place for interactive messages
 139    to the user to be displayed.
 140   To have the test instructions displayed, see Sysout.
 141   To have a message to the user be displayed, see Sysout.
 142   Do not call anything in this dialog directly.
 143   */
 144 class TestDialog extends Dialog
 145 {
 146 
 147     TextArea instructionsText;
 148     TextArea messageText;
 149     int maxStringLength = 80;
 150 
 151     //DO NOT call this directly, go through Sysout
 152     public TestDialog( Frame frame, String name )
 153     {
 154         super( frame, name );
 155         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 156         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 157         add( "North", instructionsText );
 158 
 159         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 160         add("Center", messageText);
 161 
 162         pack();
 163 
 164         setVisible(true);
 165     }// TestDialog()
 166 
 167     //DO NOT call this directly, go through Sysout
 168     public void printInstructions( String[] instructions )
 169     {
 170         //Clear out any current instructions
 171         instructionsText.setText( "" );
 172 
 173         //Go down array of instruction strings
 174 
 175         String printStr, remainingStr;
 176         for( int i=0; i < instructions.length; i++ )
 177         {
 178             //chop up each into pieces maxSringLength long
 179             remainingStr = instructions[ i ];
 180             while( remainingStr.length() > 0 )
 181             {
 182                 //if longer than max then chop off first max chars to print
 183                 if( remainingStr.length() >= maxStringLength )
 184                 {
 185                     //Try to chop on a word boundary
 186                     int posOfSpace = remainingStr.
 187                         lastIndexOf( ' ', maxStringLength - 1 );
 188 
 189                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 190 
 191                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 192                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 193                 }
 194                 //else just print
 195                 else
 196                 {
 197                     printStr = remainingStr;
 198                     remainingStr = "";
 199                 }
 200 
 201                 instructionsText.append( printStr + "\n" );
 202 
 203             }// while
 204 
 205         }// for
 206 
 207     }//printInstructions()
 208 
 209     //DO NOT call this directly, go through Sysout
 210     public void displayMessage( String messageIn )
 211     {
 212         messageText.append( messageIn + "\n" );
 213         System.out.println(messageIn);
 214     }
 215 
 216 }// TestDialog  class