components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/ShowingListener.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;
       
    27 
       
    28 import java.awt.Component;
       
    29 import java.awt.event.*;
       
    30 
       
    31 /**
       
    32  * When added as a {@code HierarchyListener}, the {@code ShowingListener} class
       
    33  * listens for changes in the showing property of a single {@code Component}.  A
       
    34  * single {@code ShowingListener} should NOT be added as a listener to more than
       
    35  * one {@code Component}.
       
    36  */
       
    37 public class ShowingListener implements HierarchyListener {
       
    38     //
       
    39     // Instance data
       
    40     //
       
    41 
       
    42     private Boolean showing;
       
    43 
       
    44     //
       
    45     // HierarchyListener methods
       
    46     //
       
    47 
       
    48     @Override
       
    49     public void hierarchyChanged(HierarchyEvent e) {
       
    50 	updateShowing(e.getComponent());
       
    51     }
       
    52 
       
    53     //
       
    54     // ShowingListener methods
       
    55     //
       
    56 
       
    57     /**
       
    58      * Calls {@link #componentHidden} or {@link #componentShown} if the
       
    59      * listening {@code Component}'s showing status has changed (or if neither
       
    60      * method has been called since this listener was registered).
       
    61      *
       
    62      * @param	    comp
       
    63      *		    the listening {@code Component}
       
    64      */
       
    65     public void updateShowing(Component comp) {
       
    66 	boolean showing = comp.isShowing();
       
    67 	if (this.showing == null || showing != this.showing) {
       
    68 	    this.showing = showing;
       
    69 
       
    70 	    if (showing) {
       
    71 		componentShown();
       
    72 	    } else {
       
    73 		componentHidden();
       
    74 	    }
       
    75 	}
       
    76     }
       
    77 
       
    78     /**
       
    79      * Called when the listening {@code Component}'s showing status changes to
       
    80      * {@code false}.
       
    81      * </p>
       
    82      * This default implementation does nothing.
       
    83      */
       
    84     public void componentHidden() {
       
    85     }
       
    86 
       
    87     /**
       
    88      * Called when the listening {@code Component}'s showing status changes to
       
    89      * {@code true}.
       
    90      * </p>
       
    91      * This default implementation does nothing.
       
    92      */
       
    93     public void componentShown() {
       
    94     }
       
    95 }