1 18 package org.apache.batik.apps.svgbrowser; 19 20 import java.awt.BorderLayout ; 21 import java.awt.Color ; 22 import java.awt.Cursor ; 23 import java.awt.Dimension ; 24 import java.awt.Event ; 25 import java.awt.EventQueue ; 26 import java.awt.Font ; 27 import java.awt.Graphics2D ; 28 import java.awt.Rectangle ; 29 import java.awt.Toolkit ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ComponentAdapter ; 32 import java.awt.event.ComponentEvent ; 33 import java.awt.event.KeyEvent ; 34 import java.awt.event.MouseAdapter ; 35 import java.awt.event.MouseEvent ; 36 import java.awt.event.MouseMotionAdapter ; 37 import java.awt.event.WindowAdapter ; 38 import java.awt.event.WindowEvent ; 39 import java.awt.geom.AffineTransform ; 40 import java.awt.geom.NoninvertibleTransformException ; 41 import java.awt.geom.Point2D ; 42 import java.awt.image.BufferedImage ; 43 import java.awt.print.PrinterException ; 44 import java.io.BufferedOutputStream ; 45 import java.io.File ; 46 import java.io.FileOutputStream ; 47 import java.io.IOException ; 48 import java.io.InputStream ; 49 import java.io.OutputStream ; 50 import java.io.OutputStreamWriter ; 51 import java.io.Reader ; 52 import java.io.Writer ; 53 import java.lang.reflect.Constructor ; 54 import java.lang.reflect.Method ; 55 import java.net.MalformedURLException ; 56 import java.util.HashMap ; 57 import java.util.Iterator ; 58 import java.util.LinkedList ; 59 import java.util.List ; 60 import java.util.Locale ; 61 import java.util.Map ; 62 import java.util.MissingResourceException ; 63 import java.util.ResourceBundle ; 64 import java.util.Vector ; 65 66 import javax.swing.AbstractAction ; 67 import javax.swing.Action ; 68 import javax.swing.BorderFactory ; 69 import javax.swing.ButtonGroup ; 70 import javax.swing.JCheckBox ; 71 import javax.swing.JComponent ; 72 import javax.swing.JDialog ; 73 import javax.swing.JFileChooser ; 74 import javax.swing.JFrame ; 75 import javax.swing.JMenu ; 76 import javax.swing.JMenuBar ; 77 import javax.swing.JOptionPane ; 78 import javax.swing.JPanel ; 79 import javax.swing.JRadioButtonMenuItem ; 80 import javax.swing.JScrollPane ; 81 import javax.swing.JTextArea ; 82 import javax.swing.JToolBar ; 83 import javax.swing.JWindow ; 84 import javax.swing.KeyStroke ; 85 import javax.swing.filechooser.FileFilter ; 86 import javax.swing.text.Document ; 87 import javax.swing.text.PlainDocument ; 88 89 import org.apache.batik.bridge.DefaultExternalResourceSecurity; 90 import org.apache.batik.bridge.DefaultScriptSecurity; 91 import org.apache.batik.bridge.EmbededExternalResourceSecurity; 92 import org.apache.batik.bridge.EmbededScriptSecurity; 93 import org.apache.batik.bridge.ExternalResourceSecurity; 94 import org.apache.batik.bridge.NoLoadExternalResourceSecurity; 95 import org.apache.batik.bridge.NoLoadScriptSecurity; 96 import org.apache.batik.bridge.RelaxedExternalResourceSecurity; 97 import org.apache.batik.bridge.RelaxedScriptSecurity; 98 import org.apache.batik.bridge.ScriptSecurity; 99 import org.apache.batik.bridge.UpdateManager; 100 import org.apache.batik.bridge.UpdateManagerEvent; 101 import org.apache.batik.bridge.UpdateManagerListener; 102 import org.apache.batik.dom.StyleSheetProcessingInstruction; 103 import org.apache.batik.dom.svg.SVGOMDocument; 104 import org.apache.batik.dom.util.HashTable; 105 import org.apache.batik.dom.util.DOMUtilities; 106 import org.apache.batik.ext.swing.JAffineTransformChooser; 107 import org.apache.batik.swing.JSVGCanvas; 108 import org.apache.batik.swing.gvt.GVTTreeRendererEvent; 109 import org.apache.batik.swing.gvt.GVTTreeRendererListener; 110 import org.apache.batik.swing.svg.GVTTreeBuilderEvent; 111 import org.apache.batik.swing.svg.GVTTreeBuilderListener; 112 import org.apache.batik.swing.svg.LinkActivationEvent; 113 import org.apache.batik.swing.svg.LinkActivationListener; 114 import org.apache.batik.swing.svg.SVGDocumentLoaderEvent; 115 import org.apache.batik.swing.svg.SVGDocumentLoaderListener; 116 import org.apache.batik.swing.svg.SVGFileFilter; 117 import org.apache.batik.swing.svg.SVGLoadEventDispatcherEvent; 118 import org.apache.batik.swing.svg.SVGLoadEventDispatcherListener; 119 import org.apache.batik.swing.svg.SVGUserAgent; 120 import org.apache.batik.transcoder.TranscoderInput; 121 import org.apache.batik.transcoder.TranscoderOutput; 122 import org.apache.batik.transcoder.image.ImageTranscoder; 123 import org.apache.batik.transcoder.image.JPEGTranscoder; 124 import org.apache.batik.transcoder.image.PNGTranscoder; 125 import org.apache.batik.transcoder.image.TIFFTranscoder; 126 import org.apache.batik.transcoder.print.PrintTranscoder; 127 import org.apache.batik.transcoder.svg2svg.SVGTranscoder; 128 import org.apache.batik.util.ParsedURL; 129 import org.apache.batik.util.Service; 130 import org.apache.batik.util.SVGConstants; 131 import org.apache.batik.util.XMLConstants; 132 import org.apache.batik.util.gui.DOMViewer; 133 import org.apache.batik.util.gui.JErrorPane; 134 import org.apache.batik.util.gui.LocationBar; 135 import org.apache.batik.util.gui.MemoryMonitor; 136 import org.apache.batik.util.gui.URIChooser; 137 import org.apache.batik.util.gui.resource.ActionMap; 138 import org.apache.batik.util.gui.resource.JComponentModifier; 139 import org.apache.batik.util.gui.resource.MenuFactory; 140 import org.apache.batik.util.gui.resource.MissingListenerException; 141 import org.apache.batik.util.gui.resource.ResourceManager; 142 import org.apache.batik.util.gui.resource.ToolBarFactory; 143 import org.apache.batik.xml.XMLUtilities; 144 import org.mozilla.javascript.Context; 145 import org.mozilla.javascript.ContextListener; 146 import org.w3c.dom.Element ; 147 import org.w3c.dom.Node ; 148 import org.w3c.dom.css.ViewCSS; 149 import org.w3c.dom.svg.SVGDocument; 150 151 157 public class JSVGViewerFrame 158 extends JFrame 159 implements ActionMap, 160 SVGDocumentLoaderListener, 161 GVTTreeBuilderListener, 162 SVGLoadEventDispatcherListener, 163 GVTTreeRendererListener, 164 LinkActivationListener, 165 UpdateManagerListener { 166 167 static private String EOL; 168 static { 169 String temp; 170 try { temp = System.getProperty ("line.separator", "\n"); } 171 catch (SecurityException e) { temp = "\n"; } 172 EOL = temp; 173 } 174 175 179 protected static boolean priorJDK1_4 = true; 180 181 185 protected static final String JDK_1_4_PRESENCE_TEST_CLASS 186 = "java.util.logging.LoggingPermission"; 187 188 static { 189 Class cl = null; 190 try { 191 cl = Class.forName(JDK_1_4_PRESENCE_TEST_CLASS); 192 } catch (ClassNotFoundException e){ 193 } 194 195 if (cl != null) { 196 priorJDK1_4 = false; 197 } 198 } 199 200 static JFrame debuggerFrame = null; 201 static Class debuggerClass = null; 202 static Method clearAllBreakpoints = null; 203 static Method scriptGo = null; 204 static Method setExitAction = null; 205 static { 206 try { 207 debuggerClass = JSVGViewerFrame.class.getClassLoader().loadClass 208 ("org.mozilla.javascript.tools.debugger.Main"); 209 clearAllBreakpoints = debuggerClass.getMethod 210 ("clearAllBreakpoints", null); 211 scriptGo = debuggerClass.getMethod 212 ("go", null); 213 setExitAction = debuggerClass.getMethod 214 ("setExitAction", new Class [] {Runnable .class}); 215 } catch (ThreadDeath td) { 216 debuggerClass = null; 217 clearAllBreakpoints = null; 218 scriptGo = null; 219 setExitAction = null; 220 throw td; 221 } catch (Throwable t) { 222 debuggerClass = null; 223 clearAllBreakpoints = null; 224 scriptGo = null; 225 setExitAction = null; 226 } 227 } 228 229 public static void showDebugger() { 230 if (debuggerClass == null) return; 231 if (debuggerFrame == null) { 232 try { 233 Constructor c = debuggerClass.getConstructor 234 (new Class [] { String .class }); 235 debuggerFrame = (JFrame )c.newInstance 236 (new Object [] { "Rhino JavaScript Debugger" }); 237 JMenuBar menuBar = debuggerFrame.getJMenuBar(); 240 JMenu menu = menuBar.getMenu(0); 241 menu.getItem(0).setEnabled(false); menu.getItem(1).setEnabled(false); menu.getItem(3).setText 244 (Resources.getString("Close.text")); menu.getItem(3).setAccelerator 246 (KeyStroke.getKeyStroke(KeyEvent.VK_W, Event.CTRL_MASK)); 247 248 debuggerFrame.setSize(600, 460); 249 debuggerFrame.pack(); 250 WindowAdapter wa = new WindowAdapter () { 251 public void windowClosing(WindowEvent e) { 252 hideDebugger(); 253 }}; 254 setExitAction.invoke(debuggerFrame, 255 new Object [] { new Runnable () { 256 public void run() { 257 hideDebugger(); 258 }}}); 259 debuggerFrame.addWindowListener(wa); 260 } catch (Exception ex) { 261 ex.printStackTrace(); 262 return; 263 } 264 } 265 if (debuggerFrame != null) { 266 debuggerFrame.setVisible(true); 267 Context.addContextListener((ContextListener)debuggerFrame); 268 } 269 } 270 271 public static void hideDebugger() { 272 if (debuggerFrame == null) 273 return; 274 Context.removeContextListener((ContextListener)debuggerFrame); 275 debuggerFrame.setVisible(false); 276 try { 277 clearAllBreakpoints.invoke(debuggerFrame, null); 278 scriptGo.invoke(debuggerFrame, null); 279 } catch (Exception ex) { 280 ex.printStackTrace(); 281 } 282 } 283 284 287 public final static String RESOURCES = 288 "org.apache.batik.apps.svgbrowser.resources.GUI"; 289 290 public final static String ABOUT_ACTION = "AboutAction"; 292 public final static String OPEN_ACTION = "OpenAction"; 293 public final static String OPEN_LOCATION_ACTION = "OpenLocationAction"; 294 public final static String NEW_WINDOW_ACTION = "NewWindowAction"; 295 public final static String RELOAD_ACTION = "ReloadAction"; 296 public final static String SAVE_AS_ACTION = "SaveAsAction"; 297 public final static String BACK_ACTION = "BackAction"; 298 public final static String FORWARD_ACTION = "ForwardAction"; 299 public final static String FULL_SCREEN_ACTION = "FullScreenAction"; 300 public final static String PRINT_ACTION = "PrintAction"; 301 public final static String EXPORT_AS_JPG_ACTION = "ExportAsJPGAction"; 302 public final static String EXPORT_AS_PNG_ACTION = "ExportAsPNGAction"; 303 public final static String EXPORT_AS_TIFF_ACTION = "ExportAsTIFFAction"; 304 public final static String PREFERENCES_ACTION = "PreferencesAction"; 305 public final static String CLOSE_ACTION = "CloseAction"; 306 public final static String VIEW_SOURCE_ACTION = "ViewSourceAction"; 307 public final static String EXIT_ACTION = "ExitAction"; 308 public final static String RESET_TRANSFORM_ACTION = "ResetTransformAction"; 309 public final static String ZOOM_IN_ACTION = "ZoomInAction"; 310 public final static String ZOOM_OUT_ACTION = "ZoomOutAction"; 311 public final static String PREVIOUS_TRANSFORM_ACTION = "PreviousTransformAction"; 312 public final static String NEXT_TRANSFORM_ACTION = "NextTransformAction"; 313 public final static String USE_STYLESHEET_ACTION = "UseStylesheetAction"; 314 public final static String PLAY_ACTION = "PlayAction"; 315 public final static String PAUSE_ACTION = "PauseAction"; 316 public final static String STOP_ACTION = "StopAction"; 317 public final static String MONITOR_ACTION = "MonitorAction"; 318 public final static String DOM_VIEWER_ACTION = "DOMViewerAction"; 319 public final static String SET_TRANSFORM_ACTION = "SetTransformAction"; 320 public final static String FIND_DIALOG_ACTION = "FindDialogAction"; 321 public final static String THUMBNAIL_DIALOG_ACTION = "ThumbnailDialogAction"; 322 public final static String FLUSH_ACTION = "FlushAction"; 323 public final static String TOGGLE_DEBUGGER_ACTION = "ToggleDebuggerAction"; 324 325 328 public final static Cursor WAIT_CURSOR = 329 new Cursor (Cursor.WAIT_CURSOR); 330 331 334 public final static Cursor DEFAULT_CURSOR = 335 new Cursor (Cursor.DEFAULT_CURSOR); 336 337 340 public final static String PROPERTY_OS_NAME 341 = Resources.getString("JSVGViewerFrame.property.os.name"); 342 343 346 public final static String PROPERTY_OS_NAME_DEFAULT 347 = Resources.getString("JSVGViewerFrame.property.os.name.default"); 348 349 353 public final static String PROPERTY_OS_WINDOWS_PREFIX 354 = Resources.getString("JSVGViewerFrame.property.os.windows.prefix"); 355 356 359 protected static Vector handlers; 360 361 364 protected static SquiggleInputHandler defaultHandler = new SVGInputHandler(); 365 366 369 protected static ResourceBundle bundle; 370 371 374 protected static ResourceManager resources; 375 static { 376 bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault()); 377 resources = new ResourceManager(bundle); 378 } 379 380 383 protected Application application; 384 385 388 protected JSVGCanvas svgCanvas; 389 390 393 protected JPanel svgCanvasPanel; 394 395 398 protected JWindow window; 399 400 403 protected static JFrame memoryMonitorFrame; 404 405 408 protected File currentPath = new File (""); 409 410 413 protected File currentSavePath = new File (""); 414 415 418 protected BackAction backAction = new BackAction(); 419 420 423 protected ForwardAction forwardAction = new ForwardAction(); 424 425 428 protected PlayAction playAction = new PlayAction(); 429 430 433 protected PauseAction pauseAction = new PauseAction(); 434 435 438 protected StopAction stopAction = new StopAction(); 439 440 443 protected PreviousTransformAction previousTransformAction = 444 new PreviousTransformAction(); 445 446 449 protected NextTransformAction nextTransformAction = 450 new NextTransformAction(); 451 452 455 protected UseStylesheetAction useStylesheetAction = 456 new UseStylesheetAction(); 457 458 461 protected boolean debug; 462 463 466 protected boolean autoAdjust = true; 467 468 471 protected boolean managerStopped; 472 473 476 protected SVGUserAgent userAgent = new UserAgent(); 477 478 481 protected SVGDocument svgDocument; 482 483 486 protected URIChooser uriChooser; 487 488 491 protected DOMViewer domViewer; 492 493 496 protected FindDialog findDialog; 497 498 501 protected ThumbnailDialog thumbnailDialog; 502 503 506 protected JAffineTransformChooser.Dialog transformDialog; 507 508 511 protected LocationBar locationBar; 512 513 516 protected StatusBar statusBar; 517 518 521 protected String title; 522 523 526 protected LocalHistory localHistory; 527 528 531 protected TransformHistory transformHistory = new TransformHistory(); 532 533 536 protected String alternateStyleSheet; 537 538 541 public JSVGViewerFrame(Application app) { 542 application = app; 543 544 addWindowListener(new WindowAdapter () { 545 public void windowClosing(WindowEvent e) { 546 application.closeJSVGViewerFrame(JSVGViewerFrame.this); 547 } 548 }); 549 550 svgCanvas = new JSVGCanvas(userAgent, true, true){ 556 Dimension screenSize; 557 558 { 559 screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 560 setMaximumSize(screenSize); 561 } 562 563 public Dimension getPreferredSize(){ 564 Dimension s = super.getPreferredSize(); 565 if (s.width > screenSize.width) s.width =screenSize.width; 566 if (s.height > screenSize.height) s.height = screenSize.height; 567 return s; 568 } 569 570 571 576 public void setMySize(Dimension d) { 577 setPreferredSize(d); 578 invalidate(); 579 if (JSVGViewerFrame.this.autoAdjust) { 580 JSVGViewerFrame.this.pack(); 581 } 582 } 583 584 public void setDisableInteractions(boolean b) { 585 super.setDisableInteractions(b); 586 587 590 ((Action )listeners.get(SET_TRANSFORM_ACTION)) .setEnabled(!b); 591 592 if (thumbnailDialog != null) 593 thumbnailDialog.setInteractionEnabled(!b); 594 } 595 }; 596 597 javax.swing.ActionMap map = svgCanvas.getActionMap(); 598 map.put(FULL_SCREEN_ACTION, new FullScreenAction()); 599 javax.swing.InputMap imap = svgCanvas.getInputMap(JComponent.WHEN_FOCUSED); 600 KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0); 601 imap.put(key, FULL_SCREEN_ACTION); 602 603 svgCanvas.setDoubleBufferedRendering(true); 604 605 listeners.put(ABOUT_ACTION, new AboutAction()); 606 listeners.put(OPEN_ACTION, new OpenAction()); 607 listeners.put(OPEN_LOCATION_ACTION, new OpenLocationAction()); 608 listeners.put(NEW_WINDOW_ACTION, new NewWindowAction()); 609 listeners.put(RELOAD_ACTION, new ReloadAction()); 610 listeners.put(SAVE_AS_ACTION, new SaveAsAction()); 611 listeners.put(BACK_ACTION, backAction); 612 listeners.put(FORWARD_ACTION, forwardAction); 613 listeners.put(PRINT_ACTION, new PrintAction()); 614 listeners.put(EXPORT_AS_JPG_ACTION, new ExportAsJPGAction()); 615 listeners.put(EXPORT_AS_PNG_ACTION, new ExportAsPNGAction()); 616 listeners.put(EXPORT_AS_TIFF_ACTION, new ExportAsTIFFAction()); 617 listeners.put(PREFERENCES_ACTION, new PreferencesAction()); 618 listeners.put(CLOSE_ACTION, new CloseAction()); 619 listeners.put(EXIT_ACTION, application.createExitAction(this)); 620 listeners.put(VIEW_SOURCE_ACTION, new ViewSourceAction()); 621 622 javax.swing.ActionMap cMap = svgCanvas.getActionMap(); 623 listeners.put(RESET_TRANSFORM_ACTION, 624 cMap.get(JSVGCanvas.RESET_TRANSFORM_ACTION)); 625 listeners.put(ZOOM_IN_ACTION, 626 cMap.get(JSVGCanvas.ZOOM_IN_ACTION)); 627 listeners.put(ZOOM_OUT_ACTION, 628 cMap.get(JSVGCanvas.ZOOM_OUT_ACTION)); 629 630 listeners.put(PREVIOUS_TRANSFORM_ACTION, previousTransformAction); 631 key = KeyStroke.getKeyStroke(KeyEvent.VK_K, KeyEvent.CTRL_MASK); 632 imap.put(key, previousTransformAction); 633 634 listeners.put(NEXT_TRANSFORM_ACTION, nextTransformAction); 635 key = KeyStroke.getKeyStroke(KeyEvent.VK_L, KeyEvent.CTRL_MASK); 636 imap.put(key, nextTransformAction); 637 638 listeners.put(USE_STYLESHEET_ACTION, useStylesheetAction); 639 listeners.put(PLAY_ACTION, playAction); 640 listeners.put(PAUSE_ACTION, pauseAction); 641 listeners.put(STOP_ACTION, stopAction); 642 listeners.put(MONITOR_ACTION, new MonitorAction()); 643 listeners.put(DOM_VIEWER_ACTION, new DOMViewerAction()); 644 listeners.put(SET_TRANSFORM_ACTION, new SetTransformAction()); 645 listeners.put(FIND_DIALOG_ACTION, new FindDialogAction()); 646 listeners.put(THUMBNAIL_DIALOG_ACTION, new ThumbnailDialogAction()); 647 listeners.put(FLUSH_ACTION, new FlushAction()); 648 listeners.put(TOGGLE_DEBUGGER_ACTION, new ToggleDebuggerAction()); 649 650 JPanel p = null; 651 try { 652 MenuFactory mf = new MenuFactory(bundle, this); 654 JMenuBar mb = mf.createJMenuBar("MenuBar"); 655 setJMenuBar(mb); 656 657 localHistory = new LocalHistory(mb, this); 658 659 String uri[] = application.getVisitedURIs(); 660 for (int i=0; i<uri.length; i++) { 661 if (uri[i] != null && !"".equals(uri[i])) { 662 localHistory.update(uri[i]); 663 } 664 } 665 p = new JPanel (new BorderLayout ()); 666 667 ToolBarFactory tbf = new ToolBarFactory(bundle, this); 669 JToolBar tb = tbf.createJToolBar("ToolBar"); 670 tb.setFloatable(false); 671 getContentPane().add(p, BorderLayout.NORTH); 672 p.add(tb, BorderLayout.NORTH); 673 p.add(new javax.swing.JSeparator (), BorderLayout.CENTER); 674 p.add(locationBar = new LocationBar(), BorderLayout.SOUTH); 675 676 } catch (MissingResourceException e) { 677 System.out.println(e.getMessage()); 678 System.exit(0); 679 } 680 681 svgCanvasPanel = new JPanel (new BorderLayout ()); 682 svgCanvasPanel.setBorder(BorderFactory.createEtchedBorder()); 683 684 svgCanvasPanel.add(svgCanvas, BorderLayout.CENTER); 685 p = new JPanel (new BorderLayout ()); 686 p.add(svgCanvasPanel, BorderLayout.CENTER); 687 p.add(statusBar = new StatusBar(), BorderLayout.SOUTH); 688 689 getContentPane().add(p, BorderLayout.CENTER); 690 691 svgCanvas.addSVGDocumentLoaderListener(this); 692 svgCanvas.addGVTTreeBuilderListener(this); 693 svgCanvas.addSVGLoadEventDispatcherListener(this); 694 svgCanvas.addGVTTreeRendererListener(this); 695 svgCanvas.addLinkActivationListener(this); 696 svgCanvas.addUpdateManagerListener(this); 697 698 svgCanvas.addMouseMotionListener(new MouseMotionAdapter () { 699 public void mouseMoved(MouseEvent e) { 700 if (svgDocument == null) { 701 statusBar.setXPosition(e.getX()); 702 statusBar.setYPosition(e.getY()); 703 } else { 704 try { 705 AffineTransform at; 706 at = svgCanvas.getViewBoxTransform(); 707 if (at != null) { 708 at = at.createInverse(); 709 Point2D p2d = 710 at.transform(new Point2D.Float (e.getX(), e.getY()), 711 null); 712 statusBar.setXPosition((float)p2d.getX()); 713 statusBar.setYPosition((float)p2d.getY()); 714 return; 715 } 716 } catch (NoninvertibleTransformException ex) { 717 } 718 statusBar.setXPosition(e.getX()); 719 statusBar.setYPosition(e.getY()); 720 } 721 } 722 }); 723 svgCanvas.addMouseListener(new MouseAdapter () { 724 public void mouseExited(MouseEvent e) { 725 Dimension dim = svgCanvas.getSize(); 726 if (svgDocument == null) { 727 statusBar.setWidth(dim.width); 728 statusBar.setHeight(dim.height); 729 } else { 730 try { 731 AffineTransform at; 732 at = svgCanvas.getViewBoxTransform(); 733 if (at != null) { 734 at = at.createInverse(); 735 Point2D o = 736 at.transform(new Point2D.Float (0, 0), 737 null); 738 Point2D p2d = 739 at.transform(new Point2D.Float (dim.width, 740 dim.height), 741 null); 742 statusBar.setWidth((float)(p2d.getX() - o.getX())); 743 statusBar.setHeight((float)(p2d.getY() - o.getY())); 744 return; 745 } 746 } catch (NoninvertibleTransformException ex) { 747 } 748 statusBar.setWidth(dim.width); 749 statusBar.setHeight(dim.height); 750 } 751 } 752 }); 753 svgCanvas.addComponentListener(new ComponentAdapter () { 754 public void componentResized(ComponentEvent e) { 755 Dimension dim = svgCanvas.getSize(); 756 if (svgDocument == null) { 757 statusBar.setWidth(dim.width); 758 statusBar.setHeight(dim.height); 759 } else { 760 try { 761 AffineTransform at; 762 at = svgCanvas.getViewBoxTransform(); 763 if (at != null) { 764 at = at.createInverse(); 765 Point2D o = 766 at.transform(new Point2D.Float (0, 0), 767 null); 768 Point2D p2d = 769 at.transform(new Point2D.Float (dim.width, 770 dim.height), 771 null); 772 statusBar.setWidth((float)(p2d.getX() - o.getX())); 773 statusBar.setHeight((float)(p2d.getY() - o.getY())); 774 return; 775 } 776 } catch (NoninvertibleTransformException ex) { 777 } 778 statusBar.setWidth(dim.width); 779 statusBar.setHeight(dim.height); 780 } 781 } 782 }); 783 784 locationBar.addActionListener(new AbstractAction () { 785 public void actionPerformed(ActionEvent e) { 786 String st = locationBar.getText().trim(); 787 int i = st.indexOf("#"); 788 String t = ""; 789 if (i != -1) { 790 t = st.substring(i + 1); 791 st = st.substring(0, i); 792 } 793 if (!st.equals("")) { 794 try{ 795 File f = new File (st); 796 if (f.exists()) { 797 if (f.isDirectory()) { 798 st = null; 799 } else { 800 try { 801 st = f.getCanonicalPath(); 802 if (st.startsWith("/")) { 803 st = "file:" + st; 804 } else { 805 st = "file:/" + st; 806 } 807 } catch (IOException ex) { 808 } 809 } 810 } 811 }catch(SecurityException se){ 812 } 818 819 if (st != null) { 820 if (svgDocument != null) { 821 ParsedURL docPURL 822 = new ParsedURL(svgDocument.getURL()); 823 ParsedURL purl = new ParsedURL(docPURL, st); 824 String fi = svgCanvas.getFragmentIdentifier(); 825 fi = (fi == null) ? "" : fi; 826 if (docPURL.equals(purl) && t.equals(fi)) { 827 return; 828 } 829 } 830 if (t.length() != 0) { 831 st += "#" + t; 832 } 833 locationBar.setText(st); 834 locationBar.addToHistory(st); 835 showSVGDocument(st); 836 } 837 } 838 } 839 }); 840 841 } 842 843 846 public void dispose() { 847 svgCanvas.dispose(); 848 super.dispose(); 849 } 850 851 854 public void setDebug(boolean b) { 855 debug = b; 856 } 857 858 861 public void setAutoAdjust(boolean b) { 862 autoAdjust = b; 863 } 864 865 868 public JSVGCanvas getJSVGCanvas() { 869 return svgCanvas; 870 } 871 872 875 private static File makeAbsolute(File f){ 876 if(!f.isAbsolute()){ 877 return f.getAbsoluteFile(); 878 } 879 return f; 880 } 881 882 885 public class AboutAction extends AbstractAction { 886 public AboutAction(){ 887 } 888 889 public void actionPerformed(ActionEvent e){ 890 AboutDialog dlg = new AboutDialog(JSVGViewerFrame.this); 891 dlg.setSize(dlg.getPreferredSize()); 893 dlg.setLocationRelativeTo(JSVGViewerFrame.this); 894 dlg.setVisible(true); 895 dlg.toFront(); 896 } 897 } 898 899 902 public class OpenAction extends AbstractAction { 903 904 public OpenAction() { 905 } 906 public void actionPerformed(ActionEvent e) { 907 JFileChooser fileChooser = null; 908 909 String os = System.getProperty(PROPERTY_OS_NAME, PROPERTY_OS_NAME_DEFAULT); 912 SecurityManager sm = System.getSecurityManager(); 913 914 if ( priorJDK1_4 && sm != null && os.indexOf(PROPERTY_OS_WINDOWS_PREFIX) != -1 ){ 915 fileChooser = new JFileChooser (makeAbsolute(currentPath), 916 new WindowsAltFileSystemView()); 917 } else { 918 fileChooser = new JFileChooser (makeAbsolute(currentPath)); 919 } 920 921 fileChooser.setFileHidingEnabled(false); 922 fileChooser.setFileSelectionMode 923 (JFileChooser.FILES_ONLY); 924 925 Iterator iter = getHandlers().iterator(); 929 while (iter.hasNext()) { 930 SquiggleInputHandler handler 931 = (SquiggleInputHandler)iter.next(); 932 fileChooser.addChoosableFileFilter 933 (new SquiggleInputHandlerFilter(handler)); 934 } 935 936 int choice = fileChooser.showOpenDialog(JSVGViewerFrame.this); 937 if (choice == JFileChooser.APPROVE_OPTION) { 938 File f = fileChooser.getSelectedFile(); 939 940 currentPath = f; 941 try { 942 String furl = f.toURL().toString(); 943 showSVGDocument(furl); 944 } catch (MalformedURLException ex) { 945 if (userAgent != null) { 946 userAgent.displayError(ex); 947 } 948 } 949 } 950 } 951 } 952 953 956 public void showSVGDocument(String uri){ 957 try { 958 ParsedURL purl = new ParsedURL(uri); 959 SquiggleInputHandler 960 handler = getInputHandler(purl); 961 962 handler.handle(purl, 963 JSVGViewerFrame.this); 964 } catch (Exception e) { 965 if (userAgent != null) { 966 userAgent.displayError(e); 967 } 968 } 969 970 } 971 972 975 public SquiggleInputHandler getInputHandler(ParsedURL purl) throws IOException { 976 Iterator iter = getHandlers().iterator(); 977 SquiggleInputHandler handler = null; 978 979 while (iter.hasNext()) { 980 SquiggleInputHandler curHandler = 981 (SquiggleInputHandler)iter.next(); 982 if (curHandler.accept(purl)) { 983 handler = curHandler; 984 break; 985 } 986 } 987 988 if (handler == null) { 990 handler = defaultHandler; 991 } 992 993 return handler; 994 } 995 996 997 1000 protected static Vector getHandlers() { 1001 if (handlers != null) { 1002 return handlers; 1003 } 1004 1005 handlers = new Vector (); 1006 registerHandler(new SVGInputHandler()); 1007 1008 Iterator iter = Service.providers(SquiggleInputHandler.class); 1009 while (iter.hasNext()) { 1010 SquiggleInputHandler handler 1011 = (SquiggleInputHandler)iter.next(); 1012 1013 registerHandler(handler); 1014 } 1015 1016 return handlers; 1017 } 1018 1019 1023 public static synchronized 1024 void registerHandler(SquiggleInputHandler handler) { 1025 Vector handlers = getHandlers(); 1026 handlers.addElement(handler); 1027 } 1028 1029 1032 public class OpenLocationAction extends AbstractAction { 1033 public OpenLocationAction() {} 1034 public void actionPerformed(ActionEvent e) { 1035 if (uriChooser == null) { 1036 uriChooser = new URIChooser(JSVGViewerFrame.this); 1037 uriChooser.setFileFilter(new SVGFileFilter()); 1038 uriChooser.pack(); 1039 Rectangle fr = getBounds(); 1040 Dimension sd = uriChooser.getSize(); 1041 uriChooser.setLocation(fr.x + (fr.width - sd.width) / 2, 1042 fr.y + (fr.height - sd.height) / 2); 1043 } 1044 if (uriChooser.showDialog() == URIChooser.OK_OPTION) { 1045 String s = uriChooser.getText(); 1046 int i = s.indexOf("#"); 1047 String t = ""; 1048 if (i != -1) { 1049 t = s.substring(i + 1); 1050 s = s.substring(0, i); 1051 } 1052 if (!s.equals("")) { 1053 File f = new File (s); 1054 if (f.exists()) { 1055 if (f.isDirectory()) { 1056 s = null; 1057 } else { 1058 try { 1059 s = f.getCanonicalPath(); 1060 if (s.startsWith("/")) { 1061 s = "file:" + s; 1062 } else { 1063 s = "file:/" + s; 1064 } 1065 } catch (IOException ex) { 1066 } 1067 } 1068 } 1069 if (s != null) { 1070 if (svgDocument != null) { 1071 ParsedURL docPURL 1072 = new ParsedURL(svgDocument.getURL()); 1073 ParsedURL purl = new ParsedURL(docPURL, s); 1074 String fi = svgCanvas.getFragmentIdentifier(); 1075 if (docPURL.equals(purl) && t.equals(fi)) { 1076 return; 1077 } 1078 } 1079 if (t.length() != 0) { 1080 s += "#" + t; 1081 } 1082 1083 showSVGDocument(s); 1084 } 1085 } 1086 } 1087 } 1088 } 1089 1090 1093 public class NewWindowAction extends AbstractAction { 1094 public NewWindowAction() {} 1095 public void actionPerformed(ActionEvent e) { 1096 JSVGViewerFrame vf = application.createAndShowJSVGViewerFrame(); 1097 1098 vf.autoAdjust = autoAdjust; 1100 vf.debug = debug; 1101 vf.svgCanvas.setProgressivePaint(svgCanvas.getProgressivePaint()); 1102 vf.svgCanvas.setDoubleBufferedRendering 1103 (svgCanvas.getDoubleBufferedRendering()); 1104 } 1105 } 1106 1107 1110 public class PreferencesAction extends AbstractAction { 1111 public PreferencesAction() {} 1112 public void actionPerformed(ActionEvent e) { 1113 application.showPreferenceDialog(JSVGViewerFrame.this); 1114 } 1115 } 1116 1117 1120 public class CloseAction extends AbstractAction { 1121 public CloseAction() {} 1122 public void actionPerformed(ActionEvent e) { 1123 application.closeJSVGViewerFrame(JSVGViewerFrame.this); 1124 } 1125 } 1126 1127 1130 public class ReloadAction extends AbstractAction { 1131 public ReloadAction() {} 1132 public void actionPerformed(ActionEvent e) { 1133 if ((e.getModifiers() & ActionEvent.SHIFT_MASK) == 1) { 1134 svgCanvas.flushImageCache(); 1135 } 1136 if (svgDocument != null) { 1137 localHistory.reload(); 1138 } 1139 } 1140 } 1141 1142 1145 public class BackAction extends AbstractAction 1146 implements JComponentModifier { 1147 List components = new LinkedList (); 1148 public BackAction() {} 1149 public void actionPerformed(ActionEvent e) { 1150 if (localHistory.canGoBack()) { 1151 localHistory.back(); 1152 } 1153 } 1154 1155 public void addJComponent(JComponent c) { 1156 components.add(c); 1157 c.setEnabled(false); 1158 } 1159 1160 protected void update() { 1161 boolean b = localHistory.canGoBack(); 1162 Iterator it = components.iterator(); 1163 while (it.hasNext()) { 1164 ((JComponent )it.next()).setEnabled(b); 1165 } 1166 } 1167 } 1168 1169 1172 public class ForwardAction extends AbstractAction 1173 implements JComponentModifier { 1174 List components = new LinkedList (); 1175 public ForwardAction() {} 1176 public void actionPerformed(ActionEvent e) { 1177 if (localHistory.canGoForward()) { 1178 localHistory.forward(); 1179 } 1180 } 1181 1182 public void addJComponent(JComponent c) { 1183 components.add(c); 1184 c.setEnabled(false); 1185 } 1186 1187 protected void update() { 1188 boolean b = localHistory.canGoForward(); 1189 Iterator it = components.iterator(); 1190 while (it.hasNext()) { 1191 ((JComponent )it.next()).setEnabled(b); 1192 } 1193 } 1194 } 1195 1196 1199 public class PrintAction extends AbstractAction { 1200 public PrintAction() {} 1201 public void actionPerformed(ActionEvent e) { 1202 if (svgDocument != null) { 1203 final SVGDocument doc = svgDocument; 1204 new Thread () { 1205 public void run(){ 1206 String uri = doc.getURL(); 1207 String fragment = svgCanvas.getFragmentIdentifier(); 1208 if (fragment != null) { 1209 uri += "#"+fragment; 1210 } 1211 1212 PrintTranscoder pt = new PrintTranscoder(); 1217 1218 pt.addTranscodingHint(PrintTranscoder.KEY_XML_PARSER_CLASSNAME, 1222 application.getXMLParserClassName()); 1223 1224 pt.addTranscodingHint(PrintTranscoder.KEY_SHOW_PAGE_DIALOG, 1225 Boolean.TRUE); 1226 1227 1228 pt.addTranscodingHint(PrintTranscoder.KEY_SHOW_PRINTER_DIALOG, 1229 Boolean.TRUE); 1230 1231 pt.transcode(new TranscoderInput(uri), null); 1235 1236 try { 1240 pt.print(); 1241 } catch (PrinterException ex) { 1242 userAgent.displayError(ex); 1243 } 1244 } 1245 }.start(); 1246 } 1247 } 1248 } 1249 1250 1253 public class SaveAsAction extends AbstractAction { 1254 public SaveAsAction() {} 1255 1256 public void actionPerformed(ActionEvent e) { 1257 JFileChooser fileChooser; 1258 fileChooser = new JFileChooser (makeAbsolute(currentSavePath)); 1259 fileChooser.setDialogTitle(resources.getString("SaveAs.title")); 1260 fileChooser.setFileHidingEnabled(false); 1261 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 1262 fileChooser.addChoosableFileFilter(new ImageFileFilter(".svg")); 1263 1264 int choice = fileChooser.showSaveDialog(JSVGViewerFrame.this); 1265 if (choice != JFileChooser.APPROVE_OPTION) 1266 return; 1267 1268 final File f = fileChooser.getSelectedFile(); 1269 1270 SVGOptionPanel sop; 1271 sop = SVGOptionPanel.showDialog(JSVGViewerFrame.this); 1272 1273 final boolean useXMLBase = sop.getUseXMLBase(); 1274 final boolean prettyPrint = sop.getPrettyPrint(); 1275 sop = null; 1276 1277 final SVGDocument svgDoc = svgCanvas.getSVGDocument(); 1278 if (svgDoc == null) return; 1279 1280 statusBar.setMessage(resources.getString("Message.saveAs")); 1281 currentSavePath = f; 1282 OutputStreamWriter w = null; 1283 try { 1284 OutputStream tos = null; 1285 tos = new FileOutputStream (f); 1286 tos = new BufferedOutputStream (tos); 1287 w = new OutputStreamWriter (tos, "utf-8"); 1288 } catch (Exception ex) { 1289 userAgent.displayError(ex); 1290 return; 1291 } 1292 1293 final OutputStreamWriter writer = w; 1294 1295 final Runnable doneRun = new Runnable () { 1296 public void run() { 1297 String doneStr = resources.getString("Message.done"); 1298 statusBar.setMessage(doneStr); 1299 } 1300 }; 1301 Runnable r = new Runnable () { 1302 public void run() { 1303 try { 1304 writer.write 1306 ("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 1307 writer.write (EOL); 1308 1309 Node fc = svgDoc.getFirstChild(); 1310 if (fc.getNodeType() != Node.DOCUMENT_TYPE_NODE) { 1311 writer.write ("<!DOCTYPE svg PUBLIC '"); 1314 writer.write (SVGConstants.SVG_PUBLIC_ID); 1315 writer.write ("' '"); 1316 writer.write (SVGConstants.SVG_SYSTEM_ID); 1317 writer.write ("'>"); 1318 writer.write (EOL); 1319 writer.write (EOL); 1320 } 1321 Element root = svgDoc.getRootElement(); 1322 boolean doXMLBase = useXMLBase; 1323 if (root.hasAttributeNS 1324 (XMLConstants.XML_NAMESPACE_URI, "base")) 1325 doXMLBase = false; 1326 1327 if (doXMLBase) { 1328 root.setAttributeNS 1329 (XMLConstants.XML_NAMESPACE_URI, 1330 "xml:base", 1331 svgDoc.getURL()); 1332 } 1333 1334 if (prettyPrint) { 1335 SVGTranscoder trans = new SVGTranscoder(); 1336 trans.transcode(new TranscoderInput(svgDoc), 1337 new TranscoderOutput(writer)); 1338 } else { 1339 DOMUtilities.writeDocument(svgDoc, writer); 1340 } 1341 1342 writer.close(); 1343 1344 if (doXMLBase) 1345 root.removeAttributeNS 1346 (XMLConstants.XML_NAMESPACE_URI, 1347 "xml:base"); 1348 1349 if (EventQueue.isDispatchThread()) { 1350 doneRun.run(); 1351 } else { 1352 EventQueue.invokeLater(doneRun); 1353 } 1354 } catch (Exception ex) { 1355 userAgent.displayError(ex); 1356 } 1357 } 1358 }; 1359 1360 UpdateManager um = svgCanvas.getUpdateManager(); 1361 if ((um != null) && (um.isRunning())) { 1362 um.getUpdateRunnableQueue().invokeLater(r); 1363 } else { 1364 r.run(); 1365 } 1366 } 1367 } 1368 1369 1372 public class ExportAsJPGAction extends AbstractAction { 1373 public ExportAsJPGAction() {} 1374 public void actionPerformed(ActionEvent e) { 1375 JFileChooser fileChooser = 1376 new JFileChooser (makeAbsolute(currentSavePath)); 1377 fileChooser.setDialogTitle(resources.getString("ExportAsJPG.title")); 1378 fileChooser.setFileHidingEnabled(false); 1379 fileChooser.setFileSelectionMode 1380 (JFileChooser.FILES_ONLY); 1381 fileChooser.addChoosableFileFilter(new ImageFileFilter(".jpg")); 1382 1383 int choice = fileChooser.showSaveDialog(JSVGViewerFrame.this); 1384 if (choice == JFileChooser.APPROVE_OPTION) { 1385 float quality = 1386 JPEGOptionPanel.showDialog(JSVGViewerFrame.this); 1387 1388 final File f = fileChooser.getSelectedFile(); 1389 BufferedImage buffer = svgCanvas.getOffScreen(); 1390 if (buffer != null) { 1391 statusBar.setMessage 1392 (resources.getString("Message.exportAsJPG")); 1393 1394 int w = buffer.getWidth(); 1396 int h = buffer.getHeight(); 1397 final ImageTranscoder trans = new JPEGTranscoder(); 1398 trans.addTranscodingHint 1399 (JPEGTranscoder.KEY_XML_PARSER_CLASSNAME, 1400 application.getXMLParserClassName()); 1401 trans.addTranscodingHint 1402 (JPEGTranscoder.KEY_QUALITY, new Float (quality)); 1403 1404 final BufferedImage img = trans.createImage(w, h); 1405 1406 Graphics2D g2d = img.createGraphics(); 1408 g2d.setColor(Color.white); 1409 g2d.fillRect(0, 0, w, h); 1410 g2d.drawImage(buffer, null, 0, 0); 1411 new Thread () { 1412 public void run() { 1413 try { 1414 currentSavePath = f; 1415 OutputStream ostream = 1416 new BufferedOutputStream (new FileOutputStream (f)); 1417 trans.writeImage(img, new TranscoderOutput(ostream)); 1418 ostream.flush(); 1419 ostream.close(); 1420 } catch (Exception ex) { } 1421 statusBar.setMessage 1422 (resources.getString("Message.done")); 1423 } 1424 }.start(); 1425 } 1426 } 1427 } 1428 } 1429 1430 1433 public class ExportAsPNGAction extends AbstractAction { 1434 public ExportAsPNGAction() {} 1435 public void actionPerformed(ActionEvent e) { 1436 JFileChooser fileChooser = 1437 new JFileChooser (makeAbsolute(currentSavePath)); 1438 fileChooser.setDialogTitle(resources.getString("ExportAsPNG.title")); 1439 fileChooser.setFileHidingEnabled(false); 1440 fileChooser.setFileSelectionMode 1441 (JFileChooser.FILES_ONLY); 1442 fileChooser.addChoosableFileFilter(new ImageFileFilter(".png")); 1443 1444 int choice = fileChooser.showSaveDialog(JSVGViewerFrame.this); 1445 if (choice == JFileChooser.APPROVE_OPTION) { 1446 1447 boolean isIndexed = 1449 PNGOptionPanel.showDialog(JSVGViewerFrame.this); 1450 1452 final File f = fileChooser.getSelectedFile(); 1453 BufferedImage buffer = svgCanvas.getOffScreen(); 1454 if (buffer != null) { 1455 statusBar.setMessage 1456 (resources.getString("Message.exportAsPNG")); 1457 1458 int w = buffer.getWidth(); 1460 int h = buffer.getHeight(); 1461 final ImageTranscoder trans = new PNGTranscoder(); 1462 trans.addTranscodingHint(PNGTranscoder.KEY_XML_PARSER_CLASSNAME, 1463 application.getXMLParserClassName()); 1464 trans.addTranscodingHint(PNGTranscoder.KEY_FORCE_TRANSPARENT_WHITE, 1465 new Boolean (true)); 1466 1467 if(isIndexed){ 1469 trans.addTranscodingHint 1470 (PNGTranscoder.KEY_INDEXED,new Integer (256)); 1471 } 1472 1474 final BufferedImage img = trans.createImage(w, h); 1475 1476 Graphics2D g2d = img.createGraphics(); 1478 g2d.drawImage(buffer, null, 0, 0); 1479 new Thread () { 1480 public void run() { 1481 try { 1482 currentSavePath = f; 1483 OutputStream ostream = 1484 new BufferedOutputStream (new FileOutputStream (f)); 1485 trans.writeImage(img, 1486 new TranscoderOutput(ostream)); 1487 ostream.flush(); 1488 } catch (Exception ex) {} 1489 statusBar.setMessage 1490 (resources.getString("Message.done")); 1491 } 1492 }.start(); 1493 } 1494 } 1495 } 1496 } 1497 1498 1501 public class ExportAsTIFFAction extends AbstractAction { 1502 public ExportAsTIFFAction() {} 1503 public void actionPerformed(ActionEvent e) { 1504 JFileChooser fileChooser = 1505 new JFileChooser (makeAbsolute(currentSavePath)); 1506 fileChooser.setDialogTitle(resources.getString("ExportAsTIFF.title")); 1507 fileChooser.setFileHidingEnabled(false); 1508 fileChooser.setFileSelectionMode 1509 (JFileChooser.FILES_ONLY); 1510 fileChooser.addChoosableFileFilter(new ImageFileFilter(".tiff")); 1511 1512 int choice = fileChooser.showSaveDialog(JSVGViewerFrame.this); 1513 if (choice == JFileChooser.APPROVE_OPTION) { 1514 final File f = fileChooser.getSelectedFile(); 1515 BufferedImage buffer = svgCanvas.getOffScreen(); 1516 if (buffer != null) { 1517 statusBar.setMessage 1518 (resources.getString("Message.exportAsTIFF")); 1519 1520 int w = buffer.getWidth(); 1522 int h = buffer.getHeight(); 1523 final ImageTranscoder trans = new TIFFTranscoder(); 1524 trans.addTranscodingHint 1525 (TIFFTranscoder.KEY_XML_PARSER_CLASSNAME, 1526 application.getXMLParserClassName()); 1527 final BufferedImage img = trans.createImage(w, h); 1528 1529 Graphics2D g2d = img.createGraphics(); 1531 g2d.drawImage(buffer, null, 0, 0); 1532 new Thread () { 1533 public void run() { 1534 try { 1535 currentSavePath = f; 1536 OutputStream ostream = new BufferedOutputStream 1537 (new FileOutputStream (f)); 1538 trans.writeImage 1539 (img, new TranscoderOutput(ostream)); 1540 ostream.flush(); 1541 } catch (Exception ex) {} 1542 statusBar.setMessage 1543 (resources.getString("Message.done")); 1544 } 1545 }.start(); 1546 } 1547 } 1548 } 1549 } 1550 1551 1554 public class ViewSourceAction extends AbstractAction { 1555 public ViewSourceAction() {} 1556 public void actionPerformed(ActionEvent e) { 1557 if (svgDocument == null) { 1558 return; 1559 } 1560 1561 final ParsedURL u = new ParsedURL(svgDocument.getURL()); 1562 1563 final JFrame fr = new JFrame (u.toString()); 1564 fr.setSize(resources.getInteger("ViewSource.width"), 1565 resources.getInteger("ViewSource.height")); 1566 final JTextArea ta = new JTextArea (); 1567 ta.setLineWrap(true); 1568 ta.setFont(new Font ("monospaced", Font.PLAIN, 12)); 1569 1570 JScrollPane scroll = new JScrollPane (); 1571 scroll.getViewport().add(ta); 1572 scroll.setVerticalScrollBarPolicy 1573 (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 1574 fr.getContentPane().add(scroll, BorderLayout.CENTER); 1575 1576 new Thread () { 1577 public void run() { 1578 char [] buffer = new char[4096]; 1579 1580 try { 1581 Document doc = new PlainDocument (); 1582 1583 ParsedURL purl = new ParsedURL(svgDocument.getURL()); 1584 InputStream is 1585 = u.openStream(getInputHandler(purl). 1586 getHandledMimeTypes()); 1587 1589 Reader in = XMLUtilities.createXMLDocumentReader(is); 1590 int len; 1591 while ((len=in.read(buffer, 0, buffer.length)) != -1) { 1592 doc.insertString(doc.getLength(), 1593 new String (buffer, 0, len), null); 1594 } 1595 1596 ta.setDocument(doc); 1597 ta.setEditable(false); 1598 ta.setBackground(Color.white); 1599 fr.setVisible(true); 1600 } catch (Exception ex) { 1601 userAgent.displayError(ex); 1602 } 1603 } 1604 }.start(); 1605 } 1606 } 1607 1608 1611 public class FlushAction extends AbstractAction { 1612 public FlushAction() {} 1613 public void actionPerformed(ActionEvent e) { 1614 svgCanvas.flush(); 1615 svgCanvas.setRenderingTransform(svgCanvas.getRenderingTransform()); 1617 } 1618 } 1619 1620 1623 public class ToggleDebuggerAction extends AbstractAction { 1624 public ToggleDebuggerAction() { 1625 super("Toggle Debugger Action"); 1626 if (debuggerClass == null) 1627 setEnabled(false); 1628 } 1629 1630 public void actionPerformed(ActionEvent e) { 1631 if (debuggerClass == null) { 1632 setEnabled(false); 1633 return; 1634 } 1635 if ((debuggerFrame == null) || !debuggerFrame.isShowing()) 1636 showDebugger(); 1637 else 1638 hideDebugger(); 1639 } 1640 } 1641 1642 1645 public class PreviousTransformAction extends AbstractAction 1646 implements JComponentModifier { 1647 List components = new LinkedList (); 1648 public PreviousTransformAction() {} 1649 public void actionPerformed(ActionEvent e) { 1650 if (transformHistory.canGoBack()) { 1651 transformHistory.back(); 1652 update(); 1653 nextTransformAction.update(); 1654 svgCanvas.setRenderingTransform(transformHistory.currentTransform()); 1655 } 1656 } 1657 1658 public void addJComponent(JComponent c) { 1659 components.add(c); 1660 c.setEnabled(false); 1661 } 1662 1663 protected void update() { 1664 boolean b = transformHistory.canGoBack(); 1665 Iterator it = components.iterator(); 1666 while (it.hasNext()) { 1667 ((JComponent )it.next()).setEnabled(b); 1668 } 1669 } 1670 } 1671 1672 1675 public class NextTransformAction extends AbstractAction 1676 implements JComponentModifier { 1677 List components = new LinkedList (); 1678 public NextTransformAction() {} 1679 public void actionPerformed(ActionEvent e) { 1680 if (transformHistory.canGoForward()) { 1681 transformHistory.forward(); 1682 update(); 1683 previousTransformAction.update(); 1684 svgCanvas.setRenderingTransform(transformHistory.currentTransform()); 1685 } 1686 } 1687 1688 public void addJComponent(JComponent c) { 1689 components.add(c); 1690 c.setEnabled(false); 1691 } 1692 1693 protected void update() { 1694 boolean b = transformHistory.canGoForward(); 1695 Iterator it = components.iterator(); 1696 while (it.hasNext()) { 1697 ((JComponent )it.next()).setEnabled(b); 1698 } 1699 } 1700 } 1701 1702 1705 public class UseStylesheetAction extends AbstractAction 1706 implements JComponentModifier { 1707 1708 List components = new LinkedList (); 1709 1710 public UseStylesheetAction() {} 1711 1712 public void actionPerformed(ActionEvent e) { 1713 } 1714 1715 public void addJComponent(JComponent c) { 1716 components.add(c); 1717 c.setEnabled(false); 1718 } 1719 1720 protected void update() { 1721 alternateStyleSheet = null; 1722 Iterator it = components.iterator(); 1723 SVGDocument doc = svgCanvas.getSVGDocument(); 1724 while (it.hasNext()) { 1725 JComponent stylesheetMenu = (JComponent )it.next(); 1726 stylesheetMenu.removeAll(); 1727 stylesheetMenu.setEnabled(false); 1728 1729 ButtonGroup buttonGroup = new ButtonGroup (); 1730 1731 for (Node n = doc.getFirstChild(); 1732 n != null && n.getNodeType() != Node.ELEMENT_NODE; 1733 n = n.getNextSibling()) { 1734 if (n instanceof StyleSheetProcessingInstruction) { 1735 StyleSheetProcessingInstruction sspi; 1736 sspi = (StyleSheetProcessingInstruction)n; 1737 HashTable attrs = sspi.getPseudoAttributes(); 1738 final String title = (String )attrs.get("title"); 1739 String alt = (String )attrs.get("alternate"); 1740 if (title != null && "yes".equals(alt)) { 1741 JRadioButtonMenuItem button; 1742 button = new JRadioButtonMenuItem (title); 1743 1744 button.addActionListener 1745 (new java.awt.event.ActionListener () { 1746 public void actionPerformed(ActionEvent e) { 1747 SVGOMDocument doc; 1748 doc = (SVGOMDocument)svgCanvas.getSVGDocument(); 1749 doc.clearViewCSS(); 1750 alternateStyleSheet = title; 1751 svgCanvas.setSVGDocument(doc); 1752 } 1753 }); 1754 1755 buttonGroup.add(button); 1756 stylesheetMenu.add(button); 1757 stylesheetMenu.setEnabled(true); 1758 } 1759 } 1760 } 1761 } 1762 } 1763 } 1764 1765 1768 public class PlayAction extends AbstractAction 1769 implements JComponentModifier { 1770 java.util.List components = new LinkedList (); 1771 public PlayAction() {} 1772 public void actionPerformed(ActionEvent e) { 1773 svgCanvas.resumeProcessing(); 1774 } 1775 1776 public void addJComponent(JComponent c) { 1777 components.add(c); 1778 c.setEnabled(false); 1779 } 1780 1781 public void update(boolean enabled) { 1782 Iterator it = components.iterator(); 1783 while (it.hasNext()) { 1784 ((JComponent )it.next()).setEnabled(enabled); 1785 } 1786 } 1787 } 1788 1789 1792 public class PauseAction extends AbstractAction 1793 implements JComponentModifier { 1794 java.util.List components = new LinkedList (); 1795 public PauseAction() {} 1796 public void actionPerformed(ActionEvent e) { 1797 svgCanvas.suspendProcessing(); 1798 } 1799 1800 public void addJComponent(JComponent c) { 1801 components.add(c); 1802 c.setEnabled(false); 1803 } 1804 1805 public void update(boolean enabled) { 1806 Iterator it = components.iterator(); 1807 while (it.hasNext()) { 1808 ((JComponent )it.next()).setEnabled(enabled); 1809 } 1810 } 1811 } 1812 1813 1816 public class StopAction extends AbstractAction 1817 implements JComponentModifier { 1818 java.util.List components = new LinkedList (); 1819 public StopAction() {} 1820 public void actionPerformed(ActionEvent e) { 1821 svgCanvas.stopProcessing(); 1822 } 1823 1824 public void addJComponent(JComponent c) { 1825 components.add(c); 1826 c.setEnabled(false); 1827 } 1828 1829 public void update(boolean enabled) { 1830 Iterator it = components.iterator(); 1831 while (it.hasNext()) { 1832 ((JComponent )it.next()).setEnabled(enabled); 1833 } 1834 } 1835 } 1836 1837 1840 public class SetTransformAction extends AbstractAction { 1841 public SetTransformAction(){} 1842 public void actionPerformed(ActionEvent e){ 1843 if (transformDialog == null){ 1844 transformDialog 1845 = JAffineTransformChooser.createDialog 1846 (JSVGViewerFrame.this, 1847 resources.getString("SetTransform.title")); 1848 } 1849 1850 AffineTransform txf = transformDialog.showDialog(); 1851 if(txf != null){ 1852 AffineTransform at = svgCanvas.getRenderingTransform(); 1853 if(at == null){ 1854 at = new AffineTransform (); 1855 } 1856 1857 txf.concatenate(at); 1858 svgCanvas.setRenderingTransform(txf); 1859 } 1860 } 1861 } 1862 1863 1866 public class MonitorAction extends AbstractAction { 1867 public MonitorAction() {} 1868 public void actionPerformed(ActionEvent e) { 1869 if (memoryMonitorFrame == null) { 1870 memoryMonitorFrame = new MemoryMonitor(); 1871 Rectangle fr = getBounds(); 1872 Dimension md = memoryMonitorFrame.getSize(); 1873 memoryMonitorFrame.setLocation(fr.x + (fr.width - md.width) / 2, 1874 fr.y + (fr.height - md.height) / 2); 1875 } 1876 memoryMonitorFrame.setVisible(true); 1877 } 1878 } 1879 1880 1883 public class FindDialogAction extends AbstractAction { 1884 public FindDialogAction() {} 1885 public void actionPerformed(ActionEvent e) { 1886 if (findDialog == null) { 1887 findDialog = new FindDialog(JSVGViewerFrame.this, svgCanvas); 1888 findDialog.setGraphicsNode(svgCanvas.getGraphicsNode()); 1889 findDialog.pack(); 1890 Rectangle fr = getBounds(); 1891 Dimension td = findDialog.getSize(); 1892 findDialog.setLocation(fr.x + (fr.width - td.width) / 2, 1893 fr.y + (fr.height - td.height) / 2); 1894 } 1895 findDialog.setVisible(true); 1896 } 1897 } 1898 1899 1902 public class ThumbnailDialogAction extends AbstractAction { 1903 public ThumbnailDialogAction() {} 1904 public void actionPerformed(ActionEvent e) { 1905 if (thumbnailDialog == null) { 1906 thumbnailDialog 1907 = new ThumbnailDialog(JSVGViewerFrame.this, svgCanvas); 1908 thumbnailDialog.pack(); 1909 Rectangle fr = getBounds(); 1910 Dimension td = thumbnailDialog.getSize(); 1911 thumbnailDialog.setLocation(fr.x + (fr.width - td.width) / 2, 1912 fr.y + (fr.height - td.height) / 2); 1913 } 1914 thumbnailDialog.setInteractionEnabled 1915 (!svgCanvas.getDisableInteractions()); 1916 thumbnailDialog.setVisible(true); 1917 } 1918 } 1919 1920 1923 public class FullScreenAction extends AbstractAction { 1924 public FullScreenAction() {} 1925 1926 public void actionPerformed(ActionEvent e) { 1927 if (window == null || !window.isVisible()) { 1928 if (window == null) { 1929 window = new JWindow (JSVGViewerFrame.this); 1930 Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); 1931 window.setSize(size); 1932 } 1933 svgCanvas.getParent().remove(svgCanvas); 1935 window.getContentPane().add(svgCanvas); 1936 window.setVisible(true); 1937 window.toFront(); 1938 svgCanvas.requestFocus(); 1939 } else { 1940 svgCanvas.getParent().remove(svgCanvas); 1942 svgCanvasPanel.add(svgCanvas, BorderLayout.CENTER); 1943 window.setVisible(false); 1944 } 1945 } 1946 } 1947 1948 1951 public class DOMViewerAction extends AbstractAction { 1952 public DOMViewerAction() {} 1953 public void actionPerformed(ActionEvent e) { 1954 if (domViewer == null) { 1955 domViewer = new DOMViewer(); 1956 if (svgDocument != null) { 1957 domViewer.setDocument(svgDocument, 1958 (ViewCSS)svgDocument.getDocumentElement()); 1959 } 1960 Rectangle fr = getBounds(); 1961 Dimension td = domViewer.getSize(); 1962 domViewer.setLocation(fr.x + (fr.width - td.width) / 2, 1963 fr.y + (fr.height - td.height) / 2); 1964 } 1965 domViewer.setVisible(true); 1966 } 1967 } 1968 1969 1971 1974 protected Map listeners = new HashMap (); 1975 1976 1982 public Action getAction(String key) throws MissingListenerException { 1983 Action result = (Action )listeners.get(key); 1984 if (result == null) { 1988 throw new MissingListenerException("Can't find action.", RESOURCES, key); 1989 } 1990 return result; 1991 } 1992 1993 1995 long time; 1997 2000 public void documentLoadingStarted(SVGDocumentLoaderEvent e) { 2001 String msg = resources.getString("Message.documentLoad"); 2002 if (debug) { 2003 System.out.println(msg); 2004 time = System.currentTimeMillis(); 2005 } 2006 statusBar.setMainMessage(msg); 2007 stopAction.update(true); 2008 svgCanvas.setCursor(WAIT_CURSOR); 2009 } 2010 2011 2012 2015 public void documentLoadingCompleted(SVGDocumentLoaderEvent e) { 2016 if (debug) { 2017 System.out.print(resources.getString("Message.documentLoadTime")); 2018 System.out.println((System.currentTimeMillis() - time) + " ms"); 2019 } 2020 2021 setSVGDocument(e.getSVGDocument(), 2022 e.getSVGDocument().getURL(), 2023 e.getSVGDocument().getTitle()); 2024 } 2025 2026 2029 public void setSVGDocument(SVGDocument svgDocument, 2030 String svgDocumentURL, 2031 String svgDocumentTitle) { 2032 this.svgDocument = svgDocument; 2033 2034 if (domViewer != null) { 2035 if(domViewer.isVisible() && svgDocument != null) { 2036 domViewer.setDocument(svgDocument, 2037 (ViewCSS)svgDocument.getDocumentElement()); 2038 } else { 2039 domViewer.dispose(); 2040 domViewer = null; 2041 } 2042 } 2043 stopAction.update(false); 2044 svgCanvas.setCursor(DEFAULT_CURSOR); 2045 String s = svgDocumentURL; 2046 String t = svgCanvas.getFragmentIdentifier(); 2047 if (t != null) { 2048 s += "#" + t; 2049 } 2050 2051 locationBar.setText(s); 2052 if (title == null) { 2053 title = getTitle(); 2054 } 2055 2056 String dt = svgDocumentTitle; 2057 if (dt.length() != 0) { 2058 setTitle(title + ": " + dt); 2059 } else { 2060 int i = s.lastIndexOf("/"); 2061 if (i == -1) 2062 i = s.lastIndexOf("\\"); 2063 if (i == -1) { 2064 setTitle(title + ": " + s); 2065 } else { 2066 setTitle(title + ": " + s.substring(i + 1)); 2067 } 2068 } 2069 2070 localHistory.update(s); 2071 application.addVisitedURI(s); 2072 backAction.update(); 2073 forwardAction.update(); 2074 2075 transformHistory = new TransformHistory(); 2076 previousTransformAction.update(); 2077 nextTransformAction.update(); 2078 2079 useStylesheetAction.update(); 2080 } 2081 2082 2085 public void documentLoadingCancelled(SVGDocumentLoaderEvent e) { 2086 String msg = resources.getString("Message.documentCancelled"); 2087 if (debug) { 2088 System.out.println(msg); 2089 } 2090 statusBar.setMainMessage(""); 2091 statusBar.setMessage(msg); 2092 stopAction.update(false); 2093 svgCanvas.setCursor(DEFAULT_CURSOR); 2094 } 2095 2096 2099 public void documentLoadingFailed(SVGDocumentLoaderEvent e) { 2100 String msg = resources.getString("Message.documentFailed"); 2101 if (debug) { 2102 System.out.println(msg); 2103 } 2104 statusBar.setMainMessage(""); 2105 statusBar.setMessage(msg); 2106 stopAction.update(false); 2107 svgCanvas.setCursor(DEFAULT_CURSOR); 2108 } 2109 2110 2112 2116 public void gvtBuildStarted(GVTTreeBuilderEvent e) { 2117 String msg = resources.getString("Message.treeBuild"); 2118 if (debug) { 2119 System.out.println(msg); 2120 time = System.currentTimeMillis(); 2121 } 2122 statusBar.setMainMessage(msg); 2123 stopAction.update(true); 2124 svgCanvas.setCursor(WAIT_CURSOR); 2125 } 2126 2127 2130 public void gvtBuildCompleted(GVTTreeBuilderEvent e) { 2131 if (debug) { 2132 System.out.print(resources.getString("Message.treeBuildTime")); 2133 System.out.println((System.currentTimeMillis() - time) + " ms"); 2134 } 2135 if (findDialog != null) { 2136 if(findDialog.isVisible()) { 2137 findDialog.setGraphicsNode(svgCanvas.getGraphicsNode()); 2138 } else { 2139 findDialog.dispose(); 2140 findDialog = null; 2141 } 2142 } 2143 stopAction.update(false); 2144 svgCanvas.setCursor(DEFAULT_CURSOR); 2145 svgCanvas.setSelectionOverlayXORMode 2146 (application.isSelectionOverlayXORMode()); 2147 svgCanvas.requestFocus(); } 2149 2150 2153 public void gvtBuildCancelled(GVTTreeBuilderEvent e) { 2154 String msg = resources.getString("Message.treeCancelled"); 2155 if (debug) { 2156 System.out.println(msg); 2157 } 2158 statusBar.setMainMessage(""); 2159 statusBar.setMessage(msg); 2160 stopAction.update(false); 2161 svgCanvas.setCursor(DEFAULT_CURSOR); 2162 svgCanvas.setSelectionOverlayXORMode 2163 (application.isSelectionOverlayXORMode()); 2164 } 2165 2166 2169 public void gvtBuildFailed(GVTTreeBuilderEvent e) { 2170 String msg = resources.getString("Message.treeFailed"); 2171 if (debug) { 2172 System.out.println(msg); 2173 } 2174 statusBar.setMainMessage(""); 2175 statusBar.setMessage(msg); 2176 stopAction.update(false); 2177 svgCanvas.setCursor(DEFAULT_CURSOR); 2178 svgCanvas.setSelectionOverlayXORMode 2179 (application.isSelectionOverlayXORMode()); 2180 if (autoAdjust) { 2181 pack(); 2182 } 2183 } 2184 2185 2187 2190 public void svgLoadEventDispatchStarted(SVGLoadEventDispatcherEvent e) { 2191 String msg = resources.getString("Message.onload"); 2192 if (debug) { 2193 System.out.println(msg); 2194 time = System.currentTimeMillis(); 2195 } 2196 stopAction.update(true); 2197 statusBar.setMainMessage(msg); 2198 } 2199 2200 2203 public void svgLoadEventDispatchCompleted(SVGLoadEventDispatcherEvent e) { 2204 if (debug) { 2205 System.out.print(resources.getString("Message.onloadTime")); 2206 System.out.println((System.currentTimeMillis() - time) + " ms"); 2207 } 2208 stopAction.update(false); 2209 statusBar.setMainMessage(""); 2210 statusBar.setMessage(resources.getString("Message.done")); 2211 } 2212 2213 2216 public void svgLoadEventDispatchCancelled(SVGLoadEventDispatcherEvent e) { 2217 String msg = resources.getString("Message.onloadCancelled"); 2218 if (debug) { 2219 System.out.println(msg); 2220 } 2221 stopAction.update(false); 2222 statusBar.setMainMessage(""); 2223 statusBar.setMessage(msg); 2224 } 2225 2226 2229 public void svgLoadEventDispatchFailed(SVGLoadEventDispatcherEvent e) { 2230 String msg = resources.getString("Message.onloadFailed"); 2231 if (debug) { 2232 System.out.println(msg); 2233 } 2234 stopAction.update(false); 2235 statusBar.setMainMessage(""); 2236 statusBar.setMessage(msg); 2237 } 2238 2239 2241 2244 public void gvtRenderingPrepare(GVTTreeRendererEvent e) { 2245 if (debug) { 2246 String msg = resources.getString("Message.treeRenderingPrep"); 2247 System.out.println(msg); 2248 time = System.currentTimeMillis(); 2249 } 2250 stopAction.update(true); 2251 svgCanvas.setCursor(WAIT_CURSOR); 2252 statusBar.setMainMessage(resources.getString("Message.treeRendering")); 2253 } 2254 2255 2258 public void gvtRenderingStarted(GVTTreeRendererEvent e) { 2259 if (debug) { 2260 String msg = resources.getString("Message.treeRenderingPrepTime"); 2261 System.out.print(msg); 2262 System.out.println((System.currentTimeMillis() - time) + " ms"); 2263 time = System.currentTimeMillis(); 2264 msg = resources.getString("Message.treeRenderingStart"); 2265 System.out.println(msg); 2266 } 2267 } 2269 2270 2273 public void gvtRenderingCompleted(GVTTreeRendererEvent e) { 2274 if (debug) { 2275 String msg = resources.getString("Message.treeRenderingTime"); 2276 System.out.print(msg); 2277 System.out.println((System.currentTimeMillis() - time) + " ms"); 2278 } 2279 statusBar.setMainMessage(""); 2280 statusBar.setMessage(resources.getString("Message.done")); 2281 if (!svgCanvas.isDynamic() || managerStopped) { 2282 stopAction.update(false); 2283 } 2284 svgCanvas.setCursor(DEFAULT_CURSOR); 2285 2286 transformHistory.update(svgCanvas.getRenderingTransform()); 2287 previousTransformAction.update(); 2288 nextTransformAction.update(); 2289 } 2290 2291 2294 public void gvtRenderingCancelled(GVTTreeRendererEvent e) { 2295 String msg = resources.getString("Message.treeRenderingCancelled"); 2296 if (debug) { 2297 System.out.println(msg); 2298 } 2299 statusBar.setMainMessage(""); 2300 statusBar.setMessage(msg); 2301 if (!svgCanvas.isDynamic()) { 2302 stopAction.update(false); 2303 } 2304 svgCanvas.setCursor(DEFAULT_CURSOR); 2305 } 2306 2307 2310 public void gvtRenderingFailed(GVTTreeRendererEvent e) { 2311 String msg = resources.getString("Message.treeRenderingFailed"); 2312 if (debug) { 2313 System.out.println(msg); 2314 } 2315 statusBar.setMainMessage(""); 2316 statusBar.setMessage(msg); 2317 if (!svgCanvas.isDynamic()) { 2318 stopAction.update(false); 2319 } 2320 svgCanvas.setCursor(DEFAULT_CURSOR); 2321 } 2322 2323 2325 2328 public void linkActivated(LinkActivationEvent e) { 2329 String s = e.getReferencedURI(); 2330 if (svgDocument != null) { 2331 ParsedURL docURL = new ParsedURL(svgDocument.getURL()); 2332 ParsedURL url = new ParsedURL(docURL, s); 2333 if (!url.sameFile(docURL)) { 2334 return; 2335 } 2336 2337 if (s.indexOf("#") != -1) { 2338 localHistory.update(s); 2339 application.addVisitedURI(s); 2340 backAction.update(); 2341 forwardAction.update(); 2342 2343 transformHistory = new TransformHistory(); 2344 previousTransformAction.update(); 2345 nextTransformAction.update(); 2346 } 2347 } 2348 } 2349 2350 2352 2355 public void managerStarted(UpdateManagerEvent e) { 2356 if (debug) { 2357 String msg = resources.getString("Message.updateManagerStarted"); 2358 System.out.println(msg); 2359 } 2360 managerStopped = false; 2361 playAction.update(false); 2362 pauseAction.update(true); 2363 stopAction.update(true); 2364 } 2365 2366 2369 public void managerSuspended(UpdateManagerEvent e) { 2370 if (debug) { 2371 String msg = resources.getString("Message.updateManagerSuspended"); 2372 System.out.println(msg); 2373 } 2374 playAction.update(true); 2375 pauseAction.update(false); 2376 } 2377 2378 2381 public void managerResumed(UpdateManagerEvent e) { 2382 if (debug) { 2383 String msg = resources.getString("Message.updateManagerResumed"); 2384 System.out.println(msg); 2385 } 2386 playAction.update(false); 2387 pauseAction.update(true); 2388 } 2389 2390 2393 public void managerStopped(UpdateManagerEvent e) { 2394 if (debug) { 2395 String msg = resources.getString("Message.updateManagerStopped"); 2396 System.out.println(msg); 2397 } 2398 managerStopped = true; 2399 playAction.update(false); 2400 pauseAction.update(false); 2401 stopAction.update(false); 2402 } 2403 2404 2407 public void updateStarted(final UpdateManagerEvent e) { 2408 } 2409 2410 2413 public void updateCompleted(final UpdateManagerEvent e) { 2414 } 2415 2416 2419 public void updateFailed(UpdateManagerEvent e) { 2420 } 2421 2422 2425 protected class UserAgent implements SVGUserAgent { 2426 2427 2430 protected UserAgent() { 2431 } 2432 2433 2436 public void displayError(String message) { 2437 if (debug) { 2438 System.err.println(message); 2439 } 2440 JOptionPane pane = 2441 new JOptionPane (message, JOptionPane.ERROR_MESSAGE); 2442 JDialog dialog = pane.createDialog(JSVGViewerFrame.this, "ERROR"); 2443 dialog.setModal(false); 2444 dialog.setVisible(true); 2445 } 2446 2447 2450 public void displayError(Exception ex) { 2451 if (debug) { 2452 ex.printStackTrace(); 2453 } 2454 JErrorPane pane = 2455 new JErrorPane(ex, JOptionPane.ERROR_MESSAGE); 2456 JDialog dialog = pane.createDialog(JSVGViewerFrame.this, "ERROR"); 2457 dialog.setModal(false); 2458 dialog.setVisible(true); 2459 } 2460 2461 2465 public void displayMessage(String message) { 2466 statusBar.setMessage(message); 2467 } 2468 2469 2472 public void showAlert(String message) { 2473 svgCanvas.showAlert(message); 2474 } 2475 2476 2479 public String showPrompt(String message) { 2480 return svgCanvas.showPrompt(message); 2481 } 2482 2483 2486 public String showPrompt(String message, String defaultValue) { 2487 return svgCanvas.showPrompt(message, defaultValue); 2488 } 2489 2490 2493 public boolean showConfirm(String message) { 2494 return svgCanvas.showConfirm(message); 2495 } 2496 2497 2500 public float getPixelUnitToMillimeter() { 2501 return 0.26458333333333333333333333333333f; } 2503 2504 2509 public float getPixelToMM() { 2510 return getPixelUnitToMillimeter(); 2511 2512 } 2513 2514 2517 public String getDefaultFontFamily() { 2518 return application.getDefaultFontFamily(); 2519 } 2520 2521 2524 public float getMediumFontSize() { 2525 return 9f * 25.4f / (72f * getPixelUnitToMillimeter()); 2527 } 2528 2529 2532 public float getLighterFontWeight(float f) { 2533 int weight = ((int)((f+50)/100))*100; 2535 switch (weight) { 2536 case 100: return 100; 2537 case 200: return 100; 2538 case 300: return 200; 2539 case 400: return 300; 2540 case 500: return 400; 2541 case 600: return 400; 2542 case 700: return 400; 2543 case 800: return 400; 2544 case 900: return 400; 2545 default: 2546 throw new IllegalArgumentException ("Bad Font Weight: " + f); 2547 } 2548 } 2549 2550 2553 public float getBolderFontWeight(float f) { 2554 int weight = ((int)((f+50)/100))*100; 2556 switch (weight) { 2557 case 100: return 600; 2558 case 200: return 600; 2559 case 300: return 600; 2560 case 400: return 600; 2561 case 500: return 600; 2562 case 600: return 700; 2563 case 700: return 800; 2564 case 800: return 900; 2565 case 900: return 900; 2566 default: 2567 throw new IllegalArgumentException ("Bad Font Weight: " + f); 2568 } 2569 } 2570 2571 2572 2575 public String getLanguages() { 2576 return application.getLanguages(); 2577 } 2578 2579 2583 public String getUserStyleSheetURI() { 2584 return application.getUserStyleSheetURI(); 2585 } 2586 2587 2590 public String getXMLParserClassName() { 2591 return application.getXMLParserClassName(); 2592 } 2593 2594 2598 public boolean isXMLParserValidating() { 2599 return application.isXMLParserValidating(); 2600 } 2601 2602 2605 public String getMedia() { 2606 return application.getMedia(); 2607 } 2608 2609 2612 public String getAlternateStyleSheet() { 2613 return alternateStyleSheet; 2614 } 2615 2616 2621 public void openLink(String uri, boolean newc) { 2622 if (newc) { 2623 application.openLink(uri); 2624 } else { 2625 showSVGDocument(uri); 2626 } 2627 } 2628 2629 2633 public boolean supportExtension(String s) { 2634 return false; 2635 } 2636 2637 public void handleElement(Element elt, Object data){ 2638 } 2639 2640 2653 public ScriptSecurity getScriptSecurity(String scriptType, 2654 ParsedURL scriptURL, 2655 ParsedURL docURL){ 2656 if (!application.canLoadScriptType(scriptType)) { 2657 return new NoLoadScriptSecurity(scriptType); 2658 } else { 2659 switch(application.getAllowedScriptOrigin()) { 2660 case ResourceOrigin.ANY: 2661 return new RelaxedScriptSecurity(scriptType, 2662 scriptURL, 2663 docURL); 2664 case ResourceOrigin.DOCUMENT: 2665 return new DefaultScriptSecurity(scriptType, 2666 scriptURL, 2667 docURL); 2668 case ResourceOrigin.EMBEDED: 2669 return new EmbededScriptSecurity(scriptType, 2670 scriptURL, 2671 docURL); 2672 default: 2673 return new NoLoadScriptSecurity(scriptType); 2674 } 2675 } 2676 } 2677 2678 2696 public void checkLoadScript(String scriptType, 2697 ParsedURL scriptURL, 2698 ParsedURL docURL) throws SecurityException { 2699 ScriptSecurity s = getScriptSecurity(scriptType, 2700 scriptURL, 2701 docURL); 2702 2703 if (s != null) { 2704 s.checkLoadScript(); 2705 } 2706 } 2707 2708 2719 public ExternalResourceSecurity 2720 getExternalResourceSecurity(ParsedURL resourceURL, 2721 ParsedURL docURL){ 2722 switch(application.getAllowedExternalResourceOrigin()) { 2723 case ResourceOrigin.ANY: 2724 return new RelaxedExternalResourceSecurity(resourceURL, 2725 docURL); 2726 case ResourceOrigin.DOCUMENT: 2727 return new DefaultExternalResourceSecurity(resourceURL, 2728 docURL); 2729 case ResourceOrigin.EMBEDED: 2730 return new EmbededExternalResourceSecurity(resourceURL); 2731 default: 2732 return new NoLoadExternalResourceSecurity(); 2733 } 2734 } 2735 2736 2752 public void 2753 checkLoadExternalResource(ParsedURL resourceURL, 2754 ParsedURL docURL) throws SecurityException { 2755 ExternalResourceSecurity s 2756 = getExternalResourceSecurity(resourceURL, docURL); 2757 2758 if (s != null) { 2759 s.checkLoadExternalResource(); 2760 } 2761 } 2762 } 2763 2764 2767 protected static class ImageFileFilter extends FileFilter { 2768 2769 2770 protected String extension; 2771 2772 public ImageFileFilter(String extension) { 2773 this.extension = extension; 2774 } 2775 2776 2780 public boolean accept(File f) { 2781 boolean accept = false; 2782 String fileName = null; 2783 if (f != null) { 2784 if (f.isDirectory()) { 2785 accept = true; 2786 } else { 2787 fileName = f.getPath().toLowerCase(); 2788 if (fileName.endsWith(extension)) { 2789 accept = true; 2790 } 2791 } 2792 } 2793 return accept; 2794 } 2795 2796 2799 public String getDescription() { 2800 return extension; 2801 } 2802 } 2803 2804} 2805 | Popular Tags |