Search in sources :

Example 11 with LocationListener

use of org.eclipse.swt.browser.LocationListener in project netxms by netxms.

the class BrowserView method createPartControl.

/**
 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
 */
@Override
public void createPartControl(Composite parent) {
    browser = new Browser(parent, SWT.NONE);
    browser.addLocationListener(new LocationListener() {

        @Override
        public void changing(LocationEvent event) {
            if (title == null)
                setPartName(String.format(Messages.get().BrowserView_PartName_Changing, event.location));
            actionStop.setEnabled(true);
        }

        @Override
        public void changed(LocationEvent event) {
            if (title == null)
                setPartName(String.format(Messages.get().BrowserView_PartName_Changed, getTitle(browser.getText(), event.location)));
            actionStop.setEnabled(false);
        }
    });
    createActions();
    contributeToActionBars();
    if (title != null)
        setPartName(title);
    if (initialUrl != null)
        openUrl(initialUrl);
}
Also used : LocationListener(org.eclipse.swt.browser.LocationListener) LocationEvent(org.eclipse.swt.browser.LocationEvent) Browser(org.eclipse.swt.browser.Browser)

Example 12 with LocationListener

use of org.eclipse.swt.browser.LocationListener in project pmcms by th-schwarz.

the class BrowserManager method initializeBrowser.

private void initializeBrowser(final Shell shell, final Display display, final Browser browser) {
    final String urlToIgnore = "about:blank";
    final TreeViewManager treeViewManager = this.treeViewManager;
    browser.addLocationListener(new LocationListener() {

        @Override
        public void changing(LocationEvent event) {
            if (!event.location.equals(urlToIgnore))
                logger.debug("url is changing to: " + event.location);
        }

        @Override
        public void changed(LocationEvent event) {
            if (!event.location.equals(urlToIgnore)) {
                String url = event.location;
                logger.debug("url has changed to: " + url);
                Link link = InitializationManager.getBean(Link.class);
                link.init(event.location);
                if (!link.isPoormansRequest())
                    return;
                String idParam = link.getParameter("id");
                if (idParam != null && idParam.equals(APoormansObject.UNSET_VALUE + ""))
                    idParam = null;
                APoormansObject<?> po = StringUtils.isBlank(idParam) ? null : ContextUtil.getpo(link);
                // synchronizing with the tree view
                treeViewManager.fillAndExpands(po);
                // persist the site, if a poormans object was saved
                if (url.endsWith(Constants.LINK_IDENTICATOR_SAVE)) {
                    try {
                        SitePersister.write(siteHolder.getSite());
                    } catch (IOException e) {
                        show(e);
                    }
                }
            }
        }
    });
    // visibility handling
    browser.addVisibilityWindowListener(new VisibilityWindowListener() {

        @Override
        public void hide(WindowEvent event) {
            Browser browser = (Browser) event.widget;
            Shell shell = browser.getShell();
            shell.setVisible(false);
        }

        @Override
        public void show(WindowEvent event) {
            Browser browser = (Browser) event.widget;
            final Shell shell = browser.getShell();
            if (event.location != null)
                shell.setLocation(event.location);
            if (event.size != null) {
                Point size = event.size;
                shell.setSize(shell.computeSize(size.x, size.y));
            }
            shell.open();
        }
    });
    // setting title of the shell
    browser.addTitleListener(new org.eclipse.swt.browser.TitleListener() {

        @Override
        public void changed(org.eclipse.swt.browser.TitleEvent event) {
            final Browser tmpBrowser = (Browser) event.getSource();
            if (mainBrowser.equals(tmpBrowser) && StringUtils.isNotBlank(event.title)) {
                mainWindow.setTitle(event.title);
            }
        }
    });
    // BEGIN popup handling
    browser.addOpenWindowListener(new OpenWindowListener() {

        @Override
        public void open(WindowEvent event) {
            Shell shell = new Shell(display, SWT.APPLICATION_MODAL | SWT.CLOSE);
            shell.setImages(new Image[] { ImageHolder.SHELL_ICON_SMALL, ImageHolder.SHELL_ICON_BIG });
            shell.setLayout(new FillLayout());
            SWTUtils.center(shell, parentShell.getBounds());
            Browser browser = getBrowser(shell);
            initializeBrowser(shell, display, browser);
            final Browser tempBrowser = browser;
            browser.addTitleListener(new // set title for the popup
            TitleListener() {

                @Override
                public void changed(TitleEvent event) {
                    tempBrowser.getShell().setText(event.title);
                }
            });
            browser.addVisibilityWindowListener(new VisibilityWindowListener() {

                @Override
                public void hide(WindowEvent event) {
                    Browser browser = (Browser) event.widget;
                    Shell shell = browser.getShell();
                    shell.setVisible(false);
                }

                @Override
                public void show(WindowEvent event) {
                    final Browser browser = (Browser) event.widget;
                    Shell shell = browser.getShell();
                    if (event.location != null)
                        shell.setLocation(event.location);
                    if (event.size != null) {
                        Point size = event.size;
                        // hack for bug with too big of safari popups
                        if (StringUtils.containsIgnoreCase(browserType, "webkit") && (size.x > parentShell.getSize().x || size.y > parentShell.getSize().y)) {
                            size.x = parentShell.getSize().x - 50;
                            size.y = parentShell.getSize().y - 50;
                            shell.setLocation(parentShell.getLocation().x + 15, parentShell.getLocation().y + 15);
                            shell.setSize(shell.computeSize(size.x, size.y));
                        } else
                            shell.setSize(shell.computeSize(size.x, size.y));
                    }
                    shell.open();
                }
            });
            event.browser = browser;
        }
    });
    // END popup handling
    browser.addCloseWindowListener(new CloseWindowListener() {

        @Override
        public void close(WindowEvent event) {
            Browser browser = (Browser) event.widget;
            Shell shell = browser.getShell();
            shell.close();
        }
    });
}
Also used : TreeViewManager(de.thischwa.pmcms.gui.treeview.TreeViewManager) APoormansObject(de.thischwa.pmcms.model.domain.pojo.APoormansObject) TitleListener(org.eclipse.swt.browser.TitleListener) IOException(java.io.IOException) Point(org.eclipse.swt.graphics.Point) TitleListener(org.eclipse.swt.browser.TitleListener) FillLayout(org.eclipse.swt.layout.FillLayout) Image(org.eclipse.swt.graphics.Image) Shell(org.eclipse.swt.widgets.Shell) TitleEvent(org.eclipse.swt.browser.TitleEvent) CloseWindowListener(org.eclipse.swt.browser.CloseWindowListener) LocationListener(org.eclipse.swt.browser.LocationListener) WindowEvent(org.eclipse.swt.browser.WindowEvent) VisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener) LocationEvent(org.eclipse.swt.browser.LocationEvent) Link(de.thischwa.pmcms.tool.Link) TitleEvent(org.eclipse.swt.browser.TitleEvent) OpenWindowListener(org.eclipse.swt.browser.OpenWindowListener) Browser(org.eclipse.swt.browser.Browser)

Example 13 with LocationListener

use of org.eclipse.swt.browser.LocationListener in project windup-eclipse-plugin by windup.

the class HintMessageTab method createBrowser.

private void createBrowser(Composite parent) {
    browser = new Browser(parent, SWT.NONE);
    GridDataFactory.fillDefaults().grab(true, true).hint(SWT.DEFAULT, 150).applyTo(browser);
    browser.addLocationListener(new LocationListener() {

        public void changed(LocationEvent event) {
            event.doit = false;
        }

        public void changing(LocationEvent event) {
            // if it looks like an absolute URL
            if (event.location.matches("([a-zA-Z]{3,8})://?.*")) {
                // $NON-NLS-1$
                // workaround for browser problem (bug 262043)
                // $NON-NLS-1$
                int idxOfSlashHash = event.location.indexOf("/#");
                if (idxOfSlashHash != -1) {
                    // allow javascript-based scrolling to work
                    if (!event.location.startsWith("file:///#")) {
                        // $NON-NLS-1$
                        event.doit = false;
                    }
                    return;
                }
                // workaround end
                event.doit = false;
                try {
                    IWebBrowser webBrowser = PlatformUI.getWorkbench().getBrowserSupport().createBrowser(// $NON-NLS-1$
                    "org.eclipse.ui.browser");
                    webBrowser.openURL(new URL(event.location));
                } catch (Exception e) {
                    new URLHyperlink(new Region(0, 1), event.location).open();
                }
            }
        }
    });
}
Also used : URLHyperlink(org.eclipse.jface.text.hyperlink.URLHyperlink) LocationListener(org.eclipse.swt.browser.LocationListener) Region(org.eclipse.jface.text.Region) IWebBrowser(org.eclipse.ui.browser.IWebBrowser) LocationEvent(org.eclipse.swt.browser.LocationEvent) URL(java.net.URL) CoreException(org.eclipse.core.runtime.CoreException) IWebBrowser(org.eclipse.ui.browser.IWebBrowser) Browser(org.eclipse.swt.browser.Browser)

Example 14 with LocationListener

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

the class Test_org_eclipse_swt_browser_Browser method test_LocationListener_evaluateInCallback.

/**
 *  This test replicates what happens internally
 *  if you click on a link in a javadoc popup hoverbox.
 *  I.e, in a location listener, evaluation() is performed.
 *
 *  The goal of this test is to ensure there are no 'Freezes'/deadlocks if
 *  javascript evaluation is invoked inside an SWT listener.
 *
 *  At time of writing, it also highlights that evaluation inside SWT listeners
 *  is not consistent across browsers.
 */
@Test
public void test_LocationListener_evaluateInCallback() {
    AtomicBoolean changingFinished = new AtomicBoolean(false);
    AtomicBoolean changedFinished = new AtomicBoolean(false);
    browser.addLocationListener(new LocationListener() {

        @Override
        public void changing(LocationEvent event) {
            browser.evaluate("SWTchanging = true");
            changingFinished.set(true);
        }

        @Override
        public void changed(LocationEvent event) {
            browser.evaluate("SWTchanged = true");
            changedFinished.set(true);
        }
    });
    shell.open();
    browser.setText("<body>Hello <b>World</b></body>");
    // Wait till both listeners were fired.
    if (SwtTestUtil.isWindows) {
        // Windows doesn't reach changedFinished.get();
        waitForPassCondition(changingFinished::get);
    } else
        waitForPassCondition(() -> (changingFinished.get() && changedFinished.get()));
    // Inspect if evaluate() was executed correctly.
    Boolean changed = false;
    try {
        changed = (Boolean) browser.evaluate("return SWTchanged");
    } catch (SWTException e) {
    }
    Boolean changing = false;
    try {
        changing = (Boolean) browser.evaluate("return SWTchanging");
    } catch (SWTException e) {
    }
    String errMsg = "\n  changing:  fired:" + changingFinished.get() + "    evaluated:" + changing + "\n  changed:   fired:" + changedFinished.get() + "    evaluated:" + changed;
    boolean passed = false;
    if (SwtTestUtil.isGTK) {
        // Evaluation works in all cases.
        passed = changingFinished.get() && changedFinished.get() && changed && changing;
    } else if (SwtTestUtil.isCocoa) {
        // On Cocoa, evaluation in 'changing' fails.
        // && changing (broken)
        passed = changingFinished.get() && changedFinished.get() && changed;
    } else if (SwtTestUtil.isWindows) {
        // On Windows, evaluation inside SWT listeners fails altogether.
        // Further, only 'changing' is fired if evaluation is invoked inside listeners.
        passed = changingFinished.get();
    }
    assertTrue(errMsg, passed);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SWTException(org.eclipse.swt.SWTException) LocationListener(org.eclipse.swt.browser.LocationListener) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocationEvent(org.eclipse.swt.browser.LocationEvent) Test(org.junit.Test)

Example 15 with LocationListener

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

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) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocationListener(org.eclipse.swt.browser.LocationListener) LocationEvent(org.eclipse.swt.browser.LocationEvent) Test(org.junit.Test)

Aggregations

LocationEvent (org.eclipse.swt.browser.LocationEvent)29 LocationListener (org.eclipse.swt.browser.LocationListener)29 Browser (org.eclipse.swt.browser.Browser)18 ProgressEvent (org.eclipse.swt.browser.ProgressEvent)9 ProgressListener (org.eclipse.swt.browser.ProgressListener)9 FillLayout (org.eclipse.swt.layout.FillLayout)9 Shell (org.eclipse.swt.widgets.Shell)9 URL (java.net.URL)7 Display (org.eclipse.swt.widgets.Display)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 SWTException (org.eclipse.swt.SWTException)6 Test (org.junit.Test)6 StatusTextListener (org.eclipse.swt.browser.StatusTextListener)5 TitleListener (org.eclipse.swt.browser.TitleListener)5 Point (org.eclipse.swt.graphics.Point)5 MalformedURLException (java.net.MalformedURLException)4 OpenWindowListener (org.eclipse.swt.browser.OpenWindowListener)4 WindowEvent (org.eclipse.swt.browser.WindowEvent)4 MouseEvent (org.eclipse.swt.events.MouseEvent)4 IOException (java.io.IOException)3