Search in sources :

Example 11 with ProgressListener

use of org.eclipse.swt.browser.ProgressListener in project aero.minova.rcp by minova-afis.

the class Preview method postConstruct.

@PostConstruct
public void postConstruct(Composite parent, MWindow window) {
    window.getContext().set(Preview.class, this);
    parent.setLayout(new FillLayout());
    browser = new Browser(parent, SWT.NONE);
    DisposeListener dl = e -> window.getContext().set(Preview.class, null);
    browser.addDisposeListener(dl);
    browser.addProgressListener(new ProgressListener() {

        @Override
        public void completed(ProgressEvent event) {
            loading = false;
        }

        @Override
        public void changed(ProgressEvent event) {
            loading = true;
        }
    });
}
Also used : Browser(org.eclipse.swt.browser.Browser) DisposeListener(org.eclipse.swt.events.DisposeListener) EPartService(org.eclipse.e4.ui.workbench.modeling.EPartService) ProgressListener(org.eclipse.swt.browser.ProgressListener) Display(org.eclipse.swt.widgets.Display) MessageFormat(java.text.MessageFormat) Inject(javax.inject.Inject) MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) PartState(org.eclipse.e4.ui.workbench.modeling.EPartService.PartState) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) Composite(org.eclipse.swt.widgets.Composite) SWT(org.eclipse.swt.SWT) PostConstruct(javax.annotation.PostConstruct) MWindow(org.eclipse.e4.ui.model.application.ui.basic.MWindow) FillLayout(org.eclipse.swt.layout.FillLayout) DisposeListener(org.eclipse.swt.events.DisposeListener) ProgressListener(org.eclipse.swt.browser.ProgressListener) FillLayout(org.eclipse.swt.layout.FillLayout) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) Browser(org.eclipse.swt.browser.Browser) PostConstruct(javax.annotation.PostConstruct)

Example 12 with ProgressListener

use of org.eclipse.swt.browser.ProgressListener in project olca-app by GreenDelta.

the class UI method onLoaded.

/**
 * Calls the given function when the browser has loaded the given URL. No
 * threads are spawned here. You have to make sure that the given function
 * accesses the browser in the UI thread.
 */
public static void onLoaded(Browser browser, String url, Runnable fn) {
    if (browser == null || url == null)
        return;
    browser.addProgressListener(new ProgressListener() {

        @Override
        public void completed(ProgressEvent event) {
            if (fn != null) {
                fn.run();
                browser.removeProgressListener(this);
            }
        }

        @Override
        public void changed(ProgressEvent event) {
        }
    });
    browser.setUrl(url);
}
Also used : ProgressListener(org.eclipse.swt.browser.ProgressListener) ProgressEvent(org.eclipse.swt.browser.ProgressEvent)

Example 13 with ProgressListener

use of org.eclipse.swt.browser.ProgressListener in project eclipse.platform.swt by eclipse-platform.

the class Test_org_eclipse_swt_browser_Browser method test_BrowserFunction_multiprocess.

@Test
public void test_BrowserFunction_multiprocess() {
    // Test that BrowserFunctions work in multiple Browser instances simultaneously.
    Browser browser1 = new Browser(shell, SWT.NONE);
    Browser browser2 = new Browser(shell, SWT.NONE);
    class JavaFunc extends BrowserFunction {

        JavaFunc(Browser browser) {
            super(browser, "javaFunc");
        }

        @Override
        public Object function(Object[] arguments) {
            return arguments[0];
        }
    }
    new JavaFunc(browser1);
    new JavaFunc(browser2);
    assertEquals("value1", browser1.evaluate("return javaFunc('value1')"));
    assertEquals("value2", browser2.evaluate("return javaFunc('value2')"));
    // Ensure that navigation to a different page preserves BrowserFunctions.
    int[] completed = new int[1];
    ProgressListener listener = new ProgressAdapter() {

        @Override
        public void completed(ProgressEvent event) {
            completed[0]++;
        }
    };
    browser1.addProgressListener(listener);
    browser2.addProgressListener(listener);
    browser1.setText("<body>new_page1");
    browser2.setText("<body>new_page2");
    waitForPassCondition(() -> completed[0] == 2);
    assertEquals("value1", browser1.evaluate("return javaFunc('value1')"));
    assertEquals("value2", browser2.evaluate("return javaFunc('value2')"));
    browser1.dispose();
    browser2.dispose();
}
Also used : BrowserFunction(org.eclipse.swt.browser.BrowserFunction) ProgressListener(org.eclipse.swt.browser.ProgressListener) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) Browser(org.eclipse.swt.browser.Browser) Test(org.junit.Test)

Example 14 with ProgressListener

use of org.eclipse.swt.browser.ProgressListener in project eclipse.platform.swt by eclipse-platform.

the class Test_org_eclipse_swt_browser_Browser method test_setUrl_remote_with_post.

/**
 * This test requires working Internet connection
 */
@Test
public void test_setUrl_remote_with_post() {
    // This test sometimes times out if build server has a bad connection. Thus for this test we have a longer timeout.
    secondsToWaitTillFail = 35;
    String url = "https://bugs.eclipse.org/bugs/buglist.cgi";
    // Skip this test if we don't have a working Internet connection.
    assumeTrue("Skipping test due to bad internet connection", checkInternet(url));
    testLog.append("checkInternet() passed");
    Runnable browserSetFunc = () -> {
        testLog.append("Setting Browser url to:" + url);
        boolean opSuccess = browser.setUrl(url, "bug_severity=enhancement&bug_status=NEW&email1=rgrunber&emailassigned_to1=1&emailtype1=substring", null);
        assertTrue("Expecting setUrl() to return true", opSuccess);
    };
    final AtomicReference<Boolean> completed = new AtomicReference<>(false);
    browser.addProgressListener(completedAdapter(event -> {
        testLog.append("ProgressListener fired");
        completed.set(true);
    }));
    browserSetFunc.run();
    shell.open();
    boolean hasFinished = waitForPassCondition(() -> completed.get().booleanValue());
    assertTrue("Test timed out. ProgressListener not fired " + testLog.toString(), hasFinished);
    // Even a successful empty query returns about 10000 chars of HTML
    int numChars = browser.getText().length();
    assertTrue("Response data contained " + numChars + " chars.", numChars > 10000);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) LocationAdapter(org.eclipse.swt.browser.LocationAdapter) CloseWindowListener(org.eclipse.swt.browser.CloseWindowListener) ProgressListener(org.eclipse.swt.browser.ProgressListener) VisibilityWindowAdapter(org.eclipse.swt.browser.VisibilityWindowAdapter) Point(org.eclipse.swt.graphics.Point) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LocationListener.changedAdapter(org.eclipse.swt.browser.LocationListener.changedAdapter) WindowEvent(org.eclipse.swt.browser.WindowEvent) ProgressListener.completedAdapter(org.eclipse.swt.browser.ProgressListener.completedAdapter) Assert.fail(org.junit.Assert.fail) SWTException(org.eclipse.swt.SWTException) VisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener) AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) OpenWindowListener(org.eclipse.swt.browser.OpenWindowListener) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) Display(org.eclipse.swt.widgets.Display) Instant(java.time.Instant) Assert.assertFalse(org.junit.Assert.assertFalse) SWT(org.eclipse.swt.SWT) Assume.assumeTrue(org.junit.Assume.assumeTrue) StatusTextListener(org.eclipse.swt.browser.StatusTextListener) Assume.assumeFalse(org.junit.Assume.assumeFalse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TitleListener(org.eclipse.swt.browser.TitleListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) BrowserFunction(org.eclipse.swt.browser.BrowserFunction) Assert.assertSame(org.junit.Assert.assertSame) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) TestName(org.junit.rules.TestName) VisibilityWindowListener.showAdapter(org.eclipse.swt.browser.VisibilityWindowListener.showAdapter) LocationListener(org.eclipse.swt.browser.LocationListener) FillLayout(org.eclipse.swt.layout.FillLayout) Before(org.junit.Before) Browser(org.eclipse.swt.browser.Browser) Shell(org.eclipse.swt.widgets.Shell) MalformedURLException(java.net.MalformedURLException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) LocationListener.changingAdapter(org.eclipse.swt.browser.LocationListener.changingAdapter) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) LocationEvent(org.eclipse.swt.browser.LocationEvent) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Point(org.eclipse.swt.graphics.Point) Test(org.junit.Test)

Example 15 with ProgressListener

use of org.eclipse.swt.browser.ProgressListener in project eclipse.platform.swt by eclipse-platform.

the class BrowserExample method show.

void show(boolean owned, Point location, Point size, boolean addressBar, boolean menuBar, boolean statusBar, boolean toolBar) {
    final Shell shell = browser.getShell();
    if (owned) {
        if (location != null)
            shell.setLocation(location);
        if (size != null)
            shell.setSize(shell.computeSize(size.x, size.y));
    }
    FormData data = null;
    if (toolBar) {
        toolbar = new ToolBar(parent, SWT.NONE);
        data = new FormData();
        data.top = new FormAttachment(0, 5);
        toolbar.setLayoutData(data);
        itemBack = new ToolItem(toolbar, SWT.PUSH);
        itemBack.setText(getResourceString("Back"));
        itemForward = new ToolItem(toolbar, SWT.PUSH);
        itemForward.setText(getResourceString("Forward"));
        final ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
        itemStop.setText(getResourceString("Stop"));
        final ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
        itemRefresh.setText(getResourceString("Refresh"));
        final ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
        itemGo.setText(getResourceString("Go"));
        itemBack.setEnabled(browser.isBackEnabled());
        itemForward.setEnabled(browser.isForwardEnabled());
        Listener listener = event -> {
            ToolItem item = (ToolItem) event.widget;
            if (item == itemBack)
                browser.back();
            else if (item == itemForward)
                browser.forward();
            else if (item == itemStop)
                browser.stop();
            else if (item == itemRefresh)
                browser.refresh();
            else if (item == itemGo)
                browser.setUrl(locationBar.getText());
        };
        itemBack.addListener(SWT.Selection, listener);
        itemForward.addListener(SWT.Selection, listener);
        itemStop.addListener(SWT.Selection, listener);
        itemRefresh.addListener(SWT.Selection, listener);
        itemGo.addListener(SWT.Selection, listener);
        canvas = new Canvas(parent, SWT.NO_BACKGROUND);
        data = new FormData();
        data.width = 24;
        data.height = 24;
        data.top = new FormAttachment(0, 5);
        data.right = new FormAttachment(100, -5);
        canvas.setLayoutData(data);
        final Rectangle rect = images[0].getBounds();
        canvas.addListener(SWT.Paint, e -> {
            Point pt = ((Canvas) e.widget).getSize();
            e.gc.drawImage(images[index], 0, 0, rect.width, rect.height, 0, 0, pt.x, pt.y);
        });
        canvas.addListener(SWT.MouseDown, e -> browser.setUrl(getResourceString("Startup")));
        final Display display = parent.getDisplay();
        display.asyncExec(new Runnable() {

            @Override
            public void run() {
                if (canvas.isDisposed())
                    return;
                if (busy) {
                    index++;
                    if (index == images.length)
                        index = 0;
                    canvas.redraw();
                }
                display.timerExec(150, this);
            }
        });
    }
    if (addressBar) {
        locationBar = new Text(parent, SWT.BORDER);
        data = new FormData();
        if (toolbar != null) {
            data.top = new FormAttachment(toolbar, 0, SWT.TOP);
            data.left = new FormAttachment(toolbar, 5, SWT.RIGHT);
            data.right = new FormAttachment(canvas, -5, SWT.DEFAULT);
        } else {
            data.top = new FormAttachment(0, 0);
            data.left = new FormAttachment(0, 0);
            data.right = new FormAttachment(100, 0);
        }
        locationBar.setLayoutData(data);
        locationBar.addListener(SWT.DefaultSelection, e -> browser.setUrl(locationBar.getText()));
    }
    if (statusBar) {
        status = new Label(parent, SWT.NONE);
        progressBar = new ProgressBar(parent, SWT.NONE);
        data = new FormData();
        data.left = new FormAttachment(0, 5);
        data.right = new FormAttachment(progressBar, 0, SWT.DEFAULT);
        data.bottom = new FormAttachment(100, -5);
        status.setLayoutData(data);
        data = new FormData();
        data.right = new FormAttachment(100, -5);
        data.bottom = new FormAttachment(100, -5);
        progressBar.setLayoutData(data);
        browser.addStatusTextListener(event -> status.setText(event.text));
    }
    parent.setLayout(new FormLayout());
    Control aboveBrowser = toolBar ? (Control) canvas : (addressBar ? (Control) locationBar : null);
    data = new FormData();
    data.left = new FormAttachment(0, 0);
    data.top = aboveBrowser != null ? new FormAttachment(aboveBrowser, 5, SWT.DEFAULT) : new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    data.bottom = status != null ? new FormAttachment(status, -5, SWT.DEFAULT) : new FormAttachment(100, 0);
    browser.setLayoutData(data);
    if (statusBar || toolBar) {
        browser.addProgressListener(new ProgressListener() {

            @Override
            public void changed(ProgressEvent event) {
                if (event.total == 0)
                    return;
                int ratio = event.current * 100 / event.total;
                if (progressBar != null)
                    progressBar.setSelection(ratio);
                busy = event.current != event.total;
                if (!busy) {
                    index = 0;
                    if (canvas != null)
                        canvas.redraw();
                }
            }

            @Override
            public void completed(ProgressEvent event) {
                if (progressBar != null)
                    progressBar.setSelection(0);
                busy = false;
                index = 0;
                if (canvas != null) {
                    itemBack.setEnabled(browser.isBackEnabled());
                    itemForward.setEnabled(browser.isForwardEnabled());
                    canvas.redraw();
                }
            }
        });
    }
    if (addressBar || statusBar || toolBar) {
        browser.addLocationListener(LocationListener.changedAdapter(event -> {
            busy = true;
            if (event.top && locationBar != null)
                locationBar.setText(event.location);
        }));
    }
    if (title) {
        browser.addTitleListener(event -> shell.setText(event.title + " - " + getResourceString("window.title")));
    }
    parent.layout(true);
    if (owned)
        shell.open();
}
Also used : FormData(org.eclipse.swt.layout.FormData) ProgressBar(org.eclipse.swt.widgets.ProgressBar) ToolBar(org.eclipse.swt.widgets.ToolBar) Image(org.eclipse.swt.graphics.Image) Rectangle(org.eclipse.swt.graphics.Rectangle) ProgressListener(org.eclipse.swt.browser.ProgressListener) ImageData(org.eclipse.swt.graphics.ImageData) Point(org.eclipse.swt.graphics.Point) SWTError(org.eclipse.swt.SWTError) ResourceBundle(java.util.ResourceBundle) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) Composite(org.eclipse.swt.widgets.Composite) Listener(org.eclipse.swt.widgets.Listener) LocationListener(org.eclipse.swt.browser.LocationListener) Canvas(org.eclipse.swt.widgets.Canvas) VisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener) FillLayout(org.eclipse.swt.layout.FillLayout) Browser(org.eclipse.swt.browser.Browser) Text(org.eclipse.swt.widgets.Text) Shell(org.eclipse.swt.widgets.Shell) FormLayout(org.eclipse.swt.layout.FormLayout) FormData(org.eclipse.swt.layout.FormData) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) FormAttachment(org.eclipse.swt.layout.FormAttachment) Display(org.eclipse.swt.widgets.Display) ToolItem(org.eclipse.swt.widgets.ToolItem) SWT(org.eclipse.swt.SWT) Label(org.eclipse.swt.widgets.Label) Control(org.eclipse.swt.widgets.Control) InputStream(java.io.InputStream) FormLayout(org.eclipse.swt.layout.FormLayout) ProgressListener(org.eclipse.swt.browser.ProgressListener) Listener(org.eclipse.swt.widgets.Listener) LocationListener(org.eclipse.swt.browser.LocationListener) VisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener) Canvas(org.eclipse.swt.widgets.Canvas) Rectangle(org.eclipse.swt.graphics.Rectangle) Label(org.eclipse.swt.widgets.Label) Text(org.eclipse.swt.widgets.Text) Point(org.eclipse.swt.graphics.Point) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) Shell(org.eclipse.swt.widgets.Shell) Control(org.eclipse.swt.widgets.Control) ProgressListener(org.eclipse.swt.browser.ProgressListener) ToolBar(org.eclipse.swt.widgets.ToolBar) ProgressBar(org.eclipse.swt.widgets.ProgressBar) FormAttachment(org.eclipse.swt.layout.FormAttachment) ToolItem(org.eclipse.swt.widgets.ToolItem) Display(org.eclipse.swt.widgets.Display)

Aggregations

ProgressEvent (org.eclipse.swt.browser.ProgressEvent)32 ProgressListener (org.eclipse.swt.browser.ProgressListener)32 Browser (org.eclipse.swt.browser.Browser)22 FillLayout (org.eclipse.swt.layout.FillLayout)14 Display (org.eclipse.swt.widgets.Display)13 Shell (org.eclipse.swt.widgets.Shell)13 LocationListener (org.eclipse.swt.browser.LocationListener)11 LocationEvent (org.eclipse.swt.browser.LocationEvent)10 GridLayout (org.eclipse.swt.layout.GridLayout)8 Composite (org.eclipse.swt.widgets.Composite)8 IOException (java.io.IOException)6 Test (org.junit.Test)6 SWT (org.eclipse.swt.SWT)5 TitleListener (org.eclipse.swt.browser.TitleListener)5 WindowEvent (org.eclipse.swt.browser.WindowEvent)5 Point (org.eclipse.swt.graphics.Point)5 MalformedURLException (java.net.MalformedURLException)4 SWTError (org.eclipse.swt.SWTError)4 BrowserFunction (org.eclipse.swt.browser.BrowserFunction)4 CloseWindowListener (org.eclipse.swt.browser.CloseWindowListener)4