Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 180544 Details for
Bug 327385
[Browser] Add JSON notation support to BrowserFunction as map
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Modified 307 snippet to send JSON object.
Snippet307JSON.java (text/java), 3.99 KB, created by
Christopher Deckers
on 2010-10-09 06:10:41 EDT
(
hide
)
Description:
Modified 307 snippet to send JSON object.
Filename:
MIME Type:
Creator:
Christopher Deckers
Created:
2010-10-09 06:10:41 EDT
Size:
3.99 KB
patch
obsolete
>package test; > >/* > * 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.SWT; >import org.eclipse.swt.SWTError; >import org.eclipse.swt.browser.Browser; >import org.eclipse.swt.browser.BrowserFunction; >import org.eclipse.swt.browser.LocationAdapter; >import org.eclipse.swt.browser.LocationEvent; >import org.eclipse.swt.browser.ProgressAdapter; >import org.eclipse.swt.browser.ProgressEvent; >import org.eclipse.swt.layout.FillLayout; >import org.eclipse.swt.widgets.Display; >import org.eclipse.swt.widgets.Shell; > >public class Snippet307JSON { > >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 ()); > final BrowserFunction function = new CustomFunction (browser, "theJavaFunction"); > > 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 { > CustomFunction (Browser browser, String name) { > super (browser, name); > } > @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"); > } 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({\n"); > buffer.append (" 'number': 12,\n"); > buffer.append (" 'boolean': false,\n"); > buffer.append (" 'null': null,\n"); > buffer.append (" 'array': [3.6, ['swt', true]],\n"); > buffer.append (" 'string': 'eclipse'\n"); > buffer.append (" });\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 (); >} > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 327385
:
180544
|
180565