1 /* 2 * Copyright (c) 2012, 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 4157271 27 @summary Checks that when a Frame is created it honors the state it 28 was set to. The bug was that if setState(Frame.ICONIFIED) was 29 called before setVisible(true) the Frame would be shown in NORMAL 30 state instead of ICONIFIED. 31 @author JTG East Team: area=awt.Frame 32 @run applet/manual=yesno FrameStateTest.html 33 */ 34 35 /** 36 * FrameStateTest.java 37 * 38 * summary: Checks that when setState(Frame.ICONIFIED) is called before 39 * setVisible(true) the Frame is shown in the proper iconified state. 40 * The problem was that it did not honor the initial iconic state, but 41 * instead was shown in the NORMAL state. 42 */ 43 44 import java.awt.event.*; 45 import java.awt.*; 46 import java.lang.*; 47 import java.applet.Applet; 48 49 50 public class FrameStateTest extends Applet implements ActionListener, ItemListener{ 51 52 Button btnCreate = new Button("Create Frame"); 53 Button btnDispose = new Button("Dispose Frame"); 54 CheckboxGroup cbgState = new CheckboxGroup(); 55 CheckboxGroup cbgResize = new CheckboxGroup(); 56 Checkbox cbIconState = new Checkbox("Frame state ICONIFIED",cbgState,false); 57 Checkbox cbNormState = new Checkbox("Frame state NORMAL",cbgState,true); 58 Checkbox cbNonResize = new Checkbox("Frame Nonresizable",cbgResize,false); 59 Checkbox cbResize = new Checkbox("Frame Resizable",cbgResize,true); 60 int iState = 0; 61 boolean bResize = true; 62 CreateFrame icontst; 63 64 public void init() { 65 this.setLayout (new BorderLayout ()); 66 67 String[] instructions = 68 { 69 "Steps to try to reproduce this problem:", 70 "When this test is run an Applet Viewer window will display. In the", 71 "Applet Viewer window select the different options for the Frame (i.e.", 72 "{Normal, Non-resizalbe}, {Normal, Resizable}, {Iconified, Resizable},", 73 "{Iconified, Non-resizalbe}). After chosing the Frame's state click the", 74 "Create Frame button. After the Frame (Frame State Test (Window2)) comes", 75 "up make sure the proper behavior occurred (Frame shown in proper state).", 76 "Click the Dispose button to close the Frame. Do the above steps for all", 77 "the different Frame state combinations available. If you observe the", 78 "proper behavior the test has passed, Press the Pass button. Otherwise", 79 "the test has failed, Press the Fail button.", 80 "Note: In Frame State Test (Window2) you can also chose the different", 81 "buttons to see different Frame behavior. An example of a problem that", 82 "has been seen, With the Frame nonresizable you can not iconify the Frame." 83 }; 84 Sysout.createDialogWithInstructions( instructions ); 85 86 btnDispose.setEnabled(false); 87 add(btnCreate, BorderLayout.NORTH); 88 add(btnDispose, BorderLayout.SOUTH); 89 90 Panel p = new Panel(new GridLayout(0,1)); 91 p.add(cbIconState); 92 p.add(cbResize); 93 add(p, BorderLayout.WEST); 94 95 p = new Panel(new GridLayout(0,1)); 96 p.add(cbNormState); 97 p.add(cbNonResize); 98 add(p, BorderLayout.EAST); 99 100 // Add Listeners 101 btnDispose.addActionListener(this); 102 btnCreate.addActionListener(this); 103 cbNormState.addItemListener(this); 104 cbResize.addItemListener(this); 105 cbIconState.addItemListener(this); 106 cbNonResize.addItemListener(this); 107 108 resize(600, 200); 109 110 }//End init() 111 112 public void actionPerformed(ActionEvent evt) { 113 114 115 if (evt.getSource() == btnCreate) { 116 btnCreate.setEnabled(false); 117 btnDispose.setEnabled(true); 118 icontst = new CreateFrame(iState, bResize); 119 icontst.show(); 120 } else if (evt.getSource() == btnDispose) { 121 btnCreate.setEnabled(true); 122 btnDispose.setEnabled(false); 123 icontst.dispose(); 124 } 125 } 126 127 public void itemStateChanged(ItemEvent evt) { 128 129 if (cbNormState.getState()) iState = 0; 130 if (cbIconState.getState()) iState = 1; 131 if (cbResize.getState()) bResize = true; 132 if (cbNonResize.getState()) bResize = false; 133 134 } 135 136 }// class FrameStateTest 137 138 139 class CreateFrame extends Frame implements ActionListener , WindowListener { 140 141 static int e=0; 142 static int u=0; 143 static int p=0; 144 static int i=0; 145 static int v=0; 146 147 Button b1, b2, b3, b4, b5, b6, b7; 148 boolean resizable = true; 149 boolean iconic = false; 150 String name = "Frame State Test"; 151 152 CreateFrame (int iFrameState, boolean bFrameResizable) { 153 154 setTitle("Frame State Test (Window 2)"); 155 156 if (iFrameState == 1) { 157 iconic = true; 158 } 159 160 if (!(bFrameResizable)) { 161 resizable = false; 162 } 163 164 System.out.println("CREATING FRAME - Initially "+ 165 ((iconic) ? "ICONIFIED" : "NORMAL (NON-ICONIFIED)") + " and " + 166 ((resizable) ? "RESIZABLE" : "NON-RESIZABLE") ); 167 168 Sysout.println("CREATING FRAME - Initially "+ 169 ((iconic) ? "ICONIFIED" : "NORMAL (NON-ICONIFIED)") + " and " + 170 ((resizable) ? "RESIZABLE" : "NON-RESIZABLE") ); 171 172 setLayout(new FlowLayout() ); 173 b1 = new Button("resizable"); 174 add(b1); 175 b2 = new Button("resize"); 176 add(b2); 177 b3 = new Button("iconify"); 178 add(b3); 179 b4 = new Button("iconify and restore"); 180 add(b4); 181 b5 = new Button("hide and show"); 182 add(b5); 183 b6 = new Button("hide, iconify and show"); 184 add(b6); 185 b7 = new Button("hide, iconify, show, and restore"); 186 add(b7); 187 b1.addActionListener(this); 188 b2.addActionListener(this); 189 b3.addActionListener(this); 190 b4.addActionListener(this); 191 b5.addActionListener(this); 192 b6.addActionListener(this); 193 b7.addActionListener(this); 194 addWindowListener(this); 195 196 setBounds(100,2,200, 200); 197 setState(iconic ? Frame.ICONIFIED: Frame.NORMAL); 198 setResizable(resizable); 199 pack(); 200 setVisible(true); 201 202 } 203 204 public void actionPerformed ( ActionEvent e ) 205 { 206 if ( e.getSource() == b2 ) { 207 Rectangle r = this.getBounds(); 208 r.width += 10; 209 System.out.println(" - button pressed - setting bounds on Frame to: "+r); 210 setBounds(r); 211 validate(); 212 } else if ( e.getSource() == b1 ) { 213 resizable = !resizable; 214 System.out.println(" - button pressed - setting Resizable to: "+resizable); 215 ((Frame)(b1.getParent())).setResizable(resizable); 216 } else if ( e.getSource() == b3 ) { 217 System.out.println(" - button pressed - setting Iconic: "); 218 dolog(); 219 ((Frame)(b1.getParent())).setState(Frame.ICONIFIED); 220 dolog(); 221 } else if ( e.getSource() == b4 ) { 222 System.out.println(" - button pressed - setting Iconic: "); 223 dolog(); 224 ((Frame)(b1.getParent())).setState(Frame.ICONIFIED); 225 dolog(); 226 try { 227 Thread.sleep(1000); 228 } catch (Exception ex) {}; 229 System.out.println(" - now restoring: "); 230 ((Frame)(b1.getParent())).setState(Frame.NORMAL); 231 dolog(); 232 } else if ( e.getSource() == b5 ) { 233 System.out.println(" - button pressed - hiding : "); 234 dolog(); 235 ((Frame)(b1.getParent())).setVisible(false); 236 dolog(); 237 try { 238 Thread.sleep(1000); 239 } catch (Exception ex) {}; 240 System.out.println(" - now reshowing: "); 241 ((Frame)(b1.getParent())).setVisible(true); 242 dolog(); 243 } else if ( e.getSource() == b6 ) { 244 System.out.println(" - button pressed - hiding : "); 245 dolog(); 246 ((Frame)(b1.getParent())).setVisible(false); 247 dolog(); 248 try { 249 Thread.sleep(1000); 250 } catch (Exception ex) {}; 251 System.out.println(" - setting Iconic: "); 252 dolog(); 253 ((Frame)(b1.getParent())).setState(Frame.ICONIFIED); 254 try { 255 Thread.sleep(1000); 256 } catch (Exception ex) {}; 257 System.out.println(" - now reshowing: "); 258 ((Frame)(b1.getParent())).setVisible(true); 259 dolog(); 260 } else if ( e.getSource() == b7 ) { 261 System.out.println(" - button pressed - hiding : "); 262 dolog(); 263 ((Frame)(b1.getParent())).setVisible(false); 264 dolog(); 265 try { 266 Thread.sleep(1000); 267 } catch (Exception ex) {}; 268 System.out.println(" - setting Iconic: "); 269 dolog(); 270 ((Frame)(b1.getParent())).setState(Frame.ICONIFIED); 271 try { 272 Thread.sleep(1000); 273 } catch (Exception ex) {}; 274 System.out.println(" - now reshowing: "); 275 ((Frame)(b1.getParent())).setVisible(true); 276 dolog(); 277 try { 278 Thread.sleep(1000); 279 } catch (Exception ex) {}; 280 System.out.println(" - now restoring: "); 281 ((Frame)(b1.getParent())).setState(Frame.NORMAL); 282 dolog(); 283 } 284 } 285 286 public void windowActivated(WindowEvent e) { 287 System.out.println(name + " Activated"); 288 dolog(); 289 } 290 public void windowClosed(WindowEvent e) { 291 System.out.println(name + " Closed"); 292 dolog(); 293 } 294 public void windowClosing(WindowEvent e) { 295 ((Window)(e.getSource())).dispose(); 296 System.out.println(name + " Closing"); 297 dolog(); 298 } 299 public void windowDeactivated(WindowEvent e) { 300 System.out.println(name + " Deactivated"); 301 dolog(); 302 } 303 public void windowDeiconified(WindowEvent e) { 304 System.out.println(name + " Deiconified"); 305 dolog(); 306 } 307 public void windowIconified(WindowEvent e) { 308 System.out.println(name + " Iconified"); 309 dolog(); 310 } 311 public void windowOpened(WindowEvent e) { 312 System.out.println(name + " Opened"); 313 dolog(); 314 } 315 316 public void dolog() { 317 System.out.println(" getState returns: "+getState()); 318 } 319 } 320 321 // }// class FrameStateTest 322 323 /**************************************************** 324 Standard Test Machinery 325 DO NOT modify anything below -- it's a standard 326 chunk of code whose purpose is to make user 327 interaction uniform, and thereby make it simpler 328 to read and understand someone else's test. 329 ****************************************************/ 330 331 /** 332 This is part of the standard test machinery. 333 It creates a dialog (with the instructions), and is the interface 334 for sending text messages to the user. 335 To print the instructions, send an array of strings to Sysout.createDialog 336 WithInstructions method. Put one line of instructions per array entry. 337 To display a message for the tester to see, simply call Sysout.println 338 with the string to be displayed. 339 This mimics System.out.println but works within the test harness as well 340 as standalone. 341 */ 342 343 class Sysout 344 { 345 private static TestDialog dialog; 346 347 public static void createDialogWithInstructions( String[] instructions ) 348 { 349 dialog = new TestDialog( new Frame(), "Instructions" ); 350 dialog.printInstructions( instructions ); 351 dialog.show(); 352 println( "Any messages for the tester will display here." ); 353 } 354 355 public static void createDialog( ) 356 { 357 dialog = new TestDialog( new Frame(), "Instructions" ); 358 String[] defInstr = { "Instructions will appear here. ", "" } ; 359 dialog.printInstructions( defInstr ); 360 dialog.show(); 361 println( "Any messages for the tester will display here." ); 362 } 363 364 365 public static void printInstructions( String[] instructions ) 366 { 367 dialog.printInstructions( instructions ); 368 } 369 370 371 public static void println( String messageIn ) 372 { 373 dialog.displayMessage( messageIn ); 374 } 375 376 }// Sysout class 377 378 /** 379 This is part of the standard test machinery. It provides a place for the 380 test instructions to be displayed, and a place for interactive messages 381 to the user to be displayed. 382 To have the test instructions displayed, see Sysout. 383 To have a message to the user be displayed, see Sysout. 384 Do not call anything in this dialog directly. 385 */ 386 class TestDialog extends Dialog 387 { 388 389 TextArea instructionsText; 390 TextArea messageText; 391 int maxStringLength = 80; 392 393 //DO NOT call this directly, go through Sysout 394 public TestDialog( Frame frame, String name ) 395 { 396 super( frame, name ); 397 int scrollBoth = TextArea.SCROLLBARS_BOTH; 398 int scrollNone = TextArea.SCROLLBARS_NONE; 399 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 400 add( "North", instructionsText ); 401 402 messageText = new TextArea( "", 10, maxStringLength, scrollBoth ); 403 add("South", messageText); 404 405 pack(); 406 407 show(); 408 }// TestDialog() 409 410 //DO NOT call this directly, go through Sysout 411 public void printInstructions( String[] instructions ) 412 { 413 //Clear out any current instructions 414 instructionsText.setText( "" ); 415 416 //Go down array of instruction strings 417 418 String printStr, remainingStr; 419 for( int i=0; i < instructions.length; i++ ) 420 { 421 //chop up each into pieces maxSringLength long 422 remainingStr = instructions[ i ]; 423 while( remainingStr.length() > 0 ) 424 { 425 //if longer than max then chop off first max chars to print 426 if( remainingStr.length() >= maxStringLength ) 427 { 428 //Try to chop on a word boundary 429 int posOfSpace = remainingStr. 430 lastIndexOf( ' ', maxStringLength - 1 ); 431 432 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 433 434 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 435 remainingStr = remainingStr.substring( posOfSpace + 1 ); 436 } 437 //else just print 438 else 439 { 440 printStr = remainingStr; 441 remainingStr = ""; 442 } 443 444 instructionsText.append( printStr + "\n" ); 445 446 }// while 447 448 }// for 449 450 }//printInstructions() 451 452 //DO NOT call this directly, go through Sysout 453 public void displayMessage( String messageIn ) 454 { 455 messageText.append( messageIn + "\n" ); 456 } 457 458 459 }// TestDialog class