components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/SettingsToolBar.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 import javax.swing.*;
       
    31 import javax.swing.border.Border;
       
    32 import com.oracle.solaris.vp.util.swing.layout.*;
       
    33 
       
    34 /**
       
    35  * The {@code SettingsToolBar} class is a tool bar initialized with a {@code
       
    36  * RowLayout}, a {@link #getDefaultConstraint}, and containing a greedy {@code
       
    37  * #getSpacerIndex spacer component}.  Components added before the spacer will
       
    38  * be left-justified; those added after will be right-justtified.
       
    39  */
       
    40 @SuppressWarnings({"serial"})
       
    41 public class SettingsToolBar extends JPanel {
       
    42     //
       
    43     // Instance data
       
    44     //
       
    45 
       
    46     private Spacer spacer = new Spacer();
       
    47 
       
    48     private ContainerListener setVisibleOnAdd =
       
    49 	new ContainerListener() {
       
    50 	    @Override
       
    51 	    public void componentAdded(ContainerEvent e) {
       
    52 		setVisible();
       
    53 	    }
       
    54 
       
    55 	    @Override
       
    56 	    public void componentRemoved(ContainerEvent e)  {
       
    57 		setVisible();
       
    58 	    }
       
    59 	};
       
    60 
       
    61     //
       
    62     // Constructors
       
    63     //
       
    64 
       
    65     public SettingsToolBar() {
       
    66 	RowLayout layout = new RowLayout(HorizontalAnchor.FILL);
       
    67 
       
    68 	RowLayoutConstraint r = new RowLayoutConstraint(
       
    69 	    VerticalAnchor.CENTER, GUIUtil.getButtonGap());
       
    70 
       
    71 	layout.setDefaultConstraint(r);
       
    72 	setLayout(layout);
       
    73 
       
    74 	// Add spacer
       
    75 	add(spacer, r.clone().setWeight(1));
       
    76 
       
    77 	int gap = GUIUtil.getButtonGap();
       
    78 	Border border = BorderFactory.createEmptyBorder(gap, gap, gap, gap);
       
    79 	setBorder(border);
       
    80 
       
    81 	// By default, make invisible until Components are added to it
       
    82 	setVisible(false);
       
    83 	addContainerListener(setVisibleOnAdd);
       
    84     }
       
    85 
       
    86     /**
       
    87      * Gets the default constraint of the RowLayout used by this {@code
       
    88      * SettingsToolBar}.
       
    89      */
       
    90     public RowLayoutConstraint getDefaultConstraint() {
       
    91 	return ((RowLayout)getLayout()).getDefaultConstraint();
       
    92     }
       
    93 
       
    94     /**
       
    95      * Returns the index of the spacer component of this tool bar, or -1 if the
       
    96      * spacer has been removed.
       
    97      */
       
    98     public int getSpacerIndex() {
       
    99 	Component[] comps = getComponents();
       
   100 	for (int i = 0; i < comps.length; i++) {
       
   101 	    if (comps[i] == spacer) {
       
   102 		return i;
       
   103 	    }
       
   104 	}
       
   105 	return -1;
       
   106     }
       
   107 
       
   108     /**
       
   109      * Sets the visibility of this {@code SettingsToolBar} based on its contents
       
   110      * or other criteria.  Automatically called when a child {@code Component}
       
   111      * is added.
       
   112      * <p/>
       
   113      * This default implementation sets this {@code SettingsToolBar} to be
       
   114      * visible if some {@code Component} other than the {@link Spacer} has been
       
   115      * added, or invisble otherwise.
       
   116      */
       
   117     protected void setVisible() {
       
   118 	Component[] comps = getComponents();
       
   119 	setVisible(comps.length > 1 || (comps.length == 1 &&
       
   120 	    comps[0] != spacer));
       
   121     }
       
   122 }