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