jdk/test/javax/swing/JComponent/6989617/bug6989617.java
changeset 8372 786e091594a0
parent 7005 5e6d24f4303b
child 9228 6d4f3b084c97
equal deleted inserted replaced
8371:6201af876de8 8372:786e091594a0
     1 /*
     1 /*
     2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    26    @summary Enable JComponent to control repaintings of its children
    26    @summary Enable JComponent to control repaintings of its children
    27    @author Alexander Potochkin
    27    @author Alexander Potochkin
    28    @run main bug6989617
    28    @run main bug6989617
    29 */
    29 */
    30 
    30 
       
    31 import sun.awt.SunToolkit;
       
    32 
    31 import javax.swing.*;
    33 import javax.swing.*;
    32 import java.awt.*;
    34 import java.awt.*;
    33 
    35 
    34 public class bug6989617 {
    36 public class bug6989617 {
       
    37     private static MyPanel panel;
       
    38     private static JButton button;
    35 
    39 
    36     private boolean isPaintingOrigin;
    40     public static void main(String... args) throws Exception {
    37     private boolean innerPanelRepainted, outerPanelRepainted;
    41         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
       
    42         SwingUtilities.invokeAndWait(new Runnable() {
       
    43             public void run() {
       
    44                 JFrame frame = new JFrame();
       
    45                 frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    46                 panel = new MyPanel();
    38 
    47 
    39     public bug6989617() {
    48                 button = new JButton("Hello");
       
    49                 panel.add(button);
       
    50                 frame.add(panel);
    40 
    51 
    41         final JButton button = new JButton("button");
    52                 frame.setSize(200, 300);
       
    53                 frame.setVisible(true);
       
    54             }
       
    55         });
       
    56         // Testing the panel as a painting origin,
       
    57         // the panel.paintImmediately() must be triggered
       
    58         // when button.repaint() is called
       
    59         toolkit.realSync();
       
    60         SwingUtilities.invokeAndWait(new Runnable() {
       
    61             public void run() {
       
    62                 if (panel.getPaintRectangle() != null) {
       
    63                     throw new RuntimeException("paint rectangle is not null");
       
    64                 }
       
    65                 button.repaint();
       
    66             }
       
    67         });
       
    68         toolkit.realSync();
       
    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         toolkit.realSync();
       
    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         toolkit.realSync();
       
    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     }
    42 
   104 
    43         JPanel innerPanel = new JPanel() {
   105     static class MyPanel extends JPanel {
    44             protected boolean isPaintingOrigin() {
   106         private boolean isPaintingOrigin = true;
    45                 return isPaintingOrigin;
   107         private Rectangle paintRectangle;
    46             }
       
    47 
   108 
    48             public void repaint(long tm, int x, int y, int width, int height) {
   109         {
    49                 if (button.getParent() != null) {
   110             setLayout(new GridBagLayout());
    50                     innerPanelRepainted = true;
   111         }
    51                     if (!button.getSize().equals(new Dimension(width, height))) {
       
    52                         throw new RuntimeException("Wrong size of the dirty area");
       
    53                     }
       
    54                     if (!button.getLocation().equals(new Point(x, y))) {
       
    55                         throw new RuntimeException("Wrong location of the dirty area");
       
    56                     }
       
    57                 }
       
    58                 super.repaint(tm, x, y, width, height);
       
    59             }
       
    60         };
       
    61 
   112 
    62         JPanel outerPanel = new JPanel() {
   113         public boolean isPaintingOrigin() {
    63             protected boolean isPaintingOrigin() {
   114             return isPaintingOrigin;
    64                 return isPaintingOrigin;
   115         }
    65             }
       
    66 
   116 
    67             public void repaint(long tm, int x, int y, int width, int height) {
   117         public void setPaintingOrigin(boolean paintingOrigin) {
    68                 if (button.getParent() != null) {
   118             isPaintingOrigin = paintingOrigin;
    69                     outerPanelRepainted = true;
   119         }
    70                     if (!button.getSize().equals(new Dimension(width, height))) {
       
    71                         throw new RuntimeException("Wrong size of the dirty area");
       
    72                     }
       
    73                 }
       
    74                 super.repaint(tm, x, y, width, height);
       
    75             }
       
    76         };
       
    77 
   120 
       
   121         public void paintImmediately(int x, int y, int w, int h) {
       
   122             super.paintImmediately(x, y, w, h);
       
   123             paintRectangle = new Rectangle(x, y, w, h);
       
   124         }
    78 
   125 
    79         outerPanel.add(innerPanel);
   126         public Rectangle getPaintRectangle() {
    80         innerPanel.add(button);
   127             return paintRectangle == null? null: new Rectangle(paintRectangle);
       
   128         }
    81 
   129 
    82         outerPanel.setSize(100, 100);
   130         public void resetPaintRectangle() {
    83         innerPanel.setBounds(10, 10, 50, 50);
   131             this.paintRectangle = null;
    84         button.setBounds(10, 10, 20, 20);
       
    85 
       
    86         if (innerPanelRepainted || outerPanelRepainted) {
       
    87             throw new RuntimeException("Repainted flag is unexpectedly on");
       
    88         }
       
    89         button.repaint();
       
    90         if (innerPanelRepainted || outerPanelRepainted) {
       
    91             throw new RuntimeException("Repainted flag is unexpectedly on");
       
    92         }
       
    93         isPaintingOrigin = true;
       
    94         button.repaint();
       
    95         if (!innerPanelRepainted || !outerPanelRepainted) {
       
    96             throw new RuntimeException("Repainted flag is unexpectedly off");
       
    97         }
   132         }
    98     }
   133     }
    99 
       
   100     public static void main(String... args) throws Exception {
       
   101         new bug6989617();
       
   102     }
       
   103 }
   134 }