There have been various threads lately about how to replicate 'global' or
'template' event processing, as done in ND3, in ND4.
This is the kind of thing where you do the same processing on all page
fields that share some common characteristic
For example, you might want to turn all of your textboxes into static text
fields, or not, according to some runtime condition.
In ND3, you would do something like this (this from memory, excuse errors)
//this would likely be set at runtime, perhaps from a session object
boolean _shouldbestatic = true;
protected int onBeforeHtmlOutputEvent (CSpVisual visualObject)
{
int command = PROCEED;
try
{
if (_shouldbestatic)
{
if (visualObject instanceof CSpTextBox)
{
CSpTextBox textbox = (CSpTextBox) visualObject;
textbox.setHtmlText(textbox.getValue().toString());
}//if (visualObject instanceof CSpTextBox)
}//if (_shouldbestatic)
}//try
catch (Exception ex)
{
CSpLog.send(this, CSpLog.ERROR, "onBeforeHtmlOutputEvent, unexpected
Exception: " +ex);
}//catch (Exception ex)
return (command)
}//protected int onBeforeHtmlOutputEvent (CSpVisual visualObject)
With the advent of ND4, and the new event model, many of us yelled that our
code would break. So ND4 did make an effort to respond, essentially
pointing to some things in the infrastructure that they had built to
support event introspection and mapping in the new model.
Their approach does work, but it is not intuitive and hard to get right, IMHO.
Since then, I have learned more about vanilla java event handling and the
use of inner classes. Based on this, I would recommend a different
approach, as shown below.
-- Curt Springer, Team ND
(code example follows)
// This file has been generated by NetDynamics Studio on Fri Jun. 04, 1999
21:33
package test;
import java.awt.event.*;
import java.util.*;
import spider.event.*;
import spider.database.*;
import spider.visual.*;
import spider.util.*;
import spider.session.*;
//[[SPIDER_CLASS BEGIN
public class pgInnerClass extends spider.visual.CSpPage
//]]SPIDER_CLASS END
{
//[[SPIDER_EVENTS BEGIN
//[[SPIDER_EVENT<this_onAfterInitEvent>
public int this_onAfterInitEvent(CSpInitEvent event)
{
int command = PROCEED;
try
{
Vector children = getSimpleChildren(this);
//note: jdk does not support a direct reference from an inner class to
its outerclass
//Core java book suggested 'pgInnerClass.this', but that caused
compiler
error
final Object outerreference = this;
/*
create an instance of an anonymous inner class that implements the
ISpHtmlOutputListener interface
If an interface has only one method, the inner class should implement the
interface and implement the
method (as below).
If the interface has more than one method, there will be a companion
adapter class that implements all
of the methods (in a way that either does nothing or throws an Exception).
In this case, the
anonymous inner class should extend the adapter class and override one
method (in most cases). The syntax
would be identical to what is below, except that the name of the adapter
class would be substituted for
the name of the interface.
*/
ISpHtmlOutputListener listener = new ISpHtmlOutputListener()
{
//*****************************************************************
/**
* Event handler for event that occurs right before HTML is output
* to the HTTP buffer. HTML has been generated at this point, but
* not necessarily in the format of what is needed.
*
* @param event Event source
* @return int Return code that defines the next
operation
* @see spider.visual.CSpVisual
* @see spider.CSpFileBased#PROCEED
* @see spider.CSpFileBased#SKIP
* @see spider.CSpFileBased#STOP
* @see
spider.event.ISpHtmlOutputListener#onBeforeHtmlOutputEvent
* @see spider.visual.CSpVisual#addHtmlOutputListener
* @see spider.event.CSpHtmlOutputEvent
*/
//*****************************************************************
public int onBeforeHtmlOutputEvent(CSpHtmlOutputEvent event)
throws CSpSkipException
{
CSpDisplayField field = (CSpDisplayField) event.getSource();
try
{
//note reference to member variable of outer class:
if (_shouldbestatic)
{
CSpTextBox textbox = (CSpTextBox)
event.getSource();
textbox.setHtmlText(textbox.getValue().toString());
}//if (_shouldbestatic)
return(PROCEED);
} // try
catch( Exception ex )
{
// an exception has occurred.
// Stop processing for this object
//note ad hoc reference to outer class:
CSpLog.send(outerreference, CSpLog.ERROR,
"TextBoxHtmlOutputListener for "+field.getName() +",
unexpected Exception: " + ex);
//note: returning SKIP doesn't work, despite published
documentation that says it's the same.
throw (new CSpSkipException());
} // catch ( Exception ex)
}//public int onBeforeHtmlOutputEvent(CSpHtmlOutputEvent event)
};//ISpHtmlOutputListener listener = new
ISpHtmlOutputListener()......
for (Enumeration e = children.elements(); e.hasMoreElements();)
{
CSpVisual visual = (CSpVisual) e.nextElement();
if (visual instanceof CSpTextBox)
{
((CSpTextBox) visual).addHtmlOutputListener(listener);
}//if (visual instanceof CSpTextBox)
}//for (Enumeration e = children.elements();
children.hasMoreElements();)
}//try
catch (Exception ex)
{
CSpLog.send(this, CSpLog.ERROR, "this_onAfterInitEvent, unexpected
exception: " + ex);
command=STOP;
}//catch (Exception ex)
return(command);
}
//]]SPIDER_EVENT<this_onAfterInitEvent>
//]]SPIDER_EVENTS END
//this would likely be set at runtime, perhaps from a session object
//make this package protection, not private
boolean _shouldbestatic = true;
/**
* Recursive method to cycle through all the children of a page or repeated
* and return a Vector of all the simple (i.e. not repeateds) objects at
* all levels below the starting point. Skip infrastructure stuff like
* the hidden pagesession object
* <br><br>
* In real life, this would be a public static method in an abstract
utility class
* @param object Page or Repeated
* @return Vector all of the children and grandchildren
*/
private Vector getSimpleChildren(CSpDataDrivenVisual object) throws Exception
{
try
{
Vector returnvector = new Vector();
loop: for(Enumeration e = object.getChildren().elements();
e.hasMoreElements();)
{
CSpVisual visual = (CSpVisual) e.nextElement();
if (visual instanceof CSpPageSessionHidden)
{
continue loop; //skip but keep going
}//if (visual instanceof CSpPageSessionHidden)
if (visual instanceof CSpDataDrivenVisual)
{
//add children to flattened list
Vector interimvector =
getSimpleChildren((CSpDataDrivenVisual) visual);
for (Enumeration iv = interimvector.elements();
iv.hasMoreElements(); )
{
returnvector.addElement(iv.nextElement());
}//for (Enumeration iv =
interimvector.elements();
iv.hasMoreElements(); )
}//if (visual instanceof CSpDataDrivenVisual)
else
{
returnvector.addElement(visual);
}//else
}//loop: for(Enumeration e = object.getChildren().elements();
e.hasMoreElements();)
return returnvector;
}//try
catch (Exception ex)
{
CSpLog.send(this, CSpLog.ERROR, "getSimpleChildren, unexpected
Exception:
" + ex);
throw(ex);
}//catch (Exception ex)
}//private Vector getSimpleChildren(CSpDataDrivenVisual object) throws
Exception
}//public class pgInnerClass extends spider.visual.CSpPage
_________________________________________________________________________
For help in using, subscribing, and unsubscribing to the discussion
forums, please go to: http://www.netdynamics.com/support/visitdevfor.html
For dire need help, email: [EMAIL PROTECTED]