@Override
public void create(final PTInstruction create, final UIService uiService) {
final String signature = create.getString(PROPERTY.FACTORY);
final Map<String, JavascriptAddOnFactory> factories = uiService.getJavascriptAddOnFactory();
final JavascriptAddOnFactory factory = factories.get(signature);
if (factory == null) throw new RuntimeException("AddOn factory not found for signature: " + signature + ". Addons registered: " + factories.keySet());
final JSONObject params = new JSONObject();
params.put("id", new JSONString(create.getObjectID().toString()));
if (create.containsKey(PROPERTY.NATIVE)) {
final JSONObject data = create.getObject(PROPERTY.NATIVE);
params.put("data", data);
}
if (create.containsKey(PROPERTY.WIDGET)) {
final long widgetID = create.getLong(PROPERTY.WIDGET);
final PTWidget<?> object = (PTWidget<?>) uiService.getPTObject(widgetID);
final Widget cast = object.cast();
final Element element = cast.getElement();
params.put("widgetID", new JSONString("" + widgetID));
params.put("widgetElement", new JSONObject(element));
cast.addAttachHandler(new AttachEvent.Handler() {
@Override
public void onAttachOrDetach(final AttachEvent event) {
if (event.isAttached()) {
addOn.onAttach(true);
} else {
addOn.onAttach(false);
}
}
});
}
addOn = factory.newAddOn(params.getJavaScriptObject());
}