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);
}
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();
}
});
}
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();
}
}
}
});
}
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);
}
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.
*/
}
Aggregations