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-2016 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 */ 037public class CategoryPanel extends JPanel { 038 private static final long serialVersionUID = 8941374689175404431L; 039 private JPanel panel; 040 private JComponent child; 041 private Category category; 042 043 private CategoryButton expandButton; 044 private boolean expanded = true; 045 046 private static final Border categoryBorder = new AccordionElementBorder(); 047 048 /** 049 * Constructor the the panel. 050 * @param child the component that must be displayed by this panel if its 051 * CategoryButton is in a certain state. 052 * @param category the Category associated with the panel. 053 */ 054 public CategoryPanel(JComponent child, Category category) 055 { 056 this.child = child; 057 setLayout(new BorderLayout()); 058 panel = new JPanel(new BorderLayout()); 059 add(panel, BorderLayout.CENTER); 060 panel.add(child, BorderLayout.CENTER); 061 062 expandButton = new CategoryButton(category); 063 expandButton.setSelected(isExpanded()); 064 expandButton.addChangeListener(new CollapseListener()); 065 add(expandButton, BorderLayout.NORTH); 066 067 this.category = category; 068 069 setBorder(categoryBorder); 070 } 071 072 /** 073 * Sets whether the state of the panel is extended or not (if expanded the 074 * Component provided in the constructor will be displayed). 075 * @param expanded whether the panel is extended or not. 076 */ 077 public void setExpanded(boolean expanded) { 078 boolean oldExpanded = this.expanded; 079 if (oldExpanded != expanded) { 080 expandButton.setSelected(expanded); 081 this.expanded = expanded; 082 083 //setCollapseHeight(expanded? childPrefSize.height : 0); 084 child.setVisible(expanded); 085 firePropertyChange("expanded", oldExpanded, expanded); 086 } 087 } 088 089 /** 090 * Returns the category associated with this panel. 091 * @return the category associated with this panel. 092 */ 093 public Category getCategory() 094 { 095 return category; 096 } 097 098 /** 099 * Returns the component that must be displayed by this panel if its 100 * CategoryButton is in a certain state. 101 * @return the component that must be displayed by this panel if its 102 * CategoryButton is in a certain state. 103 */ 104 public JComponent getChild() 105 { 106 return child; 107 } 108 109 /** 110 * Returns <CODE>true</CODE> if the panel is extended and <CODE>false</CODE> 111 * otherwise. 112 * @return <CODE>true</CODE> if the panel is extended and <CODE>false</CODE> 113 * otherwise. 114 */ 115 public boolean isExpanded() 116 { 117 return expanded; 118 } 119 120 @Override 121 public void setForeground(Color foreground) 122 { 123 super.setForeground(foreground); 124 if (expandButton != null) 125 { 126 expandButton.setForeground(foreground); 127 } 128 } 129 130 /** The custom listener used to display the child component. */ 131 private class CollapseListener implements ChangeListener 132 { 133 @Override 134 public void stateChanged(ChangeEvent event) { 135 setExpanded(expandButton.isSelected()); 136 } 137 } 138}