4916852: RFE: LTP: BorderLayout Persistence Delegate should use 1.5 API
authormalenkov
Tue, 08 Jul 2008 16:40:38 +0400
changeset 1279 09ac82fafd79
parent 1278 86e7dcab1bef
child 1280 f58fc9f575e3
4916852: RFE: LTP: BorderLayout Persistence Delegate should use 1.5 API Reviewed-by: peterz, loneid
jdk/src/share/classes/java/beans/MetaData.java
jdk/test/java/beans/XMLEncoder/java_awt_BorderLayout.java
jdk/test/java/beans/XMLEncoder/java_awt_Component.java
--- a/jdk/src/share/classes/java/beans/MetaData.java	Tue Jul 08 11:36:19 2008 +0400
+++ b/jdk/src/share/classes/java/beans/MetaData.java	Tue Jul 08 16:40:38 2008 +0400
@@ -986,14 +986,20 @@
         // null to defined values after the Windows are made visible -
         // special case them for now.
         if (!(oldInstance instanceof java.awt.Window)) {
-            String[] fieldNames = new String[]{"background", "foreground", "font"};
-            for(int i = 0; i < fieldNames.length; i++) {
-                String name = fieldNames[i];
-                Object oldValue = ReflectionUtils.getPrivateField(oldInstance, java.awt.Component.class, name, out.getExceptionListener());
-                Object newValue = (newInstance == null) ? null : ReflectionUtils.getPrivateField(newInstance, java.awt.Component.class, name, out.getExceptionListener());
-                if (oldValue != null && !oldValue.equals(newValue)) {
-                    invokeStatement(oldInstance, "set" + NameGenerator.capitalize(name), new Object[]{oldValue}, out);
-                }
+            Object oldBackground = c.isBackgroundSet() ? c.getBackground() : null;
+            Object newBackground = c2.isBackgroundSet() ? c2.getBackground() : null;
+            if (!MetaData.equals(oldBackground, newBackground)) {
+                invokeStatement(oldInstance, "setBackground", new Object[] { oldBackground }, out);
+            }
+            Object oldForeground = c.isForegroundSet() ? c.getForeground() : null;
+            Object newForeground = c2.isForegroundSet() ? c2.getForeground() : null;
+            if (!MetaData.equals(oldForeground, newForeground)) {
+                invokeStatement(oldInstance, "setForeground", new Object[] { oldForeground }, out);
+            }
+            Object oldFont = c.isFontSet() ? c.getFont() : null;
+            Object newFont = c2.isFontSet() ? c2.getFont() : null;
+            if (!MetaData.equals(oldFont, newFont)) {
+                invokeStatement(oldInstance, "setFont", new Object[] { oldFont }, out);
             }
         }
 
@@ -1104,26 +1110,30 @@
 
 // BorderLayout
 class java_awt_BorderLayout_PersistenceDelegate extends DefaultPersistenceDelegate {
+    private static final String[] CONSTRAINTS = {
+            BorderLayout.NORTH,
+            BorderLayout.SOUTH,
+            BorderLayout.EAST,
+            BorderLayout.WEST,
+            BorderLayout.CENTER,
+            BorderLayout.PAGE_START,
+            BorderLayout.PAGE_END,
+            BorderLayout.LINE_START,
+            BorderLayout.LINE_END,
+    };
+    @Override
     protected void initialize(Class<?> type, Object oldInstance,
                               Object newInstance, Encoder out) {
         super.initialize(type, oldInstance, newInstance, out);
-        String[] locations = {"north", "south", "east", "west", "center"};
-        String[] names = {java.awt.BorderLayout.NORTH, java.awt.BorderLayout.SOUTH,
-                          java.awt.BorderLayout.EAST, java.awt.BorderLayout.WEST,
-                          java.awt.BorderLayout.CENTER};
-        for(int i = 0; i < locations.length; i++) {
-            Object oldC = ReflectionUtils.getPrivateField(oldInstance,
-                                                          java.awt.BorderLayout.class,
-                                                          locations[i],
-                                                          out.getExceptionListener());
-            Object newC = ReflectionUtils.getPrivateField(newInstance,
-                                                          java.awt.BorderLayout.class,
-                                                          locations[i],
-                                                          out.getExceptionListener());
+        BorderLayout oldLayout = (BorderLayout) oldInstance;
+        BorderLayout newLayout = (BorderLayout) newInstance;
+        for (String constraints : CONSTRAINTS) {
+            Object oldC = oldLayout.getLayoutComponent(constraints);
+            Object newC = newLayout.getLayoutComponent(constraints);
             // Pending, assume any existing elements are OK.
             if (oldC != null && newC == null) {
                 invokeStatement(oldInstance, "addLayoutComponent",
-                                new Object[]{oldC, names[i]}, out);
+                                new Object[] { oldC, constraints }, out);
             }
         }
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/beans/XMLEncoder/java_awt_BorderLayout.java	Tue Jul 08 16:40:38 2008 +0400
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4916852
+ * @summary Tests BorderLayout encoding
+ * @author Sergey Malenkov
+ */
+
+import java.awt.BorderLayout;
+import javax.swing.JLabel;
+
+public final class java_awt_BorderLayout extends AbstractTest<BorderLayout> {
+    private static final String[] CONSTRAINTS = {
+            BorderLayout.NORTH,
+            BorderLayout.SOUTH,
+            BorderLayout.EAST,
+            BorderLayout.WEST,
+            BorderLayout.CENTER,
+            BorderLayout.PAGE_START,
+            BorderLayout.PAGE_END,
+            BorderLayout.LINE_START,
+            BorderLayout.LINE_END,
+    };
+
+    public static void main(String[] args) {
+        new java_awt_BorderLayout().test(true);
+    }
+
+    @Override
+    protected BorderLayout getObject() {
+        BorderLayout layout = new BorderLayout();
+        update(layout, BorderLayout.EAST);
+        update(layout, BorderLayout.WEST);
+        update(layout, BorderLayout.NORTH);
+        update(layout, BorderLayout.SOUTH);
+        return layout;
+    }
+
+    @Override
+    protected BorderLayout getAnotherObject() {
+        BorderLayout layout = getObject();
+        update(layout, BorderLayout.CENTER);
+        return layout;
+    }
+
+    @Override
+    protected void validate(BorderLayout before, BorderLayout after) {
+        super.validate(before, after);
+
+        BeanValidator validator = new BeanValidator();
+        for (String constraint : CONSTRAINTS) {
+            validator.validate(before.getLayoutComponent(constraint),
+                               after.getLayoutComponent(constraint));
+        }
+    }
+
+    private static void update(BorderLayout layout, String constraints) {
+        layout.addLayoutComponent(new JLabel(constraints), constraints);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/beans/XMLEncoder/java_awt_Component.java	Tue Jul 08 16:40:38 2008 +0400
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4916852
+ * @summary Tests Component encoding (background, foreground and font)
+ * @author Sergey Malenkov
+ */
+
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Font;
+
+public final class java_awt_Component extends AbstractTest<Component> {
+    public static void main(String[] args) {
+        new java_awt_Component().test(true);
+    }
+
+    @Override
+    protected Component getObject() {
+        Component component = new MyComponent();
+        component.setBackground(Color.WHITE);
+        component.setFont(new Font(null, Font.BOLD, 5));
+        return component;
+    }
+
+    @Override
+    protected Component getAnotherObject() {
+        Component component = new MyComponent();
+        component.setForeground(Color.BLACK);
+        component.setFont(new Font(null, Font.ITALIC, 6));
+        return component;
+    }
+
+    public static final class MyComponent extends Component {
+    }
+}
\ No newline at end of file