Pierre's backPageComponent
Pierre's backPageComponent
- Subject: Pierre's backPageComponent
- From: "Jonathan Fleming" <email@hidden>
- Date: Fri, 23 May 2003 14:36:26 +0100
Hello Pierre Bernard or anyone who can come up with a good solution,
Can you help me to convert your backPage component so that it can be used
with direct action?
Many Thanks
Jonathan :^)
/* @(#) MYBackButtonComponent.java
*/
import com.webobjects.appserver.WOApplication;
import com.webobjects.appserver.WOComponent;
import com.webobjects.appserver.WOContext;
import com.webobjects.appserver.WOResponse;
import com.webobjects.foundation.NSLog;
import java.lang.ref.WeakReference;
/** Subclass of WOComponent that adds server side backtracking features.<BR>
*
* The idea is that the constructor of a top-level component is called when
the page
* is instantiate from the previous request-response loop by a call to
pageWithName.
* Thus in the constructor it is possible to access the calling context and
determine
* it's top-level page.<BR>
*
* A weak reference to that calling page is stored and can be retrieved by
the previousPage()
* action method.
*
* For this to work the top-level component has to extend
MYBackButtonComponent as it is the
* only one to be able get hold of the calling page.<BR>
*
* Usually sub-components that want to provide a 'Back' button should also
extend MYBackButtonComponent
* so that they can call the previousPage() action method.<BR>
*
* Note: It might be a good idea to split this component into a
MYBackButtonComponent and a MYBackButtonPage,
* where MYBackButtonPage is a subclass of MYBackButtonComponent. Only
top-level components would need
* to extend MYBackButtonPage. Components that want to provide a 'Back'
button would extend
* MYBackButtonComponent.
**/
public class MYBackButtonComponent
extends MYComponent
{
// Public class constants
/** Action name
**/
public static final String PREVIOUS_PAGE = "previousPage";
// Private instance variables
/** Weak reference to the ancessor page assigned by top-level components
(pages).
* Does not prevent the ancessor from being garbage collected one it has
expired from the cache.
**/
private WeakReference ancessor;
// Constructors
/** Only public constructor.<BR>
*
* Grabs a reference to the current page from the current context. If
this component
* is itself a top-level component, that component is the
calling/ancessor page.
**/
public MYBackButtonComponent(WOContext context)
{
super(context);
ancessor = new WeakReference(context.page());
}
// Public instance methods
/** Determines if the ancessor page is still available
*
* @return true, if the page is still available
**/
public boolean hasPreviousPage()
{
return getAncessor() != null;
}
/** Action method that returns the previous page.<BR>
*
* In the event where the previous page is no longer available a
placeholder
* component is returned that will call
WOApplication.handlePageRestorationErrorInContext()
* in its appendToResponse() method to generate the appropriate error
page.
*
* @return the ancessor page if it is still available
**/
public WOComponent previousPage()
{
WOComponent previousPage = getAncessor();
if (previousPage != null)
{
return previousPage;
}
else
{
return new MYPageMissingComponent(context());
}
}
// Private instance methods
/** Retrieves the ancessor for the current page.<BR>
*
* The ancessor is stored by the top-level component which has to be
an instance of MYBackButtonComponent.<BR>
*
* This method may return null if the ancessor page has expired from
the page cache or if the current page
* is not an instance MYBackButtonComponent.
*
* @return the ancessor page, null if it is not/no longer known
**/
private WOComponent getAncessor()
{
if (parent() == null)
{
return (WOComponent) ancessor.get();
}
else
{
WOComponent page = context().page();
if (page instanceof MYBackButtonComponent)
{
return ((MYBackButtonComponent) page).getAncessor();
}
else
{
if
(NSLog.debugLoggingAllowedForLevel(NSLog.DebugLevelCritical))
{
NSLog.out.appendln("WARNING: cannot retrieve previous
page as the current page is not a MYBackButtonComponent");
}
return null;
}
}
}
// Private inner class
/** Placeholder page returned when the requested ancessor page is no
longer available.<BR>
*
* Relies on WOApplication.handlePageRestorationErrorInContext() to
handle the generation of
* its response.
**/
private static class MYPageMissingComponent
extends WOComponent
{
public MYPageMissingComponent(WOContext context)
{
super(context);
}
public void appendToResponse(WOResponse response, WOContext context)
{
response.setContent(WOApplication.application().handlePageRestorationErrorInContext(context).content());
}
}
}
_________________________________________________________________
Hotmail messages direct to your mobile phone http://www.msn.co.uk/msnmobile
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.