I detected a very curious phenomenon when executing script code in an SWT Browser.
I've two controls in my window: One List and one Browser, placed side by side (GridLayout.numColumns=2).
The list contains some headings which I want to navigate to later in the HTML document.
private void setFocusToElement(String id) {
StringBuilder sb = new StringBuilder();
sb.append("var elem = document.getElementById('" + id + "');");
sb.append("if (elem) {");
sb.append(" elem.setAttribute('tabindex', '-1');");
sb.append(" elem.focus();");
sb.append("}");
browser.execute(sb.toString());
}
The method is called everytime I click an item in the list.
The heading is correctly selected so far the execution comes up with my expectation.
But now the focus moves without my interaction from the list to the browser.
Is this an SWT bug or is it my mistake and how can I prevent moving the focus?
+++ UPDATED +++
My list control contains ids with which I try to find and select headings h1...h6 in the browser's HTML document.
I need to set the focus on such a heading so .scrollIntoView() can't be used.
Example:
I have a list entry named "myHeading-1". I click on this item and want to select the corresponding heading in the webview (h1 with the id from the list). For this purpose I use the JavaScript code above with "myHeading-1" as input parameter.
And the result is that my list control loses the focus. That's a little bit unlogic for me because I never use browser.setFocus(). I only used the JavaScript focus function so that should not happen.
My expectation is:
The heading in the webview is selected but the focus is still placed on the list control.
Ok and here's some code to demonstrate waht I mean. Maybe then it's better to reproduce the effect:
public class Main {
private List list;
private Browser browser;
public Main() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2, false));
shell.setSize(400, 200);
shell.setText("Demo");
list = new List(shell, SWT.BORDER | SWT.SINGLE);
list.add("rtrx_001d");
list.add("rtrx_003e");
list.setSelection(0);
list.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
setFocusToElement(list.getSelection()[0]);
}
});
browser = new Browser(shell, SWT.NONE);
try {
browser.setText(readAllLines(new File("C:\\workspace\\file.html")));
} catch (IOException e) {
System.err.println(e);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private String readAllLines(File file) throws IOException {
return readAllLines(file, StandardCharsets.UTF_8);
}
private String readAllLines(File file, Charset charset) throws IOException {
return readAllLines(new FileInputStream(file), charset);
}
private String readAllLines(InputStream istream, Charset charset) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(istream, charset));
try {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append('\n');
}
return sb.toString();
} finally {
try {
reader.close();
} catch (IOException e) {
}
}
}
private void setFocusToElement(String id) {
StringBuilder sb = new StringBuilder();
sb.append("var elem = document.getElementById('" + id + "');");
sb.append("if (elem) {");
sb.append(" elem.setAttribute('tabindex', '-1');");
sb.append(" elem.focus();");
sb.append("}");
browser.execute(sb.toString());
}
public static void main(String[] args) {
new Main();
}
}
And here's my HTML document:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My page</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h1 class="title" id="nlzt_0001">
<a href="nlzt0002.smil#nlzt_0001">Introduction</a>
</h1>
<h1 id="rtrx_001d">
<a href="nlzt000a.smil#rtrx_001d">Summary</a>
</h1>
<h1 id="rtrx_001e">
<a href="nlzt000b.smil#rtrx_001e">First chapter</a>
</h1>
<h2 id="rtrx_001f">
<a href="nlzt000c.smil#rtrx_001f">1</a>
</h2>
<h1 id="rtrx_003e">
<a href="nlzt002b.smil#rtrx_003e">Second chapter</a>
</h1>
</body>
</html>