KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
21 import java.awt.Rectangle JavaDoc;
22
23 import javax.swing.AbstractButton JavaDoc;
24 import javax.swing.JComboBox JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.JMenuBar JavaDoc;
27 import javax.swing.JPopupMenu JavaDoc;
28 import javax.swing.JProgressBar JavaDoc;
29 import javax.swing.JScrollBar JavaDoc;
30 import javax.swing.JToolBar JavaDoc;
31 import javax.swing.UIManager JavaDoc;
32 import javax.swing.border.Border JavaDoc;
33 import javax.swing.plaf.ComponentUI JavaDoc;
34
35 import org.w3c.dom.Element JavaDoc;
36
37 /**
38  * This class offers a way to create an SVG document with grouping
39  * that reflects the Swing composite structure (container/components).
40  *
41  * @author Vincent Hardy
42  * @version $Id: SwingSVGPrettyPrint.java,v 1.9 2005/03/27 08:58:35 cam Exp $
43  */

44 public abstract class SwingSVGPrettyPrint implements SVGSyntax {
45
46     /**
47      * @param cmp Swing component to be converted to SVG
48      * @param svgGen SVGraphics2D to use to paint Swing components
49      * @return an SVG fragment containing an SVG equivalent of the Swing
50      * component tree.
51      */

52     public static void print(JComponent JavaDoc cmp, SVGGraphics2D svgGen) {
53         if ((cmp instanceof JComboBox JavaDoc) || (cmp instanceof JScrollBar JavaDoc)) {
54             // This is a work around unresolved issue with JComboBox
55
// and JScrollBar
56
printHack(cmp, svgGen);
57             return;
58         }
59
60         // Spawn a new Graphics2D for this component
61
SVGGraphics2D g = (SVGGraphics2D)svgGen.create();
62         g.setColor(cmp.getForeground());
63         g.setFont(cmp.getFont());
64         Element JavaDoc topLevelGroup = g.getTopLevelGroup();
65
66         // If there is no area to be painted, return here
67
if ((cmp.getWidth() <= 0) || (cmp.getHeight() <= 0))
68             return;
69
70         Rectangle JavaDoc clipRect = g.getClipBounds();
71         if (clipRect == null)
72             g.setClip(0, 0, cmp.getWidth(), cmp.getHeight());
73
74         paintComponent(cmp, g);
75         paintBorder(cmp, g);
76         paintChildren(cmp, g);
77
78         // Now, structure DOM tree to reflect this component's structure
79
Element JavaDoc cmpGroup = g.getTopLevelGroup();
80         cmpGroup.setAttributeNS(null, "id",
81                                 svgGen.getGeneratorContext().idGenerator.
82                                 generateID(cmp.getClass().getName()));
83
84         topLevelGroup.appendChild(cmpGroup);
85         svgGen.setTopLevelGroup(topLevelGroup);
86     }
87
88     /**
89      * @param cmp Swing component to be converted to SVG
90      * @param svgGen SVGraphics2D to use to paint Swing components
91      */

92     private static void printHack(JComponent JavaDoc cmp, SVGGraphics2D svgGen) {
93         // Spawn a new Graphics2D for this component
94
SVGGraphics2D g = (SVGGraphics2D)svgGen.create();
95         g.setColor(cmp.getForeground());
96         g.setFont(cmp.getFont());
97         Element JavaDoc topLevelGroup = g.getTopLevelGroup();
98
99         // If there is no area to be painted, return here
100
if ((cmp.getWidth() <= 0) || (cmp.getHeight() <= 0))
101             return;
102
103         Rectangle JavaDoc clipRect = g.getClipBounds();
104         if (clipRect == null) {
105             g.setClip(0, 0, cmp.getWidth(), cmp.getHeight());
106         }
107
108         cmp.paint(g);
109
110         // Now, structure DOM tree to reflect this component's structure
111
Element JavaDoc cmpGroup = g.getTopLevelGroup();
112         cmpGroup.setAttributeNS(null, "id",
113                                 svgGen.getGeneratorContext().idGenerator.
114                                 generateID(cmp.getClass().getName()));
115
116         topLevelGroup.appendChild(cmpGroup);
117         svgGen.setTopLevelGroup(topLevelGroup);
118     }
119
120
121     private static void paintComponent(JComponent JavaDoc cmp, SVGGraphics2D svgGen){
122         ComponentUI JavaDoc ui = UIManager.getUI(cmp);
123         if(ui != null){
124             ui.installUI(cmp);
125             ui.update(svgGen, cmp);
126         }
127     }
128
129     /**
130      * WARNING: The following code does some special case processing
131      * depending on the class of the input JComponent. This is needed
132      * because there is no generic way I could find to determine whether
133      * a component should be painted or not.
134      */

135     private static void paintBorder(JComponent JavaDoc cmp, SVGGraphics2D svgGen){
136         Border JavaDoc border = cmp.getBorder();
137         if(border != null){
138             if( (cmp instanceof AbstractButton JavaDoc)
139                 ||
140                 (cmp instanceof JPopupMenu JavaDoc)
141                 ||
142                 (cmp instanceof JToolBar JavaDoc)
143                 ||
144                 (cmp instanceof JMenuBar JavaDoc)
145                 ||
146                 (cmp instanceof JProgressBar JavaDoc) ){
147                 if( ((cmp instanceof AbstractButton JavaDoc) && ((AbstractButton JavaDoc)cmp).isBorderPainted())
148                     ||
149                     ((cmp instanceof JPopupMenu JavaDoc) && ((JPopupMenu JavaDoc)cmp).isBorderPainted())
150                     ||
151                     ((cmp instanceof JToolBar JavaDoc) && ((JToolBar JavaDoc)cmp).isBorderPainted())
152                     ||
153                     ((cmp instanceof JMenuBar JavaDoc) && ((JMenuBar JavaDoc)cmp).isBorderPainted())
154                     ||
155                     ((cmp instanceof JProgressBar JavaDoc) && ((JProgressBar JavaDoc)cmp).isBorderPainted() ))
156                     border.paintBorder(cmp, svgGen, 0, 0, cmp.getWidth(), cmp.getHeight());
157             } else {
158                 border.paintBorder(cmp, svgGen, 0, 0, cmp.getWidth(), cmp.getHeight());
159             }
160         }
161     }
162
163     private static void paintChildren(JComponent JavaDoc cmp, SVGGraphics2D svgGen){
164         int i = cmp.getComponentCount() - 1;
165         boolean isJComponent = false;
166         Rectangle JavaDoc tmpRect = new Rectangle JavaDoc();
167
168         for(; i>=0; i--){
169             Component JavaDoc comp = cmp.getComponent(i);
170
171             if(comp != null && JComponent.isLightweightComponent(comp) &&
172                (comp.isVisible() == true)) {
173                 Rectangle JavaDoc cr = null;
174                 isJComponent = (comp instanceof JComponent JavaDoc);
175
176                 if(isJComponent) {
177                     cr = tmpRect;
178                     ((JComponent JavaDoc)comp).getBounds(cr);
179                 } else {
180                     cr = comp.getBounds();
181                 }
182
183                 boolean hitClip =
184                     svgGen.hitClip(cr.x, cr.y, cr.width, cr.height);
185
186                 if (hitClip) {
187                     SVGGraphics2D cg = (SVGGraphics2D)svgGen.create(cr.x, cr.y, cr.width, cr.height);
188                     cg.setColor(comp.getForeground());
189                     cg.setFont(comp.getFont());
190                     if(comp instanceof JComponent JavaDoc)
191                         print((JComponent JavaDoc)comp, cg);
192                     else{
193                         comp.paint(cg);
194                     }
195                 }
196             }
197         }
198     }
199 }
200
Popular Tags