From 8a8843e7dc10182ef0ec109392ad56980277cef5 Mon Sep 17 00:00:00 2001 From: mdb Date: Sat, 16 Mar 2002 20:52:07 +0000 Subject: [PATCH] Created a base class for panels that work with controllers. Enhanced controller to automatically call wasShown/wasHidden when its associated panel is added to and removed from the component hierarchy. git-svn-id: https://samskivert.googlecode.com/svn/trunk@659 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/swing/ControlledPanel.java | 49 +++++++++++++++++++ .../java/com/samskivert/swing/Controller.java | 43 +++++++++++++++- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 projects/samskivert/src/java/com/samskivert/swing/ControlledPanel.java diff --git a/projects/samskivert/src/java/com/samskivert/swing/ControlledPanel.java b/projects/samskivert/src/java/com/samskivert/swing/ControlledPanel.java new file mode 100644 index 00000000..f5c1935f --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/swing/ControlledPanel.java @@ -0,0 +1,49 @@ +// +// $Id: ControlledPanel.java,v 1.1 2002/03/16 20:52:07 mdb Exp $ + +package com.samskivert.swing; + +import javax.swing.JPanel; + +/** + * A controlled panel takes care of setting up its controller and + * providing it. When operating in conjunction with a controlled panel, + * the controller can automatically invoke {@link Controller#wasAdded} and + * {@link Controller#wasRemoved}. + */ +public abstract class ControlledPanel extends JPanel + implements ControllerProvider +{ + /** + * Creates a controlled panel and its associated controller. + */ + public ControlledPanel () + { + _controller = createController(); + + // let the controller know about this panel + _controller.setControlledPanel(this); + } + + // documentation inherited from interface + public Controller getController () + { + return _controller; + } + + /** + * Called to create the controller associated with this controlled + * panel. Derived classes should override this and instantiate the + * appropriate controller derived class. Note that this will be called + * very early in the construction process, before derived classes have + * access to their member data and as such, the derived panel will not + * be able to pass anything interesting to its associated controller + * constructor. The expectation is that it will obtain a reference to + * its controller later in its own constructor and supply any extra + * initialization data that is needed at that time. + */ + protected abstract Controller createController (); + + /** The controller with which we are working. */ + protected Controller _controller; +} diff --git a/projects/samskivert/src/java/com/samskivert/swing/Controller.java b/projects/samskivert/src/java/com/samskivert/swing/Controller.java index 17ea6b1e..0a5a0588 100644 --- a/projects/samskivert/src/java/com/samskivert/swing/Controller.java +++ b/projects/samskivert/src/java/com/samskivert/swing/Controller.java @@ -1,5 +1,5 @@ // -// $Id: Controller.java,v 1.9 2002/02/28 21:55:14 mdb Exp $ +// $Id: Controller.java,v 1.10 2002/03/16 20:52:07 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -25,7 +25,11 @@ import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import javax.swing.JPanel; +import javax.swing.event.AncestorEvent; + import com.samskivert.Log; +import com.samskivert.swing.event.AncestorAdapter; import com.samskivert.swing.event.CommandEvent; /** @@ -86,6 +90,43 @@ public abstract class Controller } }; + /** + * Lets this controller know about the panel that it is controlling. + */ + public void setControlledPanel (JPanel panel) + { + panel.addAncestorListener(new AncestorAdapter() { + public void ancestorAdded (AncestorEvent event) { + wasAdded(); + } + public void ancestorRemoved (AncestorEvent event) { + wasRemoved(); + } + }); + } + + /** + * Called when the panel controlled by this controller was added to + * the user interface hierarchy. This assumes that the controlled + * panel made itself known to the controller via {@link + * #setControlledPanel} (which is done automatically by {@link + * ControlledPanel} and derived classes). + */ + public void wasAdded () + { + } + + /** + * Called when the panel controlled by this controller was removed + * from the user interface hierarchy. This assumes that the controlled + * panel made itself known to the controller via {@link + * #setControlledPanel} (which is done automatically by {@link + * ControlledPanel} and derived classes). + */ + public void wasRemoved () + { + } + /** * Instructs this controller to process this action event. When an * action is posted by a user interface element, it will be posted to