Path: blob/master/test/jdk/java/awt/FileDialog/MultipleMode/MultipleMode.java
41152 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24test25@bug 646720426@summary Need to implement "extended" native FileDialog for JFileChooser27@author [email protected] area=awt.filedialog28@run applet/manual=yesno MultipleMode.html29*/3031// Note there is no @ in front of test above. This is so that the32// harness will not mistake this file as a test file. It should33// only see the html file as a test file. (the harness runs all34// valid test files, so it would run this test twice if this file35// were valid as well as the html file.)36// Also, note the area= after Your Name in the author tag. Here, you37// should put which functional area the test falls in. See the38// AWT-core home page -> test areas and/or -> AWT team for a list of39// areas.40// There are several places where ManualYesNoTest appear. It is41// recommended that these be changed by a global search and replace,42// such as ESC-% in xemacs.43444546/**47* MultipleMode.java48*49* summary:50*/5152import java.applet.Applet;53import java.awt.*;54import java.awt.event.*;55import java.io.File;565758//Manual tests should run as applet tests if possible because they59// get their environments cleaned up, including AWT threads, any60// test created threads, and any system resources used by the test61// such as file descriptors. (This is normally not a problem as62// main tests usually run in a separate VM, however on some platforms63// such as the Mac, separate VMs are not possible and non-applet64// tests will cause problems). Also, you don't have to worry about65// synchronisation stuff in Applet tests the way you do in main66// tests...676869public class MultipleMode extends Applet70{71//Declare things used in the test, like buttons and labels here7273public void init()74{75//Create instructions for the user here, as well as set up76// the environment -- set the layout manager, add buttons,77// etc.78this.setLayout (new BorderLayout ());7980String[] instructions =81{82" 1. Turn the 'multiple' checkbox off and press the 'open' button ",83" 2. Verify that the file dialog doesn't allow the multiple file selection ",84" 3. Select any file and close the file dialog ",85" 4. The results will be displayed, verify the results ",86" 5. Turn the 'multiple' checkbox on and press the 'open' button ",87" 6. Verify that the file dialog allows the multiple file selection ",88" 7. Select several files and close the file dialog ",89" 8. The results will be displayed, verify the results "90};91Sysout.createDialogWithInstructions( instructions );9293}//End init()9495public void start ()96{97final Checkbox mode = new Checkbox("multiple", true);98Button open = new Button("open");99open.addActionListener(new ActionListener() {100@Override101public void actionPerformed(ActionEvent e) {102FileDialog d = new FileDialog((Frame)null);103d.setMultipleMode(mode.getState());104d.setVisible(true);105106// print the results107Sysout.println("DIR:");108Sysout.println(d.getDirectory());109Sysout.println("FILE:");110Sysout.println(d.getFile());111Sysout.println("FILES:");112File files[] = d.getFiles();113for (File f : files) {114Sysout.println(String.valueOf(f));115}116}117});118119setLayout(new FlowLayout());120add(mode);121add(open);122123//Get things going. Request focus, set size, et cetera124setSize (200,200);125setVisible(true);126validate();127128}// start()129130//The rest of this class is the actions which perform the test...131132//Use Sysout.println to communicate with the user NOT System.out!!133//Sysout.println ("Something Happened!");134135}// class ManualYesNoTest136137/* Place other classes related to the test after this line */138139140141142143/****************************************************144Standard Test Machinery145DO NOT modify anything below -- it's a standard146chunk of code whose purpose is to make user147interaction uniform, and thereby make it simpler148to read and understand someone else's test.149****************************************************/150151/**152This is part of the standard test machinery.153It creates a dialog (with the instructions), and is the interface154for sending text messages to the user.155To print the instructions, send an array of strings to Sysout.createDialog156WithInstructions method. Put one line of instructions per array entry.157To display a message for the tester to see, simply call Sysout.println158with the string to be displayed.159This mimics System.out.println but works within the test harness as well160as standalone.161*/162163class Sysout164{165private static TestDialog dialog;166private static boolean numbering = false;167private static int messageNumber = 0;168169public static void createDialogWithInstructions( String[] instructions )170{171dialog = new TestDialog( new Frame(), "Instructions" );172dialog.printInstructions( instructions );173dialog.setVisible(true);174println( "Any messages for the tester will display here." );175}176177public static void createDialog( )178{179dialog = new TestDialog( new Frame(), "Instructions" );180String[] defInstr = { "Instructions will appear here. ", "" } ;181dialog.printInstructions( defInstr );182dialog.setVisible(true);183println( "Any messages for the tester will display here." );184}185186/* Enables message counting for the tester. */187public static void enableNumbering(boolean enable){188numbering = enable;189}190191public static void printInstructions( String[] instructions )192{193dialog.printInstructions( instructions );194}195196197public static void println( String messageIn )198{199if (numbering) {200messageIn = "" + messageNumber + " " + messageIn;201messageNumber++;202}203dialog.displayMessage( messageIn );204}205206}// Sysout class207208/**209This is part of the standard test machinery. It provides a place for the210test instructions to be displayed, and a place for interactive messages211to the user to be displayed.212To have the test instructions displayed, see Sysout.213To have a message to the user be displayed, see Sysout.214Do not call anything in this dialog directly.215*/216class TestDialog extends Dialog217{218219TextArea instructionsText;220TextArea messageText;221int maxStringLength = 80;222223//DO NOT call this directly, go through Sysout224public TestDialog( Frame frame, String name )225{226super( frame, name );227int scrollBoth = TextArea.SCROLLBARS_BOTH;228instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );229add( "North", instructionsText );230231messageText = new TextArea( "", 5, maxStringLength, scrollBoth );232add("Center", messageText);233234pack();235236setVisible(true);237}// TestDialog()238239//DO NOT call this directly, go through Sysout240public void printInstructions( String[] instructions )241{242//Clear out any current instructions243instructionsText.setText( "" );244245//Go down array of instruction strings246247String printStr, remainingStr;248for( int i=0; i < instructions.length; i++ )249{250//chop up each into pieces maxSringLength long251remainingStr = instructions[ i ];252while( remainingStr.length() > 0 )253{254//if longer than max then chop off first max chars to print255if( remainingStr.length() >= maxStringLength )256{257//Try to chop on a word boundary258int posOfSpace = remainingStr.259lastIndexOf( ' ', maxStringLength - 1 );260261if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;262263printStr = remainingStr.substring( 0, posOfSpace + 1 );264remainingStr = remainingStr.substring( posOfSpace + 1 );265}266//else just print267else268{269printStr = remainingStr;270remainingStr = "";271}272273instructionsText.append( printStr + "\n" );274275}// while276277}// for278279}//printInstructions()280281//DO NOT call this directly, go through Sysout282public void displayMessage( String messageIn )283{284messageText.append( messageIn + "\n" );285System.out.println(messageIn);286}287288}// TestDialog class289290291