Search in sources :

Example 1 with IApplication

use of com.servoy.j2db.IApplication 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 2 with IApplication

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

the class SwingForm method printXML.

/**
 * @see com.servoy.j2db.IFormUIInternal#printXML(boolean)
 */
public String printXML(boolean printCurrentRecordOnly) {
    IApplication application = formController.getApplication();
    IFoundSetInternal fs = formController.getFoundSet();
    try {
        // $NON-NLS-1$
        application.blockGUI(Messages.getString("servoy.formPanel.status.xmlPrinting"));
        if (printCurrentRecordOnly) {
            fs = fs.copyCurrentRecordFoundSet();
        }
        FormPreviewPanel fpp = new FormPreviewPanel(application, formController, fs);
        fpp.process();
        StringWriter w = new StringWriter();
        ((PageList) fpp.getPageable()).toXML(w);
        fpp.destroy();
        return w.toString();
    } catch (Throwable ex) {
        // $NON-NLS-1$
        application.reportError(Messages.getString("servoy.formPanel.error.printDocument"), ex);
    } finally {
        application.releaseGUI();
    }
    return null;
}
Also used : IApplication(com.servoy.j2db.IApplication) StringWriter(java.io.StringWriter) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) FormPreviewPanel(com.servoy.j2db.printing.FormPreviewPanel) PageList(com.servoy.j2db.printing.PageList)

Example 3 with IApplication

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

the class SwingForm method print.

/**
 * @see com.servoy.j2db.IFormUIInternal#print(boolean, boolean, boolean, java.awt.print.PrinterJob)
 */
public void print(boolean showDialogs, boolean printCurrentRecordOnly, boolean showPrinterSelectDialog, PrinterJob printerJob) {
    IFoundSetInternal fs = formController.getFoundSet();
    if (!printCurrentRecordOnly) {
        if (showDialogs) {
            int option = willingToPrint(fs);
            if (option == 2) {
                printCurrentRecordOnly = true;
            } else if (option == 1) {
                // cancel
                return;
            }
        }
    }
    IApplication application = formController.getApplication();
    try {
        // $NON-NLS-1$
        application.blockGUI(Messages.getString("servoy.formPanel.status.printProgress"));
        if (printCurrentRecordOnly) {
            fs = fs.copyCurrentRecordFoundSet();
        }
        FormPreviewPanel fpp = new FormPreviewPanel(application, formController, fs);
        fpp.process();
        PrintPreview.startPrinting(application, fpp.getPageable(), printerJob, formController.getPreferredPrinterName(), showPrinterSelectDialog, false);
        fpp.destroy();
    } catch (Exception ex) {
        // $NON-NLS-1$
        application.reportError(Messages.getString("servoy.formPanel.error.printDocument"), ex);
    } finally {
        application.releaseGUI();
    }
}
Also used : IApplication(com.servoy.j2db.IApplication) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) FormPreviewPanel(com.servoy.j2db.printing.FormPreviewPanel) Point(java.awt.Point)

Example 4 with IApplication

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

the class FunctionDefinition method exists.

/**
 * Test if the given methodName or formName do exist. Will return one of the {@link Exist} enums.
 * @since 5.2
 */
public Exist exists(IClientPluginAccess access) {
    final Exist[] retVal = new Exist[] { Exist.METHOD_NOT_FOUND };
    if (access instanceof ClientPluginAccessProvider) {
        final IApplication application = ((ClientPluginAccessProvider) access).getApplication();
        application.invokeAndWait(new Runnable() {

            public void run() {
                if (application.getSolution() != null) {
                    if (contextName.startsWith(ScriptVariable.SCOPES_DOT_PREFIX)) {
                        GlobalScope gs = application.getScriptEngine().getScopesScope().getGlobalScope(contextName.substring(ScriptVariable.SCOPES_DOT_PREFIX.length()));
                        if (gs != null && gs.get(methodName) instanceof Function) {
                            retVal[0] = Exist.METHOD_FOUND;
                        }
                    } else {
                        IFormController fp = application.getFormManager().leaseFormPanel(contextName);
                        if (fp == null) {
                            retVal[0] = Exist.FORM_NOT_FOUND;
                        } else if (fp.getFormScope().get(methodName, fp.getFormScope()) instanceof Function) {
                            retVal[0] = Exist.METHOD_FOUND;
                        }
                    }
                } else {
                    retVal[0] = Exist.NO_SOLUTION;
                }
            }
        });
    }
    return retVal[0];
}
Also used : ClientPluginAccessProvider(com.servoy.j2db.plugins.ClientPluginAccessProvider) Function(org.mozilla.javascript.Function) IApplication(com.servoy.j2db.IApplication) IFormController(com.servoy.j2db.IFormController)

Example 5 with IApplication

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

the class WebForm method print.

// public void print(boolean showDialogs, boolean printCurrentRecordOnly, boolean showPrinterSelectDialog, int zoomFactor, PrinterJob printerJob)
// {
// print(showDialogs, printCurrentRecordOnly, showPrinterSelectDialog, printerJob);
// }
/**
 * @see com.servoy.j2db.IFormUIInternal#print(boolean, boolean, boolean, java.awt.print.PrinterJob)
 */
public void print(boolean showDialogs, boolean printCurrentRecordOnly, boolean showPrinterSelectDialog, PrinterJob printerJob) {
    IFoundSetInternal fs = formController.getFoundSet();
    try {
        if (printCurrentRecordOnly) {
            fs = fs.copyCurrentRecordFoundSet();
        }
    } catch (ServoyException e1) {
        Debug.error(e1);
    }
    IApplication application = formController.getApplication();
    ByteArrayOutputStream baos = null;
    MainPage page = null;
    if (printerJob == null) {
        page = (MainPage) findPage();
        if (page == null) {
            IMainContainer tmp = ((FormManager) application.getFormManager()).getCurrentContainer();
            if (tmp instanceof MainPage)
                page = (MainPage) tmp;
        }
    // if "page" is still null then there is no wicket front-end for this client - so printing is not intended to reach the client; print on the server instead
    // (can happen for batch processors for example)
    }
    if (page != null) {
        baos = new ByteArrayOutputStream();
        StreamPrintServiceFactory[] factories = null;
        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
        ClassLoader savedClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(((PluginManager) application.getPluginManager()).getClassLoader());
            // $NON-NLS-1$
            factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, "application/pdf");
            if (factories == null || factories.length == 0) {
                // $NON-NLS-1$
                Debug.error("No suitable pdf printer found");
                return;
            }
        } finally {
            Thread.currentThread().setContextClassLoader(savedClassLoader);
        }
        try {
            FormPreviewPanel fpp = new FormPreviewPanel(application, formController, fs);
            // AWT stuff happens here, so execute it in the AWT Event Thread - else exceptions can occur
            // for example in JEditorPane while getting the preferred size & stuff
            processFppInAWTEventQueue(fpp, application);
            StreamPrintService sps = factories[0].getPrintService(baos);
            Doc doc = new SimpleDoc(fpp.getPageable(), flavor, null);
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            sps.createPrintJob().print(doc, pras);
            fpp.destroy();
        } catch (Exception ex) {
            // $NON-NLS-1$
            application.reportError(application.getI18NMessage("servoy.formPanel.error.printDocument"), ex);
        }
        // $NON-NLS-1$
        String contentType = "application/pdf";
        // BTW, "application/octet-stream" works for all browsers, but it is not that accurate
        if (// if it's not batch processor/jsp, because if it is, then getClientInfo() gives NullPointerException
        application.getApplicationType() != IApplication.HEADLESS_CLIENT) {
            ClientInfo info = Session.get().getClientInfo();
            if (info instanceof WebClientInfo) {
                String userAgent = ((WebClientInfo) info).getProperties().getNavigatorUserAgent();
                if (// $NON-NLS-1$
                userAgent != null && userAgent.toLowerCase().contains("safari")) {
                    // $NON-NLS-1$
                    contentType = "application/octet-stream";
                }
            }
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        String url = page.serveResource(formController.getName() + ".pdf", baos.toByteArray(), contentType, "attachment");
        // $NON-NLS-1$
        page.setShowURLCMD(url, "_self", null, 0, false);
    } else {
        try {
            FormPreviewPanel fpp = new FormPreviewPanel(application, formController, fs);
            // AWT stuff happens here, so execute it in the AWT Event Thread - else exceptions can occur
            // for example in JEditorPane while getting the preferred size & stuff
            processFppInAWTEventQueue(fpp, application);
            PrintPreview.startPrinting(application, fpp.getPageable(), printerJob, formController.getPreferredPrinterName(), false, true);
            fpp.destroy();
        } catch (Exception ex) {
            // $NON-NLS-1$
            application.reportError(application.getI18NMessage("servoy.formPanel.error.printDocument"), ex);
        }
    }
}
Also used : WebClientInfo(org.apache.wicket.protocol.http.request.WebClientInfo) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) FormPreviewPanel(com.servoy.j2db.printing.FormPreviewPanel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamPrintService(javax.print.StreamPrintService) ServoyException(com.servoy.j2db.util.ServoyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServoyException(com.servoy.j2db.util.ServoyException) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) IApplication(com.servoy.j2db.IApplication) SimpleDoc(javax.print.SimpleDoc) IBasicFormManager(com.servoy.j2db.IBasicFormManager) FormManager(com.servoy.j2db.FormManager) IMainContainer(com.servoy.j2db.IMainContainer) Doc(javax.print.Doc) SimpleDoc(javax.print.SimpleDoc) WebClientInfo(org.apache.wicket.protocol.http.request.WebClientInfo) ClientInfo(org.apache.wicket.request.ClientInfo) StreamPrintServiceFactory(javax.print.StreamPrintServiceFactory) DocFlavor(javax.print.DocFlavor) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Aggregations

IApplication (com.servoy.j2db.IApplication)18 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)6 QuerySelect (com.servoy.j2db.query.QuerySelect)4 ServoyException (com.servoy.j2db.util.ServoyException)4 RepositoryException (com.servoy.j2db.persistence.RepositoryException)3 FormPreviewPanel (com.servoy.j2db.printing.FormPreviewPanel)3 ArrayList (java.util.ArrayList)3 BaseQueryTable (com.servoy.base.query.BaseQueryTable)2 ApplicationException (com.servoy.j2db.ApplicationException)2 IFormController (com.servoy.j2db.IFormController)2 IDisplayData (com.servoy.j2db.dataprocessing.IDisplayData)2 ISwingFoundSet (com.servoy.j2db.dataprocessing.ISwingFoundSet)2 SortColumn (com.servoy.j2db.dataprocessing.SortColumn)2 DbIdentValue (com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue)2 Column (com.servoy.j2db.persistence.Column)2 Form (com.servoy.j2db.persistence.Form)2 IRepository (com.servoy.j2db.persistence.IRepository)2 Relation (com.servoy.j2db.persistence.Relation)2 Solution (com.servoy.j2db.persistence.Solution)2 Placeholder (com.servoy.j2db.query.Placeholder)2