KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2003 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
22 import org.apache.batik.parser.ParseException;
23 import org.apache.batik.parser.TransformListHandler;
24 import org.apache.batik.parser.TransformListParser;
25 import org.w3c.dom.DOMException JavaDoc;
26 import org.w3c.dom.svg.SVGException;
27 import org.w3c.dom.svg.SVGMatrix;
28 import org.w3c.dom.svg.SVGTransform;
29 import org.w3c.dom.svg.SVGTransformList;
30
31 /**
32  * This class is the implementation of
33  * <code>SVGTransformList</code>.
34  *
35  * @author nicolas.socheleau@bitflash.com
36  * @version $Id: AbstractSVGTransformList.java,v 1.4 2004/08/18 07:13:13 vhardy Exp $
37  */

38 public abstract class AbstractSVGTransformList
39     extends AbstractSVGList
40     implements SVGTransformList {
41
42     /**
43      * Separator for a point list.
44      */

45     public final static String JavaDoc SVG_TRANSFORMATION_LIST_SEPARATOR
46         = "";
47
48     /**
49      * Creates a new SVGTransformationList.
50      */

51     protected AbstractSVGTransformList() {
52         super();
53     }
54
55     /**
56      * Return the separator between transform in the list.
57      */

58     protected String JavaDoc getItemSeparator(){
59         return SVG_TRANSFORMATION_LIST_SEPARATOR;
60     }
61
62     /**
63      * Create an SVGException when the checkItemType fails.
64      *
65      * @return SVGException
66      */

67     protected abstract SVGException createSVGException(short type,
68                                                        String JavaDoc key,
69                                                        Object JavaDoc[] args);
70
71     /**
72      */

73     public SVGTransform initialize ( SVGTransform newItem )
74         throws DOMException JavaDoc, SVGException {
75
76         return (SVGTransform)initializeImpl(newItem);
77     }
78     /**
79      */

80     public SVGTransform getItem ( int index )
81         throws DOMException JavaDoc {
82
83         return (SVGTransform)getItemImpl(index);
84     }
85
86     /**
87      */

88     public SVGTransform insertItemBefore ( SVGTransform newItem, int index )
89         throws DOMException JavaDoc, SVGException {
90
91         return (SVGTransform)insertItemBeforeImpl(newItem,index);
92     }
93
94     /**
95      */

96     public SVGTransform replaceItem ( SVGTransform newItem, int index )
97         throws DOMException JavaDoc, SVGException {
98
99         return (SVGTransform)replaceItemImpl(newItem,index);
100     }
101
102     /**
103      */

104     public SVGTransform removeItem ( int index )
105         throws DOMException JavaDoc {
106
107         return (SVGTransform)removeItemImpl(index);
108     }
109
110     /**
111      */

112     public SVGTransform appendItem ( SVGTransform newItem )
113         throws DOMException JavaDoc, SVGException {
114
115         return (SVGTransform) appendItemImpl(newItem);
116     }
117
118     /**
119      */

120     public SVGTransform createSVGTransformFromMatrix ( SVGMatrix matrix ){
121         SVGOMTransform transform = new SVGOMTransform();
122         transform.setMatrix(matrix);
123         return transform;
124     }
125
126     /**
127      */

128     public SVGTransform consolidate ( ){
129         revalidate();
130
131         if ( itemList.size() == 0 ){
132             return null;
133         }
134         if ( itemList.size() == 1 ){
135             return getItem(0);
136         }
137
138         SVGTransform view = (SVGTransform)getItemImpl(0);
139
140         AffineTransform JavaDoc transform = (AffineTransform JavaDoc)
141             ((SVGTransformItem)view).affineTransform.clone();
142
143         for(int i = 1 ; i < itemList.size() ; i++ ){
144             view = (SVGTransform)getItemImpl(i);
145             transform.concatenate(((SVGTransformItem)view).affineTransform);
146         }
147         SVGOMMatrix matrix = new SVGOMMatrix(transform);
148         return initialize(createSVGTransformFromMatrix(matrix));
149     }
150
151     /**
152      */

153     protected SVGItem createSVGItem(Object JavaDoc newItem){
154         
155         SVGTransform transform= (SVGTransform)newItem;
156
157         return new SVGTransformItem(transform);
158     }
159
160     /**
161      * Parse the 'points' attribute.
162      *
163      * @param value 'points' attribute value
164      * @param handler : list handler
165      */

166     protected void doParse(String JavaDoc value, ListHandler handler)
167         throws ParseException{
168
169         TransformListParser transformListParser = new TransformListParser();
170         
171         TransformListBuilder builder = new TransformListBuilder(handler);
172         
173         transformListParser.setTransformListHandler(builder);
174         transformListParser.parse(value);
175         
176     }
177
178     /**
179      * Check if the item is an SVGPoint.
180      */

181     protected void checkItemType(Object JavaDoc newItem){
182         if ( !( newItem instanceof SVGTransform ) ){
183             createSVGException(SVGException.SVG_WRONG_TYPE_ERR,
184                                "expected SVGTransform",
185                                null);
186         }
187     }
188
189     /**
190      * Internal representation of the item SVGPoint.
191      */

192     protected class SVGTransformItem extends AbstractSVGTransform
193         implements SVGItem {
194
195         protected boolean xOnly;
196         protected boolean angleOnly;
197
198         /**
199          * List the item belongs to.
200          */

201         protected AbstractSVGList parent;
202         
203         /**
204          * String representation of the item.
205          *
206          * This is a cached representation of the
207          * item while it is not changed.
208          */

209         protected String JavaDoc itemStringValue;
210
211         protected SVGTransformItem(){
212         }
213
214         /**
215          * Notifies the parent list that
216          * the item has changed.
217          *
218          * Discard the cached representation
219          * of the item.
220          */

221         protected void resetAttribute(){
222             if ( parent != null ){
223                 itemStringValue = null;
224                 parent.itemChanged();
225             }
226         }
227
228         /**
229          * Assign a parent list to this item.
230          *
231          * @param list : list the item belongs.
232          */

233         public void setParent(AbstractSVGList list){
234             parent = list;
235         }
236         
237         /**
238          * Return the parent list of the item.
239          *
240          * @return list the item belongs.
241          */

242         public AbstractSVGList getParent(){
243             return parent;
244         }
245
246         /**
247          * Return the cached representation
248          * of the item if valid otherwise
249          * re-computes the String representation
250          * of the item.
251          */

252         public String JavaDoc getValueAsString(){
253             if ( itemStringValue == null ){
254                 itemStringValue = getStringValue();
255             }
256             return itemStringValue;
257         }
258
259         protected SVGTransformItem(SVGTransform transform){
260             super();
261             type = transform.getType();
262             SVGMatrix matrix = transform.getMatrix();
263             switch(type){
264             case SVGTransform.SVG_TRANSFORM_TRANSLATE:
265                 setTranslate(matrix.getE(),matrix.getF());
266                 break;
267             case SVGTransform.SVG_TRANSFORM_SCALE:
268                 setScale(matrix.getA(),matrix.getD());
269                 break;
270             case SVGTransform.SVG_TRANSFORM_ROTATE:
271                 if (matrix.getE() == 0.0f ){
272                     rotate(transform.getAngle());
273                 }
274                 else{
275                     angleOnly = false;
276                     if ( matrix.getA() == 1.0f ){
277                         setRotate(transform.getAngle(),matrix.getE(),matrix.getF());
278                     }
279                     else{
280                         if ( transform instanceof AbstractSVGTransform){
281                             AbstractSVGTransform internal = (AbstractSVGTransform)transform;
282                             setRotate(internal.getAngle(),internal.getX(),internal.getY());
283                         }
284                     }
285                 }
286                 break;
287             case SVGTransform.SVG_TRANSFORM_SKEWX:
288                 setSkewX(transform.getAngle());
289                 break;
290             case SVGTransform.SVG_TRANSFORM_SKEWY:
291                 setSkewY(transform.getAngle());
292                 break;
293             case SVGTransform.SVG_TRANSFORM_MATRIX:
294                 setMatrix(matrix);
295                 break;
296             }
297             
298         }
299
300         protected void translate(float x){
301             xOnly = true;
302             setTranslate(x,0.0f);
303         }
304         protected void rotate(float angle){
305             angleOnly = true;
306             setRotate(angle,0.0f,0.0f);
307         }
308         protected void scale(float x){
309             xOnly = true;
310             setScale(x,x);
311         }
312         protected void matrix(float a,float b,float c,
313                               float d,float e,float f){
314             setMatrix(new SVGOMMatrix(new AffineTransform JavaDoc(a,b,c,d,e,f)));
315         }
316
317
318         public void setMatrix ( SVGMatrix matrix ){
319             super.setMatrix(matrix);
320             resetAttribute();
321         }
322         public void setTranslate ( float tx, float ty ){
323             super.setTranslate(tx,ty);
324             resetAttribute();
325         }
326         public void setScale ( float sx, float sy ){
327             super.setScale(sx,sy);
328             resetAttribute();
329         }
330         public void setRotate ( float angle, float cx, float cy ){
331             super.setRotate(angle,cx,cy);
332             resetAttribute();
333         }
334         public void setSkewX ( float angle ){
335             super.setSkewX(angle);
336             resetAttribute();
337         }
338         public void setSkewY ( float angle ){
339             super.setSkewY(angle);
340             resetAttribute();
341         }
342
343         protected SVGMatrix createMatrix(){
344             return new AbstractSVGMatrix(){
345                 protected AffineTransform JavaDoc getAffineTransform(){
346                     return SVGTransformItem.this.affineTransform;
347                 }
348
349                 public void setA(float a) throws DOMException JavaDoc {
350                     SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX;
351                     super.setA(a);
352                     SVGTransformItem.this.resetAttribute();
353                 }
354                 public void setB(float b) throws DOMException JavaDoc {
355                     SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX;
356                     super.setB(b);
357                     SVGTransformItem.this.resetAttribute();
358                 }
359                 public void setC(float c) throws DOMException JavaDoc {
360                     SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX;
361                     super.setC(c);
362                     SVGTransformItem.this.resetAttribute();
363                 }
364                 public void setD(float d) throws DOMException JavaDoc {
365                     SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX;
366                     super.setD(d);
367                     SVGTransformItem.this.resetAttribute();
368                 }
369                 public void setE(float e) throws DOMException JavaDoc {
370                     SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX;
371                     super.setE(e);
372                     SVGTransformItem.this.resetAttribute();
373                 }
374                 public void setF(float f) throws DOMException JavaDoc {
375                     SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX;
376                     super.setF(f);
377                     SVGTransformItem.this.resetAttribute();
378                 }
379             };
380         }
381
382         protected String JavaDoc getStringValue(){
383             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
384             switch(type){
385             case SVGTransform.SVG_TRANSFORM_TRANSLATE:
386                 buf.append("translate(");
387                 buf.append(affineTransform.getTranslateX());
388                 if( !xOnly ){
389                     buf.append(' ');
390                     buf.append(affineTransform.getTranslateY());
391                 }
392                 buf.append(')');
393                 break;
394             case SVGTransform.SVG_TRANSFORM_ROTATE:
395                 buf.append("rotate(");
396                 buf.append(angle);
397                 if ( !angleOnly ){
398                     buf.append(' ');
399                     buf.append(x);
400                     buf.append(' ');
401                     buf.append(y);
402                 }
403                 buf.append(')');
404                 break;
405             case SVGTransform.SVG_TRANSFORM_SCALE:
406                 buf.append("scale(");
407                 buf.append(affineTransform.getScaleX());
408                 if ( !xOnly ){
409                     buf.append(' ');
410                     buf.append(affineTransform.getScaleY());
411                 }
412                 buf.append(')');
413                 break;
414             case SVGTransform.SVG_TRANSFORM_SKEWX:
415                 buf.append("skewX(");
416                 buf.append(angle);
417                 buf.append(')');
418                 break;
419             case SVGTransform.SVG_TRANSFORM_SKEWY:
420                 buf.append("skewY(");
421                 buf.append(angle);
422                 buf.append(')');
423                 break;
424             case SVGTransform.SVG_TRANSFORM_MATRIX:
425                 buf.append("matrix(");
426                 double[] matrix = new double[6];
427                 affineTransform.getMatrix(matrix);
428                 for(int i = 0 ; i < 6 ; i++ ){
429                     if ( i != 0 ){
430                         buf.append(' ');
431                     }
432                     buf.append((float)matrix[i]);
433                 }
434                 buf.append(')');
435                 break;
436             }
437             return buf.toString();
438         }
439     }
440
441     /**
442      * Helper class to interface the <code>PointsParser</code>
443      * and the <code>ListHandler</code>
444      */

445     protected class TransformListBuilder
446         implements TransformListHandler {
447
448         /**
449          * list handler.
450          */

451         protected ListHandler listHandler;
452         
453         public TransformListBuilder(ListHandler listHandler){
454             this.listHandler = listHandler;
455         }
456
457         public void startTransformList() throws ParseException{
458             listHandler.startList();
459         }
460
461     /**
462      * Invoked when 'matrix(a, b, c, d, e, f)' has been parsed.
463      *
464      * @exception ParseException if an error occured while processing
465      * the transform
466      */

467         public void matrix(float a, float b, float c,
468                            float d, float e, float f)
469             throws ParseException{
470             SVGTransformItem item = new SVGTransformItem();
471             item.matrix(a,b,c,d,e,f);
472             listHandler.item(item);
473         }
474
475     /**
476      * Invoked when 'rotate(theta)' has been parsed.
477      *
478      * @exception ParseException if an error occured while processing
479      * the transform
480      */

481         public void rotate(float theta) throws ParseException{
482             SVGTransformItem item = new SVGTransformItem();
483             item.rotate(theta);
484             listHandler.item(item);
485         }
486
487     /**
488      * Invoked when 'rotate(theta, cx, cy)' has been parsed.
489      *
490      * @exception ParseException if an error occured while processing
491      * the transform
492      */

493         public void rotate(float theta, float cx, float cy) throws ParseException{
494             SVGTransformItem item = new SVGTransformItem();
495             item.setRotate(theta,cx,cy);
496             listHandler.item(item);
497         }
498
499     /**
500      * Invoked when 'translate(tx)' has been parsed.
501      *
502      * @exception ParseException if an error occured while processing
503      * the transform
504      */

505         public void translate(float tx) throws ParseException{
506             SVGTransformItem item = new SVGTransformItem();
507             item.translate(tx);
508             listHandler.item(item);
509
510         }
511
512     /**
513      * Invoked when 'translate(tx, ty)' has been parsed.
514      *
515      * @exception ParseException if an error occured while processing
516      * the transform
517      */

518         public void translate(float tx, float ty) throws ParseException{
519             SVGTransformItem item = new SVGTransformItem();
520             item.setTranslate(tx,ty);
521             listHandler.item(item);
522         }
523
524     /**
525      * Invoked when 'scale(sx)' has been parsed.
526      *
527      * @exception ParseException if an error occured while processing
528      * the transform
529      */

530         public void scale(float sx) throws ParseException{
531             SVGTransformItem item = new SVGTransformItem();
532             item.scale(sx);
533             listHandler.item(item);
534
535         }
536
537     /**
538      * Invoked when 'scale(sx, sy)' has been parsed.
539      *
540      * @exception ParseException if an error occured while processing
541      * the transform
542      */

543         public void scale(float sx, float sy) throws ParseException{
544             SVGTransformItem item = new SVGTransformItem();
545             item.setScale(sx,sy);
546             listHandler.item(item);
547         }
548
549     /**
550      * Invoked when 'skewX(skx)' has been parsed.
551      *
552      * @exception ParseException if an error occured while processing
553      * the transform
554      */

555         public void skewX(float skx) throws ParseException{
556             SVGTransformItem item = new SVGTransformItem();
557             item.setSkewX(skx);
558             listHandler.item(item);
559         }
560
561     /**
562      * Invoked when 'skewY(sky)' has been parsed.
563      *
564      * @exception ParseException if an error occured while processing
565      * the transform
566      */

567         public void skewY(float sky) throws ParseException{
568             SVGTransformItem item = new SVGTransformItem();
569             item.setSkewY(sky);
570             listHandler.item(item);
571         }
572
573     /**
574      * Invoked when the transform ends.
575      *
576      * @exception ParseException if an error occured while processing
577      * the transform
578      */

579         public void endTransformList() throws ParseException{
580             listHandler.endList();
581         }
582     }
583     
584 }
585
Popular Tags