components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/DisablableTabTabbedPane.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) 2009, 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.*;
       
    31 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    32 import com.oracle.solaris.vp.util.swing.layout.*;
       
    33 
       
    34 @SuppressWarnings({"serial"})
       
    35 public class DisablableTabTabbedPane extends JTabbedPane
       
    36     implements DescendantEnabler {
       
    37 
       
    38     //
       
    39     // Inner classes
       
    40     //
       
    41 
       
    42     public class TabComponent extends JPanel {
       
    43 	//
       
    44 	// Instance data
       
    45 	//
       
    46 
       
    47 	private Component mComponent;
       
    48 	private JLabel label;
       
    49 	private JCheckBox checkBox;
       
    50 	private RowLayout layout;
       
    51 
       
    52 	//
       
    53 	// Constructors
       
    54 	//
       
    55 
       
    56 	public TabComponent(String title, Component mComponent) {
       
    57 	    this.mComponent = mComponent;
       
    58 
       
    59 	    setOpaque(false);
       
    60 
       
    61 	    label = new JLabel(title);
       
    62 	    checkBox = new JCheckBox();
       
    63 	    checkBox.setToolTipText(Finder.getString(
       
    64 		"tabbedpane.checkbox.tooltip"));
       
    65 
       
    66 	    checkBox.addActionListener(
       
    67 		new ActionListener() {
       
    68 		    @Override
       
    69 		    public void actionPerformed(ActionEvent e) {
       
    70 			setSelectedComponent(getMainComponent());
       
    71 		    }
       
    72 		});
       
    73 
       
    74 	    GUIUtil.setEnableStateOnSelect(checkBox, true, true,
       
    75 		mComponent);
       
    76 
       
    77 	    // Set label and checkbox font
       
    78 	    setFont(DisablableTabTabbedPane.this.getFont());
       
    79 
       
    80 	    layout = new RowLayout(HorizontalAnchor.FILL);
       
    81 	    RowLayoutConstraint r = new RowLayoutConstraint(
       
    82 		VerticalAnchor.CENTER, GUIUtil.getHalfGap());
       
    83 	    r.setLayoutIfInvisible(true);
       
    84 	    layout.setDefaultConstraint(r);
       
    85 
       
    86 	    setLayout(layout);
       
    87 	    add(label, r);
       
    88 	    add(checkBox, r);
       
    89 	}
       
    90 
       
    91 	//
       
    92 	// Component methods
       
    93 	//
       
    94 
       
    95 	@Override
       
    96 	public void setFont(Font font) {
       
    97 	    super.setFont(font);
       
    98 	    try {
       
    99 		getLabel().setFont(font);
       
   100 		getCheckBox().setFont(font);
       
   101 	    } catch (NullPointerException ignore) {
       
   102 	    }
       
   103 	}
       
   104 
       
   105 	//
       
   106 	// Container methods
       
   107 	//
       
   108 
       
   109 	@Override
       
   110 	public RowLayout getLayout() {
       
   111 	    return layout;
       
   112 	}
       
   113 
       
   114 	//
       
   115 	// TabComponent methods
       
   116 	//
       
   117 
       
   118 	public JCheckBox getCheckBox() {
       
   119 	    return checkBox;
       
   120 	}
       
   121 
       
   122 	public JLabel getLabel() {
       
   123 	    return label;
       
   124 	}
       
   125 
       
   126 	public Component getMainComponent() {
       
   127 	    return mComponent;
       
   128 	}
       
   129     }
       
   130 
       
   131     //
       
   132     // Constructors
       
   133     //
       
   134 
       
   135     public DisablableTabTabbedPane() {
       
   136 	setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
       
   137     }
       
   138 
       
   139     //
       
   140     // Component methods
       
   141     //
       
   142 
       
   143     @Override
       
   144     public void setEnabled(boolean enabled) {
       
   145 	super.setEnabled(enabled);
       
   146 
       
   147 	if (!enabled) {
       
   148 	    for (Component component : getComponents()) {
       
   149 		GUIUtil.setEnabledRecursive(component, enabled);
       
   150 	    }
       
   151 	} else {
       
   152 	    for (int i = 0, n = getTabCount(); i < n; i++) {
       
   153 		boolean e = enabled;
       
   154 
       
   155 		Component tComponent = getTabComponentAt(i);
       
   156 		if (tComponent != null) {
       
   157 		    GUIUtil.setEnabledRecursive(tComponent, enabled);
       
   158 		    if (tComponent instanceof TabComponent) {
       
   159 			e = ((TabComponent)tComponent).getCheckBox().
       
   160 			    isSelected();
       
   161 		    }
       
   162 		}
       
   163 
       
   164 		Component component = getComponentAt(i);
       
   165 		if (component != null) {
       
   166 		    GUIUtil.setEnabledRecursive(component, e);
       
   167 		}
       
   168 	    }
       
   169 	}
       
   170     }
       
   171 
       
   172     @Override
       
   173     public void setFont(Font font) {
       
   174 	super.setFont(font);
       
   175 
       
   176 	for (int i = 0, n = getTabCount(); i < n; i++) {
       
   177 	    Component tComponent = getTabComponentAt(i);
       
   178 	    if (tComponent != null) {
       
   179 		tComponent.setFont(font);
       
   180 	    }
       
   181 	}
       
   182     }
       
   183 
       
   184     //
       
   185     // DisablableTabTabbedPane methods
       
   186     //
       
   187 
       
   188     public TabComponent addDisablableTab(String title, Component component) {
       
   189 	final int i = getTabCount();
       
   190 	addTab(null, component);
       
   191 
       
   192 	TabComponent tComponent = new TabComponent(title, component);
       
   193 	setTabComponentAt(i, tComponent);
       
   194 
       
   195 	return tComponent;
       
   196     }
       
   197 }