KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > svg > SVGOMSVGElement


1 /*
2
3    Copyright 2000-2004 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.dom.svg;
19
20 import java.awt.geom.AffineTransform JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.batik.dom.AbstractDocument;
24 import org.apache.batik.dom.util.XLinkSupport;
25 import org.apache.batik.dom.util.XMLSupport;
26 import org.apache.batik.dom.util.ListNodeList;
27
28 import org.w3c.dom.DOMException JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32 import org.w3c.dom.css.CSSStyleDeclaration;
33 import org.w3c.dom.css.DocumentCSS;
34 import org.w3c.dom.css.ViewCSS;
35 import org.w3c.dom.events.DocumentEvent JavaDoc;
36 import org.w3c.dom.events.Event JavaDoc;
37 import org.w3c.dom.stylesheets.DocumentStyle;
38 import org.w3c.dom.stylesheets.StyleSheetList;
39 import org.w3c.dom.svg.SVGAngle;
40 import org.w3c.dom.svg.SVGAnimatedBoolean;
41 import org.w3c.dom.svg.SVGAnimatedLength;
42 import org.w3c.dom.svg.SVGAnimatedPreserveAspectRatio;
43 import org.w3c.dom.svg.SVGAnimatedRect;
44 import org.w3c.dom.svg.SVGElement;
45 import org.w3c.dom.svg.SVGException;
46 import org.w3c.dom.svg.SVGLength;
47 import org.w3c.dom.svg.SVGMatrix;
48 import org.w3c.dom.svg.SVGNumber;
49 import org.w3c.dom.svg.SVGPoint;
50 import org.w3c.dom.svg.SVGRect;
51 import org.w3c.dom.svg.SVGSVGElement;
52 import org.w3c.dom.svg.SVGStringList;
53 import org.w3c.dom.svg.SVGTransform;
54 import org.w3c.dom.svg.SVGViewSpec;
55 import org.w3c.dom.views.AbstractView;
56 import org.w3c.dom.views.DocumentView;
57
58 /**
59  * This class implements {@link org.w3c.dom.svg.SVGSVGElement}.
60  *
61  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
62  * @version $Id: SVGOMSVGElement.java,v 1.31 2005/02/28 17:37:18 deweese Exp $
63  */

64 public class SVGOMSVGElement
65     extends SVGStylableElement
66     implements SVGSVGElement {
67
68     /**
69      * The attribute initializer.
70      */

71     protected final static AttributeInitializer attributeInitializer;
72     static {
73         attributeInitializer = new AttributeInitializer(7);
74         attributeInitializer.addAttribute(XMLSupport.XMLNS_NAMESPACE_URI,
75                                           null,
76                                           "xmlns",
77                                           SVG_NAMESPACE_URI);
78         attributeInitializer.addAttribute(XMLSupport.XMLNS_NAMESPACE_URI,
79                                           "xmlns",
80                                           "xlink",
81                                           XLinkSupport.XLINK_NAMESPACE_URI);
82         attributeInitializer.addAttribute(null,
83                                           null,
84                                           SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE,
85                                           "xMidYMid meet");
86         attributeInitializer.addAttribute(null,
87                                           null,
88                                           SVG_ZOOM_AND_PAN_ATTRIBUTE,
89                                           SVG_MAGNIFY_VALUE);
90         attributeInitializer.addAttribute(null,
91                                           null,
92                                           SVG_VERSION_ATTRIBUTE,
93                                           SVG_VERSION);
94         attributeInitializer.addAttribute(null,
95                                           null,
96                                           SVG_CONTENT_SCRIPT_TYPE_ATTRIBUTE,
97                                           "text/ecmascript");
98         attributeInitializer.addAttribute(null,
99                                           null,
100                                           SVG_CONTENT_STYLE_TYPE_ATTRIBUTE,
101                                           "text/css");
102     }
103
104     /**
105      * Creates a new SVGOMSVGElement object.
106      */

107     protected SVGOMSVGElement() {
108     }
109
110     /**
111      * Creates a new SVGOMSVGElement object.
112      * @param prefix The namespace prefix.
113      * @param owner The owner document.
114      */

115     public SVGOMSVGElement(String JavaDoc prefix, AbstractDocument owner) {
116         super(prefix, owner);
117     }
118
119     /**
120      * <b>DOM</b>: Implements {@link Node#getLocalName()}.
121      */

122     public String JavaDoc getLocalName() {
123         return SVG_SVG_TAG;
124     }
125
126     /**
127      * <b>DOM</b>: Implements {@link SVGSVGElement#getX()}.
128      */

129     public SVGAnimatedLength getX() {
130         return getAnimatedLengthAttribute
131             (null, SVG_X_ATTRIBUTE, SVG_RECT_X_DEFAULT_VALUE,
132              SVGOMAnimatedLength.HORIZONTAL_LENGTH);
133     }
134
135     /**
136      * <b>DOM</b>: Implements {@link SVGSVGElement#getY()}.
137      */

138     public SVGAnimatedLength getY() {
139         return getAnimatedLengthAttribute
140             (null, SVG_Y_ATTRIBUTE, SVG_SVG_Y_DEFAULT_VALUE,
141              SVGOMAnimatedLength.VERTICAL_LENGTH);
142     }
143
144     /**
145      * <b>DOM</b>: Implements {@link SVGSVGElement#getWidth()}.
146      */

147     public SVGAnimatedLength getWidth() {
148         return getAnimatedLengthAttribute
149             (null, SVG_WIDTH_ATTRIBUTE, SVG_SVG_WIDTH_DEFAULT_VALUE,
150              SVGOMAnimatedLength.HORIZONTAL_LENGTH);
151     }
152
153     /**
154      * <b>DOM</b>: Implements {@link SVGSVGElement#getHeight()}.
155      */

156     public SVGAnimatedLength getHeight() {
157         return getAnimatedLengthAttribute
158             (null, SVG_HEIGHT_ATTRIBUTE, SVG_SVG_HEIGHT_DEFAULT_VALUE,
159              SVGOMAnimatedLength.VERTICAL_LENGTH);
160     }
161
162     /**
163      * <b>DOM</b>: Implements {@link SVGSVGElement#getContentScriptType()}.
164      */

165     public String JavaDoc getContentScriptType() {
166         return getAttributeNS(null, SVG_CONTENT_SCRIPT_TYPE_ATTRIBUTE);
167     }
168
169     /**
170      * <b>DOM</b>: Implements {@link SVGSVGElement#setContentScriptType(String)}.
171      */

172     public void setContentScriptType(String JavaDoc type) {
173         setAttributeNS(null, SVG_CONTENT_SCRIPT_TYPE_ATTRIBUTE, type);
174     }
175
176     /**
177      * <b>DOM</b>: Implements {@link SVGSVGElement#getContentStyleType()}.
178      */

179     public String JavaDoc getContentStyleType() {
180         return getAttributeNS(null, SVG_CONTENT_STYLE_TYPE_ATTRIBUTE);
181     }
182
183     /**
184      * <b>DOM</b>: Implements {@link SVGSVGElement#setContentStyleType(String)}.
185      */

186     public void setContentStyleType(String JavaDoc type) {
187         setAttributeNS(null, SVG_CONTENT_STYLE_TYPE_ATTRIBUTE, type);
188     }
189
190     /**
191      * <b>DOM</b>: Implements {@link SVGSVGElement#getViewport()}.
192      */

193     public SVGRect getViewport() {
194         SVGContext ctx = getSVGContext();
195         return new SVGOMRect(0, 0, ctx.getViewportWidth(),
196                              ctx.getViewportHeight());
197     }
198
199     public float getPixelUnitToMillimeterX( ) {
200         return getSVGContext().getPixelUnitToMillimeter();
201     }
202     public float getPixelUnitToMillimeterY( ) {
203         return getSVGContext().getPixelUnitToMillimeter();
204     }
205     public float getScreenPixelToMillimeterX( ) {
206         return getSVGContext().getPixelUnitToMillimeter();
207     }
208     public float getScreenPixelToMillimeterY( ) {
209         return getSVGContext().getPixelUnitToMillimeter();
210     }
211     public boolean getUseCurrentView( ) {
212         throw new Error JavaDoc();
213     }
214     public void setUseCurrentView( boolean useCurrentView )
215         throws DOMException JavaDoc {
216         throw new Error JavaDoc();
217     }
218     public SVGViewSpec getCurrentView( ) {
219         throw new Error JavaDoc();
220     }
221     public float getCurrentScale( ) {
222         AffineTransform JavaDoc scrnTrans = getSVGContext().getScreenTransform();
223         if (scrnTrans != null)
224             return (float)Math.sqrt(scrnTrans.getDeterminant());
225         return 1;
226     }
227     public void setCurrentScale( float currentScale ) throws DOMException JavaDoc {
228         SVGContext context = getSVGContext();
229         AffineTransform JavaDoc scrnTrans = context.getScreenTransform();
230         float scale = 1;
231         if (scrnTrans != null)
232             scale = (float)Math.sqrt(scrnTrans.getDeterminant());
233         float delta = currentScale/scale;
234         // The way currentScale, currentTranslate are defined
235
// changing scale has no effect on translate.
236
scrnTrans = new AffineTransform JavaDoc
237             (scrnTrans.getScaleX()*delta, scrnTrans.getShearY()*delta,
238              scrnTrans.getShearX()*delta, scrnTrans.getScaleY()*delta,
239              scrnTrans.getTranslateX(), scrnTrans.getTranslateY());
240         context.setScreenTransform(scrnTrans);
241     }
242
243     public SVGPoint getCurrentTranslate( ) {
244         final SVGOMElement svgelt = this;
245         return new SVGPoint() {
246                 AffineTransform JavaDoc getScreenTransform() {
247                     SVGContext context = svgelt.getSVGContext();
248                     return context.getScreenTransform();
249                 }
250                     
251                 public float getX() {
252                     AffineTransform JavaDoc scrnTrans = getScreenTransform();
253                     return (float)scrnTrans.getTranslateX();
254                 }
255                 public float getY() {
256                     AffineTransform JavaDoc scrnTrans = getScreenTransform();
257                     return (float)scrnTrans.getTranslateY();
258                 }
259                 public void setX(float newX) {
260                     SVGContext context = svgelt.getSVGContext();
261                     AffineTransform JavaDoc scrnTrans = context.getScreenTransform();
262                     scrnTrans = new AffineTransform JavaDoc
263                         (scrnTrans.getScaleX(), scrnTrans.getShearY(),
264                          scrnTrans.getShearX(), scrnTrans.getScaleY(),
265                          newX, scrnTrans.getTranslateY());
266                     context.setScreenTransform(scrnTrans);
267                 }
268                 public void setY(float newY) {
269                     SVGContext context = svgelt.getSVGContext();
270                     AffineTransform JavaDoc scrnTrans = context.getScreenTransform();
271                     scrnTrans = new AffineTransform JavaDoc
272                         (scrnTrans.getScaleX(), scrnTrans.getShearY(),
273                          scrnTrans.getShearX(), scrnTrans.getScaleY(),
274                          scrnTrans.getTranslateX(), newY);
275                     context.setScreenTransform(scrnTrans);
276                 }
277                 public SVGPoint matrixTransform ( SVGMatrix mat ) {
278                     AffineTransform JavaDoc scrnTrans = getScreenTransform();
279                     float x = (float)scrnTrans.getTranslateX();
280                     float y = (float)scrnTrans.getTranslateY();
281                     float newX = mat.getA()*x + mat.getC()*y + mat.getE();
282                     float newY = mat.getB()*x + mat.getD()*y + mat.getF();
283                     return new SVGOMPoint(newX, newY);
284                 }
285             };
286     }
287
288     public int suspendRedraw ( int max_wait_milliseconds ) {
289         throw new Error JavaDoc();
290     }
291     public void unsuspendRedraw ( int suspend_handle_id )
292         throws DOMException JavaDoc {
293         throw new Error JavaDoc();
294     }
295     public void unsuspendRedrawAll ( ) {
296         throw new Error JavaDoc();
297     }
298     public void forceRedraw ( ) {
299         throw new Error JavaDoc();
300     }
301     public void pauseAnimations ( ) {
302         throw new Error JavaDoc();
303     }
304     public void unpauseAnimations ( ) {
305         throw new Error JavaDoc();
306     }
307     public boolean animationsPaused ( ) {
308         throw new Error JavaDoc();
309     }
310     public float getCurrentTime ( ) {
311         throw new Error JavaDoc();
312     }
313     public void setCurrentTime ( float seconds ) {
314         throw new Error JavaDoc();
315     }
316
317     public NodeList getIntersectionList ( SVGRect rect,
318                                                SVGElement referenceElement ) {
319         SVGSVGContext ctx = (SVGSVGContext)getSVGContext();
320         List JavaDoc list = ctx.getIntersectionList(rect, referenceElement);
321         return new ListNodeList(list);
322     }
323
324     public NodeList getEnclosureList ( SVGRect rect,
325                                             SVGElement referenceElement ) {
326         SVGSVGContext ctx = (SVGSVGContext)getSVGContext();
327         List JavaDoc list = ctx.getEnclosureList(rect, referenceElement);
328         return new ListNodeList(list);
329     }
330     public boolean checkIntersection(SVGElement element, SVGRect rect) {
331         SVGSVGContext ctx = (SVGSVGContext)getSVGContext();
332         return ctx.checkIntersection(element, rect);
333     }
334     public boolean checkEnclosure(SVGElement element, SVGRect rect) {
335         SVGSVGContext ctx = (SVGSVGContext)getSVGContext();
336         return ctx.checkEnclosure(element, rect);
337     }
338
339     public void deselectAll ( ) {
340         ((SVGSVGContext)getSVGContext()).deselectAll();
341     }
342
343     /**
344      * <b>DOM</b>: Implements {@link SVGSVGElement#createSVGNumber()}.
345      */

346     public SVGNumber createSVGNumber() {
347         return new SVGNumber() {
348                 float value;
349                 public float getValue() {
350                     return value;
351                 }
352                 public void setValue(float f) {
353                     value = f;
354                 }
355             };
356     }
357
358     /**
359      * <b>DOM</b>: Implements {@link SVGSVGElement#createSVGLength()}.
360      */

361     public SVGLength createSVGLength() {
362         return new SVGOMLength(this);
363     }
364
365     public SVGAngle createSVGAngle ( ) {
366         throw new Error JavaDoc();
367     }
368
369     /**
370      * <b>DOM</b>: Implements {@link SVGSVGElement#createSVGPoint()}.
371      */

372     public SVGPoint createSVGPoint() {
373         return new SVGOMPoint(0, 0);
374     }
375
376     public SVGMatrix createSVGMatrix ( ) {
377         return new AbstractSVGMatrix() {
378                 AffineTransform JavaDoc at = new AffineTransform JavaDoc();
379                 protected AffineTransform JavaDoc getAffineTransform() { return at; }
380             };
381     }
382     public SVGRect createSVGRect ( ) {
383         return new SVGOMRect(0,0,0,0);
384     }
385     public SVGTransform createSVGTransform () {
386         SVGOMTransform ret = new SVGOMTransform();
387         ret.setType(SVGTransform.SVG_TRANSFORM_MATRIX);
388         return ret;
389     }
390     public SVGTransform createSVGTransformFromMatrix ( SVGMatrix matrix ) {
391         SVGOMTransform tr = new SVGOMTransform();
392         tr.setMatrix(matrix);
393         return tr;
394     }
395     public Element JavaDoc getElementById ( String JavaDoc elementId ) {
396         return ownerDocument.getChildElementById(this, elementId);
397     }
398
399     // SVGLocatable ///////////////////////////////////////////////////////
400

401     /**
402      * <b>DOM</b>: Implements {@link
403      * org.w3c.dom.svg.SVGLocatable#getNearestViewportElement()}.
404      */

405     public SVGElement getNearestViewportElement() {
406     return SVGLocatableSupport.getNearestViewportElement(this);
407     }
408
409     /**
410      * <b>DOM</b>: Implements {@link
411      * org.w3c.dom.svg.SVGLocatable#getFarthestViewportElement()}.
412      */

413     public SVGElement getFarthestViewportElement() {
414     return SVGLocatableSupport.getFarthestViewportElement(this);
415     }
416
417     /**
418      * <b>DOM</b>: Implements {@link
419      * org.w3c.dom.svg.SVGLocatable#getBBox()}.
420      */

421     public SVGRect getBBox() {
422     return SVGLocatableSupport.getBBox(this);
423     }
424
425     /**
426      * <b>DOM</b>: Implements {@link
427      * org.w3c.dom.svg.SVGLocatable#getCTM()}.
428      */

429     public SVGMatrix getCTM() {
430     return SVGLocatableSupport.getCTM(this);
431     }
432
433     /**
434      * <b>DOM</b>: Implements {@link
435      * org.w3c.dom.svg.SVGLocatable#getScreenCTM()}.
436      */

437     public SVGMatrix getScreenCTM() {
438     return SVGLocatableSupport.getScreenCTM(this);
439     }
440
441     /**
442      * <b>DOM</b>: Implements {@link
443      * org.w3c.dom.svg.SVGLocatable#getTransformToElement(SVGElement)}.
444      */

445     public SVGMatrix getTransformToElement(SVGElement element)
446     throws SVGException {
447     return SVGLocatableSupport.getTransformToElement(this, element);
448     }
449
450     // ViewCSS ////////////////////////////////////////////////////////////////
451

452     /**
453      * <b>DOM</b>: Implements {@link
454      * org.w3c.dom.views.AbstractView#getDocument()}.
455      */

456     public DocumentView getDocument() {
457         return (DocumentView)getOwnerDocument();
458     }
459
460     /**
461      * <b>DOM</b>: Implements {@link
462      * org.w3c.dom.css.ViewCSS#getComputedStyle(Element,String)}.
463      */

464     public CSSStyleDeclaration getComputedStyle(Element JavaDoc elt,
465                                                 String JavaDoc pseudoElt) {
466         AbstractView av = ((DocumentView)getOwnerDocument()).getDefaultView();
467         return ((ViewCSS)av).getComputedStyle(elt, pseudoElt);
468     }
469
470     // DocumentEvent /////////////////////////////////////////////////////////
471

472     /**
473      * <b>DOM</b>: Implements {@link
474      * org.w3c.dom.events.DocumentEvent#createEvent(String)}.
475      */

476     public Event JavaDoc createEvent(String JavaDoc eventType) throws DOMException JavaDoc {
477         return ((DocumentEvent JavaDoc)getOwnerDocument()).createEvent(eventType);
478     }
479
480     // DocumentCSS ////////////////////////////////////////////////////////////
481

482     /**
483      * <b>DOM</b>: Implements {@link
484      * org.w3c.dom.stylesheets.DocumentStyle#getStyleSheets()}.
485      */

486     public StyleSheetList getStyleSheets() {
487         return ((DocumentStyle)getOwnerDocument()).getStyleSheets();
488     }
489
490     /**
491      * <b>DOM</b>: Implements {@link
492      * org.w3c.dom.css.DocumentCSS#getOverrideStyle(Element,String)}.
493      */

494     public CSSStyleDeclaration getOverrideStyle(Element JavaDoc elt,
495                                                 String JavaDoc pseudoElt) {
496         return ((DocumentCSS)getOwnerDocument()).getOverrideStyle(elt,
497                                                                   pseudoElt);
498     }
499
500     // SVGLangSpace support //////////////////////////////////////////////////
501

502     /**
503      * <b>DOM</b>: Returns the xml:lang attribute value.
504      */

505     public String JavaDoc getXMLlang() {
506         return XMLSupport.getXMLLang(this);
507     }
508
509     /**
510      * <b>DOM</b>: Sets the xml:lang attribute value.
511      */

512     public void setXMLlang(String JavaDoc lang) {
513         setAttributeNS(XMLSupport.XML_NAMESPACE_URI,
514                        XMLSupport.XML_LANG_ATTRIBUTE,
515                        lang);
516     }
517
518     /**
519      * <b>DOM</b>: Returns the xml:space attribute value.
520      */

521     public String JavaDoc getXMLspace() {
522         return XMLSupport.getXMLSpace(this);
523     }
524
525     /**
526      * <b>DOM</b>: Sets the xml:space attribute value.
527      */

528     public void setXMLspace(String JavaDoc space) {
529         setAttributeNS(XMLSupport.XML_NAMESPACE_URI,
530                        XMLSupport.XML_SPACE_ATTRIBUTE,
531                        space);
532     }
533
534     // SVGZoomAndPan support ///////////////////////////////////////////////
535

536     /**
537      * <b>DOM</b>: Implements {@link
538      * org.w3c.dom.svg.SVGZoomAndPan#getZoomAndPan()}.
539      */

540     public short getZoomAndPan() {
541         return SVGZoomAndPanSupport.getZoomAndPan(this);
542     }
543
544     /**
545      * <b>DOM</b>: Implements {@link
546      * org.w3c.dom.svg.SVGZoomAndPan#getZoomAndPan()}.
547      */

548     public void setZoomAndPan(short val) {
549         SVGZoomAndPanSupport.setZoomAndPan(this, val);
550     }
551
552     // SVGFitToViewBox support ////////////////////////////////////////////
553

554     /**
555      * <b>DOM</b>: Implements {@link
556      * org.w3c.dom.svg.SVGFitToViewBox#getViewBox()}.
557      */

558     public SVGAnimatedRect getViewBox() {
559         throw new RuntimeException JavaDoc(" !!! TODO: getViewBox()");
560     }
561
562     /**
563      * <b>DOM</b>: Implements {@link
564      * org.w3c.dom.svg.SVGFitToViewBox#getPreserveAspectRatio()}.
565      */

566     public SVGAnimatedPreserveAspectRatio getPreserveAspectRatio() {
567         return SVGPreserveAspectRatioSupport.getPreserveAspectRatio(this);
568     }
569
570     // SVGExternalResourcesRequired support /////////////////////////////
571

572     /**
573      * <b>DOM</b>: Implements {@link
574      * org.w3c.dom.svg.SVGExternalResourcesRequired#getExternalResourcesRequired()}.
575      */

576     public SVGAnimatedBoolean getExternalResourcesRequired() {
577         return SVGExternalResourcesRequiredSupport.
578             getExternalResourcesRequired(this);
579     }
580
581     // SVGTests support ///////////////////////////////////////////////////
582

583     /**
584      * <b>DOM</b>: Implements {@link
585      * org.w3c.dom.svg.SVGTests#getRequiredFeatures()}.
586      */

587     public SVGStringList getRequiredFeatures() {
588         return SVGTestsSupport.getRequiredFeatures(this);
589     }
590
591     /**
592      * <b>DOM</b>: Implements {@link
593      * org.w3c.dom.svg.SVGTests#getRequiredExtensions()}.
594      */

595     public SVGStringList getRequiredExtensions() {
596         return SVGTestsSupport.getRequiredExtensions(this);
597     }
598
599     /**
600      * <b>DOM</b>: Implements {@link
601      * org.w3c.dom.svg.SVGTests#getSystemLanguage()}.
602      */

603     public SVGStringList getSystemLanguage() {
604         return SVGTestsSupport.getSystemLanguage(this);
605     }
606
607     /**
608      * <b>DOM</b>: Implements {@link
609      * org.w3c.dom.svg.SVGTests#hasExtension(String)}.
610      */

611     public boolean hasExtension(String JavaDoc extension) {
612         return SVGTestsSupport.hasExtension(this, extension);
613     }
614
615     /**
616      * Returns the AttributeInitializer for this element type.
617      * @return null if this element has no attribute with a default value.
618      */

619     protected AttributeInitializer getAttributeInitializer() {
620         return attributeInitializer;
621     }
622
623     /**
624      * Returns a new uninitialized instance of this object's class.
625      */

626     protected Node JavaDoc newNode() {
627         return new SVGOMSVGElement();
628     }
629 }
630
Popular Tags