public class IlvJavaBehavior extends IlvSingleBehavior
IlvJavaBehavior
is used to specify the side effects
associated with a property by writing Java code. It is used to define complex
behaviors that cannot be expressed using the predefined subclasses of the
class IlvBehavior
, or to hard-code a behavior that has high
performance requirements.
The Java code that should be executed when the property handled by this behavior object is set or read is specified by two static methods of a Java class. The names of these methods are specified in the constructor of the behavior object. The name of the class in which these methods are defined can also be specified in the constructor. If the class name is not specified, the default class name is the name of the group (generally a prototype) to which the behavior object is attached; if the group is a prototype and if this prototype belongs to a prototype library, the class must belong to a package with the same name as the prototype library.
Example:
Let us suppose that you want to define a prototype called
fiblabel
that computes the Fibonacci function of an integer and
displays it inside a label. Here is how you would define the prototype:
IlvPrototype proto = new IlvPrototype("fiblabel"); proto.addElement(new IlvGraphicElement(new IlvLabel(new IlvPoint(0, 0), "no value"), "label")); proto.addBehavior(new IlvJavaBehavior("n", "fiblabel", "computeFib", null));You would then define the class
fiblabel
with the static method
computeFib
as follows:
class fiblabel { public static void computeFib(IlvGroup obj, Object value) { try { int n = IlvValueConverter.convertToInt(value); int f = fib(n); obj.set("label.label", new Integer(f)); } catch (Exception e) { e.printStackTrace(); } } public static int fib(int n) { if (n < 2) return (0); else return (fib(n - 1) + fib(n - 2)); } }
You would then compile the class fiblabel
, and make sure that
the resulting class file can be found using the Java class path.
When an application creates an instance of the prototype
fiblabel
, it loads the class fiblabel
and calls its
computeFib
method each time the n
value is set.
If you do not specify the className
parameter when creating the
IlvJavaBehavior
instance, like in:
proto.addBehavior(new IlvJavaBehavior("n", "computeFib", null));then, the
fiblabel
class must be defined in a package with the
same name as the prototype library that contains the fiblabel
prototype.
In all cases, you must make sure that the class that contains the setter and getter methods can be found in the Java class path.
IlvPrototypeLibrary
,
Serialized FormConstructor and Description |
---|
IlvJavaBehavior(IlvInputStream in)
Reads the behavior from an
IlvInputStream stream. |
IlvJavaBehavior(IlvJavaBehavior source)
Creates a new behavior object by copying an existing one.
|
IlvJavaBehavior(String name,
String setMethod,
String getMethod)
Creates a new Java behavior object.
|
IlvJavaBehavior(String name,
String className,
String setMethod,
String getMethod)
Creates a new Java behavior object.
|
Modifier and Type | Method and Description |
---|---|
IlvBehavior |
copy()
Returns a copy of the behavior.
|
protected Object |
get(IlvGroup group,
String name)
Calls the method specified by the
getMethod parameter passed
to the constructor. |
String |
getClassName()
Gets the
className parameter for this behavior object. |
String |
getGetMethod()
Gets the
getMethod parameter for this behavior object. |
String |
getSetMethod()
Gets the
setMethod parameter for this behavior object. |
boolean |
isReadable()
Returns
true if the getMethod argument passed to
the constructor was not set to "none" . |
boolean |
isWritable()
Returns
true if the setMethod argument passed to
the constructor was not set to "none" . |
protected void |
set(IlvGroup group,
String name,
Object value)
Calls the method specified by the
setMethod parameter passed
to the constructor. |
static void |
setClassLoader(ClassLoader classLoader)
Changes the class loader used to load the classes that define the
setter/getter methods of
IlvJavaBehavior objects. |
void |
setClassName(String className)
Sets the
className parameter for this behavior object. |
void |
setGetMethod(String getMethod)
Sets the
getMethod parameter for this behavior object. |
void |
setSetMethod(String setMethod)
Sets the
setMethod parameter for this behavior object. |
String |
toString()
Returns a short text describing this behavior object.
|
void |
write(IlvOutputStream out)
Writes the behavior to an
IlvOutputStream . |
get, getName, getValueNames, set, setName
attach, detach, getParameter, isOutput
public IlvJavaBehavior(String name, String className, String setMethod, String getMethod)
name
- the name of the behavior object.className
- the fully qualified name of the class that defines the setter and
getter methods. If className
is "none"
or null
, a default class name is used. The default
class name is the name of the group (or prototype) to which this
behavior object is attached. If the group is a prototype, the
default class must belong to a package which has the name of the
prototype library containing the prototype.setMethod
- the name of the method called when the value handled by this
behavior object is set. If no code is to be executed when the
value is set, this parameter can be null or it can be set to the
string "none"
.
The method must be declared as follows:
public static void setMethod(IlvGroup obj, Object value)When the method is called, the
obj
parameter is set
to the group/prototype to which the behavior object is attached,
and the value
parameter is set to the new value.
The method must be defined in a class that has the same name as the group/prototype to which this behavior object is attached. In addition, the class must belong to a package with the same name as the prototype library to which the prototype belongs.
getMethod
- the name of the method called when the value of this behavior
object is read. If no code is to be executed when the value is
read, this parameter can be null or it can be set to the string
"none"
.
The method must be declared as follows:
public static Object getMethod(IlvGroup obj)When the method is called, the
obj
parameter is set
to the group/prototype to which the behavior object is attached.
The method must compute the requested value, wrap it in a class
instance (for example, an Integer
object if the value
is of type int
), and return the value.
The method must be defined in a class that has the same name as the group/prototype to which this behavior object is attached. In addition, the class must belong to a package with the same name as the prototype library to which the prototype belongs.
public IlvJavaBehavior(String name, String setMethod, String getMethod)
This constructor calls the first constructor with the
className
parameter set to "none"
.
public IlvJavaBehavior(IlvJavaBehavior source)
source
- the behavior object to be copied.public IlvJavaBehavior(IlvInputStream in) throws IlvReadFileException
IlvInputStream
stream. This
constructor must be called by the input constructors of subclasses.in
- The input stream.IlvReadFileException
- if the stream format is not correct.public void write(IlvOutputStream out) throws IOException
IlvOutputStream
.write
in interface IlvPersistentObject
write
in class IlvSingleBehavior
out
- The output stream.IOException
- thrown when an exception occurs during
the write operation for this object.public IlvBehavior copy()
copy
in class IlvBehavior
public String toString()
toString
in class IlvBehavior
public boolean isReadable()
true
if the getMethod
argument passed to
the constructor was not set to "none"
.isReadable
in class IlvBehavior
public boolean isWritable()
true
if the setMethod
argument passed to
the constructor was not set to "none"
.isWritable
in class IlvBehavior
protected void set(IlvGroup group, String name, Object value) throws IlvValueException
setMethod
parameter passed
to the constructor. The name of the class defining this method is the same
as the name of the prototype with which this behavior object is associated.
This class must be added to a package whose name is that of the prototype
library to which the prototype belongs. The first argument of
setMethod
is set to group
. The second argument of
setMethod
is set to value
.
If setMethod
is "none"
, this method does nothing.
set
in class IlvSingleBehavior
group
- the group to which this behavior object is attached.name
- the name of this behavior object.value
- the new valueIlvValueException
- if an error occurred while setting a property.IlvSingleBehavior.set(ilog.views.prototypes.IlvGroup,
java.lang.String[], java.lang.Object[], boolean[])
protected Object get(IlvGroup group, String name) throws IlvValueException
getMethod
parameter passed
to the constructor. The name of the class defining this method is the same
as the name of the prototype with which this behavior object is associated.
This class must be added to a package whose name is that of the prototype
library to which the prototype belongs. The argument of
getMethod
is set to group
. The result of
getMethod
is returned directly by this method.
If setMethod
is "none"
, this method returns
null
.
get
in class IlvSingleBehavior
group
- the group to which this behavior object is attached.name
- the name of this behavior object.getMethod
, or null
if
getMethod
is "none"
.IlvValueException
- if an error occurred while reading a property.IlvSingleBehavior.get(ilog.views.prototypes.IlvGroup,
java.lang.String[], java.lang.Object[], boolean[])
public static void setClassLoader(ClassLoader classLoader)
IlvJavaBehavior
objects.
This method is used by the Perforce JViews editor (Composer) to load several versions of a class.
classLoader
- the new class loader.public void setClassName(String className)
className
parameter for this behavior object.public String getClassName()
className
parameter for this behavior object.public void setSetMethod(String setMethod)
setMethod
parameter for this behavior object.public String getSetMethod()
setMethod
parameter for this behavior object.public void setGetMethod(String getMethod)
getMethod
parameter for this behavior object.public String getGetMethod()
getMethod
parameter for this behavior object.© Copyright 2024 Rogue Wave Software, Inc., a Perforce company.. All Rights Reserved.