use of com.servoy.j2db.scripting.IExecutingEnviroment in project servoy-client by Servoy.
the class NGClient method createScriptEngine.
@Override
protected IExecutingEnviroment createScriptEngine() {
IExecutingEnviroment scriptEngine = super.createScriptEngine();
WebObjectSpecification[] serviceSpecifications = WebServiceSpecProvider.getSpecProviderState().getAllWebComponentSpecifications();
PluginScope scope = (PluginScope) scriptEngine.getSolutionScope().get("plugins", scriptEngine.getSolutionScope());
scope.setLocked(false);
for (WebObjectSpecification serviceSpecification : serviceSpecifications) {
if (serviceSpecification.getApiFunctions().size() != 0 || serviceSpecification.getAllPropertiesNames().size() != 0) {
scope.put(ClientService.convertToJSName(serviceSpecification.getName()), scope, new WebServiceScriptable(this, serviceSpecification, scriptEngine.getSolutionScope()));
}
}
scope.setLocked(true);
return scriptEngine;
}
use of com.servoy.j2db.scripting.IExecutingEnviroment in project servoy-client by Servoy.
the class BasicFormController method executeFunction.
@SuppressWarnings("nls")
protected Object executeFunction(Function f, Object[] args, Scriptable scope, Scriptable thisObject, boolean saveData, Object src, boolean testFindMode, boolean focusEvent, String methodKey, boolean executeWhenFieldValidationFailed, boolean useFormAsEventSourceEventually, boolean throwException) throws Exception {
if (// only run certain methods in find
!(testFindMode && isInFindMode())) {
// this is a semi saveData , we do NOT want the record go out of edit(!) and is updated in db
if (saveData) {
application.getFoundSetManager().getEditRecordList().prepareForSave(false);
}
if (f != null) {
if (!executeWhenFieldValidationFailed && Boolean.TRUE.equals(application.getRuntimeProperties().get(IServiceProvider.RT_LASTFIELDVALIDATIONFAILED_FLAG))) {
if (Debug.tracing()) {
Debug.trace("Function not executed because a field is marked invalid");
}
return null;
}
FormAndComponent formAndComponent = getJSApplicationNames(src, f, useFormAsEventSourceEventually);
try {
currentFormExecutingFunctionCount.incrementAndGet();
Object[] newArgs = args;
if (formAndComponent != null) {
// for use of deprecated aplication.getMethodTriggerElementName() and aplication.getMethodTriggerFormName()
IExecutingEnviroment scriptEngine = application.getScriptEngine();
if (scriptEngine instanceof ScriptEngine) {
((ScriptEngine) scriptEngine).getJSApplication().pushLastNames(formAndComponent);
}
if (methodKey != null) {
// add form event if needed
MethodTemplate methodTemplate = MethodTemplate.getTemplate(null, methodKey);
if (methodTemplate != null) {
MethodArgument[] methodArguments = methodTemplate.getArguments();
for (int i = 0; methodArguments != null && i < methodArguments.length; i++) {
if (methodArguments[i].getType() == ArgumentType.JSEvent) {
// method template declares an event argument
if (args == null || args.length <= i || args[i] == null) {
// no event argument there yet, insert a form event
JSEvent event = getJSEvent(formAndComponent.src, methodKey);
if (args == null || args.length <= i) {
newArgs = new Object[i + 1];
if (args != null) {
System.arraycopy(args, 0, newArgs, 0, args.length);
}
}
newArgs[i] = event;
}
break;
}
}
}
}
}
if (newArgs != null && newArgs.length > 0) {
for (Object newArg : newArgs) {
if (newArg instanceof JSEvent) {
JSEvent event = (JSEvent) newArg;
if (formScope != null && event.getSource() instanceof IComponent && ((IComponent) event.getSource()).getName() != null) {
Object elementScope = formScope.get("elements");
if (elementScope instanceof Scriptable) {
Object elementSrc = ((Scriptable) elementScope).get(((IComponent) event.getSource()).getName(), (Scriptable) elementScope);
if (elementSrc != null) {
if (elementSrc instanceof ITwoNativeJavaObject) {
Object scriptable = event.getSource();
if (scriptable instanceof IScriptableProvider) {
scriptable = ((IScriptableProvider) scriptable).getScriptObject();
}
((ITwoNativeJavaObject) elementSrc).setRealObject(scriptable);
}
event.setSource(elementSrc);
}
}
}
break;
}
}
}
return application.getScriptEngine().executeFunction(f, scope, thisObject, newArgs, focusEvent, throwException);
} finally {
currentFormExecutingFunctionCount.decrementAndGet();
if (formAndComponent != null) {
IExecutingEnviroment scriptEngine = application.getScriptEngine();
if (scriptEngine instanceof ScriptEngine) {
((ScriptEngine) scriptEngine).getJSApplication().popLastStackNames(formAndComponent);
}
}
// after a script clear the unchanged records so that no records keep hanging around.
if (!focusEvent && !"onRecordEditStopMethodID".equals(methodKey) && !"onRenderMethodID".equals(methodKey) && application.getFoundSetManager() != null) {
application.getFoundSetManager().getEditRecordList().removeUnChangedRecords(false, false);
}
}
}
}
return null;
}
use of com.servoy.j2db.scripting.IExecutingEnviroment in project servoy-client by Servoy.
the class BasicFormController method initForJSUsage.
// if the js init is not done , do!
public synchronized JSForm initForJSUsage() {
if (formScope == null) {
if (destroyed) {
Debug.error("Calling initForJSUsage on a destroyed form: " + this, new RuntimeException("Calling initForJSUsage on a destroyed form: " + this));
return null;
}
Context.enter();
try {
IExecutingEnviroment se = application.getScriptEngine();
if (se != null) {
SolutionScope solScope = application.getScriptEngine().getSolutionScope();
// $NON-NLS-1$
initForJSUsage((CreationalPrototype) solScope.get("forms", solScope));
} else {
// saw this happen on MAC debug smart client - edit the value in a combobox (change it) then directly click on the close window button;
// shutDown executed first (windowClosing) - setting solution to null, then the combo item state change (generated by focus lost) event was triggered
// $NON-NLS-1$
Debug.log("Trying to initForJSUsage with null script engine - probably a method was about to be invoked post-shutdown", null);
}
} catch (Exception ex) {
// $NON-NLS-1$
application.reportError(application.getI18NMessage("servoy.formPanel.error.initScriptEngine"), ex);
} finally {
Context.exit();
}
}
return scriptableForm;
}
use of com.servoy.j2db.scripting.IExecutingEnviroment in project servoy-client by Servoy.
the class DataAdapterList method destroy.
/**
*/
public void destroy() {
if (currentRecord != null) {
// With prototype you can still get global foundsets
// setRecord(new PrototypeState(currentRecord.getParentFoundSet()), true);
setRecord(null, false);
}
if (formController != null && !formController.isDestroyed() && formController.getFormScope() != null) {
formController.getFormScope().getModificationSubject().removeModificationListener(this);
}
IExecutingEnviroment er = application.getScriptEngine();
if (er != null) {
SolutionScope ss = er.getSolutionScope();
if (ss != null) {
ScopesScope gs = ss.getScopesScope();
if (gs != null) {
gs.getModificationSubject().removeModificationListener(this);
}
}
}
if (servoyAwareBeans != null) {
for (IServoyAwareBean b : servoyAwareBeans) {
try {
if (b instanceof IDestroyable) {
((IDestroyable) b).destroy();
}
} catch (RuntimeException e) {
// never make the app break on faulty beans
Debug.error(e);
}
}
}
servoyAwareBeans = null;
if (relatedDataAdapters != null) {
for (IDisplayRelatedData drd : relatedDataAdapters) {
drd.destroy();
}
}
relatedDataAdapters = null;
if (dataDisplays != null) {
for (IDisplayData dd : dataDisplays) {
if (dd instanceof IDestroyable) {
((IDestroyable) dd).destroy();
}
}
}
dataDisplays = null;
if (dataAdapters != null) {
for (IDataAdapter da : dataAdapters.values()) {
if (da instanceof IDestroyable) {
((IDestroyable) da).destroy();
}
}
}
dataAdapters = null;
currentDisplay = null;
visible = false;
destroyed = true;
if (currentRecord != null) {
// $NON-NLS-1$
Debug.error("After destroy there is still a current record in DataAdapterList of formcontroller: " + formController, new RuntimeException());
currentRecord.removeModificationListener(this);
}
}
use of com.servoy.j2db.scripting.IExecutingEnviroment in project servoy-client by Servoy.
the class FoundSet method sort.
/**
* Sorts the foundset based on the given record comparator function.
* Tries to preserve selection based on primary key. If first record is selected or cannot select old record it will select first record after sort.
* The comparator function is called to compare
* two records, that are passed as arguments, and
* it will return -1/0/1 if the first record is less/equal/greater
* then the second record.
*
* The function based sorting does not work with printing.
* It is just a temporary in-memory sort.
*
* NOTE: starting with 7.2 release this function doesn't save the data anymore
*
* @sample
* %%prefix%%foundset.sort(mySortFunction);
*
* function mySortFunction(r1, r2)
* {
* var o = 0;
* if(r1.id < r2.id)
* {
* o = -1;
* }
* else if(r1.id > r2.id)
* {
* o = 1;
* }
* return o;
* }
*
* @param recordComparisonFunction record comparator function
*/
@JSFunction
@JSSignature(arguments = { Function.class })
public void sort(Object recordComparisonFunction) {
if (recordComparisonFunction instanceof Function) {
final Function func = (Function) recordComparisonFunction;
final IExecutingEnviroment scriptEngine = fsm.getApplication().getScriptEngine();
final Scriptable recordComparatorScope = func.getParentScope();
sort(new Comparator<Object[]>() {
public int compare(Object[] o1, Object[] o2) {
try {
Object compareResult = scriptEngine.executeFunction(func, recordComparatorScope, recordComparatorScope, new Object[] { getRecord(o1), getRecord(o2) }, false, true);
double cmp = Utils.getAsDouble(compareResult, true);
return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
} catch (Exception ex) {
Debug.error(ex);
}
return 0;
}
});
}
}
Aggregations