80
|
1 |
/*
|
87
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
80
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.microedition.m2g;
|
|
19 |
|
|
20 |
import java.io.ByteArrayInputStream;
|
|
21 |
import java.io.IOException;
|
|
22 |
import java.io.InputStream;
|
|
23 |
import javax.microedition.m2g.ExternalResourceHandler;
|
|
24 |
import javax.microedition.m2g.SVGImage;
|
|
25 |
import org.w3c.dom.Document;
|
|
26 |
import org.w3c.dom.svg.SVGElement;
|
|
27 |
import org.w3c.dom.DOMException;
|
|
28 |
import com.nokia.microedition.m2g.connection.*;
|
|
29 |
import com.nokia.mj.impl.utils.Logger;
|
|
30 |
import com.nokia.mj.impl.utils.StreamUtils;
|
|
31 |
|
|
32 |
|
|
33 |
/*
|
|
34 |
* Image implementation
|
|
35 |
*/
|
|
36 |
public class M2GSVGImage
|
|
37 |
extends SVGImage
|
|
38 |
{
|
|
39 |
//--------------------------------------------------
|
|
40 |
// STATIC CONSTANTS
|
|
41 |
//--------------------------------------------------
|
87
|
42 |
|
80
|
43 |
|
|
44 |
//--------------------------------------------------
|
|
45 |
// VARIABLES
|
|
46 |
//--------------------------------------------------
|
|
47 |
M2GDocument iDocument = null;
|
|
48 |
private SVGElement iFocusedNode = null;
|
|
49 |
|
|
50 |
//--------------------------------------------------
|
|
51 |
// METHODS
|
|
52 |
//--------------------------------------------------
|
|
53 |
/**
|
|
54 |
* Constructor
|
|
55 |
*/
|
|
56 |
M2GSVGImage()
|
|
57 |
{
|
|
58 |
super();
|
|
59 |
}
|
|
60 |
|
|
61 |
/**
|
|
62 |
* @see javax.microedition.m2g.SVGImage#activate()
|
|
63 |
*/
|
|
64 |
public void activate()
|
|
65 |
{
|
|
66 |
SVGElement node = iFocusedNode;
|
|
67 |
while (node != null)
|
|
68 |
{
|
|
69 |
((M2GDocument)getDocument()).handleEvent(new M2GEvent(
|
|
70 |
M2GSVGConstants.EVENT_ACTIVATE,
|
|
71 |
node,
|
|
72 |
M2GEvent.EVENT_TARGET));
|
|
73 |
node = (SVGElement)node.getParentNode();
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
/**
|
|
78 |
* @see javax.microedition.m2g.SVGImage#dispatchMouseEvent()
|
|
79 |
*/
|
|
80 |
public void dispatchMouseEvent(String type, int x, int y)
|
|
81 |
{
|
|
82 |
if (type == null || type.equals(""))
|
|
83 |
{
|
|
84 |
Logger.ELOG(Logger.EJavaUI, "dispatchMouseEvent() NullPointerException");
|
|
85 |
throw new NullPointerException(/*SF*/"The type is null."/*SF*/);
|
|
86 |
}
|
|
87 |
if (x < 0 || y < 0)
|
|
88 |
{
|
|
89 |
Logger.ELOG(Logger.EJavaUI,
|
|
90 |
"dispatchMouseEvent() IllegalArgumentException");
|
|
91 |
throw new IllegalArgumentException(/*SF*/"The x or y values are negative."/*SF*/);
|
|
92 |
}
|
|
93 |
if (!type.equals(M2GSVGConstants.EVENT_CLICK))
|
|
94 |
{
|
|
95 |
Logger.ELOG(Logger.EJavaUI,
|
|
96 |
"dispatchMouseEvent() DOMException - only click event is supported");
|
|
97 |
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, /*SF*/"The event type is not supported."/*SF*/);
|
|
98 |
}
|
|
99 |
int elementHandle = _dispatchMouseEvent(
|
|
100 |
iDocument.getNativeSVGProxyHandle(),
|
|
101 |
iDocument.getHandle(), x, y);
|
|
102 |
|
|
103 |
SVGElement targetElement = M2GSVGElement.buildElement(elementHandle, iDocument);
|
|
104 |
SVGElement usedFromElement = null;
|
|
105 |
if ((targetElement != null) && ((M2GSVGElement)targetElement).isUsed())
|
|
106 |
{
|
|
107 |
usedFromElement = ((M2GSVGElement)((M2GSVGElement)targetElement).getUsedFromElement());
|
|
108 |
}
|
|
109 |
if (targetElement instanceof M2GSVGElement)
|
|
110 |
{
|
|
111 |
iFocusedNode = targetElement;
|
|
112 |
}
|
|
113 |
while (targetElement != null)
|
|
114 |
{
|
|
115 |
// click event
|
|
116 |
iDocument.handleEvent(new M2GEvent(
|
|
117 |
M2GSVGConstants.EVENT_CLICK, targetElement, M2GEvent.EVENT_TARGET));
|
|
118 |
// DOMActivate event
|
|
119 |
iDocument.handleEvent(new M2GEvent(
|
|
120 |
M2GSVGConstants.EVENT_ACTIVATE, targetElement, M2GEvent.EVENT_TARGET));
|
|
121 |
// Gets parent node
|
|
122 |
targetElement = (SVGElement)targetElement.getParentNode();
|
|
123 |
}
|
|
124 |
// The "target element" is only a clone. The "used from element" is the actual element.
|
|
125 |
if (usedFromElement != null)
|
|
126 |
{
|
|
127 |
iDocument.handleEvent(new M2GEvent(
|
|
128 |
M2GSVGConstants.EVENT_CLICK, usedFromElement, M2GEvent.WRAPPED_EVENT_TARGET));
|
|
129 |
iDocument.handleEvent(new M2GEvent(
|
|
130 |
M2GSVGConstants.EVENT_ACTIVATE, usedFromElement, M2GEvent.WRAPPED_EVENT_TARGET));
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
/**
|
|
135 |
* @see javax.microedition.m2g.SVGImage#focusOn()
|
|
136 |
*/
|
|
137 |
public void focusOn(SVGElement element)
|
|
138 |
{
|
|
139 |
SVGElement tempNode = element;
|
|
140 |
// If focusOn is called on the same element, do not focus out.
|
|
141 |
boolean callFocusOut =
|
|
142 |
(((iFocusedNode != null) && (iFocusedNode == element ? false : true)) || ((iFocusedNode != null) && (element == null)));
|
|
143 |
//Checking to see if the element is hooked into the tree.
|
|
144 |
if (element != null)
|
|
145 |
{
|
|
146 |
tempNode = element;
|
|
147 |
while (tempNode != null)
|
|
148 |
{
|
|
149 |
if ((tempNode.getLocalName()).equals(M2GSVGConstants.ROOT_SVG_ELEMENT_NAME))
|
|
150 |
{
|
|
151 |
// Breaks if tempNode is root svg
|
|
152 |
break;
|
|
153 |
}
|
|
154 |
tempNode = (SVGElement)tempNode.getParentNode();
|
|
155 |
}
|
|
156 |
// This indicates that the element does not contain a root element,
|
|
157 |
// therefore does not belong to the document.
|
|
158 |
if (tempNode == null)
|
|
159 |
{
|
|
160 |
Logger.ELOG(Logger.EJavaUI, "focusOn() - DOMException invalid element");
|
|
161 |
throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, /*SF*/"Invalid element."/*SF*/);
|
|
162 |
}
|
|
163 |
}
|
|
164 |
// Focus out from the current focused node and its parents
|
|
165 |
if (callFocusOut)
|
|
166 |
{
|
|
167 |
if (iFocusedNode instanceof M2GSVGElement)
|
|
168 |
{
|
|
169 |
_focusOut(
|
|
170 |
((M2GSVGElement)iFocusedNode).getNativeSVGProxyHandle(),
|
|
171 |
((M2GSVGElement)iFocusedNode).getDocument().getHandle(),
|
|
172 |
((M2GSVGElement)iFocusedNode).getHandle());
|
|
173 |
}
|
|
174 |
|
|
175 |
// Focus out the current focused node and its parents
|
|
176 |
tempNode = iFocusedNode;
|
|
177 |
while (tempNode != null)
|
|
178 |
{
|
|
179 |
iDocument.handleEvent(new M2GEvent(
|
|
180 |
M2GSVGConstants.EVENT_FOCUS_OUT, tempNode, M2GEvent.EVENT_TARGET));
|
|
181 |
tempNode = (SVGElement)tempNode.getParentNode();
|
|
182 |
}
|
|
183 |
}
|
|
184 |
//focus in for element and its parents
|
|
185 |
if ((element != null) && (element instanceof M2GSVGElement))
|
|
186 |
{
|
|
187 |
_focusOn(
|
|
188 |
((M2GSVGElement)element).getNativeSVGProxyHandle(),
|
|
189 |
((M2GSVGElement)element).getDocument().getHandle(),
|
87
|
190 |
((M2GSVGElement)element).getHandle());
|
80
|
191 |
}
|
|
192 |
tempNode = element;
|
|
193 |
while (tempNode != null)
|
|
194 |
{
|
|
195 |
iDocument.handleEvent(new M2GEvent(
|
|
196 |
M2GSVGConstants.EVENT_FOCUS_IN, tempNode, M2GEvent.EVENT_TARGET));
|
|
197 |
tempNode = (SVGElement)tempNode.getParentNode();
|
|
198 |
}
|
|
199 |
// Sets the new focus
|
|
200 |
iFocusedNode = element;
|
|
201 |
}
|
|
202 |
|
|
203 |
/**
|
|
204 |
* @see javax.microedition.m2g.SVGImage#getDocument()
|
|
205 |
*/
|
|
206 |
public Document getDocument()
|
|
207 |
{
|
|
208 |
return iDocument;
|
|
209 |
}
|
|
210 |
|
|
211 |
/**
|
|
212 |
* @see javax.microedition.m2g.ScalableImage#getViewportHeight()
|
|
213 |
*/
|
|
214 |
public synchronized int getViewportHeight()
|
|
215 |
{
|
|
216 |
return iDocument.getViewportHeight();
|
|
217 |
}
|
|
218 |
|
|
219 |
|
|
220 |
/*
|
|
221 |
* @see javax.microedition.m2g.ScalableImage#getViewportWidth()
|
|
222 |
*/
|
|
223 |
public synchronized int getViewportWidth()
|
|
224 |
{
|
|
225 |
return iDocument.getViewportWidth();
|
|
226 |
}
|
|
227 |
|
|
228 |
/**
|
|
229 |
* @see javax.microedition.m2g.SVGImage#incrementTime()
|
|
230 |
*/
|
|
231 |
public void incrementTime(float seconds)
|
|
232 |
{
|
|
233 |
((M2GSVGSVGElement)iDocument.getDocumentElement()).incrementTime(seconds);
|
|
234 |
}
|
|
235 |
|
|
236 |
/**
|
|
237 |
* @see javax.microedition.m2g.ScalableImage#requestCompleted()
|
|
238 |
*/
|
|
239 |
public void requestCompleted(String uri, InputStream resourceData) throws IOException
|
|
240 |
{
|
|
241 |
if (uri == null)
|
|
242 |
{
|
|
243 |
Logger.ELOG(Logger.EJavaUI,
|
|
244 |
"requestCompleted() throw NullPointerException");
|
|
245 |
throw new NullPointerException(/*SF*/"The URI is null."/*SF*/);
|
|
246 |
}
|
|
247 |
|
|
248 |
if (resourceData != null)
|
|
249 |
{
|
|
250 |
byte receiveData[] = StreamUtils.readBytesFromStream(resourceData, -1);
|
|
251 |
iDocument.requestCompleted(uri, receiveData);
|
|
252 |
}
|
|
253 |
else
|
|
254 |
{
|
|
255 |
// Indicates that the requested resource could not be
|
|
256 |
// fetched by the ResourceHandler or application,
|
|
257 |
// and in this event the SVG engine will not make further attempts
|
|
258 |
// to load this resource.
|
|
259 |
// Pass the null to the native side so it can delete the image
|
|
260 |
iDocument.requestCompleted(uri, null);
|
|
261 |
}
|
|
262 |
}
|
|
263 |
|
|
264 |
/**
|
|
265 |
* Set document
|
|
266 |
* @param aDocument -
|
|
267 |
*/
|
|
268 |
public void setDocument(M2GDocument aDocument)
|
|
269 |
{
|
|
270 |
iDocument = aDocument;
|
|
271 |
}
|
|
272 |
|
|
273 |
/**
|
|
274 |
* @see javax.microedition.m2g.ScalableImage#setViewportHeight()
|
|
275 |
*/
|
|
276 |
public synchronized void setViewportHeight(int height)
|
|
277 |
{
|
|
278 |
iDocument.setViewportHeight(height);
|
|
279 |
}
|
|
280 |
|
|
281 |
/**
|
|
282 |
* @see javax.microedition.m2g.ScalableImage#setViewportWidth()
|
|
283 |
*/
|
|
284 |
public synchronized void setViewportWidth(int width)
|
|
285 |
{
|
|
286 |
iDocument.setViewportWidth(width);
|
|
287 |
}
|
|
288 |
|
|
289 |
//--------------------------------------------------
|
|
290 |
// STATIC METHODS
|
|
291 |
//--------------------------------------------------
|
|
292 |
/*
|
|
293 |
* @see javax.microedition.m2g.SVGImage#createEmptyImage()
|
|
294 |
*/
|
|
295 |
public static M2GSVGImage buildEmptyImage(ExternalResourceHandler handler)
|
|
296 |
{
|
|
297 |
try
|
|
298 |
{
|
|
299 |
return M2GSVGImage.buildImage(
|
|
300 |
new ByteArrayInputStream(
|
|
301 |
M2GSVGConstants.getInstance().iEmptySvgDocument.getBytes()),
|
|
302 |
handler, null, null);
|
|
303 |
}
|
|
304 |
catch (IOException e)
|
|
305 |
{
|
|
306 |
Logger.ELOG(Logger.EJavaUI, "M2GSVGImage: buildEmptyImage - IOEception: " + e.toString());
|
|
307 |
}
|
|
308 |
return null;
|
|
309 |
}
|
|
310 |
|
|
311 |
/**
|
|
312 |
* Build svg image
|
|
313 |
* @see javax.microedition.m2g.ScalableImage#createImage()
|
|
314 |
* @param stream Plain text stream that contains svg data
|
|
315 |
* @param handler External resource handler. If null then default
|
|
316 |
* external resource handler is used.
|
|
317 |
* @param baseUrl A base url from where a document is downloaded.
|
|
318 |
* @param suffixUrl The suffix url according to the DRM opening mode of the SVG file
|
|
319 |
* Null if it's not known.
|
|
320 |
*/
|
|
321 |
public static M2GSVGImage buildImage(
|
|
322 |
InputStream stream,
|
|
323 |
ExternalResourceHandler handler,
|
|
324 |
String baseUrl,
|
|
325 |
String suffixUrl) throws IOException
|
|
326 |
{
|
|
327 |
if (stream == null)
|
|
328 |
{
|
|
329 |
throw new NullPointerException();
|
|
330 |
}
|
87
|
331 |
|
80
|
332 |
|
|
333 |
String strData = new String(StreamUtils.readBytesFromStream(stream, -1));
|
|
334 |
|
|
335 |
// Default handler is used if the specified handler is null
|
|
336 |
if (handler == null)
|
|
337 |
{
|
|
338 |
handler = M2GConnectionFactory.getExternalResourceHandler(null);
|
|
339 |
}
|
|
340 |
|
|
341 |
// Creates and setups svg image
|
|
342 |
M2GSVGImage image = new M2GSVGImage();
|
87
|
343 |
|
80
|
344 |
M2GDocument document = M2GDocument.buildDocument(
|
|
345 |
image,
|
|
346 |
baseUrl,
|
|
347 |
suffixUrl,
|
|
348 |
strData,
|
|
349 |
handler);
|
|
350 |
// Checks document validity
|
87
|
351 |
|
80
|
352 |
image.setDocument(document);
|
87
|
353 |
|
80
|
354 |
if (handler != null)
|
|
355 |
{
|
|
356 |
// Gets size of external resources
|
|
357 |
int itemCount = _getExternalListSize(
|
|
358 |
document.getNativeSVGProxyHandle(),
|
|
359 |
document.getHandle());
|
|
360 |
|
|
361 |
// Loops through external items
|
|
362 |
for (int index = 0; index < itemCount; index++)
|
|
363 |
{
|
|
364 |
String url = _getExternalListItem(
|
|
365 |
document.getNativeSVGProxyHandle(),
|
|
366 |
document.getHandle(),
|
|
367 |
index);
|
|
368 |
// Calls an external implementation of the resource handler
|
|
369 |
if ((url != null) && !(url.equals("")))
|
|
370 |
{
|
|
371 |
document.invokeResourceHandler(url);
|
|
372 |
}
|
|
373 |
}
|
|
374 |
}
|
87
|
375 |
|
80
|
376 |
// initialise the viewport
|
|
377 |
_initViewport(document.getNativeSVGProxyHandle(),
|
|
378 |
document.getHandle());
|
87
|
379 |
|
80
|
380 |
|
|
381 |
return image;
|
|
382 |
}
|
|
383 |
|
|
384 |
/**
|
|
385 |
* @see javax.microedition.m2g.ScalableImage#createImage()
|
|
386 |
*/
|
|
387 |
public static M2GSVGImage buildImage(
|
|
388 |
String url, ExternalResourceHandler handler) throws IOException
|
|
389 |
{
|
|
390 |
// Checks passed parameters
|
|
391 |
if (url == null)
|
|
392 |
{
|
|
393 |
Logger.ELOG(Logger.EJavaUI,
|
|
394 |
"M2GSVGImage: 2 buildImage() - url is null");
|
|
395 |
throw new NullPointerException();
|
|
396 |
}
|
|
397 |
if (url.length() == 0)
|
|
398 |
{
|
|
399 |
Logger.ELOG(Logger.EJavaUI,
|
|
400 |
"M2GSVGImage: 2 buildImage() - url is empty");
|
|
401 |
throw new IllegalArgumentException();
|
|
402 |
}
|
|
403 |
// Default handler is used if the specified handler is null
|
|
404 |
if (handler == null)
|
|
405 |
{
|
|
406 |
handler = M2GConnectionFactory.getExternalResourceHandler(url);
|
|
407 |
}
|
|
408 |
|
|
409 |
// Open the url and get input stream
|
|
410 |
M2GSVGImage image = null;
|
|
411 |
M2GConnectionProxy connectionProxy = null;
|
|
412 |
try
|
|
413 |
{
|
|
414 |
connectionProxy = M2GConnectionFactory.create(url);
|
|
415 |
image = M2GSVGImage.buildImage(
|
|
416 |
connectionProxy.getInputStream(),
|
|
417 |
handler,
|
|
418 |
connectionProxy.getBaseUrl(),
|
|
419 |
connectionProxy.getSuffixUrl());
|
|
420 |
// Set connection policy
|
|
421 |
if (image != null)
|
|
422 |
{
|
|
423 |
Document doc = image.getDocument();
|
|
424 |
if (doc != null)
|
|
425 |
{
|
|
426 |
((M2GDocument)doc).setConnectionPolicy(
|
|
427 |
connectionProxy.getConnectionPolicy());
|
|
428 |
}
|
|
429 |
}
|
|
430 |
return image;
|
|
431 |
}
|
|
432 |
finally
|
|
433 |
{
|
|
434 |
if (connectionProxy != null)
|
|
435 |
{
|
|
436 |
connectionProxy.close();
|
|
437 |
connectionProxy = null;
|
|
438 |
}
|
|
439 |
}
|
|
440 |
}
|
|
441 |
|
|
442 |
//--------------------------------------------------
|
|
443 |
// NATIVE METHODS
|
|
444 |
//--------------------------------------------------
|
|
445 |
private native static int _dispatchMouseEvent(
|
87
|
446 |
int aSvgProxyHandle,int aDocumentHandle,
|
80
|
447 |
int aX, int aY);
|
|
448 |
|
|
449 |
private native static void _focusOn(
|
87
|
450 |
int aSvgProxyHandle,int aDocumentHandle,
|
80
|
451 |
int aSvgElementHandle);
|
|
452 |
|
|
453 |
private native static void _focusOut(
|
87
|
454 |
int aSvgProxyHandle,int aDocumentHandle,
|
80
|
455 |
int aSvgElementHandle);
|
|
456 |
|
|
457 |
native static int _getExternalListSize(
|
87
|
458 |
int aSvgProxyHandle, int aDocumentHandle);
|
80
|
459 |
|
|
460 |
native static String _getExternalListItem(
|
|
461 |
int aSvgProxyHandle, int aDocumentHandle,
|
|
462 |
int aIndex);
|
|
463 |
|
|
464 |
private native static void _initViewport(
|
|
465 |
int aSvgProxyHandle, int aDocumentHandle);
|
|
466 |
}
|