1 18 package org.apache.batik.svggen; 19 20 import java.awt.Color ; 21 import java.awt.Dimension ; 22 import java.awt.GridLayout ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.OutputStreamWriter ; 26 27 import javax.swing.JFrame ; 28 import javax.swing.JPanel ; 29 30 import org.w3c.dom.DOMImplementation ; 31 import org.w3c.dom.Document ; 32 33 import org.apache.batik.dom.svg.SVGDOMImplementation; 34 import org.apache.batik.swing.JSVGCanvas; 35 import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter; 36 import org.apache.batik.swing.svg.SVGDocumentLoaderEvent; 37 import org.apache.batik.util.SVGConstants; 38 39 49 public class JPainterCompare extends JPanel implements SVGConstants{ 50 53 public static final Dimension CANVAS_SIZE 54 = new Dimension (300, 400); 55 56 public static String MESSAGES_USAGE 57 = "JPainterCompare.messages.usage"; 58 59 public static String MESSAGES_LOADING_CLASS 60 = "JPainterCompare.messages.loading.class"; 61 62 public static String MESSAGES_LOADED_CLASS 63 = "JPainterCompare.messages.loaded.class"; 64 65 public static String MESSAGES_INSTANCIATED_OBJECT 66 = "JPainterCompare.messages.instanciated.object"; 67 68 public static String ERROR_COULD_NOT_LOAD_CLASS 69 = "JPainterCompare.error.could.not.load.class"; 70 71 public static String ERROR_COULD_NOT_INSTANCIATE_OBJECT 72 = "JPainterCompare.error.could.not.instanciate.object"; 73 74 public static String ERROR_CLASS_NOT_PAINTER 75 = "JPainterCompare.error.class.not.painter"; 76 77 public static String ERROR_COULD_NOT_TRANSCODE_TO_SVG 78 = "JPainterCompare.error.could.not.transcode.to.svg"; 79 80 public static String ERROR_COULD_NOT_CONVERT_FILE_PATH_TO_URL 81 = "JPainterCompare.error.could.not.convert.file.path.to.url"; 82 83 public static String ERROR_COULD_NOT_RENDER_GENERATED_SVG 84 = "JPainterCompare.error.could.not.render.generated.svg"; 85 86 public static String CONFIG_TMP_FILE_PREFIX 87 = "JPainterCompare.config.tmp.file.prefix"; 88 89 93 protected SVGGraphics2D buildSVGGraphics2D() { 94 DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); 95 String namespaceURI = SVGDOMImplementation.SVG_NAMESPACE_URI; 96 Document domFactory = impl.createDocument(namespaceURI, SVG_SVG_TAG, null); 97 98 SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(domFactory); 100 101 GenericImageHandler ihandler = new CachedImageHandlerBase64Encoder(); 102 ctx.setGenericImageHandler(ihandler); 103 104 return new SVGGraphics2D(ctx, false); 105 } 106 107 static class LoaderListener extends SVGDocumentLoaderAdapter{ 108 public final String sem = "sem"; 109 public boolean success = false; 110 public void documentLoadingFailed(SVGDocumentLoaderEvent e){ 111 synchronized(sem){ 112 sem.notifyAll(); 113 } 114 } 115 116 public void documentLoadingCompleted(SVGDocumentLoaderEvent e){ 117 success = true; 118 synchronized(sem){ 119 sem.notifyAll(); 120 } 121 } 122 } 123 124 127 public JPainterCompare(Painter painter){ 128 JPainterComponent ref = new JPainterComponent(painter); 130 131 SVGGraphics2D g2d = buildSVGGraphics2D(); 133 134 g2d.setSVGCanvasSize(CANVAS_SIZE); 135 136 File tmpFile = null; 140 try{ 141 tmpFile = File.createTempFile(CONFIG_TMP_FILE_PREFIX, 142 ".svg"); 143 144 OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream (tmpFile), "UTF-8"); 145 146 painter.paint(g2d); 147 g2d.stream(osw); 148 osw.flush(); 149 }catch(Exception e){ 150 e.printStackTrace(); 151 throw new IllegalArgumentException 152 (Messages.formatMessage(ERROR_COULD_NOT_TRANSCODE_TO_SVG, 153 new Object []{e.getClass().getName()})); 154 } 155 156 JSVGCanvas svgCanvas = new JSVGCanvas(); 160 LoaderListener l = new LoaderListener(); 161 svgCanvas.addSVGDocumentLoaderListener(l); 162 163 try{ 164 svgCanvas.setURI(tmpFile.toURL().toString()); 165 synchronized(l.sem){ 166 l.sem.wait(); 167 } 168 }catch(Exception e){ 169 e.printStackTrace(); 170 new Error 171 (Messages.formatMessage(ERROR_COULD_NOT_CONVERT_FILE_PATH_TO_URL, 172 new Object []{e.getMessage()})); 173 } 174 175 if(l.success){ 176 setLayout(new GridLayout (1,2)); 177 add(ref); 178 add(svgCanvas); 179 } 180 181 else{ 182 throw new Error 183 (Messages.formatMessage(ERROR_COULD_NOT_RENDER_GENERATED_SVG,null)); 184 } 185 } 186 187 public Dimension getPreferredSize(){ 188 return new Dimension (CANVAS_SIZE.width*2, CANVAS_SIZE.height); 189 } 190 191 198 public static void main(String args[]){ 199 if(args.length <= 0){ 200 System.out.println(Messages.formatMessage 201 (MESSAGES_USAGE, null)); 202 System.exit(0); 203 } 204 205 String className = args[0]; 207 System.out.println 208 (Messages.formatMessage(MESSAGES_LOADING_CLASS, 209 new Object []{className})); 210 211 Class cl = null; 212 213 try{ 214 cl = Class.forName(className); 215 System.out.println 216 (Messages.formatMessage(MESSAGES_LOADED_CLASS, 217 new Object []{className})); 218 }catch(Exception e){ 219 System.out.println 220 (Messages.formatMessage(ERROR_COULD_NOT_LOAD_CLASS, 221 new Object [] {className, 222 e.getClass().getName() })); 223 System.exit(0); 224 } 225 226 Object o = null; 228 229 try{ 230 o = cl.newInstance(); 231 System.out.println 232 (Messages.formatMessage(MESSAGES_INSTANCIATED_OBJECT, 233 null)); 234 }catch(Exception e){ 235 System.out.println 236 (Messages.formatMessage(ERROR_COULD_NOT_INSTANCIATE_OBJECT, 237 new Object [] {className, 238 e.getClass().getName()})); 239 System.exit(0); 240 } 241 242 Painter p = null; 244 245 try{ 246 p = (Painter)o; 247 }catch(ClassCastException e){ 248 System.out.println 249 (Messages.formatMessage(ERROR_CLASS_NOT_PAINTER, 250 new Object []{className})); 251 System.exit(0); 252 } 253 254 JFrame f = new JFrame (); 256 JPainterCompare c = new JPainterCompare(p); 257 c.setBackground(Color.white); 258 c.setPreferredSize(new Dimension (300, 400)); 259 f.getContentPane().add(c); 260 f.getContentPane().setBackground(Color.white); 261 f.pack(); 262 f.setVisible(true); 263 } 264 265 } 266 | Popular Tags |