From d75eac596225d941f721a445eb3f643c39ec9a1e Mon Sep 17 00:00:00 2001 From: ray Date: Thu, 4 Jul 2002 04:42:11 +0000 Subject: [PATCH] Sort of like a collapsable list, only we have real components inside of us that can be laid out anywhichway. git-svn-id: https://samskivert.googlecode.com/svn/trunk@789 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../samskivert/swing/CollapsiblePanel.java | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 projects/samskivert/src/java/com/samskivert/swing/CollapsiblePanel.java diff --git a/projects/samskivert/src/java/com/samskivert/swing/CollapsiblePanel.java b/projects/samskivert/src/java/com/samskivert/swing/CollapsiblePanel.java new file mode 100644 index 00000000..332d3534 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/swing/CollapsiblePanel.java @@ -0,0 +1,98 @@ +// +// $Id: CollapsiblePanel.java,v 1.1 2002/07/04 04:42:11 ray Exp $ + +package com.samskivert.swing; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JPanel; +import javax.swing.JButton; + +/** + * A panel that contains a button which will collapse the rest of the content. + */ +public class CollapsiblePanel extends JPanel + implements ActionListener +{ + /** + * Construct a collapsible panel with the specified button as + * the trigger. The text of the button will be used as the triggertext. + */ + public CollapsiblePanel (JButton trigger) + { + _trigger = trigger; + _text = trigger.getText(); + + VGroupLayout gl = new VGroupLayout(VGroupLayout.NONE); + gl.setOffAxisPolicy(VGroupLayout.STRETCH); + gl.setGap(0); + gl.setJustification(VGroupLayout.TOP); + gl.setOffAxisJustification(VGroupLayout.LEFT); + setLayout(gl); + + _trigger.addActionListener(this); + + // these are our only two components. + add(_trigger); + add(_content); + + // and start us out showing + setCollapsed(false); + } + + /** + * Construct a collapsible panel with the specified button text. + */ + public CollapsiblePanel (String triggertext) + { + this(new JButton(triggertext)); + } + + /** + * Get the content panel for filling in with sweet content goodness. + */ + public JPanel getContent () + { + return _content; + } + + // documentation from interface ActionListener + public void actionPerformed (ActionEvent e) + { + if (e.getSource() == _trigger) { + setCollapsed(!isCollapsed()); + } + } + + /** + * Is the panel collapsed? + */ + public boolean isCollapsed () + { + return !_content.isVisible(); + } + + /** + * Set the collapsion state. + */ + public void setCollapsed (boolean collapse) + { + if (collapse) { + _content.hide(); + _trigger.setText("+ " + _text); + } else { + _content.show(); + _trigger.setText("- " + _text); + } + } + + /** The button that triggers collapsion. */ + protected JButton _trigger; + + /** The original text in the button, oh this's gonna have to change. */ + protected String _text; + + /** The who in the what now? */ + protected JPanel _content = new JPanel(); +}