Search in sources :

Example 1 with LocationEvent

use of org.eclipse.swt.browser.LocationEvent in project cubrid-manager by CUBRID.

the class BrowserEditorPart method initBrowser.

private void initBrowser(Composite parent) {
    browser = new Browser(parent, SWT.None);
    browser.setLayoutData(createGridData(FILL_BOTH, 3, 1, -1, -1));
    // Add location change listener
    browser.addLocationListener(new LocationListener() {

        public void changing(LocationEvent e) {
            location.setText(e.location);
        }

        public void changed(LocationEvent e) {
            noOp();
        }
    });
    // Add loading listener
    browser.addProgressListener(new ProgressListener() {

        // Set stopItem and progress bar status
        public void changed(ProgressEvent e) {
            if (!stopItem.isEnabled() && e.total != e.current) {
                stopItem.setEnabled(true);
            }
        }

        // Set stopItem,backItem,forwardItem and progress bar status
        public void completed(ProgressEvent e) {
            stopItem.setEnabled(false);
            backItem.setEnabled(browser.isBackEnabled());
            forwardItem.setEnabled(browser.isForwardEnabled());
        }
    });
}
Also used : ProgressListener(org.eclipse.swt.browser.ProgressListener) LocationListener(org.eclipse.swt.browser.LocationListener) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) LocationEvent(org.eclipse.swt.browser.LocationEvent) Browser(org.eclipse.swt.browser.Browser)

Example 2 with LocationEvent

use of org.eclipse.swt.browser.LocationEvent in project azure-tools-for-java by Microsoft.

the class LoginDialog method createDlgBody.

private void createDlgBody(Composite container) {
    final Browser browser = new Browser(container, SWT.NONE);
    browser.addLocationListener(new LocationAdapter() {

        @Override
        public void changing(LocationEvent locationEvent) {
            System.out.println("\t--> locationEvent.location: " + locationEvent.location);
            if (locationEvent.location.startsWith(redirectUriStr)) {
                setResult(locationEvent.location);
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        //Browser.clearSessions();
                        close();
                    }
                });
            }
        }
    });
    //        String[] headers = new String[] {
    //                "User-Agent: SwtBrowser"
    //        };
    //        browser.setUrl(requestUriStr, null, headers);
    browser.setUrl(requestUriStr);
}
Also used : LocationAdapter(org.eclipse.swt.browser.LocationAdapter) LocationEvent(org.eclipse.swt.browser.LocationEvent) Browser(org.eclipse.swt.browser.Browser)

Example 3 with LocationEvent

use of org.eclipse.swt.browser.LocationEvent in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_browser_Browser method test_LocationListener_ProgressListener_cancledLoad.

@Test
public /**
 * "event.doit = false" in Location.changing() should stop 'Loction.changed & progress.completed' from getting fired.
 */
void test_LocationListener_ProgressListener_cancledLoad() {
    AtomicBoolean locationChanging = new AtomicBoolean(false);
    AtomicBoolean unexpectedLocationChanged = new AtomicBoolean(false);
    AtomicBoolean unexpectedProgressCompleted = new AtomicBoolean(false);
    browser.addLocationListener(new LocationListener() {

        @Override
        public void changing(LocationEvent event) {
            event.doit = false;
            locationChanging.set(true);
        }

        @Override
        public void changed(LocationEvent event) {
            if (event.location.length() != 0) {
                // See footnote 1
                unexpectedLocationChanged.set(true);
            }
        }
    });
    browser.addProgressListener(completedAdapter(event -> {
        String location = browser.getUrl();
        if (location.length() != 0) {
            // See footnote 1
            unexpectedProgressCompleted.set(true);
        }
    }));
    shell.open();
    browser.setText("You should not see this message.");
    // We must wait for events *not* to fire.
    // On Gtk, Quadcore (Intel i7-4870HQ pci-e SSD, all events fire after ~80ms.
    // For stability, wait 1000 ms.
    waitForMilliseconds(1000);
    boolean passed = locationChanging.get() && !unexpectedLocationChanged.get() && !unexpectedProgressCompleted.get();
    String errMsg = "\nUnexpected event fired. \n" + "LocationChanging (should be true): " + locationChanging.get() + "\n" + "LocationChanged unexpectedly (should be false): " + unexpectedLocationChanged.get() + "\n" + "ProgressChanged unexpectedly (should be false): " + unexpectedProgressCompleted.get() + "\n";
    assertTrue(errMsg, passed);
/* FOOTNOTE 1
	 *
	 * Feature on Internet Explorer. If there is no current location, IE still fires a DocumentComplete
	 * following the BeforeNavigate2 cancel event. This DocumentComplete event contains an empty URL
	 * since the URL in BeforeNavigate2 was correctly cancelled.
	 * The test considers it is OK to send a Location.changed and a Progress.completed events after
	 * a Location.changing cancel true - at the condition that the current location is empty,
	 * otherwise it is considered that the location was not successfully cancelled.
	 */
}
Also used : HttpURLConnection(java.net.HttpURLConnection) StatusTextListener(org.eclipse.swt.browser.StatusTextListener) URL(java.net.URL) Assume.assumeFalse(org.junit.Assume.assumeFalse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocationAdapter(org.eclipse.swt.browser.LocationAdapter) CloseWindowListener(org.eclipse.swt.browser.CloseWindowListener) ProgressListener(org.eclipse.swt.browser.ProgressListener) TitleListener(org.eclipse.swt.browser.TitleListener) VisibilityWindowAdapter(org.eclipse.swt.browser.VisibilityWindowAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Point(org.eclipse.swt.graphics.Point) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) BrowserFunction(org.eclipse.swt.browser.BrowserFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) TestName(org.junit.rules.TestName) LocationListener.changedAdapter(org.eclipse.swt.browser.LocationListener.changedAdapter) VisibilityWindowListener.showAdapter(org.eclipse.swt.browser.VisibilityWindowListener.showAdapter) WindowEvent(org.eclipse.swt.browser.WindowEvent) LocationListener(org.eclipse.swt.browser.LocationListener) 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) FillLayout(org.eclipse.swt.layout.FillLayout) AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Before(org.junit.Before) OpenWindowListener(org.eclipse.swt.browser.OpenWindowListener) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) 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) Display(org.eclipse.swt.widgets.Display) Instant(java.time.Instant) LocationListener.changingAdapter(org.eclipse.swt.browser.LocationListener.changingAdapter) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Assert.assertFalse(org.junit.Assert.assertFalse) SWT(org.eclipse.swt.SWT) LocationEvent(org.eclipse.swt.browser.LocationEvent) Assume.assumeTrue(org.junit.Assume.assumeTrue) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocationListener(org.eclipse.swt.browser.LocationListener) LocationEvent(org.eclipse.swt.browser.LocationEvent) Test(org.junit.Test)

Example 4 with LocationEvent

use of org.eclipse.swt.browser.LocationEvent in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_browser_Browser method test_LocationListener_changingAndOnlyThenChanged.

@Test
public void test_LocationListener_changingAndOnlyThenChanged() {
    // Test proper order of events.
    // Check that 'changed' is only fired after 'changing' has fired at least once.
    AtomicBoolean changingFired = new AtomicBoolean(false);
    AtomicBoolean changedFired = new AtomicBoolean(false);
    AtomicBoolean changedFiredTooEarly = new AtomicBoolean(false);
    AtomicBoolean finished = new AtomicBoolean(false);
    browser.addLocationListener(new LocationListener() {

        @Override
        public void changing(LocationEvent event) {
            // Multiple changing events can occur during a load.
            changingFired.set(true);
        }

        @Override
        public void changed(LocationEvent event) {
            if (!changingFired.get())
                changedFiredTooEarly.set(true);
            changedFired.set(true);
            finished.set(true);
        }
    });
    shell.open();
    browser.setText("Hello world");
    waitForPassCondition(() -> finished.get());
    if (finished.get() && changingFired.get() && changedFired.get() && !changedFiredTooEarly.get()) {
        // pass
        return;
    } else if (!finished.get()) {
        fail("Test timed out. 'changed()' never fired");
    } else {
        if (changedFiredTooEarly.get())
            fail("changed() was fired before changing(). Wrong signal order");
        else if (!changingFired.get())
            fail("changing() was never fired");
        else {
            fail("LocationListener test failed. changing():" + changingFired.get() + "  changed():" + changedFired.get() + " changedFiredTooEarly:" + changedFiredTooEarly.get());
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocationListener(org.eclipse.swt.browser.LocationListener) LocationEvent(org.eclipse.swt.browser.LocationEvent) Test(org.junit.Test)

Example 5 with LocationEvent

use of org.eclipse.swt.browser.LocationEvent in project pentaho-kettle by pentaho.

the class ShowHelpDialog method addProgressAndLocationListener.

private void addProgressAndLocationListener() {
    ProgressListener progressListener = new ProgressListener() {

        @Override
        public void changed(ProgressEvent event) {
        }

        @Override
        public void completed(ProgressEvent event) {
            if (fromPrint) {
                wBrowser.execute(PRINT_SCRIPT);
                fromPrint = false;
            }
            setForwardBackEnable();
        }
    };
    LocationListener listener = new LocationListener() {

        @Override
        public void changing(LocationEvent event) {
            if (event.location.endsWith(".pdf")) {
                Program.launch(event.location);
                event.doit = false;
            }
        }

        @Override
        public void changed(LocationEvent event) {
            textURL.setText(event.location);
        }
    };
    wBrowser.addProgressListener(progressListener);
    wBrowser.addLocationListener(listener);
}
Also used : ProgressListener(org.eclipse.swt.browser.ProgressListener) LocationListener(org.eclipse.swt.browser.LocationListener) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) LocationEvent(org.eclipse.swt.browser.LocationEvent)

Aggregations

LocationEvent (org.eclipse.swt.browser.LocationEvent)17 LocationListener (org.eclipse.swt.browser.LocationListener)12 Browser (org.eclipse.swt.browser.Browser)11 LocationAdapter (org.eclipse.swt.browser.LocationAdapter)6 URL (java.net.URL)4 SWTException (org.eclipse.swt.SWTException)4 ProgressEvent (org.eclipse.swt.browser.ProgressEvent)4 ProgressListener (org.eclipse.swt.browser.ProgressListener)4 Point (org.eclipse.swt.graphics.Point)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 SWTError (org.eclipse.swt.SWTError)3 Composite (org.eclipse.swt.widgets.Composite)3 Test (org.junit.Test)3 MalformedURLException (java.net.MalformedURLException)2 OpenWindowListener (org.eclipse.swt.browser.OpenWindowListener)2 TitleListener (org.eclipse.swt.browser.TitleListener)2 WindowEvent (org.eclipse.swt.browser.WindowEvent)2 MouseAdapter (org.eclipse.swt.events.MouseAdapter)2 MouseEvent (org.eclipse.swt.events.MouseEvent)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2