jdk/src/share/classes/sun/swing/JLightweightFrame.java
changeset 23280 df31f522531f
parent 20136 594c0830f18c
child 23281 93c0a5484bb5
equal deleted inserted replaced
23279:16c1ddb7b66a 23280:df31f522531f
     1 /*
     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 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.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    34 import java.awt.Graphics;
    34 import java.awt.Graphics;
    35 import java.awt.Graphics2D;
    35 import java.awt.Graphics2D;
    36 import java.awt.MouseInfo;
    36 import java.awt.MouseInfo;
    37 import java.awt.Point;
    37 import java.awt.Point;
    38 import java.awt.Rectangle;
    38 import java.awt.Rectangle;
       
    39 import java.awt.Window;
    39 import java.awt.event.ContainerEvent;
    40 import java.awt.event.ContainerEvent;
    40 import java.awt.event.ContainerListener;
    41 import java.awt.event.ContainerListener;
    41 import java.awt.image.BufferedImage;
    42 import java.awt.image.BufferedImage;
    42 import java.awt.image.DataBufferInt;
    43 import java.awt.image.DataBufferInt;
    43 import java.beans.PropertyChangeEvent;
    44 import java.beans.PropertyChangeEvent;
    44 import java.beans.PropertyChangeListener;
    45 import java.beans.PropertyChangeListener;
    45 import java.security.AccessController;
    46 import java.security.AccessController;
       
    47 import javax.swing.JComponent;
    46 
    48 
    47 import javax.swing.JLayeredPane;
    49 import javax.swing.JLayeredPane;
    48 import javax.swing.JPanel;
    50 import javax.swing.JPanel;
    49 import javax.swing.JRootPane;
    51 import javax.swing.JRootPane;
    50 import javax.swing.LayoutFocusTraversalPolicy;
    52 import javax.swing.LayoutFocusTraversalPolicy;
       
    53 import javax.swing.RepaintManager;
    51 import javax.swing.RootPaneContainer;
    54 import javax.swing.RootPaneContainer;
    52 import javax.swing.SwingUtilities;
    55 import javax.swing.SwingUtilities;
    53 
    56 
    54 import sun.awt.LightweightFrame;
    57 import sun.awt.LightweightFrame;
    55 import sun.security.action.GetPropertyAction;
    58 import sun.security.action.GetPropertyAction;
       
    59 import sun.swing.SwingUtilities2.RepaintListener;
    56 
    60 
    57 /**
    61 /**
    58  * The frame serves as a lightweight container which paints its content
    62  * The frame serves as a lightweight container which paints its content
    59  * to an offscreen image and provides access to the image's data via the
    63  * to an offscreen image and provides access to the image's data via the
    60  * {@link LightweightContent} interface. Note, that it may not be shown
    64  * {@link LightweightContent} interface. Note, that it may not be shown
    87      */
    91      */
    88     private boolean copyBufferEnabled;
    92     private boolean copyBufferEnabled;
    89     private int[] copyBuffer;
    93     private int[] copyBuffer;
    90 
    94 
    91     private PropertyChangeListener layoutSizeListener;
    95     private PropertyChangeListener layoutSizeListener;
       
    96     private RepaintListener repaintListener;
    92 
    97 
    93     static {
    98     static {
    94         SwingAccessor.setJLightweightFrameAccessor(new SwingAccessor.JLightweightFrameAccessor() {
    99         SwingAccessor.setJLightweightFrameAccessor(new SwingAccessor.JLightweightFrameAccessor() {
    95             @Override
   100             @Override
    96             public void updateCursor(JLightweightFrame frame) {
   101             public void updateCursor(JLightweightFrame frame) {
   128                 } else if ("minimumSize".equals(e.getPropertyName())) {
   133                 } else if ("minimumSize".equals(e.getPropertyName())) {
   129                     content.minimumSizeChanged(d.width, d.height);
   134                     content.minimumSizeChanged(d.width, d.height);
   130                 }
   135                 }
   131             }
   136             }
   132         };
   137         };
       
   138 
       
   139         repaintListener = (JComponent c, int x, int y, int w, int h) -> {
       
   140             Window jlf = SwingUtilities.getWindowAncestor(c);
       
   141             if (jlf != JLightweightFrame.this) {
       
   142                 return;
       
   143             }
       
   144             Point p = SwingUtilities.convertPoint(c, x, y, jlf);
       
   145             Rectangle r = new Rectangle(p.x, p.y, w, h).intersection(
       
   146                     new Rectangle(0, 0, bbImage.getWidth(), bbImage.getHeight()));
       
   147 
       
   148             if (!r.isEmpty()) {
       
   149                 notifyImageUpdated(r.x, r.y, r.width, r.height);
       
   150             }
       
   151         };
       
   152 
       
   153         SwingAccessor.getRepaintManagerAccessor().addRepaintListener(
       
   154             RepaintManager.currentManager(this), repaintListener);
       
   155     }
       
   156 
       
   157     @Override
       
   158     public void dispose() {
       
   159         SwingAccessor.getRepaintManagerAccessor().removeRepaintListener(
       
   160             RepaintManager.currentManager(this), repaintListener);
       
   161         super.dispose();
   133     }
   162     }
   134 
   163 
   135     /**
   164     /**
   136      * Sets the {@link LightweightContent} instance for this frame.
   165      * Sets the {@link LightweightContent} instance for this frame.
   137      * The {@code JComponent} object returned by the
   166      * The {@code JComponent} object returned by the
   207         } finally {
   236         } finally {
   208             content.paintUnlock();
   237             content.paintUnlock();
   209         }
   238         }
   210     }
   239     }
   211 
   240 
       
   241     private void notifyImageUpdated(int x, int y, int width, int height) {
       
   242         if (copyBufferEnabled) {
       
   243             syncCopyBuffer(false, x, y, width, height);
       
   244         }
       
   245         content.imageUpdated(x, y, width, height);
       
   246     }
       
   247 
   212     private void initInterior() {
   248     private void initInterior() {
   213         contentPane = new JPanel() {
   249         contentPane = new JPanel() {
   214             @Override
   250             @Override
   215             public void paint(Graphics g) {
   251             public void paint(Graphics g) {
   216                 if (!copyBufferEnabled) {
   252                 if (!copyBufferEnabled) {
   229                     clip.height = Math.min(contentPane.getHeight(), clip.height);
   265                     clip.height = Math.min(contentPane.getHeight(), clip.height);
   230 
   266 
   231                     EventQueue.invokeLater(new Runnable() {
   267                     EventQueue.invokeLater(new Runnable() {
   232                         @Override
   268                         @Override
   233                         public void run() {
   269                         public void run() {
   234                             if (copyBufferEnabled) {
   270                             notifyImageUpdated(clip.x, clip.y, clip.width, clip.height);
   235                                 syncCopyBuffer(false, clip.x, clip.y, clip.width, clip.height);
       
   236                             }
       
   237                             content.imageUpdated(clip.x, clip.y, clip.width, clip.height);
       
   238                         }
   271                         }
   239                     });
   272                     });
   240                 } finally {
   273                 } finally {
   241                     if (!copyBufferEnabled) {
   274                     if (!copyBufferEnabled) {
   242                         content.paintUnlock();
   275                         content.paintUnlock();