001    /*
002     * SVGIcon.java
003     *
004     * Created on April 21, 2005, 10:43 AM
005     */
006    
007    package com.kitfox.svg.app.beans;
008    
009    import javax.swing.*;
010    import java.awt.*;
011    import java.awt.geom.*;
012    import java.net.*;
013    import java.beans.*;
014    
015    import com.kitfox.svg.*;
016    
017    /**
018     *
019     * @author  kitfox
020     */
021    public class SVGPanel extends JPanel
022    {
023        public static final long serialVersionUID = 1;
024    
025    
026        SVGUniverse svgUniverse = SVGCache.getSVGUniverse();
027        
028        private boolean antiAlias;
029        
030    //    private String svgPath;
031        URI svgURI;
032    
033        private boolean scaleToFit;
034        AffineTransform scaleXform = new AffineTransform();
035        
036        /** Creates new form SVGIcon */
037        public SVGPanel()
038        {
039            initComponents();
040        }
041            
042        public int getSVGHeight()
043        {
044            if (scaleToFit) return getPreferredSize().height;
045            
046            SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
047            if (diagram == null) return 0;
048            return (int)diagram.getHeight();
049        }
050        
051        public int getSVGWidth()
052        {
053            if (scaleToFit) return getPreferredSize().width;
054            
055            SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
056            if (diagram == null) return 0;
057            return (int)diagram.getWidth();
058        }
059        
060    //          Draw the icon at the specified location.
061        public void paintComponent(Graphics gg)
062        {
063            super.paintComponent(gg);
064            
065            Graphics2D g = (Graphics2D)gg;
066            
067            Object oldAliasHint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
068            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
069    
070            
071            SVGDiagram diagram = svgUniverse.getDiagram(svgURI);
072            if (diagram == null) return;
073            
074            if (!scaleToFit)
075            {
076                try
077                {
078                    diagram.render(g);
079                    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint);
080                }
081                catch (SVGException e)
082                {
083                    throw new RuntimeException(e);
084                }
085                return;
086            }
087            
088            Dimension dim = getSize();
089            final int width = dim.width;
090            final int height = dim.height;
091    //        int width = getWidth();
092    //        int height = getHeight();
093            
094    //        if (width == 0 || height == 0)
095    //        {
096    //           //Chances are we're rendering offscreen
097    //            Dimension dim = getSize();
098    //            width = dim.width;
099    //            height = dim.height;
100    //            return;
101    //        }
102            
103    //        g.setClip(0, 0, dim.width, dim.height);
104    
105                
106            final Rectangle2D.Double rect = new Rectangle2D.Double();
107            diagram.getViewRect(rect);
108            
109            scaleXform.setToScale(width / rect.width, height / rect.height);
110            
111            AffineTransform oldXform = g.getTransform();
112            g.transform(scaleXform);
113    
114            try
115            {
116                diagram.render(g);
117            }
118            catch (SVGException e)
119            {
120                throw new RuntimeException(e);
121            }
122            
123            g.setTransform(oldXform);
124            
125            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint);
126        }
127        
128        public SVGUniverse getSvgUniverse()
129        {
130            return svgUniverse;
131        }
132    
133        public void setSvgUniverse(SVGUniverse svgUniverse)
134        {
135            SVGUniverse old = this.svgUniverse;
136            this.svgUniverse = svgUniverse;
137            firePropertyChange("svgUniverse", old, svgUniverse);
138        }
139    
140        public URI getSvgURI()
141        {
142            return svgURI;
143        }
144    
145        public void setSvgURI(URI svgURI)
146        {
147            URI old = this.svgURI;
148            this.svgURI = svgURI;
149            firePropertyChange("svgURI", old, svgURI);
150        }
151        
152        /**
153         * Most resources your component will want to access will be resources on your classpath.  
154         * This method will interpret the passed string as a path in the classpath and use
155         * Class.getResource() to determine the URI of the SVG.
156         */
157        public void setSvgResourcePath(String resourcePath) throws SVGException
158        {
159            URI old = this.svgURI;
160            
161            try
162            {
163                svgURI = new URI(getClass().getResource(resourcePath).toString());
164    //System.err.println("SVGPanel: new URI " + svgURI + " from path " + resourcePath);
165                
166                firePropertyChange("svgURI", old, svgURI);
167                
168                repaint();
169            }
170            catch (Exception e)
171            {
172                throw new SVGException("Could not resolve path " + resourcePath, e);
173    //            svgURI = old;
174            }
175        }
176        
177        public boolean isScaleToFit()
178        {
179            return scaleToFit;
180        }
181    
182        public void setScaleToFit(boolean scaleToFit)
183        {
184            boolean old = this.scaleToFit;
185            this.scaleToFit = scaleToFit;
186            firePropertyChange("scaleToFit", old, scaleToFit);
187        }
188        
189        /**
190         * @return true if antiAliasing is turned on.
191         * @deprecated
192         */
193        public boolean getUseAntiAlias()
194        {
195            return getAntiAlias();
196        }
197    
198        /**
199         * @param antiAlias true to use antiAliasing.
200         * @deprecated
201         */
202        public void setUseAntiAlias(boolean antiAlias)
203        {
204            setAntiAlias(antiAlias);
205        }
206        
207        /**
208         * @return true if antiAliasing is turned on.
209         */
210        public boolean getAntiAlias()
211        {
212            return antiAlias;
213        }
214    
215        /**
216         * @param antiAlias true to use antiAliasing.
217         */
218        public void setAntiAlias(boolean antiAlias)
219        {
220            boolean old = this.antiAlias;
221            this.antiAlias = antiAlias;
222            firePropertyChange("antiAlias", old, antiAlias);
223        }
224        
225        /** This method is called from within the constructor to
226         * initialize the form.
227         * WARNING: Do NOT modify this code. The content of this method is
228         * always regenerated by the Form Editor.
229         */
230        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
231        private void initComponents()
232        {
233    
234            setLayout(new java.awt.BorderLayout());
235    
236        }
237        // </editor-fold>//GEN-END:initComponents
238        
239        
240        // Variables declaration - do not modify//GEN-BEGIN:variables
241        // End of variables declaration//GEN-END:variables
242        
243    }