56
|
1 |
/*
|
87
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
56
|
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 org.w3c.dom.*;
|
|
21 |
import org.w3c.dom.svg.*;
|
|
22 |
import java.io.IOException;
|
|
23 |
import com.nokia.mj.impl.utils.Logger;
|
|
24 |
|
|
25 |
public class M2GSVGSVGElement extends M2GSVGLocatableElement implements SVGSVGElement
|
|
26 |
{
|
|
27 |
//--------------------------------------------------
|
|
28 |
// STATIC CONSTANTS
|
|
29 |
//--------------------------------------------------
|
87
|
30 |
|
56
|
31 |
|
|
32 |
//------------------------------------------------------------------
|
|
33 |
// VARIABLES
|
|
34 |
//------------------------------------------------------------------
|
|
35 |
// Exception text
|
|
36 |
public static final String SCALE_BY_ZERO =
|
|
37 |
"Cannot scale by 0.";
|
|
38 |
|
|
39 |
//------------------------------------------------------------------
|
|
40 |
// VARIABLES
|
|
41 |
//------------------------------------------------------------------
|
|
42 |
private Object iTimeGuard;
|
|
43 |
private M2GSVGMatrix iMatrix;
|
|
44 |
private M2GSVGPoint iCurrentTranslatePoint;
|
|
45 |
private float iCurrentRotate;
|
|
46 |
private float iCurrentScale;
|
|
47 |
private float iCurrentTimeInSeconds;
|
|
48 |
|
|
49 |
//------------------------------------------------------------------
|
|
50 |
// METHODS
|
|
51 |
//------------------------------------------------------------------
|
|
52 |
/**
|
|
53 |
* Constructor
|
|
54 |
* @param aHandle -
|
|
55 |
* @param aDocument -
|
|
56 |
*/
|
|
57 |
protected M2GSVGSVGElement(int aHandle, M2GDocument aDocument)
|
|
58 |
{
|
|
59 |
super(aHandle, aDocument);
|
|
60 |
iTimeGuard = new Object();
|
|
61 |
iMatrix = (M2GSVGMatrix)createSVGMatrixComponents(1f, 0f, 0f, 1f, 0f, 0f);
|
|
62 |
iCurrentTranslatePoint = new M2GSVGPoint(this);
|
|
63 |
iCurrentRotate = 0.0f;
|
|
64 |
iCurrentScale = 1.0f;
|
|
65 |
iCurrentTimeInSeconds = 0.0f;
|
|
66 |
refreshMatrix();
|
|
67 |
}
|
|
68 |
|
|
69 |
/**
|
|
70 |
* @see org.w3c.dom.svg.SVGSVGElement#createSVGMatrixComponents()
|
|
71 |
*/
|
|
72 |
public SVGMatrix createSVGMatrixComponents(
|
|
73 |
float a, float b, float c, float d, float e, float f)
|
|
74 |
{
|
|
75 |
return new M2GSVGMatrix(a, b, c, d, e, f);
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* @see org.w3c.dom.svg.SVGSVGElement#createSVGMatrixComponents()
|
|
80 |
*/
|
|
81 |
public SVGPath createSVGPath()
|
|
82 |
{
|
|
83 |
return new M2GSVGPath(
|
|
84 |
M2GSVGPath._createPath(getNativeSVGProxyHandle()));
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* @see org.w3c.dom.svg.SVGSVGElement#createSVGRect()
|
|
89 |
*/
|
|
90 |
public SVGRect createSVGRect()
|
|
91 |
{
|
|
92 |
return new M2GSVGRect();
|
|
93 |
}
|
|
94 |
|
|
95 |
/**
|
|
96 |
* @see org.w3c.dom.svg.SVGSVGElement#SVGRGBColor()
|
|
97 |
*/
|
|
98 |
public SVGRGBColor createSVGRGBColor(int red, int green, int blue)
|
|
99 |
{
|
|
100 |
return new M2GSVGRGBColor(red, green, blue);
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
|
104 |
* @see com.nokia.microedition.m2g.M2GObject#doCleanup()
|
|
105 |
*/
|
|
106 |
protected void doCleanup()
|
|
107 |
{
|
|
108 |
// Note this method is not automatically called if
|
|
109 |
// doConstruct() method is not first called
|
|
110 |
super.doCleanup();
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
|
114 |
* @see com.nokia.microedition.m2g.M2GObject#getCurrentRotate()
|
|
115 |
*/
|
|
116 |
public float getCurrentRotate()
|
|
117 |
{
|
|
118 |
synchronized (this)
|
|
119 |
{
|
|
120 |
return iCurrentRotate;
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* @see com.nokia.microedition.m2g.M2GObject#getCurrentScale()
|
|
126 |
*/
|
|
127 |
public float getCurrentScale()
|
|
128 |
{
|
|
129 |
synchronized (this)
|
|
130 |
{
|
|
131 |
return iCurrentScale;
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* @see com.nokia.microedition.m2g.M2GObject#getCurrentTime()
|
|
137 |
*/
|
|
138 |
public float getCurrentTime()
|
|
139 |
{
|
|
140 |
synchronized (iTimeGuard)
|
|
141 |
{
|
|
142 |
return iCurrentTimeInSeconds;
|
|
143 |
}
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* @see com.nokia.microedition.m2g.M2GObject#getCurrentTranslate()
|
|
148 |
*/
|
|
149 |
public SVGPoint getCurrentTranslate()
|
|
150 |
{
|
|
151 |
return iCurrentTranslatePoint;
|
|
152 |
}
|
|
153 |
|
|
154 |
/**
|
|
155 |
* @org.w3c.dom.Node#getParentNode()
|
|
156 |
*/
|
|
157 |
public Node getParentNode()
|
|
158 |
{
|
|
159 |
// Optimization: if(!getDocument().getConnectionPolicy().getAccessRight())
|
|
160 |
if (!iDocument.iConnectionRight)
|
|
161 |
{
|
|
162 |
Logger.ELOG(Logger.EJavaUI, "getParentNode() - access rights failure");
|
|
163 |
throw new SecurityException(M2GSVGConstants.ACCESS_RIGHTS_ESTR);
|
|
164 |
}
|
|
165 |
return null;
|
|
166 |
}
|
|
167 |
|
|
168 |
/**
|
|
169 |
* Check svg version and base profile
|
|
170 |
* @throws IOException If version or profile is invalid
|
|
171 |
*/
|
|
172 |
public void checkVersionAndProfile() throws IOException
|
|
173 |
{
|
|
174 |
// Check version
|
|
175 |
String trait = _getStringTrait(
|
|
176 |
getNativeSVGProxyHandle(),
|
|
177 |
getHandle(),
|
|
178 |
M2GSVGConstants.AT_VERSION);
|
|
179 |
if (trait != null && (!trait.equals(
|
|
180 |
M2GSVGConstants.getInstance().iSvgBaseProfileVersion)))
|
|
181 |
{
|
|
182 |
Logger.ELOG(Logger.EJavaUI, "doCheckValidity() - excption: svg version");
|
|
183 |
throw new IOException(/*SF*/"Invalid version."/*SF*/);
|
|
184 |
}
|
|
185 |
// Check profile
|
|
186 |
trait = _getStringTrait(
|
|
187 |
getNativeSVGProxyHandle(),
|
|
188 |
getHandle(),
|
|
189 |
M2GSVGConstants.AT_BASEPROFILE);
|
|
190 |
if (trait != null && (!trait.equals(
|
|
191 |
M2GSVGConstants.getInstance().iSvgBaseProfileName)))
|
|
192 |
{
|
|
193 |
Logger.ELOG(Logger.EJavaUI, "doCheckValidity() - excption: svg base profile");
|
|
194 |
throw new IOException(/*SF*/"Invalid base profile."/*SF*/);
|
|
195 |
}
|
|
196 |
}
|
|
197 |
|
|
198 |
/**
|
|
199 |
* @javax.microedition.m2g.SVGImage#incrementTime()
|
|
200 |
*/
|
|
201 |
public void incrementTime(float seconds)
|
|
202 |
{
|
|
203 |
incrementTimeNoUpdate(seconds);
|
|
204 |
// update the media time to SVG engine
|
|
205 |
updateTime();
|
|
206 |
}
|
|
207 |
|
|
208 |
/**
|
|
209 |
* Increments the animation or media timeline for this SVGImage (in seconds)
|
|
210 |
* but it does not update it to the SVG engine
|
|
211 |
*
|
|
212 |
* @note the time is updated to the engine during M2GScalableGraphics.RenderFrame()
|
|
213 |
* @param seconds the value of time to increment in seconds
|
|
214 |
*
|
|
215 |
* @since S60 3.1
|
|
216 |
*/
|
|
217 |
public void incrementTimeNoUpdate(float seconds)
|
|
218 |
{
|
|
219 |
if (seconds < 0)
|
|
220 |
{
|
|
221 |
throw new IllegalArgumentException();
|
|
222 |
}
|
|
223 |
|
|
224 |
if (Float.isNaN(iCurrentTimeInSeconds + seconds))
|
|
225 |
{
|
|
226 |
throw new IllegalArgumentException();
|
|
227 |
}
|
|
228 |
|
|
229 |
synchronized (iTimeGuard)
|
|
230 |
{
|
|
231 |
iCurrentTimeInSeconds += seconds;
|
|
232 |
}
|
|
233 |
}
|
|
234 |
|
|
235 |
/**
|
|
236 |
* Sets matrix trait
|
|
237 |
*/
|
|
238 |
protected void refreshMatrix()
|
|
239 |
{
|
|
240 |
float matrixComponents[] = new float[M2GSVGMatrix.ARRAY_SIZE];
|
|
241 |
|
|
242 |
synchronized (this)
|
|
243 |
{
|
|
244 |
iMatrix.getComponentsToArray(matrixComponents);
|
|
245 |
}
|
|
246 |
|
|
247 |
M2GSVGElement._setMatrixTrait(
|
87
|
248 |
getNativeSVGProxyHandle(), getHandle(),
|
56
|
249 |
M2GSVGConstants.AT_TRANSFORM, matrixComponents);
|
|
250 |
|
|
251 |
// inform observer about changes in DOM
|
|
252 |
iDocument.notifyDOMChangeObserver();
|
|
253 |
}
|
|
254 |
|
|
255 |
/**
|
|
256 |
* Updates the time to engine
|
|
257 |
*/
|
|
258 |
private void updateTime()
|
|
259 |
{
|
|
260 |
float currentTimeInSeconds = 0.0f;
|
|
261 |
|
|
262 |
synchronized (iTimeGuard)
|
|
263 |
{
|
|
264 |
currentTimeInSeconds = iCurrentTimeInSeconds;
|
|
265 |
}
|
|
266 |
|
|
267 |
_setMediaTime(getNativeSVGProxyHandle(),
|
|
268 |
getDocument().getHandle(), currentTimeInSeconds);
|
|
269 |
|
|
270 |
// inform observer about changes in DOM
|
|
271 |
iDocument.notifyDOMChangeObserver();
|
|
272 |
}
|
|
273 |
|
|
274 |
/**
|
|
275 |
* @see com.nokia.microedition.m2g.M2GObject#setCurrentRotate()
|
|
276 |
*/
|
|
277 |
public void setCurrentRotate(float value)
|
|
278 |
{
|
|
279 |
String zoomAndPan = getTrait("zoomAndPan");
|
|
280 |
if ((zoomAndPan != null) && zoomAndPan.equals("disable"))
|
|
281 |
{
|
|
282 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo,
|
|
283 |
"setCurrentRotate() - zoomAndPan:" + zoomAndPan + ", fail");
|
|
284 |
return;
|
|
285 |
}
|
|
286 |
synchronized (this)
|
|
287 |
{
|
|
288 |
iCurrentRotate = value;
|
|
289 |
updateTransformMatrix();
|
|
290 |
}
|
|
291 |
}
|
|
292 |
|
|
293 |
/**
|
|
294 |
* @see com.nokia.microedition.m2g.M2GObject#setCurrentScale()
|
|
295 |
*/
|
|
296 |
public void setCurrentScale(float value)
|
|
297 |
{
|
|
298 |
String zoomAndPan = getTrait("zoomAndPan");
|
|
299 |
if ((zoomAndPan != null) && zoomAndPan.equals("disable"))
|
|
300 |
{
|
|
301 |
Logger.LOG(Logger.EJavaUI, Logger.EInfo,
|
|
302 |
"setCurrentScale() - zoomAndPan:" + zoomAndPan + ", fail");
|
|
303 |
return;
|
|
304 |
}
|
|
305 |
if (value == 0)
|
|
306 |
{
|
|
307 |
throw new DOMException(DOMException.INVALID_ACCESS_ERR, SCALE_BY_ZERO);
|
|
308 |
}
|
|
309 |
synchronized (this)
|
|
310 |
{
|
|
311 |
iCurrentScale = value;
|
|
312 |
updateTransformMatrix();
|
|
313 |
}
|
|
314 |
}
|
|
315 |
|
|
316 |
/**
|
|
317 |
* @see org.w3c.dom.svg.SVGSVGElement#setCurrentTime()
|
|
318 |
* @note no support for setting the time backwards except for setting it to zero
|
|
319 |
* @note engine resets the animation if current time is zero
|
|
320 |
*/
|
|
321 |
public void setCurrentTime(float newSeconds)
|
|
322 |
{
|
|
323 |
synchronized (iTimeGuard)
|
|
324 |
{
|
|
325 |
if (newSeconds <= iCurrentTimeInSeconds && newSeconds != 0)
|
|
326 |
{
|
|
327 |
// only forward time or zero time is accepted
|
|
328 |
return;
|
|
329 |
}
|
|
330 |
|
|
331 |
iCurrentTimeInSeconds = newSeconds;
|
|
332 |
}
|
|
333 |
|
|
334 |
updateTime();
|
|
335 |
}
|
|
336 |
|
|
337 |
/**
|
|
338 |
* <pre>
|
|
339 |
* [currentScale 0 currentTranslate.x] [cos(currentRotate) -sin(currentRotate 0]
|
|
340 |
* [ 0 currentScale currentTranslate.y] x [sin(currentRotate) cos(currentRotate) 0]
|
|
341 |
* [ 0 0 1 ] [ 0 0 1]
|
|
342 |
* </pre>
|
|
343 |
*/
|
|
344 |
public void updateTransformMatrix()
|
|
345 |
{
|
|
346 |
synchronized (this)
|
|
347 |
{
|
|
348 |
M2GSVGMatrix a = new M2GSVGMatrix(
|
|
349 |
iCurrentScale, 0f, 0f, iCurrentScale,
|
|
350 |
iCurrentTranslatePoint.getX(), iCurrentTranslatePoint.getY());
|
|
351 |
M2GSVGMatrix b = new M2GSVGMatrix(
|
|
352 |
(float)Math.cos(iCurrentRotate *(float)Math.PI / 180.0f),
|
|
353 |
(float)Math.sin(iCurrentRotate *(float)Math.PI / 180.0f),
|
|
354 |
-(float)Math.sin(iCurrentRotate *(float)Math.PI / 180.0f),
|
|
355 |
(float)Math.cos(iCurrentRotate *(float)Math.PI / 180.0f),
|
|
356 |
0f, 0f);
|
|
357 |
iMatrix = (M2GSVGMatrix)(a.mMultiply(b));
|
|
358 |
refreshMatrix();
|
|
359 |
}
|
|
360 |
}
|
|
361 |
|
|
362 |
//------------------------------------------------------------------
|
|
363 |
// STATIC METHODS
|
|
364 |
//------------------------------------------------------------------
|
|
365 |
/**
|
|
366 |
* Builds root element according to a document
|
|
367 |
* @param aDocument
|
|
368 |
* @return root element
|
|
369 |
*/
|
|
370 |
public static M2GSVGSVGElement buildRootElement(M2GDocument aDocument)
|
|
371 |
{
|
|
372 |
// Get native handle
|
|
373 |
int elementHandle = M2GSVGElement._getRootElement(
|
|
374 |
aDocument.getNativeSVGProxyHandle(),
|
|
375 |
aDocument.getHandle());
|
|
376 |
// Check native handle
|
|
377 |
if (!M2GObject.checkHandle(elementHandle))
|
|
378 |
{
|
|
379 |
return null;
|
|
380 |
}
|
|
381 |
// Create object
|
|
382 |
M2GSVGSVGElement rootElement =
|
|
383 |
new M2GSVGSVGElement(elementHandle, aDocument);
|
|
384 |
if (rootElement != null)
|
|
385 |
{
|
|
386 |
// Register element
|
|
387 |
aDocument.registerLiveElement(rootElement, new Integer(elementHandle));
|
|
388 |
}
|
|
389 |
return rootElement;
|
|
390 |
}
|
|
391 |
|
|
392 |
//------------------------------------------------------------------
|
|
393 |
// NATIVE METHODS
|
|
394 |
//------------------------------------------------------------------
|
|
395 |
protected native static float _getMediaTime(
|
87
|
396 |
int aSvgProxyHandle, int aDocumentHandle);
|
56
|
397 |
private native static void _setMediaTime(
|
|
398 |
int aSvgProxyHandle, int aDocumentHandle,
|
87
|
399 |
float aSeconds);
|
56
|
400 |
}
|
|
401 |
|