root / trunk / extensions / extHyperlink / src / org / gvsig / hyperlink / actions / SvgPanel.java @ 28132
History | View | Annotate | Download (5.29 KB)
1 |
package org.gvsig.hyperlink.actions; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.Graphics2D; |
6 |
import java.awt.Image; |
7 |
import java.awt.RenderingHints; |
8 |
import java.awt.geom.AffineTransform; |
9 |
import java.awt.geom.Rectangle2D; |
10 |
import java.awt.image.BufferedImage; |
11 |
import java.io.File; |
12 |
import java.net.MalformedURLException; |
13 |
import java.net.URI; |
14 |
|
15 |
import javax.swing.ImageIcon; |
16 |
import javax.swing.JLabel; |
17 |
|
18 |
import org.apache.batik.bridge.BridgeContext; |
19 |
import org.apache.batik.bridge.DocumentLoader; |
20 |
import org.apache.batik.bridge.GVTBuilder; |
21 |
import org.apache.batik.bridge.UserAgentAdapter; |
22 |
import org.apache.batik.bridge.ViewBox; |
23 |
import org.apache.batik.gvt.GraphicsNode; |
24 |
import org.apache.batik.gvt.renderer.StaticRenderer; |
25 |
import org.gvsig.hyperlink.AbstractHyperLinkPanel; |
26 |
import org.w3c.dom.Document; |
27 |
import org.w3c.dom.Element; |
28 |
import org.w3c.dom.svg.SVGDocument; |
29 |
|
30 |
import com.iver.andami.PluginServices; |
31 |
import com.iver.andami.messages.NotificationManager; |
32 |
import com.sun.jimi.core.Jimi; |
33 |
|
34 |
/**
|
35 |
* This class extends AbstractHyperLink, and provides suppot to open images of many formats.
|
36 |
* The common supported formats are JPG, ICO, BMP, TIFF, GIF and PNG. Implements methods from
|
37 |
* IExtensionBuilder to make it extending.
|
38 |
*
|
39 |
* @author Eustaquio Vercher (IVER)
|
40 |
* @author Cesar Martinez Izquierdo (IVER)
|
41 |
*/
|
42 |
public class SvgPanel extends AbstractHyperLinkPanel{ |
43 |
private static final long serialVersionUID = -5200841105188251551L; |
44 |
private GVTBuilder gvtBuilder = new GVTBuilder(); |
45 |
private GraphicsNode gvtRoot = null; |
46 |
private BridgeContext ctx = null; |
47 |
private StaticRenderer renderer = new StaticRenderer(); |
48 |
private Element elt; |
49 |
protected static RenderingHints defaultRenderingHints; |
50 |
static {
|
51 |
defaultRenderingHints = new RenderingHints(null); |
52 |
defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
|
53 |
RenderingHints.VALUE_ANTIALIAS_ON);
|
54 |
|
55 |
defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
|
56 |
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
57 |
} |
58 |
/**
|
59 |
* Default constructor.
|
60 |
*/
|
61 |
public SvgPanel(URI doc){ |
62 |
super(doc);
|
63 |
initialize(); |
64 |
} |
65 |
/**
|
66 |
* Initializes this panel.
|
67 |
*/
|
68 |
void initialize(){
|
69 |
this.setLayout(new BorderLayout()); |
70 |
showDocument(); |
71 |
//this.setSize(600, 400);
|
72 |
} |
73 |
|
74 |
/**
|
75 |
* Implements the necessary code to open images in this panel.
|
76 |
*/
|
77 |
protected void showDocument() { |
78 |
if (!checkAndNormalizeURI()) {
|
79 |
return;
|
80 |
} |
81 |
|
82 |
ImageIcon image;
|
83 |
// try {
|
84 |
image = new ImageIcon( |
85 |
getSvgAsImage(document.toString())); |
86 |
|
87 |
if (image==null); //Incluir error |
88 |
this.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight())); |
89 |
this.setSize(new Dimension(image.getIconWidth(), |
90 |
image.getIconHeight())); |
91 |
JLabel label = new JLabel(image); |
92 |
this.add(label);
|
93 |
|
94 |
// } catch (MalformedURLException e) {
|
95 |
//
|
96 |
// }
|
97 |
} |
98 |
|
99 |
|
100 |
/**
|
101 |
* Allows paint SVG images in the panel.
|
102 |
*
|
103 |
* @param file, this file has been extracted from the URI
|
104 |
*/
|
105 |
private Image getSvgAsImage(String uri) { |
106 |
BufferedImage img = new BufferedImage(400, 400, |
107 |
BufferedImage.TYPE_INT_ARGB);
|
108 |
Graphics2D g = img.createGraphics();
|
109 |
Rectangle2D rect = new Rectangle2D.Double(); |
110 |
rect.setFrame(0,0,400,400); |
111 |
obtainStaticRenderer(uri); |
112 |
drawSVG(g,rect, null);
|
113 |
return img;
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* Render the image to add to the panel.
|
118 |
* @param file, this file has been extracted from the URI
|
119 |
*/
|
120 |
private void obtainStaticRenderer(String uri) { |
121 |
try {
|
122 |
UserAgentAdapter userAgent = new UserAgentAdapter();
|
123 |
DocumentLoader loader = new DocumentLoader(userAgent);
|
124 |
ctx = new BridgeContext(userAgent, loader);
|
125 |
// Document svgDoc = loader.loadDocument(file.toURI().toString());
|
126 |
Document svgDoc = loader.loadDocument(uri);
|
127 |
gvtRoot = gvtBuilder.build(ctx, svgDoc); |
128 |
renderer.setTree(gvtRoot); |
129 |
elt = ((SVGDocument) svgDoc).getRootElement(); |
130 |
} catch (Exception ex) { |
131 |
NotificationManager.addWarning(PluginServices.getText(this, "Hyperlink_linked_field_doesnot_exist"), ex); |
132 |
} |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Draw SVG in the Graphics that receives like parameter.
|
137 |
*
|
138 |
* @param g Graphics
|
139 |
* @param rect Rectangle that fills the Graphic.
|
140 |
* @param rv Rectangle. This forms the visible part in the Layout
|
141 |
*/
|
142 |
private void drawSVG(Graphics2D g, Rectangle2D rect, Rectangle2D rv) { |
143 |
if ((rv == null) || rv.contains(rect)) { |
144 |
AffineTransform ataux = new AffineTransform(); |
145 |
|
146 |
ataux.translate(rect.getX(), rect.getY()); |
147 |
try {
|
148 |
ataux.concatenate(ViewBox.getViewTransform(null, elt,
|
149 |
(float) rect.getWidth(), (float) rect.getHeight(), ctx)); |
150 |
gvtRoot.setTransform(ataux); |
151 |
|
152 |
} catch (Exception e) { |
153 |
// TODO: handle exception
|
154 |
} |
155 |
} else {
|
156 |
AffineTransform ataux = new AffineTransform(); |
157 |
|
158 |
ataux.translate(rect.getX(), rect.getY()); |
159 |
ataux.concatenate(ViewBox.getViewTransform(null, elt,
|
160 |
(float) rect.getWidth(), (float) rect.getHeight(), ctx)); |
161 |
|
162 |
gvtRoot.setTransform(ataux); |
163 |
} |
164 |
|
165 |
RenderingHints renderingHints = defaultRenderingHints;
|
166 |
g.setRenderingHints(renderingHints); |
167 |
|
168 |
if (gvtRoot != null) { |
169 |
gvtRoot.paint(g); |
170 |
} |
171 |
} |
172 |
|
173 |
} |