You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by hi...@apache.org on 2001/03/19 10:15:54 UTC

cvs commit: xml-batik/sources/org/apache/batik/apps/svgbrowser LocalHistory.java JSVGViewerFrame.java

hillion     01/03/19 01:15:53

  Modified:    resources/org/apache/batik/apps/svgbrowser/resources
                        GUI.properties
               sources/org/apache/batik/apps/svgbrowser
                        JSVGViewerFrame.java
  Added:       resources/org/apache/batik/apps/svgbrowser/resources
                        back.gif forward.gif
               sources/org/apache/batik/apps/svgbrowser LocalHistory.java
  Log:
  Added navigation features to the browser.
  
  Revision  Changes    Path
  1.8       +45 -13    xml-batik/resources/org/apache/batik/apps/svgbrowser/resources/GUI.properties
  
  Index: GUI.properties
  ===================================================================
  RCS file: /home/cvs/xml-batik/resources/org/apache/batik/apps/svgbrowser/resources/GUI.properties,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- GUI.properties	2001/03/18 16:29:20	1.7
  +++ GUI.properties	2001/03/19 09:15:47	1.8
  @@ -9,7 +9,7 @@
   # The viewer's GUI resources.
   #
   # Author: stephane@hillion.org
  -# $Id: GUI.properties,v 1.7 2001/03/18 16:29:20 hillion Exp $
  +# $Id: GUI.properties,v 1.8 2001/03/19 09:15:47 hillion Exp $
   #
   
   ViewSource.width = 750
  @@ -35,10 +35,11 @@
   # Definition of the menu bar
   #
   
  -MenuBar = File View Processing Options Tools
  +MenuBar = File View Processing Go Options Tools
   
   # File menu ################
  -File = Open OpenLocation NewWindow Reload - Print ExportAs - Close Exit
  +File = Open OpenLocation NewWindow Reload - \
  +       Print ExportAs - Close Exit
   File.type = MENU
   File.text = File
   File.mnemonic = F
  @@ -64,13 +65,6 @@
   NewWindow.action      = NewWindowAction
   NewWindow.accelerator = Ctrl+N
   
  -Close.type        = ITEM
  -Close.text        = Close
  -Close.icon        = resources/blank.gif
  -Close.mnemonic    = C
  -Close.action      = CloseAction
  -Close.accelerator = Ctrl+W
  -
   Reload.type        = ITEM
   Reload.text        = Reload Document
   Reload.icon        = resources/reload.gif
  @@ -78,6 +72,13 @@
   Reload.action      = ReloadAction
   Reload.accelerator = Ctrl+R
   
  +Close.type        = ITEM
  +Close.text        = Close
  +Close.icon        = resources/blank.gif
  +Close.mnemonic    = C
  +Close.action      = CloseAction
  +Close.accelerator = Ctrl+W
  +
   Exit.type        = ITEM
   Exit.text        = Exit
   Exit.icon        = resources/blank.gif
  @@ -93,7 +94,6 @@
   Print.accelerator = Ctrl+P
   
   # Export As submenu ###########
  -
   ExportAs             = PNG JPG
   ExportAs.type        = MENU
   ExportAs.text        = Export As
  @@ -162,6 +162,29 @@
   Stop.accelerator = Ctrl+S
   
   
  +# Go menu ################
  +Go = Back Forward - HistoryMarker
  +Go.type = MENU
  +Go.text = Go
  +Go.mnemonic = G
  +
  +Back.type        = ITEM
  +Back.text        = Back
  +Back.icon        = resources/back.gif
  +Back.mnemonic    = B
  +Back.action      = BackAction
  +Back.accelerator = Ctrl+Left
  +
  +Forward.type        = ITEM
  +Forward.text        = Forward
  +Forward.icon        = resources/forward.gif
  +Forward.mnemonic    = F
  +Forward.action      = ForwardAction
  +Forward.accelerator = Ctrl+Right
  +
  +HistoryMarker.type        = ITEM
  +HistoryMarker.text        = @@@
  +
   # Options menu ############
   Options           = ShowRendering AutoAdjust - Language StyleSheet - ShowDebug
   Options.type      = MENU
  @@ -234,8 +257,9 @@
   # Definition of the tool bar
   #
   
  -ToolBar = ToolOpen - ToolReload - ToolZoomIn ToolZoomOut - ToolViewSource \
  -          ToolDOMViewer - ToolPrint - ToolStop
  +ToolBar = ToolOpen - ToolReload ToolBack ToolForward - \
  +          ToolZoomIn ToolZoomOut - ToolViewSource ToolDOMViewer - \
  +          ToolPrint - ToolStop
   
   ToolOpen.icon      = resources/open.gif
   ToolOpen.action    = OpenAction
  @@ -244,6 +268,14 @@
   ToolReload.icon      = resources/reload.gif
   ToolReload.action    = ReloadAction
   ToolReload.tooltip   = Reload the current document.
  +
  +ToolBack.icon      = resources/back.gif
  +ToolBack.action    = BackAction
  +ToolBack.tooltip   = Back To The Previous The Document
  +
  +ToolForward.icon      = resources/forward.gif
  +ToolForward.action    = ForwardAction
  +ToolForward.tooltip   = Forward To The Next The Document
   
   ToolPrint.icon      = resources/print.gif
   ToolPrint.action    = PrintAction
  
  
  
  1.1                  xml-batik/resources/org/apache/batik/apps/svgbrowser/resources/back.gif
  
  	<<Binary file>>
  
  
  1.1                  xml-batik/resources/org/apache/batik/apps/svgbrowser/resources/forward.gif
  
  	<<Binary file>>
  
  
  1.8       +134 -42   xml-batik/sources/org/apache/batik/apps/svgbrowser/JSVGViewerFrame.java
  
  Index: JSVGViewerFrame.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/apps/svgbrowser/JSVGViewerFrame.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- JSVGViewerFrame.java	2001/03/18 18:18:17	1.7
  +++ JSVGViewerFrame.java	2001/03/19 09:15:51	1.8
  @@ -46,9 +46,11 @@
   import java.net.MalformedURLException;
   import java.net.URL;
   
  +import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.LinkedList;
  +import java.util.List;
   import java.util.Locale;
   import java.util.Map;
   import java.util.MissingResourceException;
  @@ -63,6 +65,7 @@
   import javax.swing.JComponent;
   import javax.swing.JFileChooser;
   import javax.swing.JFrame;
  +import javax.swing.JMenuBar;
   import javax.swing.JOptionPane;
   import javax.swing.JPanel;
   import javax.swing.JScrollPane;
  @@ -122,7 +125,7 @@
    * This class represents a SVG viewer swing frame.
    *
    * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
  - * @version $Id: JSVGViewerFrame.java,v 1.7 2001/03/18 18:18:17 vhardy Exp $
  + * @version $Id: JSVGViewerFrame.java,v 1.8 2001/03/19 09:15:51 hillion Exp $
    */
   public class JSVGViewerFrame
       extends    JFrame
  @@ -142,6 +145,8 @@
       public final static String OPEN_LOCATION_ACTION = "OpenLocationAction";
       public final static String NEW_WINDOW_ACTION = "NewWindowAction";
       public final static String RELOAD_ACTION = "ReloadAction";
  +    public final static String BACK_ACTION = "BackAction";
  +    public final static String FORWARD_ACTION = "ForwardAction";
       public final static String PRINT_ACTION = "PrintAction";
       public final static String EXPORT_AS_PNG_ACTION = "ExportAsPNGAction";
       public final static String EXPORT_AS_JPG_ACTION = "ExportAsJPGAction";
  @@ -213,6 +218,16 @@
       protected String currentExportPath = ".";
   
       /**
  +     * The back action
  +     */
  +    protected BackAction backAction = new BackAction();
  +
  +    /**
  +     * The forward action
  +     */
  +    protected ForwardAction forwardAction = new ForwardAction();
  +
  +    /**
        * The stop action
        */
       protected StopAction stopAction = new StopAction();
  @@ -288,6 +303,11 @@
       protected String title;
   
       /**
  +     * The local history.
  +     */
  +    protected LocalHistory localHistory;
  +
  +    /**
        * Creates a new SVG viewer frame.
        */
       public JSVGViewerFrame(Application app) {
  @@ -303,6 +323,8 @@
           listeners.put(OPEN_LOCATION_ACTION, new OpenLocationAction());
           listeners.put(NEW_WINDOW_ACTION, new NewWindowAction());
           listeners.put(RELOAD_ACTION, new ReloadAction());
  +        listeners.put(BACK_ACTION, backAction);
  +        listeners.put(FORWARD_ACTION, forwardAction);
           listeners.put(PRINT_ACTION, new PrintAction());
           listeners.put(EXPORT_AS_PNG_ACTION, new ExportAsPNGAction());
           listeners.put(EXPORT_AS_JPG_ACTION, new ExportAsJPGAction());
  @@ -322,13 +344,19 @@
           listeners.put(MONITOR_ACTION, new MonitorAction());
           listeners.put(DOM_VIEWER_ACTION, new DOMViewerAction());
   
  +        svgCanvas = new JSVGCanvas(userAgent, true, true);
  +
           JPanel p = null;
           try {
               // Create the menu
               MenuFactory mf = new MenuFactory(bundle, this);
  -            setJMenuBar(mf.createJMenuBar("MenuBar"));
  +            JMenuBar mb = mf.createJMenuBar("MenuBar");
  +            setJMenuBar(mb);
   
  +            localHistory = new LocalHistory(mb, svgCanvas);
  +
               p = new JPanel(new BorderLayout());
  +
               // Create the toolbar
               ToolBarFactory tbf = new ToolBarFactory(bundle, this);
               JToolBar tb = tbf.createJToolBar("ToolBar");
  @@ -346,8 +374,7 @@
           JPanel p2 = new JPanel(new BorderLayout());
           p2.setBorder(BorderFactory.createEtchedBorder());
   
  -        p2.add(svgCanvas = new JSVGCanvas(userAgent, true, true),
  -               BorderLayout.CENTER);
  +        p2.add(svgCanvas, BorderLayout.CENTER);
           p = new JPanel(new BorderLayout());
           p.add(p2, BorderLayout.CENTER);
           p.add(statusBar = new StatusBar(), BorderLayout.SOUTH);
  @@ -540,7 +567,7 @@
                       currentPath = f.getCanonicalPath();
                       svgCanvas.loadSVGDocument(f.toURL().toString());
                   } catch (IOException ex) {
  -                    // !!! Error dialog
  +                    userAgent.displayError(ex);
                   }
               }
           }
  @@ -573,7 +600,7 @@
                       }
                       svgCanvas.loadSVGDocument(u.toString());
                   } catch (Exception ex) {
  -                    // !!! Error dialog
  +                    userAgent.displayError(ex);
                   }
               }
           }
  @@ -606,54 +633,105 @@
           public ReloadAction() {}
           public void actionPerformed(ActionEvent e) {
               if (svgDocument != null) {
  -                String url = ((SVGOMDocument)svgDocument).getURLObject().toString();
  -                svgCanvas.loadSVGDocument(url.toString());
  +                localHistory.reload();
               }
           }
       }
   
       /**
  +     * To go back to the previous document
  +     */
  +    public class BackAction extends    AbstractAction
  +                            implements JComponentModifier {
  +        List components = new LinkedList();
  +        public BackAction() {}
  +        public void actionPerformed(ActionEvent e) {
  +            localHistory.back();
  +        }
  +
  +        public void addJComponent(JComponent c) {
  +            components.add(c);
  +            c.setEnabled(false);
  +        }
  +
  +        protected void update() {
  +            boolean b = localHistory.canGoBack();
  +            Iterator it = components.iterator();
  +            while (it.hasNext()) {
  +                ((JComponent)it.next()).setEnabled(b);
  +            }
  +        }
  +    }
  +
  +    /**
  +     * To go forward to the previous document
  +     */
  +    public class ForwardAction extends    AbstractAction
  +                               implements JComponentModifier {
  +        List components = new LinkedList();
  +        public ForwardAction() {}
  +        public void actionPerformed(ActionEvent e) {
  +            localHistory.forward();
  +        }
  +
  +        public void addJComponent(JComponent c) {
  +            components.add(c);
  +            c.setEnabled(false);
  +        }
  +
  +        protected void update() {
  +            boolean b = localHistory.canGoForward();
  +            Iterator it = components.iterator();
  +            while (it.hasNext()) {
  +                ((JComponent)it.next()).setEnabled(b);
  +            }
  +        }
  +    }
  +
  +    /**
        * To print the current document.
        */
       public class PrintAction extends AbstractAction {
           public PrintAction() {}
           public void actionPerformed(ActionEvent e) {
               if (svgDocument != null) {
  +                final SVGDocument doc = svgDocument;
                   new Thread() {
  -                        public void run(){
  -                            //
  -                            // Build a PrintTranscoder to handle printing 
  -                            // of the svgDocument object
  -                            //
  -                            PrintTranscoder printTranscoder 
  -                                = new PrintTranscoder();
  +                    public void run(){
  +                        //
  +                        // Build a PrintTranscoder to handle printing 
  +                        // of the svgDocument object
  +                        //
  +                        PrintTranscoder pt = new PrintTranscoder();
                               
  -                            //
  -                            // Set transcoding hints
  -                            //
  -                            printTranscoder.addTranscodingHint(PrintTranscoder.KEY_XML_PARSER_CLASSNAME,
  -                                                               application.getXMLParserClassName());
  -                            printTranscoder.addTranscodingHint(PrintTranscoder.KEY_SHOW_PAGE_DIALOG,
  -                                                               new Boolean(true));
  -                            printTranscoder.addTranscodingHint(PrintTranscoder.KEY_SHOW_PRINTER_DIALOG,
  -                            new Boolean(true));
  +                        //
  +                        // Set transcoding hints
  +                        //
  +                        pt.addTranscodingHint(pt.KEY_XML_PARSER_CLASSNAME,
  +                                              application.getXMLParserClassName());
  +
  +                        pt.addTranscodingHint(pt.KEY_SHOW_PAGE_DIALOG,
  +                                              Boolean.TRUE);
  +
  +
  +                        pt.addTranscodingHint(pt.KEY_SHOW_PRINTER_DIALOG,
  +                                              Boolean.TRUE);
                               
  -                            //
  -                            // Do transcoding now
  -                            //
  -                            printTranscoder.transcode(new TranscoderInput(svgDocument), null);
  +                        //
  +                        // Do transcoding now
  +                        //
  +                        pt.transcode(new TranscoderInput(doc), null);
                               
  -                            //
  -                            // Print
  -                            //
  -                            try{
  -                                printTranscoder.print();
  -                            }catch(PrinterException ex){
  -                                // <!> TEMPORARY: NEED BETTER ERROR HANDLING STRATEGY
  -                                JOptionPane.showMessageDialog(JSVGViewerFrame.this, ex);
  -                            }
  +                        //
  +                        // Print
  +                        //
  +                        try {
  +                            pt.print();
  +                        } catch (PrinterException ex) {
  +                            userAgent.displayError(ex);
                           }
  -                    }.start();
  +                    }
  +                }.start();
               }
           }
       }
  @@ -830,8 +908,7 @@
                           ta.setBackground(Color.white);
                           fr.show();
                       } catch (Exception ex) {
  -                        // !!! TODO : dialog
  -                        System.err.println(ex.toString());
  +                        userAgent.displayError(ex);
                       }
                   }
               }.start();
  @@ -1103,10 +1180,19 @@
           }
           int i = s.lastIndexOf("/");
           if (i == -1) {
  -            setTitle(title + ":" + s);
  +            i = s.lastIndexOf("\\");
  +            if (i == -1) {
  +                setTitle(title + ":" + s);
  +            } else {
  +                setTitle(title + ":" + s.substring(i + 1));
  +            }
           } else {
               setTitle(title + ":" + s.substring(i + 1));
           }
  +
  +        localHistory.update(s);
  +        backAction.update();
  +        forwardAction.update();
       }
   
       /**
  @@ -1199,7 +1285,7 @@
        */
       public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
           if (debug) {
  -            System.out.println("Rendering started...");
  +            System.out.println("Rendering preparation...");
               time = System.currentTimeMillis();
           }
           stopAction.update(true);
  @@ -1211,6 +1297,12 @@
        * Called when a rendering started.
        */
       public void gvtRenderingStarted(GVTTreeRendererEvent e) {
  +        if (debug) {
  +            System.out.print("Rendering prepared in ");
  +            System.out.println((System.currentTimeMillis() - time) + " ms");
  +            time = System.currentTimeMillis();
  +            System.out.println("Rendering started...");
  +        }
           // Do nothing
       }
   
  
  
  
  1.1                  xml-batik/sources/org/apache/batik/apps/svgbrowser/LocalHistory.java
  
  Index: LocalHistory.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.apps.svgbrowser;
  
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.swing.ButtonGroup;
  import javax.swing.JRadioButtonMenuItem;
  import javax.swing.JMenu;
  import javax.swing.JMenuBar;
  import javax.swing.JMenuItem;
  
  import org.apache.batik.swing.JSVGCanvas;
  
  /**
   * This class represents an history of the files visited by a single
   * browser frame.
   *
   * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
   * @version $Id: LocalHistory.java,v 1.1 2001/03/19 09:15:52 hillion Exp $
   */
  public class LocalHistory {
  
      /**
       * The canvas to manage.
       */
      protected JSVGCanvas svgCanvas;
  
      /**
       * The menu which contains the history.
       */
      protected JMenu menu;
  
      /**
       * The index of the first history item in this menu.
       */
      protected int index;
  
      /**
       * The visited URIs.
       */
      protected List visitedURIs = new ArrayList();
  
      /**
       * The index of the current URI.
       */
      protected int currentURI = -1;
  
      /**
       * The button group for the menu items.
       */
      protected ButtonGroup group = new ButtonGroup();
  
      /**
       * The action listener.
       */
      protected ActionListener actionListener = new RadioListener();
  
      /**
       * Creates a new local history.
       * @param mb The menubar used to display the history. It must
       *        contains one '@@@' item used as marker to place the
       *        history items.
       * @param canvas The canvas to manage.
       */
      public LocalHistory(JMenuBar mb, JSVGCanvas canvas) {
          svgCanvas = canvas;
  
          // Find the marker.
          int mc = mb.getMenuCount();
          for (int i = 0; i < mc; i++) {
              JMenu m = mb.getMenu(i);
              int ic = m.getItemCount();
              for (int j = 0; j < ic; j++) {
                  JMenuItem mi = m.getItem(j);
                  if (mi != null) {
                      String s = mi.getText();
                      if ("@@@".equals(s)) {
                          menu = m;
                          index = j;
                          m.remove(j);
                          return;
                      }
                  }
              }
          }
          throw new IllegalArgumentException("No '@@@' marker found");
      }
  
      /**
       * Goes back of one position in the history.
       * Assumes that <tt>canGoBack()</tt> is true.
       */
      public void back() {
          currentURI -= 2;
          svgCanvas.loadSVGDocument((String)visitedURIs.get(currentURI + 1));
      }
  
      /**
       * Whether it is possible to go back.
       */
      public boolean canGoBack() {
          return currentURI > 0;
      }
  
      /**
       * Goes forward of one position in the history.
       * Assumes that <tt>canGoForward()</tt> is true.
       */
      public void forward() {
          svgCanvas.loadSVGDocument((String)visitedURIs.get(currentURI + 1));
      }
  
      /**
       * Whether it is possible to go forward.
       */
      public boolean canGoForward() {
          return currentURI < visitedURIs.size() - 1;
      }
  
      /**
       * Reloads the current document.
       */
      public void reload() {
          currentURI--;
          svgCanvas.loadSVGDocument((String)visitedURIs.get(currentURI + 1));
      }
  
      /**
       * Updates the history.
       * @param uri The URI of the document just loaded.
       */
      public void update(String uri) {
          if (++currentURI < visitedURIs.size()) {
              if (!visitedURIs.get(currentURI).equals(uri)) {
                  for (int i = currentURI + 1; i < visitedURIs.size(); i++) {
                      JMenuItem mi = menu.getItem(index + i);
                      group.remove(mi);
                      menu.remove(index + i);
                  }
                  visitedURIs = visitedURIs.subList(0, currentURI + 1);
              }
              JMenuItem mi = menu.getItem(index + currentURI);
              group.remove(mi);
              menu.remove(index + currentURI);
              visitedURIs.set(currentURI, uri);
          } else {
              visitedURIs.add(uri);
          }
  
          // Computes the button text.
          String text = uri;
          int i = uri.lastIndexOf("/");
          if (i == -1) {
              i = uri.lastIndexOf("\\");
              if (i != -1) {
                  text = uri.substring(i + 1);
              }
          } else {
              text = uri.substring(i + 1);
          }
  
          JMenuItem mi = new JRadioButtonMenuItem(text);
          mi.setActionCommand(uri);
          mi.addActionListener(actionListener);
          group.add(mi);
          mi.setSelected(true);
          menu.insert(mi, index + currentURI);
      }
  
      /**
       * To listen to the radio buttons.
       */
      protected class RadioListener implements ActionListener {
          public RadioListener() {}
  	public void actionPerformed(ActionEvent e) {
  	    String uri = e.getActionCommand();
              currentURI = getItemIndex((JMenuItem)e.getSource()) - 1;
  	    svgCanvas.loadSVGDocument(uri);
  	}
          public int getItemIndex(JMenuItem item) {
              int ic = menu.getItemCount();
              for (int i = index; i < ic; i++) {
                  if (menu.getItem(i) == item) {
                      return i - index;
                  }
              }
              throw new InternalError();
          }
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org