001: package com.xoetrope.svg;
002:
003: import com.kitfox.svg.SVGCache;
004: import com.kitfox.svg.SVGDiagram;
005: import com.kitfox.svg.SVGException;
006: import com.kitfox.svg.SVGRoot;
007: import com.kitfox.svg.SVGUniverse;
008: import java.awt.BorderLayout;
009: import java.awt.Graphics;
010: import java.awt.Graphics2D;
011: import java.awt.RenderingHints;
012: import java.awt.image.BufferedImage;
013: import java.net.URI;
014: import java.net.URL;
015: import javax.swing.JComponent;
016: import net.xoetrope.xui.XProject;
017: import net.xoetrope.xui.XProjectManager;
018: import net.xoetrope.xui.build.BuildProperties;
019:
020: /**
021: *
022: *
023: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
024: * the GNU Public License (GPL), please see license.txt for more details. If
025: * you make commercial use of this software you must purchase a commercial
026: * license from Xoetrope.</p>
027: * <p> $Revision: 1.2 $</p>
028: */
029: public class XDisplaySvg extends JComponent {
030: protected URL url;
031: protected URI uri;
032: protected SVGDiagram diagram;
033: protected SVGUniverse universe;
034: protected BufferedImage bufferedImage;
035: private XProject currentProject;
036:
037: /** Creates a new instance of DisplaySvg */
038: public XDisplaySvg() {
039: currentProject = XProjectManager.getCurrentProject();
040:
041: setLayout(new BorderLayout());
042: universe = SVGCache.getSVGUniverse();
043: setOpaque(true);
044: }
045:
046: /**
047: * Used to set the URL pointing to the SVG image that is to be displayed.
048: * @param url <code>URL</code> object specifying the location of the SVG image.
049: */
050: public void setURL(URL url) {
051: this .url = url;
052: }
053:
054: /**
055: * Used to set the attributes of a componenent without knowing the individual accessor methods
056: * @param attribName <code>String</code> specifying the attribute name
057: * @param attribValue <code>Object</code> specifying the attribute value
058: */
059: public int setAttribute(String attribName, Object attribValue) {
060: if (attribName.equals("content") || attribName.equals("image"))
061: setURL(currentProject.findResource(attribValue.toString()));
062:
063: return 0;
064: }
065:
066: /**
067: * Displays the SVG image by adding the file to an <CODE>SVGUniverse</CODE> instance and
068: * extracting an <CODE>SVGDiagram</CODE> instance from the <CODE>SVGUniverse</CODE> instance.
069: */
070: public SVGDiagram display() {
071: try {
072: uri = universe.loadSVG(url);
073: diagram = universe.getDiagram(uri);
074: } catch (Exception ex) {
075: if (BuildProperties.DEBUG)
076: ex.printStackTrace();
077: }
078:
079: return diagram;
080: }
081:
082: /**
083: * Used to paint a buffered image of the rendered svg to the component.
084: * @param g the delegate <CODE>Graphics</CODE> object.
085: */
086: public void paintComponent(Graphics g) {
087: Graphics2D g2d = (Graphics2D) g.create();
088:
089: if (bufferedImage == null) {
090: bufferedImage = new BufferedImage(getWidth(), getHeight(),
091: BufferedImage.TYPE_INT_ARGB);
092: Graphics2D buffer = bufferedImage.createGraphics();
093: buffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
094: RenderingHints.VALUE_ANTIALIAS_ON);
095: diagram.setIgnoringClipHeuristic(true);
096: SVGRoot root = diagram.getRoot();
097:
098: try {
099: root.render(buffer);
100: } catch (SVGException ex) {
101: ex.printStackTrace();
102: }
103:
104: g2d.clearRect(0, 0, getWidth(), getHeight());
105: g2d.drawImage(bufferedImage, 0, 0, this );
106: buffer.dispose();
107: } else {
108: g2d.clearRect(0, 0, getWidth(), getHeight());
109: g2d.drawImage(bufferedImage, 0, 0, this);
110: }
111:
112: g2d.dispose();
113: }
114: }
|