components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/AutoHidePanel.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     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;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.awt.event.*;
       
    30 import javax.swing.JPanel;
       
    31 
       
    32 /**
       
    33  * The {@code AutoHidePanel} is a {@code JPanel} whose visibility is
       
    34  * automatically set to {@code false} when it contains no visible child {@code
       
    35  * Component}s, {@code true} otherwise.
       
    36  */
       
    37 @SuppressWarnings({"serial"})
       
    38 public class AutoHidePanel extends JPanel {
       
    39     public static class AutoHideListener implements ContainerListener {
       
    40 	//
       
    41 	// Instance data
       
    42 	//
       
    43 
       
    44 	private ComponentListener CHILD_LISTENER =
       
    45 	    new ComponentAdapter() {
       
    46 		@Override
       
    47 		public void componentHidden(ComponentEvent event) {
       
    48 		    Component comp = event.getComponent();
       
    49 		    setVisible(comp.getParent());
       
    50 		}
       
    51 
       
    52 		@Override
       
    53 		public void componentShown(ComponentEvent event) {
       
    54 		    Component comp = event.getComponent();
       
    55 		    setVisible(comp.getParent());
       
    56 		}
       
    57 	    };
       
    58 
       
    59 	//
       
    60 	// ContainerListener methods
       
    61 	//
       
    62 
       
    63 	@Override
       
    64 	public void componentAdded(ContainerEvent event) {
       
    65 	    event.getChild().addComponentListener(CHILD_LISTENER);
       
    66 	    setVisible(event.getContainer());
       
    67 	}
       
    68 
       
    69 	@Override
       
    70 	public void componentRemoved(ContainerEvent event) {
       
    71 	    event.getChild().removeComponentListener(CHILD_LISTENER);
       
    72 	    setVisible(event.getContainer());
       
    73 	}
       
    74 
       
    75 	//
       
    76 	// AutoHideListener methods
       
    77 	//
       
    78 
       
    79 	public void deinit(Container container) {
       
    80 	    container.removeContainerListener(this);
       
    81 	    for (Component comp : container.getComponents()) {
       
    82 		comp.removeComponentListener(CHILD_LISTENER);
       
    83 	    }
       
    84 	}
       
    85 
       
    86 	public void init(Container container) {
       
    87 	    container.addContainerListener(this);
       
    88 	    for (Component comp : container.getComponents()) {
       
    89 		comp.addComponentListener(CHILD_LISTENER);
       
    90 	    }
       
    91 	    setVisible(container);
       
    92 	}
       
    93 
       
    94 	//
       
    95 	// Private methods
       
    96 	//
       
    97 
       
    98 	/**
       
    99 	 * Sets the visibility of the given {@code Container} based on whether
       
   100 	 * it has any visible child {@code Component}s.
       
   101 	 */
       
   102 	private void setVisible(Container container) {
       
   103 	    boolean visible = false;
       
   104 	    for (Component comp : container.getComponents()) {
       
   105 		if (comp.isVisible()) {
       
   106 		    visible = true;
       
   107 		    break;
       
   108 		}
       
   109 	    }
       
   110 	    container.setVisible(visible);
       
   111 	}
       
   112     }
       
   113 
       
   114     //
       
   115     // Static data
       
   116     //
       
   117 
       
   118     private static final AutoHideListener LISTENER = new AutoHideListener();
       
   119 
       
   120     //
       
   121     // Constructors
       
   122     //
       
   123 
       
   124     public AutoHidePanel() {
       
   125 	init();
       
   126     }
       
   127 
       
   128     public AutoHidePanel(boolean isDoubleBuffered) {
       
   129 	super(isDoubleBuffered);
       
   130 	init();
       
   131     }
       
   132 
       
   133     public AutoHidePanel(LayoutManager layout) {
       
   134 	super(layout);
       
   135 	init();
       
   136     }
       
   137 
       
   138     public AutoHidePanel(LayoutManager layout, boolean isDoubleBuffered) {
       
   139 	super(layout, isDoubleBuffered);
       
   140 	init();
       
   141     }
       
   142 
       
   143     //
       
   144     // Private methods
       
   145     //
       
   146 
       
   147     private void init() {
       
   148 	autoHide(this);
       
   149     }
       
   150 
       
   151     //
       
   152     // Static methods
       
   153     //
       
   154 
       
   155     /**
       
   156      * Add an {@link AutoHideListener} to the given {@code Container} to
       
   157      * automatically set its visibility whenever its text or icon properties
       
   158      * change.
       
   159      *
       
   160      * @return	    the added {@link AutoHideListener}
       
   161      */
       
   162     public static AutoHideListener autoHide(Container container) {
       
   163 	LISTENER.init(container);
       
   164 	return LISTENER;
       
   165     }
       
   166 }