components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/AutoHideLabel.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.beans.*;
       
    29 import javax.swing.*;
       
    30 
       
    31 /**
       
    32  * The {@code AutoHideLabel} is a {@code JLabel} whose visibility is
       
    33  * automatically set to {@code false} when its text and icon are {@code null} or
       
    34  * empty, {@code true} otherwise.
       
    35  */
       
    36 @SuppressWarnings({"serial"})
       
    37 public class AutoHideLabel extends JLabel {
       
    38     public static class AutoHideListener implements PropertyChangeListener {
       
    39 	//
       
    40 	// PropertyChangeListener methods
       
    41 	//
       
    42 
       
    43 	@Override
       
    44 	public void propertyChange(PropertyChangeEvent event) {
       
    45 	    setVisible((JLabel)event.getSource());
       
    46 	}
       
    47 
       
    48 	//
       
    49 	// AutoHideListener methods
       
    50 	//
       
    51 
       
    52 	public void deinit(JLabel label) {
       
    53 	    label.removePropertyChangeListener("text", this);
       
    54 	    label.removePropertyChangeListener("icon", this);
       
    55 	}
       
    56 
       
    57 	public void init(JLabel label) {
       
    58 	    label.addPropertyChangeListener("text", this);
       
    59 	    label.addPropertyChangeListener("icon", this);
       
    60 	    setVisible(label);
       
    61 	}
       
    62 
       
    63 	//
       
    64 	// Private methods
       
    65 	//
       
    66 
       
    67 	/**
       
    68 	 * Sets the visibility of the given {@code label} based on whether it
       
    69 	 * has a non-{@code null}/empty icon or text.
       
    70 	 */
       
    71 	private void setVisible(JLabel label) {
       
    72 	    String text = label.getText();
       
    73 	    label.setVisible((text != null && !text.isEmpty()) ||
       
    74 		label.getIcon() != null);
       
    75 	}
       
    76     }
       
    77 
       
    78     //
       
    79     // Static data
       
    80     //
       
    81 
       
    82     private static final AutoHideListener LISTENER = new AutoHideListener();
       
    83 
       
    84     //
       
    85     // Constructors
       
    86     //
       
    87 
       
    88     public AutoHideLabel() {
       
    89 	init();
       
    90     }
       
    91 
       
    92     public AutoHideLabel(Icon icon) {
       
    93 	super(icon);
       
    94 	init();
       
    95     }
       
    96 
       
    97     public AutoHideLabel(Icon icon, int horizontalAlignment) {
       
    98 	super(icon, horizontalAlignment);
       
    99 	init();
       
   100     }
       
   101 
       
   102     public AutoHideLabel(String text) {
       
   103 	super(text);
       
   104 	init();
       
   105     }
       
   106 
       
   107     public AutoHideLabel(String text, Icon icon, int horizontalAlignment) {
       
   108 	super(text, icon, horizontalAlignment);
       
   109 	init();
       
   110     }
       
   111 
       
   112     public AutoHideLabel(String text, int horizontalAlignment) {
       
   113 	super(text, horizontalAlignment);
       
   114 	init();
       
   115     }
       
   116 
       
   117     //
       
   118     // Private methods
       
   119     //
       
   120 
       
   121     private void init() {
       
   122 	autoHide(this);
       
   123     }
       
   124 
       
   125     //
       
   126     // Static methods
       
   127     //
       
   128 
       
   129     /**
       
   130      * Add an {@link AutoHideListener} to the given {@code JLabel} to
       
   131      * automatically set its visibility whenever its text or icon properties
       
   132      * change.
       
   133      *
       
   134      * @return	    the added {@link AutoHideListener}
       
   135      */
       
   136     public static AutoHideListener autoHide(JLabel label) {
       
   137 	LISTENER.init(label);
       
   138 	return LISTENER;
       
   139     }
       
   140 }