My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.applet.Applet;

import netscape.javascript.JSException;
import netscape.javascript.JSObject;

/**
* Enables Java to read joystick values via JavaScript using LiveConnect.
*/
public class Joystick implements Controller {
/**
* The JavaScript object that wraps around the joystick plug-in.
*/
private final JSObject plugin;

/**
* Creates a joystick instance in the applet's containing page.
*
* @param applet the calling applet
*/
public Joystick(JSObject plugin) {
this.plugin = plugin;
System.out.println(isConnected());
}

/*
* (non-Javadoc)
* @see Controller#isConnected()
*/
public boolean isConnected() {
try {
return ((Boolean) plugin.call("isConnected", null)).booleanValue();
} catch (JSException e) {
return false;
}
}

/*
* (non-Javadoc)
* @see Controller#poll()
*/
public void poll() {
try {
plugin.call("poll", null);
} catch (JSException e) {}
}

/*
* (non-Javadoc)
* @see Controller#getX()
*/
public int getX() {
try {
return ((Number) plugin.getMember("x")).intValue();
} catch (JSException e) {
return CENTRE;
}
}

/*
* (non-Javadoc)
* @see Controller#getY()
*/
public int getY() {
try {
return ((Number) plugin.getMember("y")).intValue();
} catch (JSException e) {
return CENTRE;
}
}

public int getZ() {
try {
return ((Number) plugin.getMember("z")).intValue();
} catch (JSException e) {
return CENTRE;
}
}

/*
* (non-Javadoc)
* @see Controller#getButtons()
*/
public int getButtons() {
try {
return ((Number) plugin.getMember("buttons")).intValue();
} catch (JSException e) {
return 0;
}
}

/**
* Creates an instance of the {@code Joystick} class, dynamically
* inserting the relevant browser plug-in into the document containing
* {@code applet}.
*
* @param applet applet instance
* @return an instance of {@code Joystick} (or {@code null} if no plug-in counterpart can be created)
*/
public static Joystick createJoystickInstance(Applet applet) {
JSObject win = null;
try {
win = (JSObject) JSObject.getWindow(applet);
} catch (JSException e) {
return null;
}
JSObject plugin = createPluginInstance(win);
if (plugin != null) {
return new Joystick(plugin);
}
return null;
}

/**
* Creates an instance of the plug-in object in the browser.
*
* @param win browser's window object
* @return plug-in instance or {@code null} if one cannot be created
*/
public static JSObject createPluginInstance(JSObject win) {
if (win == null) {
/*
* Early exit, since not all browsers support the LiveConnect API.
*/
return null;
}

JSObject obj = (JSObject) win.eval("document.createElement('object')");
try {
obj.setMember("classid", "CLSID:3AE9ED90-4B59-47A0-873B-7B71554B3C3E");
if (obj.call("setDevice", new Object[] {new Integer(0)}) != null) {
/*
* IE always returns a Boolean for this call, so any non-null
* value is success at this point/
*/
return obj;
}
} catch (JSException e) {
/*
* Either we're not using IE or the plug-in is not installed,
* so ignore any exceptions and try the next method.
*/
}

obj = (JSObject) win.eval("document.createElement('embed')");
if (obj != null) {
JSObject doc = (JSObject) win.eval("document.body");
try {
obj.setMember("type", "application/x-vnd.numfum-joystick");
obj.setMember("width", 0);
obj.setMember("height", 0);
/*
* Before accessing the plug-in's script interface it needs to
* be added to the page. If the 'setDevice' call fails, the
* plug-in is assumed to either not be installed or not working
* in this browser, in which case it is removed in the catch.
*/
doc.call("appendChild", new Object[] {obj, doc});
if (obj.call("setDevice", new Object[] {new Double(0)}) != null) {
/*
* As with the code for IE, any non-null value is a
* success. Note that doubles are passed for the index due
* to workarounds for Firefox and Safari (internal to the
* plug-in.
*/
return obj;
}
} catch (JSException e) {
if (obj != null) {
doc.call("removeChild", new Object[] {obj});
}
}
}

return null;
}
}

Change log

r30 by cwoffenden on Jun 24, 2010   Diff
Updated docs and enabled the Eclipse
project to build standalone.
Go to: 
Project members, sign in to write a code review

Older revisions

r16 by cwoffenden on Jul 1, 2009   Diff
General tidy. Exposed more plug-in
methods (isConnected and getZ).
r15 by cwoffenden on Jun 30, 2009   Diff
Initial import.
All revisions of this file

File info

Size: 4273 bytes, 176 lines
Powered by Google Project Hosting