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  * @build TestBundle TestBundle_es TestBundle_iw
31  * @build TestBundle1 TestBundle1_ar
32  * @run main WindowTest
33  */
34 
35 import java.awt.Frame;
36 import java.awt.Panel;
37 import java.awt.FlowLayout;
38 import java.awt.BorderLayout;
39 import java.awt.Button;
40 import java.awt.Component;
41 import java.awt.ComponentOrientation;
42 import java.awt.Container;
43 import java.util.Locale;
44 import java.util.ResourceBundle;
45 
46 public class WindowTest {
47 
main(String args[])48     public static void main(String args[]) throws Exception {
49         Frame frame  = new Frame();
50         frame.setSize(200,200);
51         frame.setVisible(true);
52         try {
53             doTest(frame);
54         } finally {
55             frame.setVisible(false);
56             frame.dispose();
57         }
58     }
59 
doTest(Frame myFrame)60     public static void doTest (Frame  myFrame) throws Exception{
61         System.out.println("WindowTest {");
62 
63         ResourceBundle rb;
64 
65         // Create a window containing a hierarchy of components.
66         System.out.println("  Creating component hierarchy...");
67         myFrame.setLayout(new FlowLayout());
68         Panel panel1 = new Panel();
69         panel1.setLayout(new BorderLayout());
70         panel1.add("North", new Button("North"));
71         panel1.add("South", new Button("South"));
72         panel1.add("East", new Button("East"));
73         panel1.add("West", new Button("West"));
74         panel1.add("Center", new Button("Center"));
75         myFrame.add(panel1);
76 
77         Panel panel2 = new Panel();
78         panel2.setLayout(new BorderLayout());
79         panel2.add(BorderLayout.BEFORE_FIRST_LINE, new Button("FirstLine"));
80         panel2.add(BorderLayout.AFTER_LAST_LINE, new Button("LastLine"));
81         panel2.add(BorderLayout.BEFORE_LINE_BEGINS, new Button("FirstItem"));
82         panel2.add(BorderLayout.AFTER_LINE_ENDS, new Button("LastItem"));
83         panel2.add("Center", new Button("Center"));
84         myFrame.add(panel2);
85 
86         // After construction, all of the components' orientations should be
87         // set to ComponentOrientation.UNKNOWN.
88         System.out.println("  Verifying orientation is UNKNOWN...");
89         verifyOrientation(myFrame, ComponentOrientation.UNKNOWN);
90 
91         // This will load TestBundle1 using the default locale and apply
92         // it to the component hierarchy.  Since the bundle has no Orientation
93         // specified, this should fall back to the bundle-locale's orientation
94         System.out.println("  Applying TestBundle1 by name and verifying...");
95         myFrame.applyResourceBundle("TestBundle1");
96         verifyOrientation(myFrame,
97                     ComponentOrientation.getOrientation(
98                         ResourceBundle.getBundle("TestBundle1", Locale.getDefault())));
99 
100         System.out.println("  Applying TestBundle_iw and verifying...");
101         rb = ResourceBundle.getBundle("TestBundle", new Locale("iw", ""));
102         myFrame.applyResourceBundle(rb);
103         verifyOrientation(myFrame, ComponentOrientation.RIGHT_TO_LEFT);
104 
105         System.out.println("  Applying TestBundle_es and verifying...");
106         rb = ResourceBundle.getBundle("TestBundle", new Locale("es", ""));
107         myFrame.applyResourceBundle(rb);
108         verifyOrientation(myFrame, ComponentOrientation.LEFT_TO_RIGHT);
109 
110         System.out.println("}");
111     }
112 
verifyOrientation(Component c, ComponentOrientation orient)113     static void verifyOrientation(Component c, ComponentOrientation orient) {
114 
115         ComponentOrientation o = c.getComponentOrientation();
116 
117         if (o != orient) {
118             throw new RuntimeException("ERROR: expected " + oString(orient) +
119                                         ", got " + oString(o) +
120                                         " on component " + c);
121         }
122 
123         if (c instanceof Container) {
124             Container cont = (Container) c;
125             int ncomponents = cont.getComponentCount();
126 
127             for (int i = 0 ; i < ncomponents ; ++i) {
128                 Component comp = cont.getComponent(i);
129                 verifyOrientation(comp, orient);
130             }
131         }
132     }
133 
oString(ComponentOrientation o)134     static String oString(ComponentOrientation o) {
135         if (o == ComponentOrientation.LEFT_TO_RIGHT) {
136             return "LEFT_TO_RIGHT";
137         }
138         else if (o == ComponentOrientation.RIGHT_TO_LEFT) {
139             return "RIGHT_TO_LEFT";
140         }
141         else {
142             return "UNKNOWN";
143         }
144     }
145 }
146