Search in sources :

Example 1 with Pair

use of com.servoy.j2db.util.Pair in project servoy-client by Servoy.

the class MainPage method getWindowScopeBrowserScript.

/**
 * Creates a browser javascript snippet that, when evaluated in the scriptExecutionMP (current request's main page) it will point to this MainPage's window object in the browser.
 * @return the code snippet pointing to this MainPage's browser window from the scriptExecutionMP (current request's MainPage). The snippet will end with "." if it's not an empty String. It will return null if a way to access the desired window scope was not found.
 */
@SuppressWarnings("nls")
private Pair<String, MainPage> getWindowScopeBrowserScript(MainPage scriptExecutionMP) {
    if (scriptExecutionMP == null)
        return null;
    if (scriptExecutionMP != this) {
        // generate a JS script that when ran inside browser for requestMP it will point to this main page;
        // find common parent and then generate script
        ArrayList<MainPage> requestMPsParents = new ArrayList<MainPage>();
        ArrayList<MainPage> thisMPsParents = new ArrayList<MainPage>();
        MainPage mp = scriptExecutionMP;
        while (mp != null && mp != this) {
            requestMPsParents.add(mp);
            mp = mp.callingContainer;
        }
        if (mp == this)
            requestMPsParents.add(this);
        mp = this;
        while (mp != null && !requestMPsParents.contains(mp)) {
            thisMPsParents.add(mp);
            mp = mp.callingContainer;
        }
        int idx = requestMPsParents.indexOf(mp);
        if (idx != -1) {
            // found common parent; idx is the index in request page's parent array
            boolean ok = true;
            String goToCorrectScopeScript = "";
            for (int i = 0; i < idx && ok; i++) {
                // window.opener/parent depending on window type
                mp = requestMPsParents.get(i);
                if (mp.isShowingInDialog() || mp.isClosingAsDivPopup()) {
                    goToCorrectScopeScript += "window.parent.";
                } else if (mp.isShowingInWindow() || mp.closingAsWindow) {
                    goToCorrectScopeScript += "window.opener.";
                } else
                    // some windows in the window chain are closed...
                    ok = false;
            }
            for (int i = thisMPsParents.size() - 1; i >= 0 && ok; i--) {
                mp = thisMPsParents.get(i);
                if (mp.isShowingInDialog() || mp.isClosingAsDivPopup()) {
                    ServoyDivDialog dw = mp.callingContainer.divDialogs.get(mp.getPageMapName());
                    if (dw != null)
                        goToCorrectScopeScript += "Wicket.DivWindow.openWindows['" + dw.getJSId() + "'].content.contentWindow.";
                    else
                        ok = false;
                } else if (mp.isShowingInWindow() || mp.closingAsWindow) {
                    goToCorrectScopeScript += MainPage.getValidJSVariableName(mp.getPageMapName()) + ".";
                } else
                    // some windows in the window chain are closed...
                    ok = false;
            }
            if (ok) {
                return new Pair<String, MainPage>(goToCorrectScopeScript, scriptExecutionMP);
            } else {
                Debug.log("Cannot trigger ajax request between pages. Closed page.");
            }
        }
    } else {
        // the request's page is actually this page; so we are already in the correct scope
        return new Pair<String, MainPage>("", this);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Point(java.awt.Point) Pair(com.servoy.j2db.util.Pair)

Example 2 with Pair

use of com.servoy.j2db.util.Pair in project servoy-client by Servoy.

the class SessionClient method getContext.

private Pair<IRecordInternal, FormScope> getContext(String contextName) {
    try {
        String visibleFormName = contextName;
        String dataContext = null;
        if (contextName != null) {
            // $NON-NLS-1$
            StringTokenizer tk = new StringTokenizer(contextName, ".");
            String token = tk.nextToken();
            if (// $NON-NLS-1$
            token.equals("forms") && tk.hasMoreTokens()) {
                visibleFormName = tk.nextToken();
                if (tk.hasMoreTokens())
                    token = tk.nextToken();
            }
            if (// $NON-NLS-1$
            !token.equals("foundset")) {
                // todo why is this always also assigned to the datacontext?
                // shouldnt the above if be: if (token.equals("foundset") && st.hasMoreTokes()) dataContext == st.nextToken();
                // because now this data context will just be set to a form name if the contextName is just a form. (which in many cases it is defined like that)
                dataContext = token;
            }
        }
        if (visibleFormName == null) {
            IForm tmp = ((FormManager) getFormManager()).getCurrentForm();
            if (tmp != null)
                visibleFormName = tmp.getName();
        }
        if (visibleFormName != null) {
            // just overwrite the above assignment again if the datacontext is really also the form, so it wont be used later on.
            if (Utils.stringSafeEquals(visibleFormName, dataContext)) {
                dataContext = null;
            }
            FormController fp = ((FormManager) getFormManager()).leaseFormPanel(visibleFormName);
            if (!fp.isShowingData()) {
                if (fp.wantEmptyFoundSet()) {
                    if (fp.getFormModel() != null)
                        fp.getFormModel().clear();
                } else {
                    fp.loadAllRecords();
                }
            }
            IFoundSetInternal fs = fp.getFoundSet();
            if (fs != null) {
                int idx = fs.getSelectedIndex();
                if (idx < 0)
                    idx = 0;
                IRecordInternal r = fs.getRecord(idx);
                if (r != null) {
                    if (dataContext != null) {
                        IFoundSetInternal rfs = r.getRelatedFoundSet(dataContext);
                        // rfs can be null because dataContext can just be a anything see above
                        if (rfs != null) {
                            r = rfs.getRecord(rfs.getSelectedIndex());
                        }
                    }
                    return new Pair<IRecordInternal, FormScope>(r, fp.getFormScope());
                }
            }
            return new Pair<IRecordInternal, FormScope>(null, fp.getFormScope());
        }
    } catch (Exception e) {
        Debug.error(e);
    }
    return null;
}
Also used : FormController(com.servoy.j2db.FormController) IFormController(com.servoy.j2db.IFormController) StringTokenizer(java.util.StringTokenizer) IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) FormManager(com.servoy.j2db.FormManager) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) IForm(com.servoy.j2db.IForm) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServoyException(com.servoy.j2db.util.ServoyException) IOException(java.io.IOException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Pair(com.servoy.j2db.util.Pair)

Example 3 with Pair

use of com.servoy.j2db.util.Pair in project servoy-client by Servoy.

the class TemplateGenerator method getFormHTMLAndCSS.

public static Pair<String, String> getFormHTMLAndCSS(Solution solution, Form form, IServiceProvider sp, String formInstanceName) throws RepositoryException, RemoteException {
    if (form == null)
        return null;
    final IRepository repository = ApplicationServerRegistry.get().getLocalRepository();
    boolean enableAnchoring = sp != null ? Utils.getAsBoolean(sp.getRuntimeProperties().get("enableAnchors")) : Utils.getAsBoolean(Settings.getInstance().getProperty("servoy.webclient.enableAnchors", Boolean.TRUE.toString()));
    String overriddenStyleName = null;
    Pair<String, ArrayList<Pair<String, String>>> retval = formCache.getFormAndCssPair(form, formInstanceName, overriddenStyleName, repository);
    Form f = form;
    FlattenedSolution fsToClose = null;
    try {
        if (retval == null) {
            if (f.getExtendsID() > 0) {
                FlattenedSolution fs = sp == null ? null : sp.getFlattenedSolution();
                if (fs == null) {
                    try {
                        IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
                        fsToClose = fs = new FlattenedSolution(solution.getSolutionMetaData(), new AbstractActiveSolutionHandler(as) {

                            @Override
                            public IRepository getRepository() {
                                return repository;
                            }
                        });
                    } catch (RepositoryException e) {
                        Debug.error("Couldn't create flattened form for the template generator", e);
                    }
                }
                f = fs.getFlattenedForm(f);
                if (f == null) {
                    Debug.log("TemplateGenerator couldn't get a FlattenedForm for " + form + ", solution closed?");
                    f = form;
                }
            }
            StringBuffer html = new StringBuffer();
            TextualCSS css = new TextualCSS();
            IFormLayoutProvider layoutProvider = FormLayoutProviderFactory.getFormLayoutProvider(sp, solution, f, formInstanceName);
            int viewType = layoutProvider.getViewType();
            layoutProvider.renderOpenFormHTML(html, css);
            int startY = 0;
            Iterator<Part> parts = f.getParts();
            while (parts.hasNext()) {
                Part part = parts.next();
                int endY = part.getHeight();
                if (Part.rendersOnlyInPrint(part.getPartType())) {
                    startY = part.getHeight();
                    // is never shown (=printing only)
                    continue;
                }
                Color bgColor = ComponentFactory.getPartBackground(sp, part, f);
                if (part.getPartType() == Part.BODY && (viewType == FormController.TABLE_VIEW || viewType == FormController.LOCKED_TABLE_VIEW || viewType == IForm.LIST_VIEW || viewType == FormController.LOCKED_LIST_VIEW)) {
                    layoutProvider.renderOpenTableViewHTML(html, css, part);
                    // tableview == bodypart
                    createCellBasedView(f, f, html, css, layoutProvider.needsHeaders(), startY, endY, bgColor, sp, viewType, enableAnchoring, startY, endY);
                    layoutProvider.renderCloseTableViewHTML(html);
                } else {
                    layoutProvider.renderOpenPartHTML(html, css, part);
                    placePartElements(f, startY, endY, html, css, bgColor, enableAnchoring, sp);
                    layoutProvider.renderClosePartHTML(html, part);
                }
                startY = part.getHeight();
            }
            layoutProvider.renderCloseFormHTML(html);
            retval = new Pair<String, ArrayList<Pair<String, String>>>(html.toString(), css.getAsSelectorValuePairs());
            formCache.putFormAndCssPair(form, formInstanceName, overriddenStyleName, repository, retval);
        }
        Map<String, String> formIDToMarkupIDMap = null;
        if (sp instanceof IApplication) {
            Map runtimeProps = sp.getRuntimeProperties();
            Map<WebForm, Map<String, String>> clientFormsIDToMarkupIDMap = (Map<WebForm, Map<String, String>>) runtimeProps.get("WebFormIDToMarkupIDCache");
            if (clientFormsIDToMarkupIDMap == null) {
                clientFormsIDToMarkupIDMap = new WeakHashMap<WebForm, Map<String, String>>();
                runtimeProps.put("WebFormIDToMarkupIDCache", clientFormsIDToMarkupIDMap);
            }
            IForm wfc = ((IApplication) sp).getFormManager().getForm(formInstanceName);
            if (wfc instanceof FormController) {
                IFormUIInternal wf = ((FormController) wfc).getFormUI();
                if (wf instanceof WebForm) {
                    if (!((WebForm) wf).isUIRecreated())
                        formIDToMarkupIDMap = clientFormsIDToMarkupIDMap.get(wf);
                    if (formIDToMarkupIDMap == null) {
                        ArrayList<Pair<String, String>> formCSS = retval.getRight();
                        ArrayList<String> selectors = new ArrayList<String>(formCSS.size());
                        for (Pair<String, String> formCSSEntry : formCSS) selectors.add(formCSSEntry.getLeft());
                        formIDToMarkupIDMap = getWebFormIDToMarkupIDMap((WebForm) wf, selectors);
                        clientFormsIDToMarkupIDMap.put((WebForm) wf, formIDToMarkupIDMap);
                    }
                }
            }
        }
        String webFormCSS = getWebFormCSS(retval.getRight(), formIDToMarkupIDMap);
        // string the formcss/solutionname/ out of the url.
        webFormCSS = StripHTMLTagsConverter.convertMediaReferences(webFormCSS, solution.getName(), new ResourceReference("media"), "", false).toString();
        return new Pair<String, String>(retval.getLeft(), webFormCSS);
    } finally {
        if (fsToClose != null) {
            fsToClose.close(null);
        }
    }
}
Also used : IForm(com.servoy.j2db.IForm) Form(com.servoy.j2db.persistence.Form) WebForm(com.servoy.j2db.server.headlessclient.WebForm) WebForm(com.servoy.j2db.server.headlessclient.WebForm) ArrayList(java.util.ArrayList) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IForm(com.servoy.j2db.IForm) AbstractActiveSolutionHandler(com.servoy.j2db.AbstractActiveSolutionHandler) ResourceReference(org.apache.wicket.ResourceReference) IFormUIInternal(com.servoy.j2db.IFormUIInternal) Pair(com.servoy.j2db.util.Pair) FormController(com.servoy.j2db.FormController) Color(java.awt.Color) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Point(java.awt.Point) IApplication(com.servoy.j2db.IApplication) Part(com.servoy.j2db.persistence.Part) IRepository(com.servoy.j2db.persistence.IRepository) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) TreeMap(java.util.TreeMap)

Example 4 with Pair

use of com.servoy.j2db.util.Pair in project servoy-client by Servoy.

the class DataAdapterList method valueChanged.

@Override
public void valueChanged(ModificationEvent e) {
    if (record != null && e != null && e.getName() != null) {
        for (Entry<IWebFormController, String> relatedFormEntry : getVisibleChildFormCopy().entrySet()) {
            IWebFormController relatedForm = relatedFormEntry.getKey();
            String relatedFormRelation = relatedFormEntry.getValue();
            boolean depends = false;
            Relation[] relations = getApplication().getFlattenedSolution().getRelationSequence(relatedFormRelation);
            for (int r = 0; !depends && relations != null && r < relations.length; r++) {
                try {
                    IDataProvider[] primaryDataProviders = relations[r].getPrimaryDataProviders(getApplication().getFlattenedSolution());
                    for (int p = 0; !depends && primaryDataProviders != null && p < primaryDataProviders.length; p++) {
                        depends = e.getName().equals(primaryDataProviders[p].getDataProviderID());
                    }
                } catch (RepositoryException ex) {
                    Debug.log(ex);
                }
            }
            if (depends) {
                relatedForm.loadRecords(record.getRelatedFoundSet(relatedFormRelation, ((BasicFormController) relatedForm).getDefaultSortColumns()));
            }
        }
    }
    if (!findMode && this.record != null && e.getName() != null && (e.getRecord() == null || e.getRecord() == this.record) && this.lookupDependency.containsKey(e.getName()) && !(this.record instanceof PrototypeState)) {
        if (this.record.startEditing()) {
            List<Pair<String, String>> lookupDepencies = this.lookupDependency.get(e.getName());
            for (Pair<String, String> dependency : lookupDepencies) {
                Object obj = this.record.getValue(dependency.getRight());
                this.record.setValue(dependency.getLeft(), obj);
            }
        }
    }
    // one of the relations could be changed make sure they are recreated.
    createRelationListeners();
    if (getForm().isFormVisible()) {
        pushChangedValues(e.getName(), true);
    }
}
Also used : RepositoryException(com.servoy.j2db.persistence.RepositoryException) IDataProvider(com.servoy.j2db.persistence.IDataProvider) Relation(com.servoy.j2db.persistence.Relation) JSONObject(org.json.JSONObject) BaseWebObject(org.sablo.BaseWebObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) PrototypeState(com.servoy.j2db.dataprocessing.PrototypeState) BasicFormController(com.servoy.j2db.BasicFormController) Pair(com.servoy.j2db.util.Pair)

Example 5 with Pair

use of com.servoy.j2db.util.Pair in project servoy-client by Servoy.

the class ComponentFactory method getCSSPairStyleForForm.

public static Pair<IStyleSheet, IStyleRule> getCSSPairStyleForForm(IServiceProvider sp, Form form) {
    IStyleSheet styleSheet = getCSSStyleForForm(sp, form);
    IStyleRule styleRule = getCSSRuleForForm(sp, form);
    if (styleSheet != null && styleRule != null) {
        return new Pair<IStyleSheet, IStyleRule>(styleSheet, styleRule);
    }
    return null;
}
Also used : IStyleSheet(com.servoy.j2db.util.IStyleSheet) IStyleRule(com.servoy.j2db.util.IStyleRule) Pair(com.servoy.j2db.util.Pair)

Aggregations

Pair (com.servoy.j2db.util.Pair)29 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 Map (java.util.Map)8 List (java.util.List)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 SafeArrayList (com.servoy.j2db.util.SafeArrayList)6 Point (java.awt.Point)6 RepositoryException (com.servoy.j2db.persistence.RepositoryException)5 Collectors.toList (java.util.stream.Collectors.toList)5 FlattenedSolution (com.servoy.j2db.FlattenedSolution)4 CalculationDependencyData (com.servoy.j2db.dataprocessing.RowManager.RowFireNotifyChange.CalculationDependencyData)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 PropertyDescription (org.sablo.specification.PropertyDescription)4 AbstractActiveSolutionHandler (com.servoy.j2db.AbstractActiveSolutionHandler)2 FormController (com.servoy.j2db.FormController)2 IForm (com.servoy.j2db.IForm)2 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)2 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)2 IRepository (com.servoy.j2db.persistence.IRepository)2