001/* 002 * SVG Salamander 003 * Copyright (c) 2004, Mark McKay 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or 007 * without modification, are permitted provided that the following 008 * conditions are met: 009 * 010 * - Redistributions of source code must retain the above 011 * copyright notice, this list of conditions and the following 012 * disclaimer. 013 * - Redistributions in binary form must reproduce the above 014 * copyright notice, this list of conditions and the following 015 * disclaimer in the documentation and/or other materials 016 * provided with the distribution. 017 * 018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 029 * OF THE POSSIBILITY OF SUCH DAMAGE. 030 * 031 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 032 * projects can be found at http://www.kitfox.com 033 * 034 * Created on April 21, 2005, 10:43 AM 035 */ 036 037package com.kitfox.svg.app.beans; 038 039import com.kitfox.svg.*; 040import java.awt.*; 041import java.awt.geom.*; 042import java.net.*; 043import javax.swing.*; 044 045/** 046 * 047 * @author kitfox 048 */ 049public class SVGPanel extends JPanel 050{ 051 public static final long serialVersionUID = 1; 052 public static final String PROP_AUTOSIZE = "PROP_AUTOSIZE"; 053 054 SVGUniverse svgUniverse = SVGCache.getSVGUniverse(); 055 056 private boolean antiAlias; 057 058// private String svgPath; 059 URI svgURI; 060 061// private boolean scaleToFit; 062 AffineTransform scaleXform = new AffineTransform(); 063 064 public static final int AUTOSIZE_NONE = 0; 065 public static final int AUTOSIZE_HORIZ = 1; 066 public static final int AUTOSIZE_VERT = 2; 067 public static final int AUTOSIZE_BESTFIT = 3; 068 public static final int AUTOSIZE_STRETCH = 4; 069 private int autosize = AUTOSIZE_NONE; 070 071 /** Creates new form SVGIcon */ 072 public SVGPanel() 073 { 074 initComponents(); 075 } 076 077 public int getSVGHeight() 078 { 079 if (autosize == AUTOSIZE_VERT || autosize == AUTOSIZE_STRETCH 080 || autosize == AUTOSIZE_BESTFIT) 081 { 082 return getPreferredSize().height; 083 } 084 085 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 086 if (diagram == null) 087 { 088 return 0; 089 } 090 return (int)diagram.getHeight(); 091 } 092 093 public int getSVGWidth() 094 { 095 if (autosize == AUTOSIZE_HORIZ || autosize == AUTOSIZE_STRETCH 096 || autosize == AUTOSIZE_BESTFIT) 097 { 098 return getPreferredSize().width; 099 } 100 101 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 102 if (diagram == null) 103 { 104 return 0; 105 } 106 return (int)diagram.getWidth(); 107 } 108 109 @Override 110 public void paintComponent(Graphics gg) 111 { 112 super.paintComponent(gg); 113 114 Graphics2D g = (Graphics2D)gg.create(); 115 paintComponent(g); 116 g.dispose(); 117 } 118 119 private void paintComponent(Graphics2D g) 120 { 121 Object oldAliasHint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING); 122 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF); 123 124 125 SVGDiagram diagram = svgUniverse.getDiagram(svgURI); 126 if (diagram == null) 127 { 128 return; 129 } 130 131 if (autosize == AUTOSIZE_NONE) 132 { 133 try 134 { 135 diagram.render(g); 136 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint); 137 } 138 catch (SVGException e) 139 { 140 throw new RuntimeException(e); 141 } 142 return; 143 } 144 145 Dimension dim = getSize(); 146 final int width = dim.width; 147 final int height = dim.height; 148 149// final Rectangle2D.Double rect = new Rectangle2D.Double(); 150// diagram.getViewRect(rect); 151 152 double diaWidth = diagram.getWidth(); 153 double diaHeight = diagram.getHeight(); 154 155 double scaleW = 1; 156 double scaleH = 1; 157 if (autosize == AUTOSIZE_BESTFIT) 158 { 159 scaleW = scaleH = (height / diaHeight < width / diaWidth) 160 ? height / diaHeight : width / diaWidth; 161 } 162 else if (autosize == AUTOSIZE_HORIZ) 163 { 164 scaleW = scaleH = width / diaWidth; 165 } 166 else if (autosize == AUTOSIZE_VERT) 167 { 168 scaleW = scaleH = height / diaHeight; 169 } 170 else if (autosize == AUTOSIZE_STRETCH) 171 { 172 scaleW = width / diaWidth; 173 scaleH = height / diaHeight; 174 } 175 scaleXform.setToScale(scaleW, scaleH); 176 177 AffineTransform oldXform = g.getTransform(); 178 g.transform(scaleXform); 179 180 try 181 { 182 diagram.render(g); 183 } 184 catch (SVGException e) 185 { 186 throw new RuntimeException(e); 187 } 188 189 g.setTransform(oldXform); 190 191 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAliasHint); 192 } 193 194 public SVGUniverse getSvgUniverse() 195 { 196 return svgUniverse; 197 } 198 199 public void setSvgUniverse(SVGUniverse svgUniverse) 200 { 201 SVGUniverse old = this.svgUniverse; 202 this.svgUniverse = svgUniverse; 203 firePropertyChange("svgUniverse", old, svgUniverse); 204 } 205 206 public URI getSvgURI() 207 { 208 return svgURI; 209 } 210 211 public void setSvgURI(URI svgURI) 212 { 213 URI old = this.svgURI; 214 this.svgURI = svgURI; 215 firePropertyChange("svgURI", old, svgURI); 216 } 217 218 /** 219 * Most resources your component will want to access will be resources on your classpath. 220 * This method will interpret the passed string as a path in the classpath and use 221 * Class.getResource() to determine the URI of the SVG. 222 */ 223 public void setSvgResourcePath(String resourcePath) throws SVGException 224 { 225 URI old = this.svgURI; 226 227 try 228 { 229 svgURI = new URI(getClass().getResource(resourcePath).toString()); 230//System.err.println("SVGPanel: new URI " + svgURI + " from path " + resourcePath); 231 232 firePropertyChange("svgURI", old, svgURI); 233 234 repaint(); 235 } 236 catch (Exception e) 237 { 238 throw new SVGException("Could not resolve path " + resourcePath, e); 239// svgURI = old; 240 } 241 } 242 243 /** 244 * If this SVG document has a viewbox, if scaleToFit is set, will scale the viewbox to match the 245 * preferred size of this icon 246 * @deprecated 247 * @return 248 */ 249 public boolean isScaleToFit() 250 { 251 return autosize == AUTOSIZE_STRETCH; 252 } 253 254 /** 255 * @deprecated 256 */ 257 public void setScaleToFit(boolean scaleToFit) 258 { 259 setAutosize(AUTOSIZE_STRETCH); 260// boolean old = this.scaleToFit; 261// this.scaleToFit = scaleToFit; 262// firePropertyChange("scaleToFit", old, scaleToFit); 263 } 264 265 /** 266 * @return true if antiAliasing is turned on. 267 * @deprecated 268 */ 269 public boolean getUseAntiAlias() 270 { 271 return getAntiAlias(); 272 } 273 274 /** 275 * @param antiAlias true to use antiAliasing. 276 * @deprecated 277 */ 278 public void setUseAntiAlias(boolean antiAlias) 279 { 280 setAntiAlias(antiAlias); 281 } 282 283 /** 284 * @return true if antiAliasing is turned on. 285 */ 286 public boolean getAntiAlias() 287 { 288 return antiAlias; 289 } 290 291 /** 292 * @param antiAlias true to use antiAliasing. 293 */ 294 public void setAntiAlias(boolean antiAlias) 295 { 296 boolean old = this.antiAlias; 297 this.antiAlias = antiAlias; 298 firePropertyChange("antiAlias", old, antiAlias); 299 } 300 301 /** 302 * @return the autosize 303 */ 304 public int getAutosize() 305 { 306 return autosize; 307 } 308 309 /** 310 * @param autosize the autosize to set 311 */ 312 public void setAutosize(int autosize) 313 { 314 int oldAutosize = this.autosize; 315 this.autosize = autosize; 316 firePropertyChange(PROP_AUTOSIZE, oldAutosize, autosize); 317 } 318 319 /** This method is called from within the constructor to 320 * initialize the form. 321 * WARNING: Do NOT modify this code. The content of this method is 322 * always regenerated by the Form Editor. 323 */ 324 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents 325 private void initComponents() 326 { 327 328 setLayout(new java.awt.BorderLayout()); 329 330 } 331 // </editor-fold>//GEN-END:initComponents 332 333 334 // Variables declaration - do not modify//GEN-BEGIN:variables 335 // End of variables declaration//GEN-END:variables 336 337 338}