7158712: Synth Property "ComboBox.popupInsets" is ignored
authorrupashka
Tue, 10 Apr 2012 19:09:48 +0300
changeset 12405 3a06b392a805
parent 12404 de0c1d3ed1c5
child 12406 fc209a5d3c85
7158712: Synth Property "ComboBox.popupInsets" is ignored Reviewed-by: alexp
jdk/src/share/classes/javax/swing/plaf/synth/SynthComboPopup.java
jdk/test/javax/swing/plaf/synth/7158712/bug7158712.java
jdk/test/javax/swing/regtesthelpers/Util.java
--- a/jdk/src/share/classes/javax/swing/plaf/synth/SynthComboPopup.java	Tue Apr 10 18:53:15 2012 +0300
+++ b/jdk/src/share/classes/javax/swing/plaf/synth/SynthComboPopup.java	Tue Apr 10 19:09:48 2012 +0300
@@ -26,13 +26,9 @@
 package javax.swing.plaf.synth;
 
 import javax.swing.*;
-import javax.swing.event.*;
-import javax.swing.plaf.basic.*;
+import javax.swing.plaf.ComboBoxUI;
+import javax.swing.plaf.basic.BasicComboPopup;
 import java.awt.*;
-import java.awt.event.*;
-import java.beans.PropertyChangeListener;
-import java.beans.PropertyChangeEvent;
-import java.io.Serializable;
 
 
 /**
@@ -52,6 +48,7 @@
      *
      * @see #createList
      */
+    @Override
     protected void configureList() {
         list.setFont( comboBox.getFont() );
         list.setCellRenderer( comboBox.getRenderer() );
@@ -67,4 +64,27 @@
         }
         installListListeners();
     }
+
+    /**
+     * @inheritDoc
+     *
+     * Overridden to take into account any popup insets specified in
+     * SynthComboBoxUI
+     */
+    @Override
+    protected Rectangle computePopupBounds(int px, int py, int pw, int ph) {
+        ComboBoxUI ui = comboBox.getUI();
+        if (ui instanceof SynthComboBoxUI) {
+            SynthComboBoxUI sui = (SynthComboBoxUI) ui;
+            if (sui.popupInsets != null) {
+                Insets i = sui.popupInsets;
+                return super.computePopupBounds(
+                        px + i.left,
+                        py + i.top,
+                        pw - i.left - i.right,
+                        ph - i.top - i.bottom);
+            }
+        }
+        return super.computePopupBounds(px, py, pw, ph);
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/plaf/synth/7158712/bug7158712.java	Tue Apr 10 19:09:48 2012 +0300
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2012, 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 7158712
+   @summary Synth Property "ComboBox.popupInsets" is ignored
+   @library ../../../regtesthelpers
+   @author Pavel Porvatov
+*/
+
+import javax.swing.*;
+import javax.swing.plaf.basic.BasicComboPopup;
+import javax.swing.plaf.synth.SynthLookAndFeel;
+import java.awt.*;
+import java.awt.event.InputEvent;
+import java.io.ByteArrayInputStream;
+import java.util.concurrent.Callable;
+
+public class bug7158712 {
+    private static final String SYNTH_XML = "<synth>" +
+            "    <style id=\"all\">" +
+            "      <font name=\"Dialog\" size=\"12\"/>" +
+            "    </style>" +
+            "    <bind style=\"all\" type=\"REGION\" key=\".*\"/>" +
+            "    <style id=\"arrowButton\">" +
+            "      <property key=\"ArrowButton.size\" type=\"integer\" value=\"18\"/>" +
+            "    </style>" +
+            "    <bind style=\"arrowButton\" type=\"region\" key=\"ArrowButton\"/>" +
+            "    <style id=\"comboBox\">" +
+            "      <property key=\"ComboBox.popupInsets\" type=\"insets\" value=\"-5 -5 5 -5\"/>" +
+            "    </style>" +
+            "    <bind style=\"comboBox\" type=\"region\" key=\"ComboBox\"/>" +
+            "</synth>";
+
+    private static JComboBox<String> comboBox;
+
+    public static void main(String[] args) throws Exception {
+        Robot robot = new Robot();
+
+        robot.setAutoDelay(500);
+
+        SynthLookAndFeel laf = new SynthLookAndFeel();
+
+        laf.load(new ByteArrayInputStream(SYNTH_XML.getBytes("UTF8")), bug7158712.class);
+
+        UIManager.setLookAndFeel(laf);
+
+        EventQueue.invokeLater(new Runnable() {
+            public void run() {
+                comboBox = new JComboBox<>(
+                        new String[]{"Very Looooooooooooooooooooong Text Item 1", "Item 2"});
+
+                JFrame frame = new JFrame();
+
+                frame.add(comboBox, BorderLayout.NORTH);
+                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                frame.setSize(new Dimension(400, 300));
+                frame.setLocationRelativeTo(null);
+                frame.setVisible(true);
+            }
+        });
+
+        Point comboBoxLocation = Util.invokeOnEDT(new Callable<Point>() {
+            @Override
+            public Point call() throws Exception {
+                return comboBox.getLocationOnScreen();
+            }
+        });
+
+        robot.mouseMove(comboBoxLocation.x, comboBoxLocation.y);
+        robot.mousePress(InputEvent.BUTTON1_MASK);
+        robot.mouseRelease(InputEvent.BUTTON1_MASK);
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                BasicComboPopup popup = (BasicComboPopup) comboBox.getAccessibleContext().getAccessibleChild(0);
+
+                Point popupPoint = popup.getLocationOnScreen();
+                Point comboBoxPoint = comboBox.getLocationOnScreen();
+
+                if (comboBoxPoint.x - 5 != popupPoint.x ||
+                        comboBoxPoint.y + comboBox.getHeight() - 5 != popupPoint.y) {
+                    throw new RuntimeException("Invalid popup coordinates. Popup location: " + popupPoint +
+                            ", comboBox location: " + comboBoxPoint);
+                }
+
+                System.out.println("Test bug7158712 passed");
+            }
+        });
+    }
+}
--- a/jdk/test/javax/swing/regtesthelpers/Util.java	Tue Apr 10 18:53:15 2012 +0300
+++ b/jdk/test/javax/swing/regtesthelpers/Util.java	Tue Apr 10 19:09:48 2012 +0300
@@ -24,8 +24,10 @@
 import javax.swing.*;
 import java.awt.*;
 import java.awt.image.BufferedImage;
+import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.concurrent.Callable;
 
 /**
  * <p>This class contains utilities useful for regression testing.
@@ -153,4 +155,31 @@
             robot.keyRelease(keys[i]);
         }
     }
+
+    /**
+     * Invokes the <code>task</code> on the EDT thread.
+     *
+     * @return result of the <code>task</code>
+     */
+    public static <T> T invokeOnEDT(final Callable<T> task) throws Exception {
+        final List<T> result = new ArrayList<>(1);
+        final Exception[] exception = new Exception[1];
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    result.add(task.call());
+                } catch (Exception e) {
+                    exception[0] = e;
+                }
+            }
+        });
+
+        if (exception[0] != null) {
+            throw exception[0];
+        }
+
+        return result.get(0);
+    }
 }