KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > SVGPaint


1 /*
2
3    Copyright 2001,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.svggen;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.GradientPaint JavaDoc;
22 import java.awt.Paint JavaDoc;
23 import java.awt.TexturePaint JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.batik.ext.awt.g2d.GraphicContext;
28
29 /**
30  * Utility class that converts a Paint object into an
31  * SVG element.
32  *
33  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
34  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
35  * @version $Id: SVGPaint.java,v 1.11 2004/08/18 07:15:08 vhardy Exp $
36  * @see org.apache.batik.svggen.SVGLinearGradient
37  * @see org.apache.batik.svggen.SVGTexturePaint
38  */

39 public class SVGPaint implements SVGConverter {
40     /**
41      * All GradientPaint convertions are handed to svgLinearGradient
42      */

43     private SVGLinearGradient svgLinearGradient;
44
45     /**
46      * All TexturePaint convertions are handed to svgTextureGradient
47      */

48     private SVGTexturePaint svgTexturePaint;
49
50     /**
51      * All Color convertions are handed to svgColor
52      */

53     private SVGColor svgColor;
54
55     /**
56      * All custom Paint convetions are handed to svgCustomPaint
57      */

58     private SVGCustomPaint svgCustomPaint;
59
60     /**
61      * Used to generate DOM elements
62      */

63     private SVGGeneratorContext generatorContext;
64
65     /**
66      * @param generatorContext the context.
67      */

68     public SVGPaint(SVGGeneratorContext generatorContext) {
69         this.svgLinearGradient = new SVGLinearGradient(generatorContext);
70         this.svgTexturePaint = new SVGTexturePaint(generatorContext);
71         this.svgCustomPaint = new SVGCustomPaint(generatorContext);
72         this.svgColor = new SVGColor(generatorContext);
73         this.generatorContext = generatorContext;
74     }
75
76     /**
77      * @return Set of Elements defining the Paints this
78      * converter has processed since it was created
79      */

80     public List JavaDoc getDefinitionSet(){
81         List JavaDoc paintDefs = new LinkedList JavaDoc(svgLinearGradient.getDefinitionSet());
82         paintDefs.addAll(svgTexturePaint.getDefinitionSet());
83         paintDefs.addAll(svgCustomPaint.getDefinitionSet());
84         paintDefs.addAll(svgColor.getDefinitionSet());
85         return paintDefs;
86     }
87
88     public SVGTexturePaint getTexturePaintConverter(){
89         return svgTexturePaint;
90     }
91
92     public SVGLinearGradient getGradientPaintConverter(){
93         return svgLinearGradient;
94     }
95
96     public SVGCustomPaint getCustomPaintConverter(){
97         return svgCustomPaint;
98     }
99
100     public SVGColor getColorConverter(){
101         return svgColor;
102     }
103
104     /**
105      * Converts part or all of the input GraphicContext into
106      * a set of attribute/value pairs and related definitions
107      *
108      * @param gc GraphicContext to be converted
109      * @return descriptor of the attributes required to represent
110      * some or all of the GraphicContext state, along
111      * with the related definitions
112      * @see org.apache.batik.svggen.SVGDescriptor
113      */

114     public SVGDescriptor toSVG(GraphicContext gc){
115         return toSVG(gc.getPaint());
116     }
117
118     /**
119      * @param paint Paint to be converted to SVG
120      * @return a descriptor of the corresponding SVG paint
121      */

122     public SVGPaintDescriptor toSVG(Paint JavaDoc paint){
123         // we first try the extension handler because we may
124
// want to override the way a Paint is managed!
125
SVGPaintDescriptor paintDesc = svgCustomPaint.toSVG(paint);
126
127         if (paintDesc == null) {
128             if (paint instanceof Color JavaDoc)
129                 paintDesc = SVGColor.toSVG((Color JavaDoc)paint, generatorContext);
130             else if (paint instanceof GradientPaint JavaDoc)
131                 paintDesc = svgLinearGradient.toSVG((GradientPaint JavaDoc)paint);
132             else if (paint instanceof TexturePaint JavaDoc)
133                 paintDesc = svgTexturePaint.toSVG((TexturePaint JavaDoc)paint);
134         }
135
136         return paintDesc;
137     }
138 }
139
Popular Tags