root / org.gvsig.hyperlink.app / trunk / org.gvsig.hyperlink.app / org.gvsig.hyperlink.app.extension / src / main / java / org / gvsig / hyperlink / app / extension / actions / SvgPanel.java @ 15
History | View | Annotate | Download (6.6 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.hyperlink.app.extension.actions; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.Graphics2D; |
28 |
import java.awt.Image; |
29 |
import java.awt.RenderingHints; |
30 |
import java.awt.geom.AffineTransform; |
31 |
import java.awt.geom.Rectangle2D; |
32 |
import java.awt.image.BufferedImage; |
33 |
import java.net.URI; |
34 |
|
35 |
import javax.swing.ImageIcon; |
36 |
import javax.swing.JLabel; |
37 |
|
38 |
import org.apache.batik.bridge.BridgeContext; |
39 |
import org.apache.batik.bridge.DocumentLoader; |
40 |
import org.apache.batik.bridge.GVTBuilder; |
41 |
import org.apache.batik.bridge.UserAgentAdapter; |
42 |
import org.apache.batik.bridge.ViewBox; |
43 |
import org.apache.batik.gvt.GraphicsNode; |
44 |
import org.apache.batik.gvt.renderer.StaticRenderer; |
45 |
import org.w3c.dom.Document; |
46 |
import org.w3c.dom.Element; |
47 |
import org.w3c.dom.svg.SVGDocument; |
48 |
|
49 |
import org.gvsig.andami.PluginServices; |
50 |
import org.gvsig.andami.messages.NotificationManager; |
51 |
import org.gvsig.hyperlink.app.extension.AbstractHyperLinkPanel; |
52 |
|
53 |
/**
|
54 |
* This class extends AbstractHyperLink, and provides suppot to open images of
|
55 |
* many formats.
|
56 |
* The common supported formats are JPG, ICO, BMP, TIFF, GIF and PNG. Implements
|
57 |
* methods from
|
58 |
* IExtensionBuilder to make it extending.
|
59 |
*
|
60 |
* @author Eustaquio Vercher (IVER)
|
61 |
* @author Cesar Martinez Izquierdo (IVER)
|
62 |
*/
|
63 |
public class SvgPanel extends AbstractHyperLinkPanel { |
64 |
|
65 |
private static final long serialVersionUID = -5200841105188251551L; |
66 |
private GVTBuilder gvtBuilder = new GVTBuilder(); |
67 |
private GraphicsNode gvtRoot = null; |
68 |
private BridgeContext ctx = null; |
69 |
private StaticRenderer renderer = new StaticRenderer(); |
70 |
private Element elt; |
71 |
protected static RenderingHints defaultRenderingHints; |
72 |
static {
|
73 |
defaultRenderingHints = new RenderingHints(null); |
74 |
defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
|
75 |
RenderingHints.VALUE_ANTIALIAS_ON);
|
76 |
|
77 |
defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
|
78 |
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
79 |
} |
80 |
|
81 |
/**
|
82 |
* Default constructor.
|
83 |
*/
|
84 |
public SvgPanel(URI doc) { |
85 |
super(doc);
|
86 |
initialize(); |
87 |
} |
88 |
|
89 |
/**
|
90 |
* Initializes this panel.
|
91 |
*/
|
92 |
void initialize() {
|
93 |
this.setLayout(new BorderLayout()); |
94 |
showDocument(); |
95 |
// this.setSize(600, 400);
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* Implements the necessary code to open images in this panel.
|
100 |
*/
|
101 |
protected void showDocument() { |
102 |
if (!checkAndNormalizeURI()) {
|
103 |
return;
|
104 |
} |
105 |
|
106 |
ImageIcon image;
|
107 |
// try {
|
108 |
image = new ImageIcon(getSvgAsImage(document.toString())); |
109 |
|
110 |
if (image == null) |
111 |
; // Incluir error
|
112 |
this.setPreferredSize(new Dimension(image.getIconWidth(), |
113 |
image.getIconHeight())); |
114 |
this.setSize(new Dimension(image.getIconWidth(), image.getIconHeight())); |
115 |
JLabel label = new JLabel(image); |
116 |
this.add(label);
|
117 |
|
118 |
// } catch (MalformedURLException e) {
|
119 |
//
|
120 |
// }
|
121 |
} |
122 |
|
123 |
/**
|
124 |
* Allows paint SVG images in the panel.
|
125 |
*
|
126 |
* @param file
|
127 |
* , this file has been extracted from the URI
|
128 |
*/
|
129 |
private Image getSvgAsImage(String uri) { |
130 |
BufferedImage img =
|
131 |
new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB); |
132 |
Graphics2D g = img.createGraphics();
|
133 |
Rectangle2D rect = new Rectangle2D.Double(); |
134 |
rect.setFrame(0, 0, 400, 400); |
135 |
obtainStaticRenderer(uri); |
136 |
drawSVG(g, rect, null);
|
137 |
return img;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* Render the image to add to the panel.
|
142 |
*
|
143 |
* @param file
|
144 |
* , this file has been extracted from the URI
|
145 |
*/
|
146 |
private void obtainStaticRenderer(String uri) { |
147 |
try {
|
148 |
UserAgentAdapter userAgent = new UserAgentAdapter();
|
149 |
DocumentLoader loader = new DocumentLoader(userAgent);
|
150 |
ctx = new BridgeContext(userAgent, loader);
|
151 |
// Document svgDoc = loader.loadDocument(file.toURI().toString());
|
152 |
Document svgDoc = loader.loadDocument(uri);
|
153 |
gvtRoot = gvtBuilder.build(ctx, svgDoc); |
154 |
renderer.setTree(gvtRoot); |
155 |
elt = ((SVGDocument) svgDoc).getRootElement(); |
156 |
} catch (Exception ex) { |
157 |
NotificationManager.addWarning(PluginServices.getText(this,
|
158 |
"Hyperlink_linked_field_doesnot_exist"), ex);
|
159 |
} |
160 |
} |
161 |
|
162 |
/**
|
163 |
* Draw SVG in the Graphics that receives like parameter.
|
164 |
*
|
165 |
* @param g
|
166 |
* Graphics
|
167 |
* @param rect
|
168 |
* Rectangle that fills the Graphic.
|
169 |
* @param rv
|
170 |
* Rectangle. This forms the visible part in the Layout
|
171 |
*/
|
172 |
private void drawSVG(Graphics2D g, Rectangle2D rect, Rectangle2D rv) { |
173 |
if ((rv == null) || rv.contains(rect)) { |
174 |
AffineTransform ataux = new AffineTransform(); |
175 |
|
176 |
ataux.translate(rect.getX(), rect.getY()); |
177 |
try {
|
178 |
ataux.concatenate(ViewBox.getViewTransform(null,
|
179 |
elt, |
180 |
(float) rect.getWidth(),
|
181 |
(float) rect.getHeight(),
|
182 |
ctx)); |
183 |
gvtRoot.setTransform(ataux); |
184 |
|
185 |
} catch (Exception e) { |
186 |
// TODO: handle exception
|
187 |
} |
188 |
} else {
|
189 |
AffineTransform ataux = new AffineTransform(); |
190 |
|
191 |
ataux.translate(rect.getX(), rect.getY()); |
192 |
ataux.concatenate(ViewBox.getViewTransform(null,
|
193 |
elt, |
194 |
(float) rect.getWidth(),
|
195 |
(float) rect.getHeight(),
|
196 |
ctx)); |
197 |
|
198 |
gvtRoot.setTransform(ataux); |
199 |
} |
200 |
|
201 |
RenderingHints renderingHints = defaultRenderingHints;
|
202 |
g.setRenderingHints(renderingHints); |
203 |
|
204 |
if (gvtRoot != null) { |
205 |
gvtRoot.paint(g); |
206 |
} |
207 |
} |
208 |
|
209 |
} |