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
This commit is contained in:
mdb
2002-03-16 20:52:07 +00:00
parent 70ad13008f
commit 8a8843e7dc
2 changed files with 91 additions and 1 deletions
@@ -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;
}
@@ -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