/*
Copyright (C) 2001-2003 Renaud Pawlak, Laurent Martelli
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA */
package org.objectweb.jac.aspects.gui;
import java.util.Arrays;
import org.objectweb.jac.core.NameRepository;
import org.objectweb.jac.core.rtti.AbstractMethodItem;
import org.objectweb.jac.core.rtti.FieldItem;
import org.objectweb.jac.core.rtti.NamingConventions;
import org.objectweb.jac.eval.Evaluator;
import org.objectweb.jac.util.ExtBoolean;
import org.objectweb.jac.util.Strings;
/**
* This class holds a method and its parameters that can be passed to a
* peer object as a callback.
*/
public class Callback {
AbstractMethodItem method;
Object[] parameters;
String objectExpr;
/**
* Constructs the callback.
*/
public Callback(String objectExpr, AbstractMethodItem method, Object[] parameters) {
if (Strings.isEmpty(objectExpr) && !method.isStatic())
throw new RuntimeException("Method "+method+" is not static; An object must be provided");
this.objectExpr = objectExpr;
this.method = method;
this.parameters = parameters;
}
/**
* Constructs the callback.
*/
public Callback(Object object, AbstractMethodItem method, Object[] parameters) {
if (object==null && !method.isStatic())
throw new RuntimeException(
"Method "+method+" is not static; An object must be provided");
this.object = object;
this.method = method;
this.parameters = parameters;
}
/**
* The callback method. */
public AbstractMethodItem getMethod() {
return method;
}
/**
* The parameters that can be passed to the callback method. */
public Object[] getParameters() {
return parameters;
}
public String getObjectExpr() {
return objectExpr;
}
Object object;
public Object getObject() {
try {
return object!=null ? object : Evaluator.evaluate(objectExpr,null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void invoke(DisplayContext context, View source) {
EventHandler.get().onInvoke(
context,
new InvokeEvent(source,getObject(),method,parameters));
}
public void toggleBooleanField(DisplayContext context, View source) throws Exception {
EventHandler.get().onInvoke(
context,
new InvokeEvent(
source,
getObject(),
method,
new Object[] {ExtBoolean.valueOf(!getFieldBooleanValue())}));
}
/**
* Returns the field set by the method, or null. */
public FieldItem getField() {
return method.getSetField();
}
public boolean getFieldBooleanValue() throws Exception {
return ((Boolean)method.getSetField().getThroughAccessor(getObject())).booleanValue();
}
public String getLabel() {
return
method.isSetter()
? "Set "+NamingConventions.lowerFirst(GuiAC.getLabel(method.getSetField()))
: GuiAC.getLabel(method);
}
public String toString() {
return "Callback "+objectExpr+"."+method.getName()+
"("+(parameters!=null?Arrays.asList(parameters).toString():"")+")";
}
}