Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.hyperlink.app / org.gvsig.hyperlink.app.extension / src / main / java / org / gvsig / hyperlink / app / extension / actions / SvgPanel.java @ 33402

History | View | Annotate | Download (6.69 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.io.File;
34
import java.net.MalformedURLException;
35
import java.net.URI;
36

    
37
import javax.swing.ImageIcon;
38
import javax.swing.JLabel;
39

    
40
import org.apache.batik.bridge.BridgeContext;
41
import org.apache.batik.bridge.DocumentLoader;
42
import org.apache.batik.bridge.GVTBuilder;
43
import org.apache.batik.bridge.UserAgentAdapter;
44
import org.apache.batik.bridge.ViewBox;
45
import org.apache.batik.gvt.GraphicsNode;
46
import org.apache.batik.gvt.renderer.StaticRenderer;
47
import org.gvsig.andami.PluginServices;
48
import org.gvsig.andami.messages.NotificationManager;
49
import org.gvsig.hyperlink.app.extension.AbstractHyperLinkPanel;
50
import org.w3c.dom.Document;
51
import org.w3c.dom.Element;
52
import org.w3c.dom.svg.SVGDocument;
53

    
54
import com.sun.jimi.core.Jimi;
55

    
56
/**
57
 * This class extends AbstractHyperLink, and provides suppot to open images of
58
 * many formats.
59
 * The common supported formats are JPG, ICO, BMP, TIFF, GIF and PNG. Implements
60
 * methods from
61
 * IExtensionBuilder to make it extending.
62
 * 
63
 * @author Eustaquio Vercher (IVER)
64
 * @author Cesar Martinez Izquierdo (IVER)
65
 */
66
public class SvgPanel extends AbstractHyperLinkPanel {
67

    
68
    private static final long serialVersionUID = -5200841105188251551L;
69
    private GVTBuilder gvtBuilder = new GVTBuilder();
70
    private GraphicsNode gvtRoot = null;
71
    private BridgeContext ctx = null;
72
    private StaticRenderer renderer = new StaticRenderer();
73
    private Element elt;
74
    protected static RenderingHints defaultRenderingHints;
75
    static {
76
        defaultRenderingHints = new RenderingHints(null);
77
        defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
78
            RenderingHints.VALUE_ANTIALIAS_ON);
79

    
80
        defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
81
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
82
    }
83

    
84
    /**
85
     * Default constructor.
86
     */
87
    public SvgPanel(URI doc) {
88
        super(doc);
89
        initialize();
90
    }
91

    
92
    /**
93
     * Initializes this panel.
94
     */
95
    void initialize() {
96
        this.setLayout(new BorderLayout());
97
        showDocument();
98
        // this.setSize(600, 400);
99
    }
100

    
101
    /**
102
     * Implements the necessary code to open images in this panel.
103
     */
104
    protected void showDocument() {
105
        if (!checkAndNormalizeURI()) {
106
            return;
107
        }
108

    
109
        ImageIcon image;
110
        // try {
111
        image = new ImageIcon(getSvgAsImage(document.toString()));
112

    
113
        if (image == null)
114
            ; // Incluir error
115
        this.setPreferredSize(new Dimension(image.getIconWidth(),
116
            image.getIconHeight()));
117
        this.setSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
118
        JLabel label = new JLabel(image);
119
        this.add(label);
120

    
121
        // } catch (MalformedURLException e) {
122
        //
123
        // }
124
    }
125

    
126
    /**
127
     * Allows paint SVG images in the panel.
128
     * 
129
     * @param file
130
     *            , this file has been extracted from the URI
131
     */
132
    private Image getSvgAsImage(String uri) {
133
        BufferedImage img =
134
            new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB);
135
        Graphics2D g = img.createGraphics();
136
        Rectangle2D rect = new Rectangle2D.Double();
137
        rect.setFrame(0, 0, 400, 400);
138
        obtainStaticRenderer(uri);
139
        drawSVG(g, rect, null);
140
        return img;
141
    }
142

    
143
    /**
144
     * Render the image to add to the panel.
145
     * 
146
     * @param file
147
     *            , this file has been extracted from the URI
148
     */
149
    private void obtainStaticRenderer(String uri) {
150
        try {
151
            UserAgentAdapter userAgent = new UserAgentAdapter();
152
            DocumentLoader loader = new DocumentLoader(userAgent);
153
            ctx = new BridgeContext(userAgent, loader);
154
            // Document svgDoc = loader.loadDocument(file.toURI().toString());
155
            Document svgDoc = loader.loadDocument(uri);
156
            gvtRoot = gvtBuilder.build(ctx, svgDoc);
157
            renderer.setTree(gvtRoot);
158
            elt = ((SVGDocument) svgDoc).getRootElement();
159
        } catch (Exception ex) {
160
            NotificationManager.addWarning(PluginServices.getText(this,
161
                "Hyperlink_linked_field_doesnot_exist"), ex);
162
        }
163
    }
164

    
165
    /**
166
     * Draw SVG in the Graphics that receives like parameter.
167
     * 
168
     * @param g
169
     *            Graphics
170
     * @param rect
171
     *            Rectangle that fills the Graphic.
172
     * @param rv
173
     *            Rectangle. This forms the visible part in the Layout
174
     */
175
    private void drawSVG(Graphics2D g, Rectangle2D rect, Rectangle2D rv) {
176
        if ((rv == null) || rv.contains(rect)) {
177
            AffineTransform ataux = new AffineTransform();
178

    
179
            ataux.translate(rect.getX(), rect.getY());
180
            try {
181
                ataux.concatenate(ViewBox.getViewTransform(null,
182
                    elt,
183
                    (float) rect.getWidth(),
184
                    (float) rect.getHeight(),
185
                    ctx));
186
                gvtRoot.setTransform(ataux);
187

    
188
            } catch (Exception e) {
189
                // TODO: handle exception
190
            }
191
        } else {
192
            AffineTransform ataux = new AffineTransform();
193

    
194
            ataux.translate(rect.getX(), rect.getY());
195
            ataux.concatenate(ViewBox.getViewTransform(null,
196
                elt,
197
                (float) rect.getWidth(),
198
                (float) rect.getHeight(),
199
                ctx));
200

    
201
            gvtRoot.setTransform(ataux);
202
        }
203

    
204
        RenderingHints renderingHints = defaultRenderingHints;
205
        g.setRenderingHints(renderingHints);
206

    
207
        if (gvtRoot != null) {
208
            gvtRoot.paint(g);
209
        }
210
    }
211

    
212
}