Search in sources :

Example 6 with Frame

use of com.teamdev.jxbrowser.frame.Frame in project convertigo by convertigo.

the class ApplicationComponentEditor method createBrowser.

private void createBrowser(Composite parent) {
    browserScroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
    browserScroll.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
    browserScroll.setExpandHorizontal(true);
    browserScroll.setExpandVertical(true);
    Composite canvas = new Composite(browserScroll, SWT.NONE);
    browserScroll.setContent(canvas);
    GridLayout gl = new GridLayout(1, false);
    gl.marginBottom = gl.marginTop = gl.marginLeft = gl.marginRight = gl.marginHeight = gl.marginWidth = gl.horizontalSpacing = gl.verticalSpacing = 0;
    canvas.setLayout(gl);
    c8oBrowser = new C8oBrowser(canvas, SWT.NONE, applicationEditorInput.application.getProject());
    browserGD = new GridData(SWT.CENTER, SWT.CENTER, true, true);
    c8oBrowser.setLayoutData(browserGD);
    browser = c8oBrowser.getBrowser();
    debugUrl = c8oBrowser.getDebugUrl();
    browserInterface = new ApplicationComponentBrowserImpl();
    browser.set(InjectJsCallback.class, params -> {
        String url = params.frame().browser().url();
        if (baseUrl != null && url.startsWith(baseUrl)) {
            try {
                Frame frame = params.frame();
                JsObject sessionStorage = frame.executeJavaScript("sessionStorage");
                frame.executeJavaScript("sessionStorage.setItem('_c8ocafsession_storage_mode', 'session');\n" + "navigator.__defineGetter__('userAgent', function(){ return '" + deviceOS.agent() + "'});\n" + IOUtils.toString(getClass().getResourceAsStream("inject.js"), "UTF-8"));
                sessionStorage.call("setItem", "_c8ocafsession_storage_mode", "session");
                if (!dataset.equals("none")) {
                    String json = FileUtils.readFileToString(new File(datasetDir, dataset + ".json"), "UTF-8");
                    sessionStorage.call("setItem", "_c8ocafsession_storage_data", json);
                } else {
                    sessionStorage.call("setItem", "_c8ocafsession_storage_data", null);
                }
                JsObject window = frame.executeJavaScript("window");
                window.putProperty("java", browserInterface);
            } catch (Exception e) {
                Engine.logStudio.info("onScriptContextCreate failed for '" + url + "' with baseUrl '" + baseUrl + "': " + e.getMessage());
            }
        }
        // browser.setZoomLevel(zoomFactor.zoomLevel());
        return Response.proceed();
    });
    browser.set(ShowContextMenuCallback.class, (params, tell) -> {
        com.teamdev.jxbrowser.ui.Point location = params.location();
        highlightPoint(location.x(), location.y());
        tell.close();
    });
    browser.engine().permissions().set(RequestPermissionCallback.class, (params, tell) -> {
        tell.grant();
    });
}
Also used : JsObject(com.teamdev.jxbrowser.js.JsObject) Frame(com.teamdev.jxbrowser.frame.Frame) Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) C8oBrowser(com.twinsoft.convertigo.eclipse.swt.C8oBrowser) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) DatabaseObjectFoundException(com.twinsoft.convertigo.engine.DatabaseObjectFoundException) JSONException(org.codehaus.jettison.json.JSONException) GridLayout(org.eclipse.swt.layout.GridLayout) GridData(org.eclipse.swt.layout.GridData) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) File(java.io.File)

Example 7 with Frame

use of com.teamdev.jxbrowser.frame.Frame in project JxBrowser-Examples by TeamDev-IP.

the class PrintToPdf method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(OFF_SCREEN);
    Browser browser = engine.newBrowser();
    browser.set(PrintCallback.class, (params, tell) -> tell.print());
    browser.set(PrintHtmlCallback.class, (params, tell) -> {
        PdfPrinter<PdfPrinter.HtmlSettings> pdfPrinter = params.printers().pdfPrinter();
        PrintJob<HtmlSettings> printJob = pdfPrinter.printJob();
        printJob.settings().pdfFilePath(Paths.get("google.pdf").toAbsolutePath()).enablePrintingBackgrounds().orientation(PORTRAIT).apply();
        printJob.on(PrintCompleted.class, event -> {
            if (event.isSuccess()) {
                System.out.println("Printing is completed successfully.");
            } else {
                System.out.println("Printing has failed.");
            }
        });
        tell.proceed(pdfPrinter);
    });
    browser.navigation().loadUrlAndWait("https://google.com");
    browser.mainFrame().ifPresent(Frame::print);
}
Also used : Frame(com.teamdev.jxbrowser.frame.Frame) HtmlSettings(com.teamdev.jxbrowser.print.PdfPrinter.HtmlSettings) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Aggregations

Frame (com.teamdev.jxbrowser.frame.Frame)7 Browser (com.teamdev.jxbrowser.browser.Browser)5 Engine (com.teamdev.jxbrowser.engine.Engine)5 JsObject (com.teamdev.jxbrowser.js.JsObject)3 Document (com.teamdev.jxbrowser.dom.Document)2 BrowserView (com.teamdev.jxbrowser.view.swing.BrowserView)2 C8oBrowser (com.twinsoft.convertigo.eclipse.swt.C8oBrowser)2 DatabaseObjectFoundException (com.twinsoft.convertigo.engine.DatabaseObjectFoundException)2 File (java.io.File)2 JFrame (javax.swing.JFrame)2 JSONException (org.codehaus.jettison.json.JSONException)2 CoreException (org.eclipse.core.runtime.CoreException)2 ScrolledComposite (org.eclipse.swt.custom.ScrolledComposite)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 Composite (org.eclipse.swt.widgets.Composite)2 PartInitException (org.eclipse.ui.PartInitException)2 OptionElement (com.teamdev.jxbrowser.dom.OptionElement)1 SelectElement (com.teamdev.jxbrowser.dom.SelectElement)1 HARDWARE_ACCELERATED (com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED)1