use of com.servoy.j2db.scripting.ElementScope in project servoy-client by Servoy.
the class FormController method getJSApplicationNames.
/**
* @param src
* @param function
* @param useFormAsEventSourceEventually
*/
@Override
protected FormAndComponent getJSApplicationNames(Object source, Function function, boolean useFormAsEventSourceEventually) {
Object src = source;
if (src == null) {
Object window = application.getRuntimeWindowManager().getCurrentWindowWrappedObject();
if (window != null) {
src = ((Window) window).getFocusOwner();
while (src != null && !(src instanceof IComponent)) {
src = ((Component) src).getParent();
}
// Test if this component really comes from the the controllers UI.
if (src instanceof Component) {
Container container = ((Component) src).getParent();
while (container != null && !(container instanceof IFormUIInternal<?>)) {
container = container.getParent();
}
if (container != getFormUI()) {
// if not then this is not the trigger element for this form.
src = null;
}
}
}
}
Scriptable thisObject = null;
if (src instanceof IComponent && src instanceof IScriptableProvider) {
// $NON-NLS-1$
Object esObj = formScope.get("elements", formScope);
if (esObj != Scriptable.NOT_FOUND) {
ElementScope es = (ElementScope) esObj;
String name = ((IComponent) src).getName();
if (name != null && name.length() != 0) {
Object o = es.get(name, es);
if (o instanceof Scriptable) {
thisObject = (Scriptable) o;
}
}
if (thisObject == null) {
if (name == null || name.length() == 0) {
name = ComponentFactory.WEB_ID_PREFIX + System.currentTimeMillis();
// Check Web components always have a name! Because name can't be set
((IComponent) src).setName(name);
}
IScriptable scriptObject = ((IScriptableProvider) src).getScriptObject();
if (scriptObject != null) {
try {
Context.enter();
JavaMembers jm = ScriptObjectRegistry.getJavaMembers(scriptObject.getClass(), ScriptableObject.getTopLevelScope(formScope));
thisObject = new NativeJavaObject(formScope, scriptObject, jm);
es.setLocked(false);
es.put(name, es, thisObject);
es.setLocked(true);
} finally {
Context.exit();
}
}
}
}
}
if (src == null && useFormAsEventSourceEventually)
src = formScope;
return new FormAndComponent(src, getName());
}
use of com.servoy.j2db.scripting.ElementScope in project servoy-client by Servoy.
the class SwingForm method makeElementsScriptObject.
/**
* @see com.servoy.j2db.IFormUIInternal#makeElementsScriptObject(org.mozilla.javascript.Scriptable)
*/
public ElementScope makeElementsScriptObject(Scriptable fs, Map<String, Object[]> hmChildrenJavaMembers, IDataRenderer[] dataRenderers, IView controller) {
ElementScope es = new ElementScope(fs);
int counter = 0;
for (int i = FormController.FORM_RENDERER + 1; i < dataRenderers.length; i++) {
IDataRenderer dr = dataRenderers[i];
if (dr == null)
continue;
Object[] comps = null;
Object[] compsRenderer = null;
if (dr instanceof DataRenderer) {
comps = ((DataRenderer) dr).getComponents();
if (i == FormController.FORM_EDITOR && dataRenderers[FormController.FORM_RENDERER] != null) {
compsRenderer = ((DataRenderer) dataRenderers[FormController.FORM_RENDERER]).getComponents();
}
} else if (dr instanceof TableView) {
TableView tv = (TableView) dr;
List<CellAdapter> nonViewableColumns = tv.getNonViewableColumns();
int componentCount = tv.getColumnCount() + nonViewableColumns.size();
comps = new Component[componentCount];
compsRenderer = new Component[componentCount];
CellAdapter[] tableCellAdapters = new CellAdapter[componentCount];
for (int j = 0; j < componentCount; j++) {
tableCellAdapters[j] = (j < tv.getColumnCount()) ? (CellAdapter) tv.getCellEditor(0, j) : nonViewableColumns.get(j - tv.getColumnCount());
comps[j] = tableCellAdapters[j].getEditor();
compsRenderer[j] = tableCellAdapters[j].getRenderer();
}
for (int j = 0; j < comps.length; j++) {
if (compsRenderer[j] instanceof Component && !((Component) compsRenderer[j]).isVisible()) {
tv.getColumnModel().removeColumn(tableCellAdapters[j]);
}
}
} else if (dr instanceof WebMarkupContainer) {
comps = new Object[((WebMarkupContainer) dr).size()];
Iterator<?> it = ((WebMarkupContainer) dr).iterator();
int j = 0;
while (it.hasNext()) {
comps[j++] = it.next();
}
}
counter = registerComponentsToScope(fs, es, counter, comps, compsRenderer, (Component) controller, hmChildrenJavaMembers);
}
es.setLocked(true);
return es;
}
use of com.servoy.j2db.scripting.ElementScope in project servoy-client by Servoy.
the class WebForm method makeElementsScriptObject.
/**
* @see com.servoy.j2db.IFormUIInternal#makeElementsScriptObject(org.mozilla.javascript.Scriptable, java.util.Map, com.servoy.j2db.ui.IDataRenderer[],
* com.servoy.j2db.IView)
*/
public ElementScope makeElementsScriptObject(Scriptable fs, Map<String, Object[]> hmChildrenJavaMembers, IDataRenderer[] dataRenderers, IView v) {
ElementScope es = new ElementScope(fs);
int counter = 0;
for (int i = FormController.FORM_RENDERER + 1; i < dataRenderers.length; i++) {
IDataRenderer dr = dataRenderers[i];
if (dr == null)
continue;
Object[] comps = null;
if (dr instanceof WebMarkupContainer) {
comps = new Object[((WebMarkupContainer) dr).size()];
Iterator it = ((WebMarkupContainer) dr).iterator();
int j = 0;
while (it.hasNext()) {
comps[j++] = it.next();
}
}
counter = registerComponentsToScope(fs, es, counter, comps, hmChildrenJavaMembers, dr);
}
if (v instanceof WebCellBasedView) {
Object[] comps = ((WebCellBasedView) v).getComponents();
counter = registerComponentsToScope(fs, es, counter, comps, hmChildrenJavaMembers, v);
}
es.setLocked(true);
return es;
}
use of com.servoy.j2db.scripting.ElementScope in project servoy-client by Servoy.
the class JSEventType method fillJSEvent.
/**
* @param event Event that needs to be filled
* @param jsonObject The json data for that has the event data.
* @param webObject The webObject element (WEbFormComponent or WebFormUI)
* @param controller Optional the controller object if the caller knows this already
* @return
*/
@SuppressWarnings("nls")
public static void fillJSEvent(JSBaseEvent event, JSONObject jsonObject, BaseWebObject webObject, IWebFormController controller) {
event.setType(jsonObject.optString("eventType"));
String formName = controller != null ? controller.getName() : "";
String elementName = "";
if (webObject instanceof WebFormComponent) {
elementName = ((WebFormComponent) webObject).getFormElement().getRawName();
if (elementName == null)
elementName = "";
if (formName.isEmpty()) {
BaseWebObject parentWebObject = ((WebFormComponent) webObject).getParent();
while (parentWebObject != null && !(parentWebObject instanceof WebFormUI)) {
parentWebObject = ((WebFormComponent) parentWebObject).getParent();
}
if (parentWebObject instanceof WebFormUI) {
formName = parentWebObject.getName();
}
}
}
if (formName.isEmpty()) {
formName = jsonObject.optString("formName");
}
if (formName.isEmpty() && webObject instanceof WebFormUI) {
// executeInlineScript with an event in params tells a DAL to execute it and it gives a formui as context to the fromJSON conversion
// for JSEventType problem is that it can give any formName from the client (the component/service can give anything there as an arg); or, if the function
// (sent to client before through "function" type) that will be used to execute the script is a global/scope function
// then formName can be null and in that case the WebFormUI will be the main form or the window (not the most nested one)
// so this is just a fallback and we do give priority to the form name determined on client through $window.createJSEvent(...) an put into
// "jsonObject" (see if above) - to target the correct form - closest one to event
formName = webObject.getName();
}
if (elementName.isEmpty()) {
elementName = jsonObject.optString("elementName");
}
if (!formName.isEmpty())
event.setFormName(formName);
if (!elementName.isEmpty())
event.setElementName(elementName);
if (!formName.isEmpty()) {
INGApplication application = ((IContextProvider) webObject).getDataConverterContext().getApplication();
IWebFormController formController = controller != null ? controller : application.getFormManager().getForm(formName);
if (formController != null) {
FormScope formScope = formController.getFormScope();
if (formScope != null) {
ElementScope elementsScope = (ElementScope) formScope.get("elements", null);
if (elementsScope != null) {
Object scriptableElement = !elementName.isEmpty() ? elementsScope.get(elementName, null) : null;
if (scriptableElement != null && scriptableElement != Scriptable.NOT_FOUND) {
event.setSource(scriptableElement);
} else if (webObject instanceof WebFormComponent) {
// quickly create a scriptable wrapper around the component so that the source can be set to a value that we expect.
FormElement fe = ((WebFormComponent) webObject).getFormElement();
RuntimeWebComponent runtimeComponent = new RuntimeWebComponent((WebFormComponent) webObject, webObject.getSpecification());
if (fe.isLegacy() || ((fe.getForm().getView() == IForm.LIST_VIEW || fe.getForm().getView() == FormController.LOCKED_LIST_VIEW || fe.getForm().getView() == FormController.TABLE_VIEW || fe.getForm().getView() == FormController.LOCKED_TABLE_VIEW) && fe.getTypeName().startsWith("svy-"))) {
// add legacy behavior
runtimeComponent.setPrototype(new RuntimeLegacyComponent((WebFormComponent) webObject));
}
event.setSource(runtimeComponent);
}
}
}
}
}
try {
if (jsonObject.has("x"))
event.setLocation(new Point(jsonObject.optInt("x"), jsonObject.optInt("y")));
if (jsonObject.has("modifiers"))
event.setModifiers(jsonObject.optInt("modifiers"));
if (jsonObject.has("data"))
event.setData(jsonObject.opt("data"));
if (jsonObject.has("timestamp"))
event.setTimestamp(new Timestamp(jsonObject.getLong("timestamp")));
else
event.setTimestamp(new Date());
} catch (Exception e) {
Debug.error("error setting event properties from " + jsonObject + ", for component: " + elementName + " on form " + formName, e);
}
}
use of com.servoy.j2db.scripting.ElementScope in project servoy-client by Servoy.
the class WebFormUI method removeComponentFromElementsScope.
// this should be the opposite of formUI.contributeComponentToElementsScope(...)
public void removeComponentFromElementsScope(FormElement fe, WebObjectSpecification componentSpec, WebFormComponent component) {
ElementScope elementsScope = getElementsScope();
if (elementsScope != null) {
if (!FormElement.ERROR_BEAN.equals(componentSpec.getName()) && (!fe.getName().startsWith("svy_") || ((fe.getPersistIfAvailable() instanceof IFormElement) && ((IFormElement) fe.getPersistIfAvailable()).getGroupID() != null))) {
if (!fe.getName().startsWith("svy_")) {
RuntimeWebComponent runtimeComponent = (RuntimeWebComponent) elementsScope.remove(fe.getRawName());
elementsScope.removeIndexByValue(runtimeComponent);
}
String groupID = fe.getPersistIfAvailable() instanceof IFormElement ? ((IFormElement) fe.getPersistIfAvailable()).getGroupID() : null;
if (groupID != null) {
if (groups == null)
groups = new HashMap<String, RuntimeWebGroup>(4);
RuntimeWebGroup group = groups.get(groupID);
if (group != null) {
String groupName = FormElementGroup.getName(groupID);
group.remove(component);
if (group.getComponentCount() == 0)
elementsScope.remove(groupName);
groups.remove(groupID);
}
}
}
} else {
Debug.error(new RuntimeException("Trying to remove component from a non-existent elements scope for form: " + getName()));
}
}
Aggregations