Search in sources :

Example 6 with AbstractContainer

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

the class JSBaseContainer method createLayoutContainer.

protected JSLayoutContainer createLayoutContainer(int x, int y, String spec, JSONObject config) {
    checkModification();
    try {
        AbstractContainer container = getContainer();
        Form form = (Form) container.getAncestor(IRepository.FORMS);
        if (form.isResponsiveLayout()) {
            String packageName = null;
            String specName = null;
            if (spec != null) {
                String[] split = spec.split("-");
                if (split.length == 1) {
                    specName = spec.trim();
                } else if (split.length == 2) {
                    specName = split[1].trim();
                    packageName = split[0].trim();
                } else {
                    Debug.warn("Illegal spec given: " + spec);
                }
            }
            if (specName == null || packageName == null) {
                if (container instanceof LayoutContainer) {
                    LayoutContainer parent = (LayoutContainer) (getContainer());
                    packageName = parent.getPackageName();
                    if (specName == null && parent.getAllowedChildren() != null && !parent.getAllowedChildren().isEmpty()) {
                        specName = parent.getAllowedChildren().get(0);
                        if (specName.contains("."))
                            specName = specName.split("\\.")[1];
                    }
                }
            }
            if (specName == null) {
                // the parent container could be a layout container or the form
                // check if we have a sibling of the container we want to create
                LayoutContainer sibling = container.getLayoutContainers().hasNext() ? container.getLayoutContainers().next() : null;
                if (sibling != null) {
                    packageName = sibling.getPackageName();
                    specName = sibling.getSpecName();
                }
            }
            LayoutContainer layoutContainer = getContainer().createNewLayoutContainer();
            layoutContainer.setLocation(new Point(x, y));
            layoutContainer.setPackageName(packageName);
            layoutContainer.setSpecName(specName);
            JSLayoutContainer jsLayoutContainer = application.getScriptEngine().getSolutionModifier().createLayoutContainer(this, layoutContainer);
            JSONObject conf = config;
            if (config == null) {
                Map<String, PackageSpecification<WebLayoutSpecification>> layoutSpecifications = WebComponentSpecProvider.getSpecProviderState().getLayoutSpecifications();
                if (packageName != null && specName != null && layoutSpecifications != null && layoutSpecifications.get(packageName) != null) {
                    WebLayoutSpecification layoutSpec = layoutSpecifications.get(packageName).getSpecification(specName);
                    if (layoutSpec != null && (layoutSpec.getConfig() instanceof JSONObject || layoutSpec.getConfig() instanceof String)) {
                        conf = layoutSpec.getConfig() instanceof String ? new JSONObject((String) layoutSpec.getConfig()) : ((JSONObject) layoutSpec.getConfig());
                    }
                }
            }
            return configLayoutContainer(jsLayoutContainer, layoutContainer, conf);
        } else {
            // check if form was just created with the solution model and suggest correct method to use
            if (application.getFlattenedSolution().getSolutionCopy().getAllObjectsAsList().contains(form) && !application.getSolution().getAllObjectsAsList().contains(form)) {
                throw new RuntimeException("Form " + form.getName() + " is not a responsive form, cannot add a layout container on it. Please use solutionModel.newForm('" + form.getName() + "',true) to create a responsive form.");
            }
            throw new RuntimeException("Form " + form.getName() + " is not responsive, cannot add a layout container on it. Please use a responsive form or create a responsive form with solutionModel.newForm(formName, true);");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : AbstractContainer(com.servoy.j2db.persistence.AbstractContainer) Form(com.servoy.j2db.persistence.Form) Point(java.awt.Point) RepositoryException(com.servoy.j2db.persistence.RepositoryException) WebLayoutSpecification(org.sablo.specification.WebLayoutSpecification) JSONObject(org.json.JSONObject) LayoutContainer(com.servoy.j2db.persistence.LayoutContainer) PackageSpecification(org.sablo.specification.PackageSpecification)

Example 7 with AbstractContainer

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

the class JSSolutionModel method getParentContainer.

private IJSParent<?> getParentContainer(IPersist persist) {
    IJSParent<?> parent = null;
    List<AbstractContainer> parentHierarchy = new ArrayList<AbstractContainer>();
    IPersist currentParent = persist.getParent();
    while (currentParent != null) {
        if (currentParent instanceof AbstractContainer) {
            parentHierarchy.add(0, (AbstractContainer) currentParent);
        }
        if (currentParent instanceof Form)
            break;
        currentParent = currentParent.getParent();
    }
    for (AbstractContainer container : parentHierarchy) {
        if (container instanceof LayoutContainer) {
            parent = new JSLayoutContainer(parent, application, (LayoutContainer) persist);
        }
        if (container instanceof Form) {
            parent = instantiateForm((Form) container, false);
        }
    }
    return parent;
}
Also used : AbstractContainer(com.servoy.j2db.persistence.AbstractContainer) IPersist(com.servoy.j2db.persistence.IPersist) Form(com.servoy.j2db.persistence.Form) ISMForm(com.servoy.j2db.solutionmodel.ISMForm) IBaseSMForm(com.servoy.base.solutionmodel.IBaseSMForm) LayoutContainer(com.servoy.j2db.persistence.LayoutContainer) ArrayList(java.util.ArrayList)

Example 8 with AbstractContainer

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

the class JSForm method getBeans.

/**
 * Returns all JSBeans of this form.
 *
 * @sample
 * var beans = myForm.getBeans();
 * for (var b in beans)
 * {
 * 	if (beans[b].name != null)
 * 		application.output(beans[b].name);
 * }
 *
 * @param returnInheritedElements boolean true to also return the elements from parent form
 * @return the list of all JSBeans on this forms
 */
@JSFunction
public JSBean[] getBeans(boolean returnInheritedElements) {
    List<JSBean> beans = new ArrayList<JSBean>();
    AbstractContainer form2use = returnInheritedElements ? getFlattenedContainer() : getContainer();
    Iterator<Bean> iterator = form2use.getBeans();
    while (iterator.hasNext()) {
        beans.add(new JSBean(this, iterator.next(), false));
    }
    return beans.toArray(new JSBean[beans.size()]);
}
Also used : AbstractContainer(com.servoy.j2db.persistence.AbstractContainer) ArrayList(java.util.ArrayList) Bean(com.servoy.j2db.persistence.Bean) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 9 with AbstractContainer

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

the class JSForm method getTabPanels.

/**
 * Returns all JSTabPanels of this form (optionally the ones from the parent form), including the ones without a name.
 *
 * @sample
 * var frm = solutionModel.getForm("myForm");
 * var tabPanels = frm.getTabPanels();
 * for (var i in tabPanels)
 * {
 *	var tp = tabPanels[i];
 *	if (tp.name != null)
 *		application.output("Tab " + tp.name + " has text " + tp.text);
 *	else
 *		application.output("Tab with text " + tp.text + " has no name");
 * }
 *
 * @param returnInheritedElements boolean true to also return the elements from parent form
 * @return an array of all JSTabPanel objects on this form
 */
@JSFunction
public JSTabPanel[] getTabPanels(boolean returnInheritedElements) {
    List<JSTabPanel> tabPanels = new ArrayList<JSTabPanel>();
    AbstractContainer form2use = returnInheritedElements ? getFlattenedContainer() : getContainer();
    Iterator<TabPanel> iterator = form2use.getTabPanels();
    while (iterator.hasNext()) {
        tabPanels.add(new JSTabPanel(this, iterator.next(), application, false));
    }
    return tabPanels.toArray(new JSTabPanel[tabPanels.size()]);
}
Also used : TabPanel(com.servoy.j2db.persistence.TabPanel) AbstractContainer(com.servoy.j2db.persistence.AbstractContainer) ArrayList(java.util.ArrayList) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 10 with AbstractContainer

use of com.servoy.j2db.persistence.AbstractContainer 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

AbstractContainer (com.servoy.j2db.persistence.AbstractContainer)13 ArrayList (java.util.ArrayList)10 JSFunction (org.mozilla.javascript.annotations.JSFunction)9 Form (com.servoy.j2db.persistence.Form)4 Point (java.awt.Point)4 ServoyClientSupport (com.servoy.base.scripting.annotations.ServoyClientSupport)3 IPersist (com.servoy.j2db.persistence.IPersist)3 LayoutContainer (com.servoy.j2db.persistence.LayoutContainer)3 GraphicalComponent (com.servoy.j2db.persistence.GraphicalComponent)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 WebComponent (com.servoy.j2db.persistence.WebComponent)2 IBaseSMForm (com.servoy.base.solutionmodel.IBaseSMForm)1 AbstractBase (com.servoy.j2db.persistence.AbstractBase)1 BaseComponent (com.servoy.j2db.persistence.BaseComponent)1 Bean (com.servoy.j2db.persistence.Bean)1 Field (com.servoy.j2db.persistence.Field)1 IBasicWebComponent (com.servoy.j2db.persistence.IBasicWebComponent)1 IFormElement (com.servoy.j2db.persistence.IFormElement)1 ISupportBounds (com.servoy.j2db.persistence.ISupportBounds)1 ISupportChilds (com.servoy.j2db.persistence.ISupportChilds)1