Skip to content

Commit 8fd00f7

Browse files
committed
Moved Google-docs JavaFX from EE 7 samples
This should be better at home in this repo.
1 parent acd1268 commit 8fd00f7

File tree

7 files changed

+262
-0
lines changed

7 files changed

+262
-0
lines changed

google-docs-javafx/client/pom.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.websocket.google-docs</groupId>
6+
<artifactId>google-docs-sample</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.websocket.google-docs</groupId>
12+
<artifactId>client</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>jar</packaging>
15+
16+
<properties>
17+
<javafx.version>1.8-ea-b123</javafx.version>
18+
<javafx.runtime.lib.jar>${java.home}/lib/jfxrt.jar</javafx.runtime.lib.jar>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>io.undertow</groupId>
24+
<artifactId>undertow-core</artifactId>
25+
<version>1.0.16.Final</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>io.undertow</groupId>
29+
<artifactId>undertow-websockets-jsr</artifactId>
30+
<version>1.0.16.Final</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>javax</groupId>
34+
<artifactId>javaee-api</artifactId>
35+
<version>7.0</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.oracle</groupId>
39+
<artifactId>javafx</artifactId>
40+
<version>${javafx.version}</version>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<!-- Build an executable JAR -->
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-jar-plugin</artifactId>
50+
<version>2.5</version>
51+
<configuration>
52+
<archive>
53+
<manifest>
54+
<addClasspath>true</addClasspath>
55+
<mainClass>org.javaee7.websocket.googledocs.client.GoogleDocClient</mainClass>
56+
</manifest>
57+
</archive>
58+
</configuration>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.codehaus.mojo</groupId>
62+
<artifactId>exec-maven-plugin</artifactId>
63+
<version>1.2.1</version>
64+
<executions>
65+
<execution>
66+
<phase>test</phase>
67+
<goals>
68+
<goal>java</goal>
69+
</goals>
70+
<configuration>
71+
<mainClass>org.javaee7.websocket.googledocs.client.GoogleDocClient</mainClass>
72+
</configuration>
73+
</execution>
74+
</executions>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
</project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.javaee7.websocket.googledocs.client;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
import java.util.logging.Level;
7+
import java.util.logging.Logger;
8+
import javafx.application.Application;
9+
import javafx.application.Platform;
10+
import javafx.beans.value.ChangeListener;
11+
import javafx.beans.value.ObservableValue;
12+
import javafx.scene.Scene;
13+
import javafx.scene.control.TextArea;
14+
import javafx.stage.Stage;
15+
16+
import javax.websocket.ClientEndpoint;
17+
import javax.websocket.ContainerProvider;
18+
import javax.websocket.DeploymentException;
19+
import javax.websocket.OnMessage;
20+
import javax.websocket.OnOpen;
21+
import javax.websocket.Session;
22+
import javax.websocket.WebSocketContainer;
23+
24+
/**
25+
* @author Arun Gupta
26+
*/
27+
@ClientEndpoint
28+
public class GoogleDocClient extends Application {
29+
30+
static TextArea textarea;
31+
32+
public static void main(String[] args) {
33+
launch(args);
34+
}
35+
36+
@Override
37+
public void start(Stage stage) throws Exception {
38+
final Session session = connectToServer();
39+
System.out.println("Connected to server: " + session.getId());
40+
stage.setTitle("Google Docs Emulator using WebSocket");
41+
textarea = new TextArea();
42+
textarea.textProperty().addListener(
43+
new ChangeListener<String>() {
44+
45+
@Override
46+
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
47+
System.out.println("New value: " + newValue);
48+
49+
try {
50+
session.getBasicRemote().sendText(newValue);
51+
} catch (IOException ex) {
52+
Logger.getLogger(GoogleDocClient.class.getName()).log(Level.SEVERE, null, ex);
53+
}
54+
}
55+
56+
}
57+
);
58+
59+
textarea.setPrefSize(500, 300);
60+
textarea.setWrapText(true);
61+
Scene scene = new Scene(textarea);
62+
stage.setScene(scene);
63+
stage.show();
64+
}
65+
66+
@OnOpen
67+
public void onOpen(Session session) {
68+
System.out.println("Connected to endpoint: " + session.getBasicRemote());
69+
}
70+
71+
@OnMessage
72+
public void onMessage(String message, Session session) throws IOException {
73+
final String newMessage = message;
74+
System.out.println("Received message in client: " + message);
75+
Platform.runLater(new Runnable() {
76+
@Override
77+
public void run() {
78+
textarea.setText(newMessage);
79+
textarea.positionCaret(newMessage.length());
80+
}
81+
});
82+
83+
}
84+
85+
private Session connectToServer() throws URISyntaxException, DeploymentException, IOException {
86+
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
87+
return container.connectToServer(GoogleDocClient.class, new URI("ws://localhost:8080/server/websocket"));
88+
}
89+
90+
}

google-docs-javafx/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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>
3+
4+
<parent>
5+
<groupId>org.javaee7.extra</groupId>
6+
<artifactId>extra-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.extra.google-docs</groupId>
12+
<artifactId>google-docs-sample</artifactId>
13+
<packaging>pom</packaging>
14+
<name>Java EE 7 WebSocket Google Docs</name>
15+
16+
<modules>
17+
<module>server</module>
18+
<module>client</module>
19+
</modules>
20+
21+
</project>

google-docs-javafx/readme.asciidoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Install JavaFX library locally:
2+
3+
[source,text]
4+
----
5+
mvn install:install-file -Dfile=$JAVA_HOME/jre/lib/ext/jfxrt.jar -DgroupId=com.oracle -DartifactId=javafx -Dversion=1.8-ea-b123 -Dpackaging=jar
6+
----
7+

google-docs-javafx/server/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.websocket.google-docs</groupId>
6+
<artifactId>google-docs-sample</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.websocket.google-docs</groupId>
12+
<artifactId>server</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.javaee7.websocket.googledocs.server;
2+
3+
import java.io.IOException;
4+
import java.util.logging.Level;
5+
import java.util.logging.Logger;
6+
7+
import javax.websocket.EncodeException;
8+
import javax.websocket.OnMessage;
9+
import javax.websocket.OnOpen;
10+
import javax.websocket.Session;
11+
import javax.websocket.server.ServerEndpoint;
12+
13+
/**
14+
* @author Arun Gupta
15+
*/
16+
@ServerEndpoint(value = "/websocket")
17+
public class GoogleDocServer {
18+
19+
private static final Logger LOGGER = Logger.getLogger(GoogleDocServer.class.getName());
20+
21+
@OnOpen
22+
public void onOpen(Session client) {
23+
LOGGER.log(Level.INFO, "connected");
24+
}
25+
26+
@OnMessage
27+
public void broadcastText(String text, Session session) throws IOException, EncodeException {
28+
LOGGER.log(Level.INFO, "broadcastText: {0}", text);
29+
for (Session peer : session.getOpenSessions()) {
30+
if (!peer.equals(session)) {
31+
peer.getBasicRemote().sendText(text);
32+
}
33+
}
34+
}
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<!--
3+
To change this license header, choose License Headers in Project Properties.
4+
To change this template file, choose Tools | Templates
5+
and open the template in the editor.
6+
-->
7+
<html>
8+
<head>
9+
<title>Google Docs Server</title>
10+
<meta charset="UTF-8">
11+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
12+
</head>
13+
<body>
14+
<div>Google Docs Server</div>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)