001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2008 Sun Microsystems, Inc. 015 * Portions Copyright 2015 ForgeRock AS. 016 */ 017 018package org.opends.guitools.controlpanel.ui.components; 019 020import java.awt.BorderLayout; 021import java.awt.Color; 022 023import javax.swing.JComponent; 024import javax.swing.JPanel; 025import javax.swing.border.Border; 026import javax.swing.event.ChangeEvent; 027import javax.swing.event.ChangeListener; 028 029import org.opends.guitools.controlpanel.datamodel.Category; 030import org.opends.guitools.controlpanel.ui.border.AccordionElementBorder; 031 032/** 033 * The panel representing a category. It contains a CategoryButton and a panel 034 * that is displayed when the CategoryButton is in a certain state. They 035 * are used on the left side of the main Control Panel. 036 * 037 */ 038public class CategoryPanel extends JPanel { 039 private static final long serialVersionUID = 8941374689175404431L; 040 private JPanel panel; 041 private JComponent child; 042 private Category category; 043 044 private CategoryButton expandButton; 045 private boolean expanded = true; 046 047 static final Border categoryBorder = new AccordionElementBorder(); 048 049 /** 050 * Constructor the the panel. 051 * @param child the component that must be displayed by this panel if its 052 * CategoryButton is in a certain state. 053 * @param category the Category associated with the panel. 054 */ 055 public CategoryPanel(JComponent child, Category category) 056 { 057 this.child = child; 058 setLayout(new BorderLayout()); 059 panel = new JPanel(new BorderLayout()); 060 add(panel, BorderLayout.CENTER); 061 panel.add(child, BorderLayout.CENTER); 062 063 expandButton = new CategoryButton(category); 064 expandButton.setSelected(isExpanded()); 065 expandButton.addChangeListener(new CollapseListener()); 066 add(expandButton, BorderLayout.NORTH); 067 068 this.category = category; 069 070 setBorder(categoryBorder); 071 } 072 073 /** 074 * Sets whether the state of the panel is extended or not (if expanded the 075 * Component provided in the constructor will be displayed). 076 * @param expanded whether the panel is extended or not. 077 */ 078 public void setExpanded(boolean expanded) { 079 boolean oldExpanded = this.expanded; 080 if (oldExpanded != expanded) { 081 expandButton.setSelected(expanded); 082 this.expanded = expanded; 083 084 //setCollapseHeight(expanded? childPrefSize.height : 0); 085 child.setVisible(expanded); 086 firePropertyChange("expanded", oldExpanded, expanded); 087 } 088 } 089 090 /** 091 * Returns the category associated with this panel. 092 * @return the category associated with this panel. 093 */ 094 public Category getCategory() 095 { 096 return category; 097 } 098 099 /** 100 * Returns the component that must be displayed by this panel if its 101 * CategoryButton is in a certain state. 102 * @return the component that must be displayed by this panel if its 103 * CategoryButton is in a certain state. 104 */ 105 public JComponent getChild() 106 { 107 return child; 108 } 109 110 /** 111 * Returns <CODE>true</CODE> if the panel is extended and <CODE>false</CODE> 112 * otherwise. 113 * @return <CODE>true</CODE> if the panel is extended and <CODE>false</CODE> 114 * otherwise. 115 */ 116 public boolean isExpanded() 117 { 118 return expanded; 119 } 120 121 /** {@inheritDoc} */ 122 public void setForeground(Color foreground) 123 { 124 super.setForeground(foreground); 125 if (expandButton != null) 126 { 127 expandButton.setForeground(foreground); 128 } 129 } 130 131 /** The custom listener used to display the child component. */ 132 private class CollapseListener implements ChangeListener 133 { 134 /** {@inheritDoc} */ 135 public void stateChanged(ChangeEvent event) { 136 setExpanded(expandButton.isSelected()); 137 } 138 } 139}