components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/glass/ImageCaptureGlassPane.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.swing.glass;
       
    27 
       
    28 import java.awt.BorderLayout;
       
    29 import java.awt.event.*;
       
    30 import java.awt.image.BufferedImage;
       
    31 import javax.swing.*;
       
    32 import com.oracle.solaris.vp.util.swing.GraphicsUtil;
       
    33 
       
    34 @SuppressWarnings({"serial"})
       
    35 public class ImageCaptureGlassPane extends SimpleGlassPane {
       
    36     //
       
    37     // Instance data
       
    38     //
       
    39 
       
    40     private boolean refreshOnResize = true;
       
    41     private JLabel label = new JLabel();
       
    42 
       
    43     //
       
    44     // Constructors
       
    45     //
       
    46 
       
    47     public ImageCaptureGlassPane() {
       
    48 	setOpaque(true);
       
    49 	setLayout(new BorderLayout());
       
    50 	add(label, BorderLayout.CENTER);
       
    51 
       
    52 	addComponentListener(
       
    53 	    new ComponentAdapter() {
       
    54 		@Override
       
    55 		public void componentResized(ComponentEvent e) {
       
    56 		    if (refreshOnResize && isShowing()) {
       
    57 			refresh();
       
    58 		    }
       
    59 		}
       
    60 	    });
       
    61     }
       
    62 
       
    63     //
       
    64     // SimpleGlassPane methods
       
    65     //
       
    66 
       
    67     @Override
       
    68     public void componentHidden() {
       
    69 	super.componentHidden();
       
    70 	label.setIcon(null);
       
    71     }
       
    72 
       
    73     @Override
       
    74     public void componentToBeVisible() {
       
    75 	super.componentToBeVisible();
       
    76 	refresh();
       
    77     }
       
    78 
       
    79     //
       
    80     // ImageCaptureGlassPane methods
       
    81     //
       
    82 
       
    83     protected JLabel getLabel() {
       
    84 	return label;
       
    85     }
       
    86 
       
    87     public boolean getRefreshOnResize() {
       
    88 	return refreshOnResize;
       
    89     }
       
    90 
       
    91     public synchronized void refresh() {
       
    92 	Icon icon = null;
       
    93 
       
    94 	JRootPane rootPane = getRootPane();
       
    95 	if (rootPane != null) {
       
    96 
       
    97 	    JLayeredPane layeredPane = rootPane.getLayeredPane();
       
    98 	    if (layeredPane != null) {
       
    99 
       
   100 		BufferedImage bImage = GraphicsUtil.paintToImage(layeredPane);
       
   101 		if (bImage != null) {
       
   102 
       
   103 		    icon = new ImageIcon(bImage);
       
   104 		}
       
   105 	    }
       
   106 	}
       
   107 
       
   108 	label.setIcon(icon);
       
   109     }
       
   110 
       
   111     public void setRefreshOnResize(boolean refreshOnResize) {
       
   112 	this.refreshOnResize = refreshOnResize;
       
   113     }
       
   114 }