1 /*
2  * Copyright (c) 2011, 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 /* @test
25    @bug 6989617
26    @summary Enable JComponent to control repaintings of its children
27    @author Alexander Potochkin
28    @run main bug6989617
29 */
30 
31 import javax.swing.*;
32 import java.awt.*;
33 
34 public class bug6989617 {
35     private static MyPanel panel;
36     private static JButton button;
37 
main(String... args)38     public static void main(String... args) throws Exception {
39         Robot robot = new Robot();
40         SwingUtilities.invokeAndWait(new Runnable() {
41             public void run() {
42                 JFrame frame = new JFrame();
43                 frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
44                 panel = new MyPanel();
45 
46                 button = new JButton("Hello");
47                 panel.add(button);
48                 frame.add(panel);
49 
50                 frame.setSize(200, 300);
51                 frame.setVisible(true);
52             }
53         });
54         // Testing the panel as a painting origin,
55         // the panel.paintImmediately() must be triggered
56         // when button.repaint() is called
57         robot.waitForIdle();
58         SwingUtilities.invokeAndWait(new Runnable() {
59             public void run() {
60                 panel.resetPaintRectangle();
61                 button.repaint();
62             }
63         });
64         robot.waitForIdle();
65         SwingUtilities.invokeAndWait(new Runnable() {
66             public void run() {
67                 Rectangle pr = panel.getPaintRectangle();
68                 if (!pr.getSize().equals(button.getSize())) {
69                     throw new RuntimeException("wrong size of the dirty area");
70                 }
71                 if (!pr.getLocation().equals(button.getLocation())) {
72                     throw new RuntimeException("wrong location of the dirty area");
73                 }
74             }
75         });
76         // Testing the panel as NOT a painting origin
77         // the panel.paintImmediately() must NOT be triggered
78         // when button.repaint() is called
79         robot.waitForIdle();
80         SwingUtilities.invokeAndWait(new Runnable() {
81             public void run() {
82                 panel.resetPaintRectangle();
83                 panel.setPaintingOrigin(false);
84                 if (panel.getPaintRectangle() != null) {
85                     throw new RuntimeException("paint rectangle is not null");
86                 }
87                 button.repaint();
88             }
89         });
90         robot.waitForIdle();
91         SwingUtilities.invokeAndWait(new Runnable() {
92             public void run() {
93                 if(panel.getPaintRectangle() != null) {
94                     throw new RuntimeException("paint rectangle is not null");
95                 }
96                 System.out.println("Test passed...");
97             }
98         });
99     }
100 
101     static class MyPanel extends JPanel {
102         private boolean isPaintingOrigin = true;
103         private Rectangle paintRectangle;
104 
105         {
setLayout(new GridBagLayout())106             setLayout(new GridBagLayout());
107         }
108 
isPaintingOrigin()109         public boolean isPaintingOrigin() {
110             return isPaintingOrigin;
111         }
112 
setPaintingOrigin(boolean paintingOrigin)113         public void setPaintingOrigin(boolean paintingOrigin) {
114             isPaintingOrigin = paintingOrigin;
115         }
116 
paintImmediately(int x, int y, int w, int h)117         public void paintImmediately(int x, int y, int w, int h) {
118             super.paintImmediately(x, y, w, h);
119             paintRectangle = new Rectangle(x, y, w, h);
120         }
121 
getPaintRectangle()122         public Rectangle getPaintRectangle() {
123             return paintRectangle == null? null: new Rectangle(paintRectangle);
124         }
125 
resetPaintRectangle()126         public void resetPaintRectangle() {
127             this.paintRectangle = null;
128         }
129     }
130 }
131