package com.onaro.util.jfc; import java.awt.Cursor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import javax.swing.AbstractButton; import com.onaro.client.Resources; import com.onaro.commons.beans.IPropertyChangeSupport; /** * Class for an accordion (expanding/collapsing) control. * The control consists of an arrow icon indicating whether the control is * expanded or collapsed and controller text that the user can click on to * expand/collapse the control. When the control is expanded an event is fired. *

* This class has the following bound property: {@link #PROP_EXPANDED}. */ public class Accordion implements IPropertyChangeSupport { /** * Property used for notification of changes to the CollapsibleControl's expansion state */ public static final String PROP_EXPANDED = "expanded"; //$NON-NLS-1$ private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport (this); private final AbstractButton controller; private boolean isExpanded = false; /** * Create an accordion control. The accordion is collapsed by default. * * @param controller the button used to expand/collapse the accordion. * @see #setExpanded */ public Accordion (AbstractButton controller) { this.controller = controller; controller.setIcon (Resources.INSTANCE.getBoxPlusButtonSmallIcon()); controller.setCursor (Cursor.getPredefinedCursor (Cursor.HAND_CURSOR)); controller.addActionListener (new ActionListener() { @Override public void actionPerformed (ActionEvent e) { setExpanded (!isExpanded); } }); } /** * Set the expansion state of the collapsing control * @param expanded true to expand the control, false to collapse */ public void setExpanded (boolean expanded) { if (this.isExpanded == expanded) { return; } this.isExpanded = expanded; controller.setIcon (expanded ? Resources.INSTANCE.getBoxMinusButtonSmallIcon() : Resources.INSTANCE.getBoxPlusButtonSmallIcon()); controller.invalidate(); propertyChangeSupport.firePropertyChange (PROP_EXPANDED, ! expanded, expanded); } /** * Get the expansion state of the collapsing control. * @return true if the control is expanded, false if collapsed */ public boolean isExpanded() { return isExpanded; } /** * Enable or disable the control. * @param enable {@code true} to enable, {@code false} to disable */ public void setEnabled (boolean enable) { controller.setEnabled (enable); } /** * @return {@code true} if the control is enabled */ public boolean isEnabled() { return controller.isEnabled(); } /** * Get the controller (the component used to expand/collapse the control). * @return the controller */ public AbstractButton getController() { return controller; } @Override public void addPropertyChangeListener (PropertyChangeListener listener) { propertyChangeSupport.addPropertyChangeListener (listener); } @Override public void addPropertyChangeListener ( String propertyName, PropertyChangeListener listener) { propertyChangeSupport.addPropertyChangeListener (propertyName, listener); } @Override public void removePropertyChangeListener (PropertyChangeListener listener) { propertyChangeSupport.removePropertyChangeListener (listener); } @Override public void removePropertyChangeListener ( String propertyName, PropertyChangeListener listener) { propertyChangeSupport.removePropertyChangeListener (propertyName, listener); } }