1 /*
   2  * Copyright (c) 1998, 2016, 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  * @key headful
  27  * @bug     4108453 4778440 6304785
  28  * @summary Test Window.applyResourceBundle orientation support
  29  *
  30  * @run main WindowTest
  31  */
  32 
  33 import java.awt.*;
  34 
  35 public class WindowTest {
  36 
  37 
  38     public static void main(String args[]) throws Exception {
  39         Frame frame  = new Frame();
  40         frame.setSize(200,200);
  41         frame.setVisible(true);
  42         try {
  43             doTest(frame);
  44         } finally {
  45             frame.setVisible(false);
  46             frame.dispose();
  47         }
  48     }
  49 
  50     public static void doTest (Frame  myFrame) throws Exception{
  51         System.out.println("WindowTest {");
  52 
  53         // Create a window containing a hierarchy of components.
  54         System.out.println("  Creating component hierarchy...");
  55         myFrame.setLayout(new FlowLayout());
  56         Panel panel1 = new Panel();
  57         panel1.setLayout(new BorderLayout());
  58         panel1.add("North", new Button("North"));
  59         panel1.add("South", new Button("South"));
  60         panel1.add("East", new Button("East"));
  61         panel1.add("West", new Button("West"));
  62         panel1.add("Center", new Button("Center"));
  63         myFrame.add(panel1);
  64 
  65         Panel panel2 = new Panel();
  66         panel2.setLayout(new BorderLayout());
  67         panel2.add(BorderLayout.BEFORE_FIRST_LINE, new Button("FirstLine"));
  68         panel2.add(BorderLayout.AFTER_LAST_LINE, new Button("LastLine"));
  69         panel2.add(BorderLayout.BEFORE_LINE_BEGINS, new Button("FirstItem"));
  70         panel2.add(BorderLayout.AFTER_LINE_ENDS, new Button("LastItem"));
  71         panel2.add("Center", new Button("Center"));
  72         myFrame.add(panel2);
  73 
  74         // After construction, all of the components' orientations should be
  75         // set to ComponentOrientation.UNKNOWN.
  76         System.out.println("  Verifying orientation is UNKNOWN...");
  77         verifyOrientation(myFrame, ComponentOrientation.UNKNOWN);
  78 
  79         System.out.println("  Applying RIGHT_TO_LEFT orientation and verifying...");
  80         myFrame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);;
  81         verifyOrientation(myFrame, ComponentOrientation.RIGHT_TO_LEFT);
  82 
  83         System.out.println("  Applying LEFT_TO_RIGHT orientation and verifying...");
  84         myFrame.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
  85         verifyOrientation(myFrame, ComponentOrientation.LEFT_TO_RIGHT);
  86 
  87         System.out.println("}");
  88     }
  89 
  90     static void verifyOrientation(Component c, ComponentOrientation orient) {
  91 
  92         ComponentOrientation o = c.getComponentOrientation();
  93 
  94         if (o != orient) {
  95             throw new RuntimeException("ERROR: expected " + oString(orient) +
  96                                         ", got " + oString(o) +
  97                                         " on component " + c);
  98         }
  99 
 100         if (c instanceof Container) {
 101             Container cont = (Container) c;
 102             int ncomponents = cont.getComponentCount();
 103 
 104             for (int i = 0 ; i < ncomponents ; ++i) {
 105                 Component comp = cont.getComponent(i);
 106                 verifyOrientation(comp, orient);
 107             }
 108         }
 109     }
 110 
 111     static String oString(ComponentOrientation o) {
 112         if (o == ComponentOrientation.LEFT_TO_RIGHT) {
 113             return "LEFT_TO_RIGHT";
 114         }
 115         else if (o == ComponentOrientation.RIGHT_TO_LEFT) {
 116             return "RIGHT_TO_LEFT";
 117         }
 118         else {
 119             return "UNKNOWN";
 120         }
 121     }
 122 }