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