RegexpFilterTest.java revision 0:37a05a11f281
1249259Sdim/* 2249259Sdim * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. 3249259Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4249259Sdim * 5249259Sdim * This code is free software; you can redistribute it and/or modify it 6249259Sdim * under the terms of the GNU General Public License version 2 only, as 7249259Sdim * published by the Free Software Foundation. 8249259Sdim * 9249259Sdim * This code is distributed in the hope that it will be useful, but WITHOUT 10249259Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11249259Sdim * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12249259Sdim * version 2 for more details (a copy is included in the LICENSE file that 13249259Sdim * accompanied this code). 14249259Sdim * 15249259Sdim * You should have received a copy of the GNU General Public License version 16249259Sdim * 2 along with this work; if not, write to the Free Software Foundation, 17249259Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18249259Sdim * 19249259Sdim * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20249259Sdim * CA 95054 USA or visit www.sun.com if you need additional information or 21249259Sdim * have any questions. 22249259Sdim */ 23249259Sdim 24249259Sdim/* 25249259Sdim test 26249259Sdim @bug 4934185 27249259Sdim @summary JCK1.5-runtime-interactive: XToolkit FileDialog does not work as expected 28249259Sdim @author Dmitry.Cherepanov area=awt.filedialog 29249259Sdim @run applet/manual=yesno RegexpFilterTest.html 30249259Sdim*/ 31249259Sdim 32249259Sdimimport java.applet.Applet; 33249259Sdimimport java.awt.*; 34249259Sdimimport java.awt.event.*; 35249259Sdim 36249259Sdim/* 37249259Sdim * Motif file dialogs let the user specify a filter that controls the files that 38249259Sdim * are displayed in the dialog. This filter is generally specified as a regular 39249259Sdim * expression. The test verifies that Motif-like filtering works fine using 40249259Sdim * XAWT-toolkit also. 41249259Sdim */ 42249259Sdimpublic class RegexpFilterTest extends Applet 43249259Sdim{ 44249259Sdim 45249259Sdim public static void main(String[] args) { 46249259Sdim Applet a = new RegexpFilterTest(); 47249259Sdim a.init(); 48249259Sdim a.start(); 49249259Sdim } 50249259Sdim 51249259Sdim public void init() 52249259Sdim { 53249259Sdim this.setLayout (new BorderLayout ()); 54249259Sdim 55249259Sdim String[] instructions = 56249259Sdim { 57249259Sdim " 0. The test is only for X platforms", 58249259Sdim " 1. Press the 'Show' button and a file dialog will appear, ", 59249259Sdim " 2. Input any string into the 'Filter' text field, ", 60249259Sdim " This filter is generally specified as a regular expression ", 61249259Sdim " (e.g., * matches all files, while *.c matches all files that end in .c) ", 62249259Sdim " 3. Press 'Enter' to refresh the displayed files with the filter, ", 63249259Sdim " 4. If the list of the files contains all actual files matched the filter, ", 64249259Sdim " then the test passed; otherwise it failed. " 65249259Sdim }; 66249259Sdim Sysout.createDialogWithInstructions( instructions ); 67249259Sdim 68249259Sdim }//End init() 69249259Sdim 70249259Sdim public void start () 71249259Sdim { 72249259Sdim setLayout(new FlowLayout()); 73249259Sdim Button button = new Button("Show"); 74249259Sdim add(button); 75249259Sdim 76249259Sdim button.addActionListener(new ActionListener() { 77249259Sdim public void actionPerformed(ActionEvent e) { 78249259Sdim FileDialog fd = new FileDialog(new Frame()); 79249259Sdim fd.setVisible(true); 80249259Sdim } 81249259Sdim }); 82249259Sdim 83249259Sdim setSize (200,200); 84249259Sdim setVisible(true); 85249259Sdim validate(); 86249259Sdim } 87249259Sdim 88249259Sdim} 89249259Sdim 90249259Sdim 91249259Sdim/**************************************************** 92249259Sdim Standard Test Machinery 93249259Sdim DO NOT modify anything below -- it's a standard 94249259Sdim chunk of code whose purpose is to make user 95249259Sdim interaction uniform, and thereby make it simpler 96249259Sdim to read and understand someone else's test. 97249259Sdim ****************************************************/ 98249259Sdim 99249259Sdim/** 100249259Sdim This is part of the standard test machinery. 101249259Sdim It creates a dialog (with the instructions), and is the interface 102249259Sdim for sending text messages to the user. 103249259Sdim To print the instructions, send an array of strings to Sysout.createDialog 104249259Sdim WithInstructions method. Put one line of instructions per array entry. 105249259Sdim To display a message for the tester to see, simply call Sysout.println 106249259Sdim with the string to be displayed. 107249259Sdim This mimics System.out.println but works within the test harness as well 108 as standalone. 109 */ 110 111class Sysout 112{ 113 private static TestDialog dialog; 114 115 public static void createDialogWithInstructions( String[] instructions ) 116 { 117 dialog = new TestDialog( new Frame(), "Instructions" ); 118 dialog.printInstructions( instructions ); 119 dialog.setVisible(true); 120 println( "Any messages for the tester will display here." ); 121 } 122 123 public static void createDialog( ) 124 { 125 dialog = new TestDialog( new Frame(), "Instructions" ); 126 String[] defInstr = { "Instructions will appear here. ", "" } ; 127 dialog.printInstructions( defInstr ); 128 dialog.setVisible(true); 129 println( "Any messages for the tester will display here." ); 130 } 131 132 133 public static void printInstructions( String[] instructions ) 134 { 135 dialog.printInstructions( instructions ); 136 } 137 138 139 public static void println( String messageIn ) 140 { 141 dialog.displayMessage( messageIn ); 142 } 143 144}// Sysout class 145 146/** 147 This is part of the standard test machinery. It provides a place for the 148 test instructions to be displayed, and a place for interactive messages 149 to the user to be displayed. 150 To have the test instructions displayed, see Sysout. 151 To have a message to the user be displayed, see Sysout. 152 Do not call anything in this dialog directly. 153 */ 154class TestDialog extends Dialog 155{ 156 157 TextArea instructionsText; 158 TextArea messageText; 159 int maxStringLength = 100; 160 161 //DO NOT call this directly, go through Sysout 162 public TestDialog( Frame frame, String name ) 163 { 164 super( frame, name ); 165 int scrollBoth = TextArea.SCROLLBARS_BOTH; 166 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 167 add( "North", instructionsText ); 168 169 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 170 add("Center", messageText); 171 172 pack(); 173 174 setVisible(true); 175 }// TestDialog() 176 177 //DO NOT call this directly, go through Sysout 178 public void printInstructions( String[] instructions ) 179 { 180 //Clear out any current instructions 181 instructionsText.setText( "" ); 182 183 //Go down array of instruction strings 184 185 String printStr, remainingStr; 186 for( int i=0; i < instructions.length; i++ ) 187 { 188 //chop up each into pieces maxSringLength long 189 remainingStr = instructions[ i ]; 190 while( remainingStr.length() > 0 ) 191 { 192 //if longer than max then chop off first max chars to print 193 if( remainingStr.length() >= maxStringLength ) 194 { 195 //Try to chop on a word boundary 196 int posOfSpace = remainingStr. 197 lastIndexOf( ' ', maxStringLength - 1 ); 198 199 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 200 201 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 202 remainingStr = remainingStr.substring( posOfSpace + 1 ); 203 } 204 //else just print 205 else 206 { 207 printStr = remainingStr; 208 remainingStr = ""; 209 } 210 211 instructionsText.append( printStr + "\n" ); 212 213 }// while 214 215 }// for 216 217 }//printInstructions() 218 219 //DO NOT call this directly, go through Sysout 220 public void displayMessage( String messageIn ) 221 { 222 messageText.append( messageIn + "\n" ); 223 System.out.println(messageIn); 224 } 225 226}// TestDialog class 227