use of org.mozilla.javascript.NativeJavaObject in project servoy-client by Servoy.
the class WebForm method registerComponentsToScope.
private int registerComponentsToScope(Scriptable fs, ElementScope es, int counter, Object[] comps, Map<String, Object[]> hmChildrenJavaMembers, Object parent) {
if (comps != null) {
for (Object comp : comps) {
if (comp instanceof WebCellBasedView) {
WebCellBasedView portal = (WebCellBasedView) comp;
counter = registerComponentsToScope(fs, es, counter, portal.getComponents(), hmChildrenJavaMembers, comp);
}
String name = null;
if (comp instanceof WrapperContainer) {
comp = ((WrapperContainer) comp).getDelegate();
}
if (comp instanceof IComponent) {
name = ((IComponent) comp).getName();
} else if (comp instanceof java.awt.Component) {
name = ((java.awt.Component) comp).getName();
}
if (comp instanceof WebImageBeanHolder) {
comp = ((WebImageBeanHolder) comp).getDelegate();
} else if (comp instanceof WebBeanHolder) {
comp = ((WebBeanHolder) comp).getDelegate();
}
String groupName = FormElementGroup.getName((String) formController.getComponentProperty(comp, ComponentFactory.GROUPID_COMPONENT_PROPERTY));
Object scriptable = comp;
if (comp instanceof IScriptableProvider)
scriptable = ((IScriptableProvider) comp).getScriptObject();
JavaMembers jm = ScriptObjectRegistry.getJavaMembers(scriptable.getClass(), ScriptableObject.getTopLevelScope(fs));
// $NON-NLS-1$
boolean named = name != null && !name.equals("") && !name.startsWith(ComponentFactory.WEB_ID_PREFIX);
if (groupName != null || named) {
try {
Scriptable s;
if (parent instanceof WebCellBasedView) {
s = new CellNativeJavaObject(fs, comp, jm, (WebCellBasedView) parent);
} else {
s = new NativeJavaObject(fs, scriptable, jm);
}
if (named) {
es.put(name, fs, s);
es.put(counter++, fs, s);
hmChildrenJavaMembers.put(name, new Object[] { jm, scriptable });
}
if (groupName != null) {
Object group = es.get(groupName, fs);
if (group == Scriptable.NOT_FOUND) {
group = new NativeJavaObject(fs, new RuntimeGroup(groupName), ScriptObjectRegistry.getJavaMembers(RuntimeGroup.class, ScriptableObject.getTopLevelScope(fs)));
es.put(groupName, fs, group);
es.put(counter++, fs, group);
}
if (scriptable instanceof IRuntimeComponent && group instanceof NativeJavaObject && ((NativeJavaObject) group).unwrap() instanceof RuntimeGroup) {
((RuntimeGroup) (((NativeJavaObject) group).unwrap())).addScriptBaseMethodsObj((IRuntimeComponent) scriptable);
}
}
} catch (Throwable ex) {
// incase classdefnot founds are thrown for beans,applets/plugins
Debug.error(ex);
}
}
}
}
return counter;
}
use of org.mozilla.javascript.NativeJavaObject in project servoy-client by Servoy.
the class FormManager method setCurrentContainer.
/**
* @param mainContainer
*/
public void setCurrentContainer(IMainContainer mainContainer, String name) {
if (mainContainer != null) {
currentContainer = mainContainer;
if (name != null) {
// reset it in the containers (must be done for the webclient)
containers.put(name, mainContainer);
}
} else {
currentContainer = getMainContainer(null);
}
enableCmds(true);
FormController formController = currentContainer.getController();
if (formController != null) {
IExecutingEnviroment scriptEngine = application.getScriptEngine();
if (scriptEngine != null) {
SolutionScope ss = scriptEngine.getSolutionScope();
Context.enter();
try {
// $NON-NLS-1$
ss.put("currentcontroller", ss, new NativeJavaObject(ss, formController.initForJSUsage(), new InstanceJavaMembers(ss, JSForm.class)));
} finally {
Context.exit();
}
}
}
application.getRuntimeWindowManager().setCurrentWindowName(name);
}
use of org.mozilla.javascript.NativeJavaObject in project servoy-client by Servoy.
the class BasicFormController method initForJSUsage.
/**
* Initialize this FormController(or related classes/methods) to be used in javascript
*/
public synchronized JSForm initForJSUsage(CreationalPrototype creationalPrototype) {
if (formScope == null) {
try {
// make scope for state delegation via prototype mechanism
List<Form> forms = application.getFlattenedSolution().getFormHierarchy(form);
List<ISupportScriptProviders> scriptProviders = new ArrayList<ISupportScriptProviders>();
for (Form frm : forms) {
scriptProviders.add(new RuntimeSupportScriptProviders(application, frm));
}
formScope = new FormScope(this, scriptProviders.toArray(new ISupportScriptProviders[scriptProviders.size()]));
if (formModel != null) {
ITable formTable = application.getFoundSetManager().getTable(form.getDataSource());
formScope.setPrototype(new SelectedRecordScope(this, formTable == null ? null : application.getScriptEngine().getTableScope(formTable)));
// $NON-NLS-1$
formScope.putWithoutFireChange("foundset", formModel);
}
// create JS place holder for this object
scriptableForm = new BasicFormController.JSForm(this);
// set parent scope
NativeJavaObject formObject = new NativeJavaObject(formScope, scriptableForm, ScriptObjectRegistry.getJavaMembers(FormController.JSForm.class, formScope));
// $NON-NLS-1$
formScope.putWithoutFireChange("controller", formObject);
// register the place holder 'scriptableForm' in CreationalPrototype scope
creationalPrototype.setLocked(false);
// $NON-NLS-1$
creationalPrototype.put(((Integer) creationalPrototype.get("length", creationalPrototype)).intValue(), creationalPrototype, formScope);
creationalPrototype.put(getName(), creationalPrototype, formScope);
creationalPrototype.setLocked(true);
formScope.createVars();
} catch (Exception ex) {
// $NON-NLS-1$
application.reportError(application.getI18NMessage("servoy.formPanel.error.initFormScope") + ": " + getName(), ex);
return null;
}
}
return scriptableForm;
}
use of org.mozilla.javascript.NativeJavaObject in project servoy-client by Servoy.
the class ServoyWrapFactory method wrap.
/**
* @see org.mozilla.javascript.WrapFactory#wrap(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, java.lang.Object, java.lang.Class)
*/
@Override
public Object wrap(Context cx, Scriptable scope, Object obj, Class staticType) {
if (application.getSolution() == null) {
throw new ExitScriptException("killing current script, client/solution already terminated");
}
if (obj == null || obj == Undefined.instance || obj instanceof Scriptable || obj instanceof String || obj instanceof CharSequence || obj instanceof Number || obj instanceof Boolean) {
return obj;
}
if (obj instanceof Date) {
return cx.newObject(scope, "Date", new Object[] { new Double(NativeDate.convertToUTCMillisFromJava(((Date) obj).getTime())) });
}
if (obj instanceof DbIdentValue || obj instanceof UUID) {
return new NativeJavaObject(scope, obj, ScriptObjectRegistry.getJavaMembers(obj.getClass(), null));
}
if (cx != null) {
if (obj instanceof JSConvertedMap<?, ?>) {
Scriptable newObject = null;
JSConvertedMap<?, ?> map = (JSConvertedMap<?, ?>) obj;
if (map.getConstructorName() != null) {
newObject = cx.newObject(scope, map.getConstructorName());
} else {
newObject = cx.newObject(scope);
}
Iterator<?> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<?, ?> next = (Entry<?, ?>) iterator.next();
Object key = next.getKey();
Object value = next.getValue();
if (value != null) {
value = wrap(cx, newObject, value, value.getClass());
}
if (key instanceof Integer) {
newObject.put(((Integer) key).intValue(), newObject, value);
} else if (key instanceof String) {
newObject.put((String) key, newObject, value);
} else {
// $NON-NLS-1$ //$NON-NLS-2$
Debug.error("Try to create a JSConvertedMap->NativeObject with a key that isnt a string or integer:" + key + " for value: " + value);
}
}
return newObject;
}
if (obj.getClass() == JSONObject.class) {
JSONObject json = (JSONObject) obj;
Scriptable newObject = cx.newObject(scope);
Iterator<String> iterator = json.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = null;
try {
value = ServoyJSONObject.jsonNullToNull(json.get(key));
} catch (JSONException e) {
Debug.error(e);
}
if (value != null) {
value = wrap(cx, newObject, value, value.getClass());
}
newObject.put(key, newObject, value);
}
return newObject;
}
if (obj.getClass() == JSONArray.class) {
JSONArray array = (JSONArray) obj;
Scriptable newObject = cx.newObject(scope, "Array");
for (int i = 0; i < array.length(); i++) {
Object value = null;
try {
value = array.get(i);
} catch (JSONException e) {
Debug.error(e);
}
if (value != null) {
value = wrap(cx, newObject, value, value.getClass());
}
newObject.put(i, newObject, value);
}
return newObject;
}
}
if (obj instanceof IDataSet) {
return new JSDataSet(application, (IDataSet) obj);
}
if (obj instanceof FormController) {
throw new IllegalArgumentException(// $NON-NLS-1$
"Bad practice: FormController cant be wrapped to javascript (for example not usable as argument in Scheduler plugin)");
}
// if it is a none primitive array
if (obj.getClass().isArray() && !obj.getClass().getComponentType().isPrimitive()) {
// then convert the array to a NativeArray, first convert all the elements of the array itself.
Object[] source = (Object[]) obj;
Object[] array = new Object[source.length];
for (int i = 0; i < source.length; i++) {
Object src = source[i];
array[i] = wrap(cx, scope, src, src != null ? src.getClass() : null);
}
return cx.newArray(scope, array);
}
return super.wrap(cx, scope, obj, staticType);
}
use of org.mozilla.javascript.NativeJavaObject in project gdmatrix by gdmatrix.
the class ScanFileFilter method evaluateFilter.
private boolean evaluateFilter(String filter, FileInfo fileInfo) {
Context ctx = ContextFactory.getGlobal().enterContext();
try {
Scriptable scope = new DefaultScriptable(ctx);
scope.put("file", scope, new NativeJavaObject(scope, fileInfo, FileInfo.class));
Object result = ctx.evaluateString(scope, filter, "<filter>", 1, null);
return Context.toBoolean(result);
} catch (Exception ex) {
return false;
} finally {
Context.exit();
}
}
Aggregations