Search in sources :

Example 1 with JavaScriptFunction

use of com.vaadin.ui.JavaScriptFunction in project VaadinUtils by rlsutton1.

the class JSCallWithReturnValue method call.

void call(final JavaScriptCallback<JsonArray> callback) {
    final Stopwatch timer = Stopwatch.createStarted();
    final ScheduledFuture<?> future = createTimeoutHook();
    JavaScript.getCurrent().addFunction(hookName, new JavaScriptFunction() {

        private static final long serialVersionUID = 1L;

        boolean done = false;

        @Override
        public void call(JsonArray arguments) {
            try {
                if (timer.elapsed(TimeUnit.MILLISECONDS) > EXPECTED_RESPONSE_TIME_MS) {
                    logger.warn(jsToExecute + "\n\nResponded after {}ms", timer.elapsed(TimeUnit.MILLISECONDS));
                }
                logger.debug("Handling response for " + hookName);
                if (!done) {
                    done = true;
                    callback.callback(arguments);
                } else {
                    logger.warn("This appears to have been a duplicate callback, ignoring it!");
                }
            } catch (Exception e) {
                logger.error(e, e);
                logger.error(trace, trace);
            } finally {
                future.cancel(false);
                removeHooks(hookName, errorHookName);
            }
        }
    });
    final String wrappedJs = wrapJSInTryCatch(jsToExecute);
    setupErrorHook(future);
    JavaScript.getCurrent().execute(wrappedJs);
}
Also used : JsonArray(elemental.json.JsonArray) JavaScriptFunction(com.vaadin.ui.JavaScriptFunction) Stopwatch(com.google.common.base.Stopwatch)

Example 2 with JavaScriptFunction

use of com.vaadin.ui.JavaScriptFunction in project VaadinUtils by rlsutton1.

the class JSCallWithReturnValue method callBlind.

void callBlind(final JavaScriptCallback<Void> javaScriptCallback) {
    final Stopwatch timer = Stopwatch.createStarted();
    final ScheduledFuture<?> future = createTimeoutHook();
    JavaScript.getCurrent().addFunction(hookName, new JavaScriptFunction() {

        boolean done = false;

        private static final long serialVersionUID = 1L;

        @Override
        public void call(JsonArray arguments) {
            logger.debug("Handling response for " + hookName);
            if (!done) {
                done = true;
                javaScriptCallback.callback(null);
            } else {
                logger.warn("This appears to have been a duplicate callback, ignoring it!");
            }
            future.cancel(false);
            removeHooks(hookName, errorHookName);
            if (timer.elapsed(TimeUnit.MILLISECONDS) > EXPECTED_RESPONSE_TIME_MS) {
                logger.warn(jsToExecute + "\n\nResponded after {}ms", timer.elapsed(TimeUnit.MILLISECONDS));
            }
        }
    });
    setupErrorHook(future);
    JavaScript.getCurrent().execute(wrapJSInTryCatchBlind(jsToExecute));
}
Also used : JsonArray(elemental.json.JsonArray) JavaScriptFunction(com.vaadin.ui.JavaScriptFunction) Stopwatch(com.google.common.base.Stopwatch)

Example 3 with JavaScriptFunction

use of com.vaadin.ui.JavaScriptFunction in project VaadinUtils by rlsutton1.

the class JSCallWithReturnValue method setupErrorHook.

private void setupErrorHook(final ScheduledFuture<?> future) {
    trace = new JavaScriptException("Java Script Invoked From Here, JS:" + jsToExecute);
    JavaScript.getCurrent().addFunction(errorHookName, new JavaScriptFunction() {

        private static final long serialVersionUID = 1L;

        @Override
        public void call(JsonArray arguments) {
            try {
                String value = arguments.getString(0);
                logger.error(jsToExecute + " -> resulted in the error: " + value, trace);
                Exception ex = new JavaScriptException(trace.getMessage() + " , JS Cause: " + value, trace);
                ErrorWindow.showErrorWindow(ex);
            } catch (Exception e) {
                ErrorWindow.showErrorWindow(trace);
            } finally {
                future.cancel(false);
                JavaScript.getCurrent().removeFunction(hookName);
                JavaScript.getCurrent().removeFunction(errorHookName);
            }
        }
    });
}
Also used : JsonArray(elemental.json.JsonArray) JavaScriptFunction(com.vaadin.ui.JavaScriptFunction)

Example 4 with JavaScriptFunction

use of com.vaadin.ui.JavaScriptFunction in project VaadinUtils by rlsutton1.

the class JasperReportLayout method initScreen.

protected void initScreen(SplitPanel panel) {
    this.setSizeFull();
    splitPanel = panel;
    this.addComponent(splitPanel.getComponent());
    splitPanel.setFirstComponent((AbstractComponent) getOptionsPanel());
    splash = new VerticalLayout();
    splash.setMargin(true);
    Label titleLabel = new Label("<h1>" + reportProperties.getReportTitle() + "</h1>");
    titleLabel.setContentMode(ContentMode.HTML);
    splash.addComponent(titleLabel);
    Label splashLabel = new Label("<font size='4' >Set the desired filters and click a print button to generate a report</font>");
    splashLabel.setContentMode(ContentMode.HTML);
    splitPanel.setSecondComponent(splash);
    // generate the report immediately if there are no visible filters
    if (!builder.hasFilters()) {
        splashLabel = new Label("<font size='4' >Please wait whilst we generate your report</font>");
        splashLabel.setContentMode(ContentMode.HTML);
        // disable the buttons and any filters
        printButton.setEnabled(false);
        exportButton.setEnabled(false);
        showButton.setEnabled(false);
        for (ExpanderComponent componet : components) {
            componet.getComponent().setEnabled(false);
        }
        // what follows is a horrible hack...
        // if we create the progress dialog at the same time as the popup
        // report window the progress dialog will be behind the popup report
        // window.
        // so I've created a refresher, and 1 seconds after the popup report
        // window opens we kick of the report generation which creates the
        // progress dialog then, which allows it to be in front.
        UI.getCurrent().setPollInterval(500);
        UI.getCurrent().addPollListener(new PollListener() {

            private static final long serialVersionUID = 1L;

            @Override
            public void poll(PollEvent event) {
                try {
                    UI.getCurrent().setPollInterval(-1);
                    UI.getCurrent().removePollListener(this);
                    generateReport(reportProperties.getDefaultFormat(), JasperReportLayout.this.builder.getReportParameters());
                } catch (Exception e) {
                    logger.catching(e);
                    Notification.show("Error", e.getMessage(), Type.ERROR_MESSAGE);
                }
            }
        });
    }
    splash.addComponent(splashLabel);
    JavaScript.getCurrent().addFunction("au.com.noojee.reportDrillDown", new JavaScriptFunction() {

        // expected syntax of a call to this javascript hook method
        // 
        // window.parent.au.com.noojee.reportDrillDown(
        // {
        // 'reportFileName':
        // 'CallDetailsPerTeamAgentPerHour_CallDetails.jasper',
        // 'reportTitle': 'Call Details Per Team Agent Per Hour'
        // },
        // {
        // 'ReportParameterStartDate'='$P{StartDate}',
        // 'ReportParameterEndDate'='$P{EndDate}',
        // 'ReportParameterExtension'='$F{loginid}',
        // 'ReportParameterTeamId'='$P{TeamId}',
        // 'ReportParameterHour'='$F{Day}.toString()'
        // }
        // 
        // );
        private static final long serialVersionUID = 1L;

        @Override
        public void call(JsonArray arguments) {
            try {
                JsonObject args = arguments.getObject(0);
                String subReportFileName = args.getString("ReportFileName");
                String subTitle = args.getString("ReportTitle");
                JsonObject params = arguments.getObject(1);
                List<ReportParameter<?>> subFilters = new LinkedList<>();
                boolean insitue = false;
                String[] itr = params.keys();
                for (String key : itr) {
                    if (key.equalsIgnoreCase("ReportParameterInsitue")) {
                        insitue = true;
                    } else {
                        subFilters.add(new ReportParameterConstant<>(key, params.getString(key), key, params.getString(key)));
                    }
                }
                if (!insitue) {
                    new JasperReportPopUp(new ChildJasperReportProperties(reportProperties, subTitle, subReportFileName, subFilters));
                } else {
                    generateReport(OutputFormat.HTML, subFilters);
                }
            } catch (Exception e) {
                logger.error(arguments.toString());
                logger.error(e, e);
            }
        }
    });
}
Also used : ExpanderComponent(au.com.vaadinutils.jasper.filter.ExpanderComponent) Label(com.vaadin.ui.Label) JsonObject(elemental.json.JsonObject) PollListener(com.vaadin.event.UIEvents.PollListener) PollEvent(com.vaadin.event.UIEvents.PollEvent) JsonArray(elemental.json.JsonArray) JavaScriptFunction(com.vaadin.ui.JavaScriptFunction) VerticalLayout(com.vaadin.ui.VerticalLayout) List(java.util.List) LinkedList(java.util.LinkedList) ReportParameterConstant(au.com.vaadinutils.jasper.parameter.ReportParameterConstant)

Aggregations

JavaScriptFunction (com.vaadin.ui.JavaScriptFunction)4 JsonArray (elemental.json.JsonArray)4 Stopwatch (com.google.common.base.Stopwatch)2 ExpanderComponent (au.com.vaadinutils.jasper.filter.ExpanderComponent)1 ReportParameterConstant (au.com.vaadinutils.jasper.parameter.ReportParameterConstant)1 PollEvent (com.vaadin.event.UIEvents.PollEvent)1 PollListener (com.vaadin.event.UIEvents.PollListener)1 Label (com.vaadin.ui.Label)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1 JsonObject (elemental.json.JsonObject)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1