Search in sources :

Example 16 with LocationListener

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

the class Bug505418_Listeners_evals method locationChange.

@SuppressWarnings("unused")
private static void locationChange() {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Button button = new Button(shell, SWT.PUSH);
    final Browser browser = new Browser(shell, SWT.NONE);
    button.setText("Click to increase count.");
    button.addMouseListener(new MouseListener() {

        @Override
        public void mouseUp(MouseEvent e) {
        }

        @Override
        public void mouseDown(MouseEvent e) {
            shell.setText("Count: " + count++);
        }

        @Override
        public void mouseDoubleClick(MouseEvent e) {
        }
    });
    browser.setText("<html><title>Snippet</title><body><a href=\"https://eclipse.org/\">Eclipse.org  Disposing on link change causes hang</a></body></html>");
    browser.addProgressListener(new ProgressListener() {

        @Override
        public void completed(ProgressEvent event) {
            browser.addLocationListener(new LocationListener() {

                @Override
                public void changing(LocationEvent event) {
                    System.out.println("changing...");
                    String value = (String) browser.evaluate("return 'Changing eval...'");
                    System.out.println("changing returned: " + value);
                // widget.browser.evaluate("return 'Changing eval...'");
                // event.doit = false; // Stop page load.
                }

                @Override
                public void changed(LocationEvent event) {
                    System.out.print("Changed!");
                    String value = (String) browser.evaluate("return 'finished callback'");
                    System.out.println("Changed returned: " + value);
                }
            });
        }

        @Override
        public void changed(ProgressEvent event) {
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Also used : MouseEvent(org.eclipse.swt.events.MouseEvent) FillLayout(org.eclipse.swt.layout.FillLayout) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) Shell(org.eclipse.swt.widgets.Shell) MouseListener(org.eclipse.swt.events.MouseListener) ProgressListener(org.eclipse.swt.browser.ProgressListener) Button(org.eclipse.swt.widgets.Button) LocationListener(org.eclipse.swt.browser.LocationListener) LocationEvent(org.eclipse.swt.browser.LocationEvent) Display(org.eclipse.swt.widgets.Display) Browser(org.eclipse.swt.browser.Browser)

Example 17 with LocationListener

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

the class Bug511797_locChanged method main.

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Browser browser = new Browser(shell, SWT.NONE);
    browser.addLocationListener(new LocationListener() {

        @Override
        public void changing(LocationEvent event) {
            System.out.println("Changing ....");
        }

        @Override
        public void changed(LocationEvent event) {
            System.out.println("Changed !!!!");
        }
    });
    browser.setText("Hello world");
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) LocationListener(org.eclipse.swt.browser.LocationListener) FillLayout(org.eclipse.swt.layout.FillLayout) LocationEvent(org.eclipse.swt.browser.LocationEvent) Display(org.eclipse.swt.widgets.Display) Browser(org.eclipse.swt.browser.Browser)

Example 18 with LocationListener

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

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() {
    assumeTrue(isWebkit2 || SwtTestUtil.isCocoa || SwtTestUtil.isWindows);
    // On Webki1 this test works, but is prone to crashes. See Bug 509411
    AtomicBoolean changingFinished = new AtomicBoolean(false);
    AtomicBoolean changedFinished = new AtomicBoolean(false);
    browser.addLocationListener(new LocationListener() {

        @Override
        public void changing(LocationEvent event) {
            // Broken on Webkit1. I.e evaluate() in a 'changing()' signal doesn't do anything.
            browser.evaluate("SWTchanging = true");
            changingFinished.set(true);
        }

        @Override
        public void changed(LocationEvent event) {
            browser.evaluate("SWTchanged = true");
            changedFinished.set(true);
        }
    });
    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 (isWebkit2) {
        // Evaluation works in all cases.
        passed = changingFinished.get() && changedFinished.get() && changed && changing;
    } else if (isWebkit1) {
        // On Webkit1, evaluation in 'changing' fails.
        // && changing (broken)
        passed = changingFinished.get() && changedFinished.get() && changed;
    } 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 19 with LocationListener

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

the class BrowserView method createPartControl.

/* (non-Javadoc)
	 * @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) {
            setPartName(String.format(Messages.get().BrowserView_PartName_Changing, event.location));
            actionStop.setEnabled(true);
        }

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

Example 20 with LocationListener

use of org.eclipse.swt.browser.LocationListener in project core by jcryptool.

the class BrowserView method createPartControl.

@Override
public void createPartControl(Composite parent) {
    GridLayout gridLayout = new GridLayout();
    gridLayout.verticalSpacing = 2;
    parent.setLayout(gridLayout);
    controls = new Controls(parent, SWT.NONE, this);
    GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
    gridData.heightHint = 28;
    controls.setLayoutData(gridData);
    browser = new Browser(parent, SWT.NONE);
    browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    browser.setUrl(BROWSER_HOME);
    browser.addLocationListener(new LocationListener() {

        public void changed(LocationEvent event) {
            history.put(browser.getUrl(), browser.getUrl());
            controls.getUrlField().setItems(history.values().toArray(new String[0]));
            controls.getUrlField().setText(browser.getUrl());
            controls.getBackButton().setEnabled(browser.isBackEnabled());
            controls.getForwardButton().setEnabled(browser.isForwardEnabled());
            controls.animateReloadButton(false);
        }

        public void changing(LocationEvent event) {
            controls.animateReloadButton(true);
        }
    });
    browser.addStatusTextListener(new StatusTextListener() {

        public void changed(StatusTextEvent event) {
            status.setText(event.text);
        }
    });
    status = new Text(parent, SWT.BORDER | SWT.READ_ONLY | SWT.SINGLE);
    status.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, // $NON-NLS-1$
    "org.jcryptool.webbrowser.webBrowserView");
}
Also used : GridLayout(org.eclipse.swt.layout.GridLayout) LocationListener(org.eclipse.swt.browser.LocationListener) GridData(org.eclipse.swt.layout.GridData) StatusTextListener(org.eclipse.swt.browser.StatusTextListener) Text(org.eclipse.swt.widgets.Text) StatusTextEvent(org.eclipse.swt.browser.StatusTextEvent) LocationEvent(org.eclipse.swt.browser.LocationEvent) Browser(org.eclipse.swt.browser.Browser)

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