Commits

Micha Kops committed 765e127 Draft

Data handling between HTML page and SWT frame added.

  • Participants
  • Parent commits 7fabb5e

Comments (0)

Files changed (3)

+<html>
+	<head>
+		<title>hasCode.com HTML-SWT-RichClient Example</title>
+		<script type="text/javascript">
+		function passToFrame(){
+			var response = jsToSwtCallback(12345, 'there is no spoon');
+			alert(response);
+			
+		}
+		</script>
+	</head>
+	<body>
+		<h1>HTML-SWT-RichClient Example</h1>
+		<hr/>
+		<input type="button" value="Callback to SWT Frame" onclick="javascript:passToFrame()"/>
+		<br/>
+		<em>2012 Micha Kops / hasCode.com</em>
+	</body>
+</html>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-  <groupId>com.hascode.tutorial</groupId>
-  <artifactId>html-swt-richclient</artifactId>
-  <version>0.0.1</version>
-  <url>http://www.hascode.com</url>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.hascode.tutorial</groupId>
+	<artifactId>html-swt-richclient</artifactId>
+	<version>0.0.1</version>
+	<url>http://www.hascode.com</url>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.eclipse.swt</groupId>
+			<artifactId>${swt.os.type}</artifactId>
+			<version>3.8</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>2.5.1</version>
+				<configuration>
+					<source>1.6</source>
+					<target>1.6</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+
+	<repositories>
+		<repository>
+			<id>swt-maven-repo</id>
+			<url>https://swt-repo.googlecode.com/svn/repo/</url>
+		</repository>
+	</repositories>
+
+
+	<profiles>
+		<profile>
+			<id>linux_32</id>
+			<activation>
+				<os>
+					<name>linux_x32</name>
+				</os>
+			</activation>
+			<properties>
+				<swt.os.type>org.eclipse.swt.gtk.linux.x86</swt.os.type>
+			</properties>
+		</profile>
+		<profile>
+			<id>linux_64</id>
+			<activation>
+				<os>
+					<name>linux</name>
+				</os>
+				<activeByDefault>true</activeByDefault>
+			</activation>
+			<properties>
+				<swt.os.type>org.eclipse.swt.gtk.linux.x86_64</swt.os.type>
+			</properties>
+		</profile>
+		<profile>
+			<id>mac_64</id>
+			<activation>
+				<os>
+					<name>mac os x 64</name>
+				</os>
+			</activation>
+			<properties>
+				<swt.os.type>org.eclipse.swt.cocoa.macosx.x86_64</swt.os.type>
+				<platformVmArgs>-XstartOnFirstThread</platformVmArgs>
+			</properties>
+		</profile>
+		<profile>
+			<id>windows_32</id>
+			<activation>
+				<os>
+					<family>windows</family>
+				</os>
+			</activation>
+			<properties>
+				<swt.os.type>org.eclipse.swt.win32.win32.x86</swt.os.type>
+			</properties>
+		</profile>
+		<profile>
+			<id>windows_64</id>
+			<activation>
+				<os>
+					<family>windows_x64</family>
+				</os>
+			</activation>
+			<properties>
+				<swt.os.type>org.eclipse.swt.win32.win32.x86_64</swt.os.type>
+			</properties>
+		</profile>
+	</profiles>
+
 </project>

src/main/java/com/hascode/app/RichClient.java

+package com.hascode.app;
+
+import java.io.File;
+import java.util.Date;
+
+import org.eclipse.swt.SWT;
+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.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+
+public class RichClient {
+	private static final File HTML_FILE = new File("app/main.html");
+
+	private Shell shell;
+	private Display display;
+	private Browser browser;
+	private GridLayout shellLayout;
+	private GridData browserLayout;
+
+	public static void main(final String... args) {
+		new RichClient().initializeApplication();
+	}
+
+	public void initializeApplication() {
+		display = new Display();
+		shell = new Shell(display);
+		shell.setText("hasCode.com SWT HTML App");
+		shell.setSize(800, 600);
+		shellLayout = new GridLayout();
+		shellLayout.numColumns = 2;
+		shell.setLayout(shellLayout);
+
+		browser = new Browser(shell, SWT.NONE);
+		browserLayout = new GridData();
+		browserLayout.verticalAlignment = GridData.FILL;
+		browserLayout.horizontalAlignment = GridData.FILL;
+		browserLayout.grabExcessHorizontalSpace = true;
+		browserLayout.grabExcessVerticalSpace = true;
+		browser.setLayoutData(browserLayout);
+		browser.setJavascriptEnabled(true);
+
+		browser.setUrl(HTML_FILE.toURI().toString());
+
+		final BrowserFunction debugCallback = new CallbackDebugFunction(
+				browser, "jsToSwtCallback");
+		browser.addProgressListener(new ProgressAdapter() {
+			@Override
+			public void completed(final ProgressEvent event) {
+				browser.addLocationListener(new LocationAdapter() {
+					@Override
+					public void changed(final LocationEvent event) {
+						browser.removeLocationListener(this);
+						debugCallback.dispose();
+					}
+				});
+			}
+		});
+		System.out.println("browser initialized");
+
+		shell.open();
+		while (!shell.isDisposed()) {
+			if (!display.readAndDispatch()) {
+				display.sleep();
+			}
+		}
+		display.dispose();
+	}
+
+	static class CallbackDebugFunction extends BrowserFunction {
+		public CallbackDebugFunction(final Browser browser, final String name) {
+			super(browser, name);
+		}
+
+		@Override
+		public Object function(final Object[] arguments) {
+			System.out.println("callback triggered by js function");
+			for (final Object o : arguments) {
+				System.out.println("argument received: " + o.toString());
+			}
+			return "The date is: " + new Date().toString();
+		}
+
+	}
+
+}