src/share/classes/sun/swing/JLightweightFrame.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.swing;
  27 

  28 import java.awt.BorderLayout;
  29 import java.awt.Color;
  30 import java.awt.Component;
  31 import java.awt.Container;
  32 import java.awt.Dimension;
  33 import java.awt.EventQueue;
  34 import java.awt.Graphics;
  35 import java.awt.Graphics2D;


  36 import java.awt.Rectangle;



  37 import java.awt.event.ComponentListener;
  38 import java.awt.event.ContainerEvent;
  39 import java.awt.event.ContainerListener;

  40 import java.awt.image.BufferedImage;
  41 import java.awt.image.DataBufferInt;
  42 import java.beans.PropertyChangeEvent;
  43 import java.beans.PropertyChangeListener;
  44 import java.security.AccessController;
  45 
  46 import javax.swing.JLayeredPane;
  47 import javax.swing.JPanel;
  48 import javax.swing.JRootPane;
  49 import javax.swing.LayoutFocusTraversalPolicy;
  50 import javax.swing.RootPaneContainer;

  51 
  52 import sun.awt.LightweightFrame;

  53 import sun.security.action.GetPropertyAction;
  54 
  55 /**
  56  * The frame serves as a lightweight container which paints its content
  57  * to an offscreen image and provides access to the image's data via the
  58  * {@link LightweightContent} interface. Note, that it may not be shown
  59  * as a standalone toplevel frame. Its purpose is to provide functionality
  60  * for lightweight embedding.
  61  *
  62  * @author Artem Ananiev
  63  * @author Anton Tarasov
  64  */
  65 public final class JLightweightFrame extends LightweightFrame implements RootPaneContainer {
  66 
  67     private final JRootPane rootPane = new JRootPane();
  68 
  69     private LightweightContent content;
  70 
  71     private Component component;
  72     private JPanel contentPane;


 104         }
 105 
 106         layoutSizeListener = new PropertyChangeListener() {
 107             @Override
 108             public void propertyChange(PropertyChangeEvent e) {
 109                 Dimension d = (Dimension)e.getNewValue();
 110 
 111                 if ("preferredSize".equals(e.getPropertyName())) {
 112                     content.preferredSizeChanged(d.width, d.height);
 113 
 114                 } else if ("maximumSize".equals(e.getPropertyName())) {
 115                     content.maximumSizeChanged(d.width, d.height);
 116 
 117                 } else if ("minimumSize".equals(e.getPropertyName())) {
 118                     content.minimumSizeChanged(d.width, d.height);
 119                 }
 120             }
 121         };
 122     }
 123 
























































 124     /**
 125      * Sets the {@link LightweightContent} instance for this frame.
 126      * The {@code JComponent} object returned by the
 127      * {@link LightweightContent#getComponent()} method is immediately
 128      * added to the frame's content pane.
 129      *
 130      * @param content the {@link LightweightContent} instance
 131      */
 132     public void setContent(final LightweightContent content) {
 133         if (content == null) {
 134             System.err.println("JLightweightFrame.setContent: content may not be null!");
 135             return;
 136         }
 137         this.content = content;
 138         this.component = content.getComponent();
 139 
 140         Dimension d = this.component.getPreferredSize();
 141         content.preferredSizeChanged(d.width, d.height);
 142 
 143         d = this.component.getMaximumSize();




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.swing;
  27 
  28 import java.awt.AWTEvent;
  29 import java.awt.BorderLayout;
  30 import java.awt.Color;
  31 import java.awt.Component;
  32 import java.awt.Container;
  33 import java.awt.Dimension;
  34 import java.awt.EventQueue;
  35 import java.awt.Graphics;
  36 import java.awt.Graphics2D;
  37 import java.awt.MouseInfo;
  38 import java.awt.Point;
  39 import java.awt.Rectangle;
  40 import java.awt.event.AWTEventListener;
  41 import java.awt.event.ComponentAdapter;
  42 import java.awt.event.ComponentEvent;
  43 import java.awt.event.ComponentListener;
  44 import java.awt.event.ContainerEvent;
  45 import java.awt.event.ContainerListener;
  46 import java.awt.event.MouseEvent;
  47 import java.awt.image.BufferedImage;
  48 import java.awt.image.DataBufferInt;
  49 import java.beans.PropertyChangeEvent;
  50 import java.beans.PropertyChangeListener;
  51 import java.security.AccessController;
  52 
  53 import javax.swing.JLayeredPane;
  54 import javax.swing.JPanel;
  55 import javax.swing.JRootPane;
  56 import javax.swing.LayoutFocusTraversalPolicy;
  57 import javax.swing.RootPaneContainer;
  58 import javax.swing.SwingUtilities;
  59 
  60 import sun.awt.LightweightFrame;
  61 import sun.awt.LightweightFramePeer;
  62 import sun.security.action.GetPropertyAction;
  63 
  64 /**
  65  * The frame serves as a lightweight container which paints its content
  66  * to an offscreen image and provides access to the image's data via the
  67  * {@link LightweightContent} interface. Note, that it may not be shown
  68  * as a standalone toplevel frame. Its purpose is to provide functionality
  69  * for lightweight embedding.
  70  *
  71  * @author Artem Ananiev
  72  * @author Anton Tarasov
  73  */
  74 public final class JLightweightFrame extends LightweightFrame implements RootPaneContainer {
  75 
  76     private final JRootPane rootPane = new JRootPane();
  77 
  78     private LightweightContent content;
  79 
  80     private Component component;
  81     private JPanel contentPane;


 113         }
 114 
 115         layoutSizeListener = new PropertyChangeListener() {
 116             @Override
 117             public void propertyChange(PropertyChangeEvent e) {
 118                 Dimension d = (Dimension)e.getNewValue();
 119 
 120                 if ("preferredSize".equals(e.getPropertyName())) {
 121                     content.preferredSizeChanged(d.width, d.height);
 122 
 123                 } else if ("maximumSize".equals(e.getPropertyName())) {
 124                     content.maximumSizeChanged(d.width, d.height);
 125 
 126                 } else if ("minimumSize".equals(e.getPropertyName())) {
 127                     content.minimumSizeChanged(d.width, d.height);
 128                 }
 129             }
 130         };
 131     }
 132 
 133     private LightweightFramePeer getLwPeer() {
 134         return (LightweightFramePeer)getPeer();
 135     }
 136 
 137     private final AWTEventListener mouseEventListener = new AWTEventListener() {
 138         @Override
 139         public void eventDispatched(AWTEvent event) {
 140             MouseEvent m = (MouseEvent) event;
 141 
 142             if (!SwingUtilities.isDescendingFrom(m.getComponent(), JLightweightFrame.this)) return;
 143 
 144             switch (m.getID()) {
 145                 case MouseEvent.MOUSE_ENTERED:
 146                     getLwPeer().setLightWeightFrameUnderMouse();
 147                     getLwPeer().updateCursorImmediately();
 148                     break;
 149                 case MouseEvent.MOUSE_EXITED:
 150                     LightweightFrame frame = JLightweightFrame.this;
 151                     Point location = SwingUtilities.convertPoint(m.getComponent(), m.getPoint(), frame);
 152                     if ((!frame.contains(location) || !frame.isActive())&&
 153                             getLwPeer().cleanLightWeightFrameUnderMouse()) {
 154                         getLwPeer().updateCursorImmediately();
 155                     }
 156                     break;
 157             }
 158         }
 159     };
 160 
 161     private final ComponentListener componentListener =  new ComponentAdapter() {
 162         @Override
 163         public void componentShown(ComponentEvent e) {
 164             if (getBounds().contains(MouseInfo.getPointerInfo().getLocation())) {
 165                 getLwPeer().setLightWeightFrameUnderMouse();
 166                 getLwPeer().updateCursorImmediately();
 167             }
 168         }
 169     };
 170 
 171     @Override
 172     public void addNotify() {
 173         super.addNotify();
 174         getToolkit().addAWTEventListener(mouseEventListener, AWTEvent.MOUSE_EVENT_MASK);
 175         addComponentListener(componentListener);
 176     }
 177 
 178     @Override
 179     public void removeNotify() {
 180         super.removeNotify();
 181         getToolkit().removeAWTEventListener(mouseEventListener);
 182         removeComponentListener(componentListener);
 183     }
 184 
 185     public void invokeOnContentsThread(Runnable r) {
 186         content.invokeOnContentsThread(r);
 187     }
 188 
 189     /**
 190      * Sets the {@link LightweightContent} instance for this frame.
 191      * The {@code JComponent} object returned by the
 192      * {@link LightweightContent#getComponent()} method is immediately
 193      * added to the frame's content pane.
 194      *
 195      * @param content the {@link LightweightContent} instance
 196      */
 197     public void setContent(final LightweightContent content) {
 198         if (content == null) {
 199             System.err.println("JLightweightFrame.setContent: content may not be null!");
 200             return;
 201         }
 202         this.content = content;
 203         this.component = content.getComponent();
 204 
 205         Dimension d = this.component.getPreferredSize();
 206         content.preferredSizeChanged(d.width, d.height);
 207 
 208         d = this.component.getMaximumSize();