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