Created a mechanism for dispatching notifications to user interface

elements when the client enters and leaves a place.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@394 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-10-04 20:02:49 +00:00
parent 20b6650dad
commit 64e66dc98a
2 changed files with 143 additions and 0 deletions
@@ -0,0 +1,58 @@
//
// $Id: PlaceView.java,v 1.1 2001/10/04 20:02:49 mdb Exp $
package com.threerings.cocktail.party.util;
import com.threerings.cocktail.party.data.PlaceObject;
/**
* This interface provides a convenient means for decoupling user
* interface components that interact with a place object and that need to
* keep themselves up to date when the client moves from place to place.
*
* <p> In general, such components need to know when the client is about
* to enter a place so that they can subscribe if necessary or at least
* extract information about the place. They also need to know when a
* client has left a place so that they can unsubscribe and clean up after
* themselves. This is the information that the place view interface makes
* available to them in a decoupled way.
*
* <p> The part of the client implementation that is responsible for the
* main user interface can act as a location observer, and it can make use
* of {@link PlaceViewUtil} to dispatch notification of place changes to
* every <code>PlaceView</code> implementing user interface element in the
* user interface hierarchy with calls to {@link
* PlaceViewUtil#dispatchWillEnterPlace} and {@link
* PlaceViewUtil#dispatchDidLeavePlace}. These functions traverse the UI
* hierarchy (starting with the element provided which would generally be
* the top-level UI element, and dispatch calls to {@link #willEnterPlace}
* and {@link #willLeavePlace} respectively on any UI element they find
* that implements <code>PlaceView</code>.
*
* <p> By doing this, the client code can simply create place-sensitive
* user interface elements and stick them in the user interface and
* essentially forget about them, knowing that they will all be notified
* of place entering and exiting by virtue of the single dispatching
* calls. It is useful to note that place-sensitive user interface
* elements will also generally need a reference to the {@link
* PartyContext} derivative in use by the client, but those are best
* supplied at construct time.
*/
public interface PlaceView
{
/**
* Called when the client has entered a place and is about to display
* the user interface for that place.
*
* @param plobj the place object that was just entered.
*/
public void willEnterPlace (PlaceObject plobj);
/**
* Called after the client has left a place and needs to clean up
* after the user interface that was displaying that place.
*
* @param plobj the place object that was just left.
*/
public void didLeavePlace (PlaceObject plobj);
}
@@ -0,0 +1,85 @@
//
// $Id: PlaceViewUtil.java,v 1.1 2001/10/04 20:02:49 mdb Exp $
package com.threerings.cocktail.party.util;
import java.awt.Component;
import java.awt.Container;
import com.threerings.cocktail.party.Log;
import com.threerings.cocktail.party.data.PlaceObject;
/**
* Provides a mechanism for dispatching notifications to all user
* interface elements in a hierarchy that implement the {@link PlaceView}
* interface. Look at the documentation for {@link PlaceView} for more
* explanation.
*/
public class PlaceViewUtil
{
/**
* Dispatches a call to {@link PlaceView#willEnterPlace} to all UI
* elements in the hierarchy rooted at the component provided via the
* <code>root</code> parameter.
*
* @param root the component at which to start traversing the UI
* hierarchy.
* @param plobj the place object that is about to be entered.
*/
public static void dispatchWillEnterPlace (
Component root, PlaceObject plobj)
{
// dispatch the call on this component if it implements PlaceView
if (root instanceof PlaceView) {
try {
((PlaceView)root).willEnterPlace(plobj);
} catch (Exception e) {
Log.warning("Component choked on willEnterPlace() " +
"[component=" + root + ", plobj=" + plobj + "].");
Log.logStackTrace(e);
}
}
// now traverse all of this component's children
if (root instanceof Container) {
Container cont = (Container)root;
int ccount = cont.getComponentCount();
for (int i = 0; i < ccount; i++) {
dispatchWillEnterPlace(cont.getComponent(i), plobj);
}
}
}
/**
* Dispatches a call to {@link PlaceView#didLeavePlace} to all UI
* elements in the hierarchy rooted at the component provided via the
* <code>root</code> parameter.
*
* @param root the component at which to start traversing the UI
* hierarchy.
* @param plobj the place object that is about to be entered.
*/
public static void dispatchDidLeavePlace (
Component root, PlaceObject plobj)
{
// dispatch the call on this component if it implements PlaceView
if (root instanceof PlaceView) {
try {
((PlaceView)root).didLeavePlace(plobj);
} catch (Exception e) {
Log.warning("Component choked on didLeavePlace() " +
"[component=" + root + ", plobj=" + plobj + "].");
Log.logStackTrace(e);
}
}
// now traverse all of this component's children
if (root instanceof Container) {
Container cont = (Container)root;
int ccount = cont.getComponentCount();
for (int i = 0; i < ccount; i++) {
dispatchDidLeavePlace(cont.getComponent(i), plobj);
}
}
}
}