-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnippet307.java
128 lines (117 loc) · 3.92 KB
/
Snippet307.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
* Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*
* Browser example snippet: call Java from JavaScript.
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.5
*/
import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class Snippet307 {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout (new FillLayout ());
shell.setBounds (10,10,300,200);
final Browser browser;
try {
browser = new Browser (shell, SWT.NONE);
} catch (SWTError e) {
System.out.println ("Could not instantiate Browser: " + e.getMessage ());
display.dispose();
return;
}
browser.setText (createHTML ());
Text tt = new Text(shell, SWT.NONE);
tt.setText("abc test");
final BrowserFunction function = new CustomFunction (browser, "theJavaFunction", tt);
browser.addProgressListener (new ProgressAdapter () {
@Override
public void completed (ProgressEvent event) {
browser.addLocationListener (new LocationAdapter () {
@Override
public void changed (LocationEvent event) {
browser.removeLocationListener (this);
System.out.println ("left java function-aware page, so disposed CustomFunction");
function.dispose ();
}
});
}
});
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ())
display.sleep ();
}
display.dispose ();
}
static class CustomFunction extends BrowserFunction {
Text tt;
CustomFunction (Browser browser, String name, Text tt) {
super (browser, name);
this.tt = tt;
}
@Override
public Object function (Object[] arguments) {
System.out.println ("theJavaFunction() called from javascript with args:");
for (int i = 0; i < arguments.length; i++) {
Object arg = arguments[i];
if (arg == null) {
System.out.println ("\t-->null");
tt.setText("jfasdksfhjk");
} else {
System.out.println ("\t-->" + arg.getClass ().getName () + ": " + arg.toString ());
}
}
Object returnValue = new Object[] {
new Short ((short)3),
new Boolean (true),
null,
new Object[] {"a string", new Boolean (false)},
"hi",
new Float (2.0f / 3.0f),
};
//int z = 3 / 0; // uncomment to cause a java error instead
return returnValue;
}
}
static String createHTML () {
StringBuffer buffer = new StringBuffer ();
buffer.append ("<html>\n");
buffer.append ("<head>\n");
buffer.append ("<script language=\"JavaScript\">\n");
buffer.append ("function function1() {\n");
buffer.append (" var result;\n");
buffer.append (" try {\n");
buffer.append (" result = theJavaFunction(12, false, null, [3.6, ['swt', true]], 'eclipse');\n");
buffer.append (" } catch (e) {\n");
buffer.append (" alert('a java error occurred: ' + e.message);\n");
buffer.append (" return;\n");
buffer.append (" }\n");
buffer.append (" for (var i = 0; i < result.length; i++) {\n");
buffer.append (" alert('returned ' + i + ': ' + result[i]);\n");
buffer.append (" }\n");
buffer.append ("}\n");
buffer.append ("</script>\n");
buffer.append ("</head>\n");
buffer.append ("<body>\n");
buffer.append ("<input id=button type=\"button\" value=\"Push to Invoke Java\" onclick=\"function1();\">\n");
buffer.append ("<p><a href=\"http://www.eclipse.org\">go to eclipse.org</a>\n");
buffer.append ("</body>\n");
buffer.append ("</html>\n");
return buffer.toString ();
}
}