Search in sources :

Example 6 with WebComponent

use of com.servoy.j2db.persistence.WebComponent in project servoy-client by Servoy.

the class JSBaseContainer method removeWebComponent.

/**
 * Removes a JSWebComponent that has the specified name. Returns true if removal was successful, false otherwise.
 *
 * @sample
 * var form = solutionModel.getForm('myform');
 * form.removeWebComponent('mybean')
 *
 * @param name the specified name of the JSWebComponent to be removed
 *
 * @return true if the JSWebComponent has been removed; false otherwise
 */
@ServoyClientSupport(mc = false, ng = true, wc = false, sc = false)
@JSFunction
public boolean removeWebComponent(String name) {
    if (name == null)
        return false;
    checkModification();
    Iterator<WebComponent> webComponents = getContainer().getWebComponents();
    while (webComponents.hasNext()) {
        WebComponent webComponent = webComponents.next();
        if (name.equals(webComponent.getName())) {
            getContainer().removeChild(webComponent);
            return true;
        }
    }
    return false;
}
Also used : WebComponent(com.servoy.j2db.persistence.WebComponent) JSFunction(org.mozilla.javascript.annotations.JSFunction) ServoyClientSupport(com.servoy.base.scripting.annotations.ServoyClientSupport)

Example 7 with WebComponent

use of com.servoy.j2db.persistence.WebComponent in project servoy-client by Servoy.

the class JSNGWebComponent method getJSONProperty.

@Override
public Object getJSONProperty(String propertyName) {
    WebComponent webComponent = getBaseComponent(false);
    JSONObject json = webComponent.getFlattenedJson();
    if (json == null)
        return Context.getUndefinedValue();
    Object value;
    WebObjectSpecification spec = WebComponentSpecProvider.getSpecProviderState().getWebComponentSpecification(webComponent.getTypeName());
    if (spec != null) {
        Pair<PropertyDescription, String> propAndName = getPropertyDescriptionAndName(propertyName, spec);
        if (!json.has(propAndName.getRight()) && propAndName.getLeft() != null && propAndName.getLeft().hasDefault()) {
            value = propAndName.getLeft().getDefaultValue();
        } else {
            value = json.opt(propAndName.getRight());
        }
        value = fromDesignToRhinoValue(value, propAndName.getLeft(), application, this, propertyName);
    // JSONArray and JSONObject are automatically wrapped when going to Rhino through ServoyWrapFactory, so no need to treat them specially here
    } else {
        value = json.opt(propertyName);
    }
    // so we need to make sure we always return a JSONObject.
    if (value instanceof ServoyJSONObject) {
        value = new JSONObject((ServoyJSONObject) value, ((ServoyJSONObject) value).keySet().toArray(new String[0]));
    } else if (value instanceof ServoyJSONArray) {
        ServoyJSONArray sArray = (ServoyJSONArray) value;
        JSONArray array = new JSONArray();
        for (int i = 0; i < sArray.length(); i++) {
            array.put(i, sArray.get(i));
        }
        value = sArray;
    }
    return value == null ? Context.getUndefinedValue() : ServoyJSONObject.jsonNullToNull(value);
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) WebComponent(com.servoy.j2db.persistence.WebComponent) WebObjectSpecification(org.sablo.specification.WebObjectSpecification) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ServoyJSONArray(com.servoy.j2db.util.ServoyJSONArray) JSONArray(org.json.JSONArray) ServoyJSONArray(com.servoy.j2db.util.ServoyJSONArray) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Example 8 with WebComponent

use of com.servoy.j2db.persistence.WebComponent in project servoy-client by Servoy.

the class JSNGWebComponent method isJSONPropertySet.

@Override
public boolean isJSONPropertySet(String propertyName) {
    WebComponent webComponent = getBaseComponent(false);
    JSONObject json = webComponent.getFlattenedJson();
    return json != null && json.has(propertyName);
}
Also used : WebComponent(com.servoy.j2db.persistence.WebComponent) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Example 9 with WebComponent

use of com.servoy.j2db.persistence.WebComponent in project servoy-client by Servoy.

the class JSNGWebComponent method resetHandler.

@Override
public void resetHandler(String handlerName) {
    WebComponent webComponent = getBaseComponent(false);
    WebObjectSpecification spec = WebComponentSpecProvider.getSpecProviderState().getWebComponentSpecification(webComponent.getTypeName());
    if (spec != null) {
        String name = handlerName;
        if (spec.getHandler(name) == null) {
            name = name + "MethodID";
        }
        if (spec.getHandler(name) != null) {
            webComponent.clearProperty(name);
            getBaseComponent(true).putMethodParameters(name, null, null);
        } else {
            int i = name.indexOf('.');
            if (i > 0) {
                String firstPart = name.substring(0, i);
                PropertyDescription property = spec.getProperty(firstPart);
                if (property != null && property.getType() instanceof IFormComponentType) {
                    // undefined means remove the property
                    Object convertedValue = fromRhinoToDesignValue(Context.getUndefinedValue(), property, application, this, handlerName);
                    webComponent.setProperty(firstPart, convertedValue);
                    return;
                }
            }
            Debug.log("Error: component " + webComponent.getTypeName() + " does not declare a handler named " + handlerName + ".");
        }
    }
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) WebComponent(com.servoy.j2db.persistence.WebComponent) WebObjectSpecification(org.sablo.specification.WebObjectSpecification) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IFormComponentType(com.servoy.j2db.util.IFormComponentType)

Example 10 with WebComponent

use of com.servoy.j2db.persistence.WebComponent in project servoy-client by Servoy.

the class JSBaseContainer method newWebComponent.

/**
 * Creates a new JSWebComponent (spec based component) object on the RESPONSIVE form.
 *
 * @sample
 * var form = solutionModel.newForm('newForm1', 'db:/server1/table1', null, true, 800, 600);
 * var container = myForm.getLayoutContainer("row1")
 * var bean = container.newWebComponent('bean','mypackage-testcomponent',1);
 *
 * @param name the specified name of the JSWebComponent object
 * @param type the webcomponent name as it appears in the spec
 * @param position the position of JSWebComponent object in its parent container
 *
 * @return a JSWebComponent object
 */
@ServoyClientSupport(mc = false, ng = true, wc = false, sc = false)
@JSFunction
public JSWebComponent newWebComponent(String name, String type, int position) {
    checkModification();
    try {
        AbstractContainer container = getContainer();
        Form form = (Form) container.getAncestor(IRepository.FORMS);
        if (form.isResponsiveLayout()) {
            if (name == null) {
                String componentName = type;
                int index = componentName.indexOf("-");
                if (index != -1) {
                    componentName = componentName.substring(index + 1);
                }
                // $NON-NLS-1$//$NON-NLS-2$
                componentName = componentName.replaceAll("-", "_");
                // $NON-NLS-1$
                name = componentName + "_" + id.incrementAndGet();
                IJSParent<?> parent = this;
                while (!(parent instanceof JSForm)) {
                    parent = parent.getJSParent();
                }
                if (parent instanceof JSForm) {
                    while (findComponent((JSForm) parent, name) != null) {
                        name = componentName + "_" + id.incrementAndGet();
                    }
                }
            }
            WebComponent webComponent = container.createNewWebComponent(IdentDocumentValidator.checkName(name), type);
            webComponent.setLocation(new Point(position, position));
            return createWebComponent(this, webComponent, application, true);
        } else {
            throw new RuntimeException("Form " + form.getName() + " is not responsive. Cannot create component without specifying the location and size.");
        }
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : WebComponent(com.servoy.j2db.persistence.WebComponent) AbstractContainer(com.servoy.j2db.persistence.AbstractContainer) Form(com.servoy.j2db.persistence.Form) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Point(java.awt.Point) Point(java.awt.Point) JSFunction(org.mozilla.javascript.annotations.JSFunction) ServoyClientSupport(com.servoy.base.scripting.annotations.ServoyClientSupport)

Aggregations

WebComponent (com.servoy.j2db.persistence.WebComponent)17 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)9 PropertyDescription (org.sablo.specification.PropertyDescription)9 WebObjectSpecification (org.sablo.specification.WebObjectSpecification)9 Form (com.servoy.j2db.persistence.Form)8 JSONObject (org.json.JSONObject)7 ArrayList (java.util.ArrayList)4 ServoyClientSupport (com.servoy.base.scripting.annotations.ServoyClientSupport)3 FlattenedForm (com.servoy.j2db.persistence.FlattenedForm)3 Dimension (java.awt.Dimension)3 JSFunction (org.mozilla.javascript.annotations.JSFunction)3 AbstractContainer (com.servoy.j2db.persistence.AbstractContainer)2 Field (com.servoy.j2db.persistence.Field)2 IPersist (com.servoy.j2db.persistence.IPersist)2 IPersistVisitor (com.servoy.j2db.persistence.IPersistVisitor)2 DataAdapterList (com.servoy.j2db.server.ngclient.DataAdapterList)2 FormElement (com.servoy.j2db.server.ngclient.FormElement)2 IDataAdapterList (com.servoy.j2db.server.ngclient.IDataAdapterList)2 ServoyDataConverterContext (com.servoy.j2db.server.ngclient.ServoyDataConverterContext)2 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)2