changeset 13642:e2c24c6dce16

8015748: [macosx] JProgressbar with Aqua LaF ignores JProgressbar#applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT) call. Reviewed-by: rchamyal, alexsch
author aniyogi
date Wed, 20 Jan 2016 16:57:53 +0530
parents 9eac69f80012
children 8625ef32d00c
files src/java.desktop/macosx/classes/com/apple/laf/AquaProgressBarUI.java test/javax/swing/JProgressBar/8015748/JProgressBarOrientationRobotTest.java
diffstat 2 files changed, 226 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/macosx/classes/com/apple/laf/AquaProgressBarUI.java	Tue Jan 19 15:23:49 2016 +0530
+++ b/src/java.desktop/macosx/classes/com/apple/laf/AquaProgressBarUI.java	Wed Jan 20 16:57:53 2016 +0530
@@ -180,10 +180,19 @@
         final int width = progressBar.getWidth() - (i.right + i.left);
         final int height = progressBar.getHeight() - (i.bottom + i.top);
 
+        Graphics2D g2 = (Graphics2D) g;
+        final AffineTransform savedAT = g2.getTransform();
+        if (!progressBar.getComponentOrientation().isLeftToRight()) {
+            //Scale operation: Flips component about pivot
+            //Translate operation: Moves component back into original position
+            g2.scale(-1, 1);
+            g2.translate(-progressBar.getWidth(), 0);
+        }
         painter.paint(g, progressBar, i.left, i.top, width, height);
 
+        g2.setTransform(savedAT);
         if (progressBar.isStringPainted() && !progressBar.isIndeterminate()) {
-            paintString(g, i.left, i.top, width, height);
+                paintString(g, i.left, i.top, width, height);
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/swing/JProgressBar/8015748/JProgressBarOrientationRobotTest.java	Wed Jan 20 16:57:53 2016 +0530
@@ -0,0 +1,216 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 8015748
+ * @summary verifies ProgressBar RightToLeft orientations for all Look and Feels
+ * @library ../../regtesthelpers
+ * @build Util
+ * @run main JProgressBarOrientationRobotTest
+ */
+import java.awt.Color;
+import java.awt.ComponentOrientation;
+import java.awt.Point;
+import java.awt.Robot;
+import javax.swing.JFrame;
+import javax.swing.JProgressBar;
+import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+
+public class JProgressBarOrientationRobotTest {
+
+    private static JFrame frame;
+    private static JProgressBar progressBar;
+    private static Robot robot;
+    private static Color colorCenter;
+    private static Color colorLeft;
+    private static Color colorRight;
+    private static final int widthBuffer = 20;
+    private static volatile String errorString = "";
+
+    public static void main(String[] args) throws Exception {
+        robot = new Robot();
+        robot.waitForIdle();
+        UIManager.LookAndFeelInfo[] lookAndFeelArray
+                = UIManager.getInstalledLookAndFeels();
+        for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
+            executeCase(lookAndFeelItem.getClassName(),
+                    lookAndFeelItem.getName());
+
+        }
+        if (!"".equals(errorString)) {
+            System.err.println(errorString);
+        }
+    }
+
+    private static void executeCase(String lookAndFeelString,
+            String shortenedLandFeelString) throws Exception {
+        if (tryLookAndFeel(lookAndFeelString)) {
+            createUI(shortenedLandFeelString);
+            robot.waitForIdle();
+
+            createLTR();
+            robot.delay(1000);
+            runTestCase();
+            robot.delay(1000);
+            testCaseLTR(shortenedLandFeelString);
+            robot.delay(1000);
+
+            createRTL();
+            robot.delay(1000);
+            runTestCase();
+            robot.delay(1000);
+            testCaseRTL(shortenedLandFeelString);
+            robot.delay(1000);
+
+            cleanUp();
+        }
+
+    }
+
+    private static void createUI(final String shortenedLookAndFeelString)
+            throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                progressBar = new JProgressBar();
+                progressBar.setValue(30);
+                frame = new JFrame(shortenedLookAndFeelString);
+                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                frame.getContentPane().add(progressBar);
+                frame.pack();
+                frame.setSize(500, frame.getSize().height);
+                frame.setLocationRelativeTo(null);
+                frame.setVisible(true);
+                frame.toFront();
+            }
+        });
+    }
+
+    private static void createLTR()
+            throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                progressBar.applyComponentOrientation(
+                        ComponentOrientation.LEFT_TO_RIGHT);
+                progressBar.repaint();
+            }
+        });
+    }
+
+    private static void createRTL()
+            throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                progressBar.applyComponentOrientation(
+                        ComponentOrientation.RIGHT_TO_LEFT);
+                progressBar.repaint();
+            }
+        });
+    }
+
+    private static void runTestCase() throws Exception {
+        Point centerPoint = Util.getCenterPoint(progressBar);
+        colorCenter = robot.getPixelColor(centerPoint.x, centerPoint.y);
+        colorRight = robot.getPixelColor(
+                (centerPoint.x + progressBar.getWidth() / 2 - widthBuffer),
+                centerPoint.y);
+        colorLeft = robot.getPixelColor(
+                (centerPoint.x - progressBar.getWidth() / 2 + widthBuffer),
+                centerPoint.y);
+        robot.waitForIdle();
+    }
+
+    private static void testCaseLTR(String shortenedLookAndFeelString)
+            throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+
+                if (colorCenter.equals(colorRight)) {
+                    if (!colorCenter.equals(colorLeft)) {
+                        System.out.println("[" + shortenedLookAndFeelString
+                                + "]: LTR orientation test passed");
+                    }
+                } else {
+                    frame.dispose();
+                    String error = "[" + shortenedLookAndFeelString
+                            + "]: [Error]: LTR orientation test failed";
+                    errorString += error;
+                    System.err.println(error);
+                }
+            }
+        });
+
+    }
+
+    private static void testCaseRTL(String shortenedLookAndFeelString)
+            throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                if (colorCenter.equals(colorLeft)) {
+                    if (!colorCenter.equals(colorRight)) {
+                        System.out.println("[" + shortenedLookAndFeelString
+                                + "]: RTL orientation test passed");
+                    }
+                } else {
+                    frame.dispose();
+                    String error = "[" + shortenedLookAndFeelString
+                            + "]: [Error]: LTR orientation test failed";
+                    errorString += error;
+                    System.err.println(error);
+                }
+            }
+        });
+    }
+
+    private static void cleanUp() throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                frame.dispose();
+            }
+        });
+    }
+
+    private static boolean tryLookAndFeel(String lookAndFeelString)
+            throws Exception {
+        try {
+            UIManager.setLookAndFeel(
+                    lookAndFeelString);
+
+        } catch (UnsupportedLookAndFeelException
+                | ClassNotFoundException
+                | InstantiationException
+                | IllegalAccessException e) {
+            errorString += e.getMessage() + "\n";
+            System.err.println("[Exception]: " + e.getMessage());
+            return false;
+        }
+        return true;
+    }
+}