KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.batik.ext.awt.g2d.TransformStackElement;
24 import org.apache.batik.util.SVGConstants;
25
26 /**
27  * Represents the SVG equivalent of a Java 2D API graphic
28  * context attribute.
29  *
30  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
31  * @version $Id: SVGGraphicContext.java,v 1.7 2004/08/18 07:15:00 vhardy Exp $
32  */

33 public class SVGGraphicContext implements SVGConstants, ErrorConstants {
34     // this properties can only be set of leaf nodes =>
35
// if they have default values they can be ignored
36
private static final String JavaDoc leafOnlyAttributes[] = {
37         SVG_OPACITY_ATTRIBUTE,
38         SVG_FILTER_ATTRIBUTE,
39         SVG_CLIP_PATH_ATTRIBUTE
40     };
41
42     private static final String JavaDoc defaultValues[] = {
43         "1",
44         SVG_NONE_VALUE,
45         SVG_NONE_VALUE
46     };
47
48     private Map JavaDoc context;
49     private Map JavaDoc groupContext;
50     private Map JavaDoc graphicElementContext;
51     private TransformStackElement transformStack[];
52
53     /**
54      * @param context Set of style attributes in this context.
55      * @param transformStack Sequence of transforms that where
56      * applied to create the context's current transform.
57      */

58     public SVGGraphicContext(Map JavaDoc context,
59                              TransformStackElement transformStack[]) {
60         if (context == null)
61             throw new SVGGraphics2DRuntimeException(ERR_MAP_NULL);
62         if (transformStack == null)
63             throw new SVGGraphics2DRuntimeException(ERR_TRANS_NULL);
64         this.context = context;
65         this.transformStack = transformStack;
66         computeGroupAndGraphicElementContext();
67     }
68
69     /**
70      * @param groupContext Set of attributes that apply to group
71      * @param graphicElementContext Set of attributes that apply to
72      * elements but not to groups (e.g., opacity, filter).
73      * @param transformStack Sequence of transforms that where
74      * applied to create the context's current transform.
75      */

76     public SVGGraphicContext(Map JavaDoc groupContext, Map JavaDoc graphicElementContext,
77                              TransformStackElement transformStack[]) {
78         if (groupContext == null || graphicElementContext == null)
79             throw new SVGGraphics2DRuntimeException(ERR_MAP_NULL);
80         if (transformStack == null)
81             throw new SVGGraphics2DRuntimeException(ERR_TRANS_NULL);
82
83         this.groupContext = groupContext;
84         this.graphicElementContext = graphicElementContext;
85         this.transformStack = transformStack;
86         computeContext();
87     }
88
89
90     /**
91      * @return set of all attributes.
92      */

93     public Map JavaDoc getContext() {
94         return context;
95     }
96
97     /**
98      * @return set of attributes that can be set on a group
99      */

100     public Map JavaDoc getGroupContext() {
101         return groupContext;
102     }
103
104     /**
105      * @return set of attributes that can be set on leaf node
106      */

107     public Map JavaDoc getGraphicElementContext() {
108         return graphicElementContext;
109     }
110
111     /**
112      * @return set of TransformStackElement for this context
113      */

114     public TransformStackElement[] getTransformStack() {
115         return transformStack;
116     }
117
118     private void computeContext() {
119         if (context != null)
120             return;
121
122         context = new HashMap JavaDoc(groupContext);
123         context.putAll(graphicElementContext);
124     }
125
126     private void computeGroupAndGraphicElementContext() {
127         if (groupContext != null)
128             return;
129         //
130
// Now, move attributes that only apply to
131
// leaf elements to a separate map.
132
//
133
groupContext = new HashMap JavaDoc(context);
134         graphicElementContext = new HashMap JavaDoc();
135         for (int i=0; i< leafOnlyAttributes.length; i++) {
136             Object JavaDoc attrValue = groupContext.get(leafOnlyAttributes[i]);
137             if (attrValue != null){
138                 if (!attrValue.equals(defaultValues[i]))
139                     graphicElementContext.put(leafOnlyAttributes[i], attrValue);
140                 groupContext.remove(leafOnlyAttributes[i]);
141             }
142         }
143     }
144 }
145
Popular Tags