Show ThinkcapWidgetManager.java syntax highlighted
package org.integratedmodelling.thinkcap.widget;
import java.util.HashMap;
import java.util.Hashtable;
import org.apache.velocity.VelocityContext;
import org.integratedmodelling.thinkcap.PageSpecs;
import org.integratedmodelling.thinkcap.ThinkcapSession;
import org.integratedmodelling.thinkcap.exceptions.ThinkcapException;
import org.integratedmodelling.thinkcap.exceptions.ThinkcapWidgetException;
import org.integratedmodelling.thinklab.exception.ThinklabException;
/**
* The WidgetManager is a singleton in each session, used in templates to create Widgets.
* It is accessed in templates using the variable $WidgetManager or through the current
* session. Widget constructors can be registered by plug-ins to allow widget creation
* in templates, through direct calls or velocimacros.
* @author Ferdinando Villa
*
*/
public class ThinkcapWidgetManager {
VelocityContext context;
static Hashtable<String, IWidgetPublisher> constructors =
new Hashtable<String, IWidgetPublisher>();
public ThinkcapWidgetManager(VelocityContext context) {
this.context = context;
}
/**
* Pass a new widget and associate it to a name in the page context. Initialize all needed
* scripts, styles etc. from the widget's requirements.
* @param widgetName
* @param widget
* @throws ThinkcapWidgetException
*/
public void addWidget(IThinkcapWidget widget) throws ThinkcapWidgetException {
if (widget != null) {
PageSpecs pageSpecs = (PageSpecs) context.get("pageSpecs");
String s = widget.outputWidget(pageSpecs);
context.put(widget.getName(), s);
}
}
public static IThinkcapWidget constructWidget(String widgetClass, String widgetName,
ThinkcapSession session, Object ...parameters)
throws ThinkcapException {
IWidgetPublisher constructor = constructors.get(widgetClass);
if (constructor == null) {
// it's just a template widget - don't need anything done
return null;
}
IThinkcapWidget ret = constructor.constructWidget(widgetName, session, parameters);
if (ret != null)
ret.setWidgetClass(constructor.getWidgetClassName());
return ret;
}
public static void registerWidgetPublisher(IWidgetPublisher constructor) {
constructors.put(constructor.getWidgetClassName(), constructor);
}
public static void processWidgetResponse(HashMap<String, String> args, VelocityContext lctx,
String widgetClass, String widgetName) throws ThinklabException {
IWidgetPublisher ic = constructors.get(widgetClass);
if (ic == null)
return;
ic.processWidgetResponse(widgetName, args, lctx);
}
}
See more files for this project here