6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
authoralexp
Thu, 10 Feb 2011 21:36:18 +0300
changeset 8372 786e091594a0
parent 8371 6201af876de8
child 8373 7e8b793dbf91
6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP Reviewed-by: rupashka
jdk/src/share/classes/javax/swing/JComponent.java
jdk/src/share/classes/javax/swing/JLayer.java
jdk/src/share/classes/javax/swing/RepaintManager.java
jdk/src/share/classes/javax/swing/SwingUtilities.java
jdk/src/share/classes/javax/swing/plaf/LayerUI.java
jdk/test/javax/swing/JComponent/6989617/bug6989617.java
--- a/jdk/src/share/classes/javax/swing/JComponent.java	Wed Feb 09 18:00:29 2011 +0900
+++ b/jdk/src/share/classes/javax/swing/JComponent.java	Thu Feb 10 21:36:18 2011 +0300
@@ -4910,14 +4910,17 @@
      * Returns {@code true} if a paint triggered on a child component should cause
      * painting to originate from this Component, or one of its ancestors.
      * <p/>
-     * Calling {@link JComponent#repaint} on a Swing component will be delegated to
-     * the first ancestor which {@code isPaintingOrigin()} returns {@true},
-     * if there are any.
+     * Calling {@link #repaint} or {@link #paintImmediately(int, int, int, int)}
+     * on a Swing component will result in calling
+     * the {@link JComponent#paintImmediately(int, int, int, int)} method of
+     * the first ancestor which {@code isPaintingOrigin()} returns {@true}, if there are any.
      * <p/>
-     * {@code JComponent} subclasses that need to be repainted when any of their
+     * {@code JComponent} subclasses that need to be painted when any of their
      * children are repainted should override this method to return {@code true}.
      *
      * @return always returns {@code false}
+     *
+     * @see #paintImmediately(int, int, int, int)
      */
     protected boolean isPaintingOrigin() {
         return false;
@@ -4932,12 +4935,16 @@
      * and can collapse redundant requests into a single paint call.
      * This method is useful if one needs to update the display while
      * the current event is being dispatched.
+     * <p>
+     * This method is to be overridden when the dirty region needs to be changed
+     * for components that are painting origins.
      *
      * @param x  the x value of the region to be painted
      * @param y  the y value of the region to be painted
      * @param w  the width of the region to be painted
      * @param h  the height of the region to be painted
      * @see #repaint
+     * @see #isPaintingOrigin()
      */
     public void paintImmediately(int x,int y,int w, int h) {
         Component c = this;
@@ -4946,6 +4953,15 @@
         if(!isShowing()) {
             return;
         }
+
+        JComponent paintingOigin = SwingUtilities.getPaintingOrigin(this);
+        if (paintingOigin != null) {
+            Rectangle rectangle = SwingUtilities.convertRectangle(
+                    c, new Rectangle(x, y, w, h), paintingOigin);
+            paintingOigin.paintImmediately(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
+            return;
+        }
+
         while(!c.isOpaque()) {
             parent = c.getParent();
             if(parent != null) {
--- a/jdk/src/share/classes/javax/swing/JLayer.java	Wed Feb 09 18:00:29 2011 +0900
+++ b/jdk/src/share/classes/javax/swing/JLayer.java	Thu Feb 10 21:36:18 2011 +0300
@@ -156,8 +156,9 @@
     // when layerUI is serializable
     private LayerUI<? super V> layerUI;
     private JPanel glassPane;
-    private boolean isPainting;
     private long eventMask;
+    private transient boolean isPainting;
+    private transient boolean isPaintingImmediately;
 
     private static final LayerEventController eventController =
             new LayerEventController();
@@ -393,17 +394,25 @@
     }
 
     /**
-     * Delegates repainting to {@link javax.swing.plaf.LayerUI#repaint} method.
+     * Delegates its functionality to the
+     * {@link javax.swing.plaf.LayerUI#paintImmediately(int, int, int, int, JLayer)} method,
+     * if {@code LayerUI} is set.
      *
-     * @param tm  this parameter is not used
-     * @param x  the x value of the dirty region
-     * @param y  the y value of the dirty region
-     * @param width  the width of the dirty region
-     * @param height  the height of the dirty region
+     * @param x  the x value of the region to be painted
+     * @param y  the y value of the region to be painted
+     * @param w  the width of the region to be painted
+     * @param h  the height of the region to be painted
      */
-    public void repaint(long tm, int x, int y, int width, int height) {
-        if (getUI() != null) {
-            getUI().repaint(tm, x, y, width, height, this);
+    public void paintImmediately(int x, int y, int w, int h) {
+        if (!isPaintingImmediately && getUI() != null) {
+            isPaintingImmediately = true;
+            try {
+                getUI().paintImmediately(x, y, w, h, this);
+            } finally {
+                isPaintingImmediately = false;
+            }
+        } else {
+            super.paintImmediately(x, y, w, h);
         }
     }
 
@@ -415,8 +424,11 @@
     public void paint(Graphics g) {
         if (!isPainting) {
             isPainting = true;
-            super.paintComponent(g);
-            isPainting = false;
+            try {
+                super.paintComponent(g);
+            } finally {
+                isPainting = false;
+            }
         } else {
             super.paint(g);
         }
--- a/jdk/src/share/classes/javax/swing/RepaintManager.java	Wed Feb 09 18:00:29 2011 +0900
+++ b/jdk/src/share/classes/javax/swing/RepaintManager.java	Thu Feb 10 21:36:18 2011 +0300
@@ -438,7 +438,6 @@
      * @param y Y coordinate of the region to repaint
      * @param w Width of the region to repaint
      * @param h Height of the region to repaint
-     * @see JComponent#isPaintingOrigin()
      * @see JComponent#repaint
      */
     public void addDirtyRegion(JComponent c, int x, int y, int w, int h)
@@ -448,16 +447,6 @@
             delegate.addDirtyRegion(c, x, y, w, h);
             return;
         }
-        Container p = c;
-        while ((p = p.getParent()) instanceof JComponent) {
-            JComponent jp = (JComponent) p;
-            if (jp.isPaintingOrigin()) {
-                Rectangle rectangle = SwingUtilities.convertRectangle(
-                        c, new Rectangle(x, y, w, h), jp);
-                jp.repaint(0, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
-                return;
-            }
-        }
         addDirtyRegion0(c, x, y, w, h);
     }
 
--- a/jdk/src/share/classes/javax/swing/SwingUtilities.java	Wed Feb 09 18:00:29 2011 +0900
+++ b/jdk/src/share/classes/javax/swing/SwingUtilities.java	Thu Feb 10 21:36:18 2011 +0300
@@ -1532,6 +1532,17 @@
         return applet;
     }
 
+    static JComponent getPaintingOrigin(JComponent c) {
+        Container p = c;
+        while ((p = p.getParent()) instanceof JComponent) {
+            JComponent jp = (JComponent) p;
+            if (jp.isPaintingOrigin()) {
+                return jp;
+            }
+        }
+        return null;
+    }
+
     /**
      * Process the key bindings for the <code>Component</code> associated with
      * <code>event</code>. This method is only useful if
--- a/jdk/src/share/classes/javax/swing/plaf/LayerUI.java	Wed Feb 09 18:00:29 2011 +0900
+++ b/jdk/src/share/classes/javax/swing/plaf/LayerUI.java	Thu Feb 10 21:36:18 2011 +0300
@@ -703,21 +703,19 @@
     }
 
     /**
-     * Adds the specified region to the dirty region list if the component
-     * is showing.  The component will be repainted after all of the
-     * currently pending events have been dispatched.
+     * Paints the specified region in the {@code JLayer} this {@code LayerUI} is set to, immediately.
      * <p/>
      * This method is to be overridden when the dirty region needs to be changed.
+     * The default implementation delegates its functionality to {@link JComponent#paintImmediately(int, int, int, int)}.
      *
-     * @param tm  this parameter is not used
-     * @param x  the x value of the dirty region
-     * @param y  the y value of the dirty region
-     * @param width  the width of the dirty region
-     * @param height  the height of the dirty region
-     * @see java.awt.Component#isShowing
-     * @see RepaintManager#addDirtyRegion
+     * @param x  the x value of the region to be painted
+     * @param y  the y value of the region to be painted
+     * @param w  the width of the region to be painted
+     * @param h  the height of the region to be painted
+     *
+     * @see JComponent#paintImmediately(int, int, int, int)
      */
-    public void repaint(long tm, int x, int y, int width, int height, JLayer<? extends V> l) {
-        RepaintManager.currentManager(l).addDirtyRegion(l, x, y, width, height);
+    public void paintImmediately(int x, int y, int width, int height, JLayer<? extends V> l) {
+        l.paintImmediately(x, y, width, height);
     }
 }
--- a/jdk/test/javax/swing/JComponent/6989617/bug6989617.java	Wed Feb 09 18:00:29 2011 +0900
+++ b/jdk/test/javax/swing/JComponent/6989617/bug6989617.java	Thu Feb 10 21:36:18 2011 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 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
@@ -28,76 +28,107 @@
    @run main bug6989617
 */
 
+import sun.awt.SunToolkit;
+
 import javax.swing.*;
 import java.awt.*;
 
 public class bug6989617 {
-
-    private boolean isPaintingOrigin;
-    private boolean innerPanelRepainted, outerPanelRepainted;
-
-    public bug6989617() {
+    private static MyPanel panel;
+    private static JButton button;
 
-        final JButton button = new JButton("button");
+    public static void main(String... args) throws Exception {
+        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                JFrame frame = new JFrame();
+                frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                panel = new MyPanel();
 
-        JPanel innerPanel = new JPanel() {
-            protected boolean isPaintingOrigin() {
-                return isPaintingOrigin;
-            }
+                button = new JButton("Hello");
+                panel.add(button);
+                frame.add(panel);
 
-            public void repaint(long tm, int x, int y, int width, int height) {
-                if (button.getParent() != null) {
-                    innerPanelRepainted = true;
-                    if (!button.getSize().equals(new Dimension(width, height))) {
-                        throw new RuntimeException("Wrong size of the dirty area");
-                    }
-                    if (!button.getLocation().equals(new Point(x, y))) {
-                        throw new RuntimeException("Wrong location of the dirty area");
-                    }
+                frame.setSize(200, 300);
+                frame.setVisible(true);
+            }
+        });
+        // Testing the panel as a painting origin,
+        // the panel.paintImmediately() must be triggered
+        // when button.repaint() is called
+        toolkit.realSync();
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                if (panel.getPaintRectangle() != null) {
+                    throw new RuntimeException("paint rectangle is not null");
                 }
-                super.repaint(tm, x, y, width, height);
+                button.repaint();
             }
-        };
-
-        JPanel outerPanel = new JPanel() {
-            protected boolean isPaintingOrigin() {
-                return isPaintingOrigin;
+        });
+        toolkit.realSync();
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                Rectangle pr = panel.getPaintRectangle();
+                if (!pr.getSize().equals(button.getSize())) {
+                    throw new RuntimeException("wrong size of the dirty area");
+                }
+                if (!pr.getLocation().equals(button.getLocation())) {
+                    throw new RuntimeException("wrong location of the dirty area");
+                }
             }
-
-            public void repaint(long tm, int x, int y, int width, int height) {
-                if (button.getParent() != null) {
-                    outerPanelRepainted = true;
-                    if (!button.getSize().equals(new Dimension(width, height))) {
-                        throw new RuntimeException("Wrong size of the dirty area");
-                    }
+        });
+        // Testing the panel as NOT a painting origin
+        // the panel.paintImmediately() must NOT be triggered
+        // when button.repaint() is called
+        toolkit.realSync();
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                panel.resetPaintRectangle();
+                panel.setPaintingOrigin(false);
+                if (panel.getPaintRectangle() != null) {
+                    throw new RuntimeException("paint rectangle is not null");
                 }
-                super.repaint(tm, x, y, width, height);
+                button.repaint();
             }
-        };
-
-
-        outerPanel.add(innerPanel);
-        innerPanel.add(button);
+        });
+        toolkit.realSync();
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                if(panel.getPaintRectangle() != null) {
+                    throw new RuntimeException("paint rectangle is not null");
+                }
+                System.out.println("Test passed...");
+            }
+        });
+    }
 
-        outerPanel.setSize(100, 100);
-        innerPanel.setBounds(10, 10, 50, 50);
-        button.setBounds(10, 10, 20, 20);
+    static class MyPanel extends JPanel {
+        private boolean isPaintingOrigin = true;
+        private Rectangle paintRectangle;
 
-        if (innerPanelRepainted || outerPanelRepainted) {
-            throw new RuntimeException("Repainted flag is unexpectedly on");
+        {
+            setLayout(new GridBagLayout());
+        }
+
+        public boolean isPaintingOrigin() {
+            return isPaintingOrigin;
         }
-        button.repaint();
-        if (innerPanelRepainted || outerPanelRepainted) {
-            throw new RuntimeException("Repainted flag is unexpectedly on");
+
+        public void setPaintingOrigin(boolean paintingOrigin) {
+            isPaintingOrigin = paintingOrigin;
         }
-        isPaintingOrigin = true;
-        button.repaint();
-        if (!innerPanelRepainted || !outerPanelRepainted) {
-            throw new RuntimeException("Repainted flag is unexpectedly off");
+
+        public void paintImmediately(int x, int y, int w, int h) {
+            super.paintImmediately(x, y, w, h);
+            paintRectangle = new Rectangle(x, y, w, h);
+        }
+
+        public Rectangle getPaintRectangle() {
+            return paintRectangle == null? null: new Rectangle(paintRectangle);
+        }
+
+        public void resetPaintRectangle() {
+            this.paintRectangle = null;
         }
     }
-
-    public static void main(String... args) throws Exception {
-        new bug6989617();
-    }
 }