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 cj...@apache.org on 2001/01/29 16:58:55 UTC

cvs commit: xml-batik/sources/org/apache/batik/util/gui LanguageDialog.java

cjolif      01/01/29 07:58:55

  Modified:    sources/org/apache/batik/apps/svgviewer ViewerFrame.java
               sources/org/apache/batik/util/gui LanguageDialog.java
  Log:
  speed up ViewerFrame launcing by delaying the initialization of
  the language dialog box (2 sec for 14 seconds of loading for me).
  take the user default locale language into account
  
  Revision  Changes    Path
  1.47      +80 -23    xml-batik/sources/org/apache/batik/apps/svgviewer/ViewerFrame.java
  
  Index: ViewerFrame.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/apps/svgviewer/ViewerFrame.java,v
  retrieving revision 1.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- ViewerFrame.java	2001/01/29 10:28:11	1.46
  +++ ViewerFrame.java	2001/01/29 15:58:54	1.47
  @@ -142,7 +142,7 @@
    *
    * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
    * @author <a href="mailto:cjolif@ilog.fr">Christophe Jolif</a>
  - * @version $Id: ViewerFrame.java,v 1.46 2001/01/29 10:28:11 tkormann Exp $
  + * @version $Id: ViewerFrame.java,v 1.47 2001/01/29 15:58:54 cjolif Exp $
    */
   public class ViewerFrame
       extends    JFrame
  @@ -281,9 +281,11 @@
       protected DOMViewer domViewer = new DOMViewer();
   
       /**
  -     * The language dialog.
  +     * The language dialog. It is private now to allow
  +     * dynamic building (very long to build). You can
  +     * get it from subclasses with getLanguageDialog().
        */
  -    protected LanguageDialog languageDialog;
  +    private LanguageDialog languageDialog;
   
       /**
        * The user style dialog.
  @@ -373,6 +375,10 @@
       public ViewerFrame(Application a) {
           application = a;
   
  +        if (!Locale.getDefault().getLanguage().equals(userLanguages))
  +            userLanguages=Locale.getDefault().getLanguage()+","+
  +                userLanguages;
  +
           setTitle(resources.getString("Frame.title"));
           setSize(resources.getInteger("Frame.width"),
                   resources.getInteger("Frame.height"));
  @@ -431,8 +437,8 @@
               ToolBarFactory tbf = new ToolBarFactory(bundle, this);
               JToolBar tb = tbf.createJToolBar("ToolBar");
               tb.setFloatable(false);
  -            getContentPane().add("North", p);
  -            p.add("North", tb);
  +            getContentPane().add(p, BorderLayout.NORTH);
  +            p.add(tb, BorderLayout.NORTH);
           } catch (MissingResourceException e) {
               System.out.println(e.getMessage());
               System.exit(0);
  @@ -443,29 +449,43 @@
           locationBar.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 1));
           locationBar.addActionListener(new LocationBarAction());
   
  -        p.add("South", locationBar);
  -        p.add("Center", new JSeparator());
  +        p.add(locationBar, BorderLayout.SOUTH);
  +        p.add(new JSeparator(), BorderLayout.CENTER);
   
           // Create the view panel
           panel = new JPanel(new BorderLayout());
           panel.setBorder(BorderFactory.createEtchedBorder());
  -        getContentPane().add("Center", panel);
  +        getContentPane().add(panel, BorderLayout.CENTER);
           p = new JPanel(new BorderLayout());
  -        panel.add("Center", p);
  +        panel.add(p, BorderLayout.CENTER);
   
           // Create the status bar
           statusBar = new StatusBar();
  -        getContentPane().add("South", statusBar);
  +        getContentPane().add(statusBar, BorderLayout.SOUTH);
   
           // Create the language dialog
  -        languageDialog = new LanguageDialog(this);
  -        languageDialog.setLanguageChangeHandler(this);
  +        /**
  +         * The dialog takes a very long time to be build
  +         * we speed up launching by doing it elsewhere
  +         * languageDialog = new LanguageDialog(this);
  +         * languageDialog.setLanguageChangeHandler(this);
  +         */
  +        // we want to do it only after init. As we don't
  +        // know if a file will be loaded just after init
  +        // the only simple means to be pretty sure to be
  +        // after is to put a timer.
  +        javax.swing.Timer timer =
  +            new javax.swing.Timer(15000,
  +                                  new InitLanguageDialog());
  +        timer.setRepeats(false);
  +        timer.start();
  +        timer = null;
   
           // Create the user style dialog
           userStyleDialog = new UserStyleDialog(this);
           userStyleDialog.setChangeHandler(this);
   
  -        panel.add("Center", canvas);
  +        panel.add(canvas, BorderLayout.CENTER);
           panel.revalidate();
           panel.repaint();
           canvas.setZoomHandler(this);
  @@ -489,9 +509,42 @@
                       statusBar.setHeight(dim.height);
                   }
               });
  +
  +            }
  +
  +    /**
  +     * Utility class for deferred initialization of
  +     * LanguageDialog instance.
  +     */
  +    private static class InitLanguageDialog
  +        implements java.awt.event.ActionListener {
  +        public void actionPerformed(ActionEvent e) {
  +            Thread t = new Thread(new Runnable() {
  +                    public void run() {
  +                        LanguageDialog.Panel.
  +                            initCountryIcons();
  +                    }
  +                });
  +            t.setPriority(Thread.MIN_PRIORITY);
  +            t.start();
  +            ((javax.swing.Timer)e.getSource()).stop();
  +        }
       }
   
       /**
  +     * Returns the instance of <code>LanguageDialog</code>
  +     * used by the <code>ViewerFrame</code>.
  +     */
  +    protected LanguageDialog getLanguageDialog()
  +    {
  +        if (languageDialog == null) {
  +            languageDialog = new LanguageDialog(this);
  +            languageDialog.setLanguageChangeHandler(this);
  +        }
  +        return languageDialog;
  +    }
  +
  +    /**
        * Tells the viewer whether or not it must be set to the size
        * of the loaded documents.
        */
  @@ -857,7 +910,8 @@
                               currentExportPath = f.getCanonicalPath();
                               OutputStream ostream =
                                 new BufferedOutputStream(new FileOutputStream(f));
  -                            trans.writeImage(img, new TranscoderOutput(ostream));
  +                            trans.writeImage(img,
  +                                             new TranscoderOutput(ostream));
                               ostream.flush();
                               ostream.close();
                               statusBar.setMessage(
  @@ -1061,7 +1115,7 @@
               scroll.getViewport().add(ta);
               scroll.setVerticalScrollBarPolicy
                   (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  -            fr.getContentPane().add("Center", scroll);
  +            fr.getContentPane().add(scroll, BorderLayout.CENTER);
   
               Document  doc = new PlainDocument();
               InputStream is = null;
  @@ -1140,7 +1194,7 @@
               scroll.getViewport().add(ta);
               scroll.setVerticalScrollBarPolicy
                   (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  -            fr.getContentPane().add("Center", scroll);
  +            fr.getContentPane().add(scroll, BorderLayout.CENTER);
   
               ta.setText(description);
               ta.setEditable(false);
  @@ -1257,7 +1311,8 @@
       public class ProgressivePaintAction extends AbstractAction {
           public ProgressivePaintAction() {}
           public void actionPerformed(ActionEvent e) {
  -            progressivePaintEnabled = ((JCheckBoxMenuItem)e.getSource()).isSelected();
  +            progressivePaintEnabled = ((JCheckBoxMenuItem)e.getSource()).
  +                isSelected();
               canvas.setProgressiveRenderingEnabled(progressivePaintEnabled);
           }
       }
  @@ -1269,11 +1324,12 @@
           public LanguageAction() {}
           public void actionPerformed(ActionEvent e) {
               Rectangle fr = getBounds();
  -            Dimension ld = languageDialog.getSize();
  -            languageDialog.setLocation(fr.x + (fr.width  - ld.width) / 2,
  +            LanguageDialog dialog = getLanguageDialog();
  +            Dimension ld = dialog.getSize();
  +            dialog.setLocation(fr.x + (fr.width  - ld.width) / 2,
                                          fr.y + (fr.height - ld.height) / 2);
  -            languageDialog.setLanguages(userLanguages);
  -            languageDialog.show();
  +            dialog.setLanguages(userLanguages);
  +            dialog.show();
           }
       }
   
  @@ -1307,7 +1363,8 @@
       /**
        * To display the about dialog.
        */
  -    public class AboutAction extends AbstractAction implements DocumentListener {
  +    public class AboutAction extends AbstractAction
  +        implements DocumentListener {
           JSVGCanvas canvas;
           public AboutAction() {}
           public void actionPerformed(ActionEvent e) {
  @@ -1335,7 +1392,7 @@
                           aboutFrame.setVisible(false);
                       }
                   });
  -                aboutFrame.getContentPane().add("South", p);
  +                aboutFrame.getContentPane().add(p, BorderLayout.SOUTH);
                   canvas = new JSVGCanvas(ViewerFrame.this);
                   canvas.setBorder(BorderFactory.createLoweredBevelBorder());
                   panel.add(canvas);
  
  
  
  1.3       +53 -11    xml-batik/sources/org/apache/batik/util/gui/LanguageDialog.java
  
  Index: LanguageDialog.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/util/gui/LanguageDialog.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- LanguageDialog.java	2000/10/20 11:06:35	1.2
  +++ LanguageDialog.java	2001/01/29 15:58:55	1.3
  @@ -54,7 +54,8 @@
    * This class represents a language selection dialog.
    *
    * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
  - * @version $Id: LanguageDialog.java,v 1.2 2000/10/20 11:06:35 hillion Exp $
  + * @author <a href="mailto:cjolif@ilog.fr">Christophe Jolif</a>
  + * @version $Id: LanguageDialog.java,v 1.3 2001/01/29 15:58:55 cjolif Exp $
    */
   public class LanguageDialog extends JDialog implements ActionMap {
       /**
  @@ -108,7 +109,7 @@
           getContentPane().add("South", createButtonsPanel());
   
           pack();
  -    }    
  +    }
   
       /**
        * Sets a language change handler.
  @@ -156,7 +157,7 @@
            * The user languages list
            */
           protected JList userList;
  -        
  +
           /**
            * The languages list
            */
  @@ -166,7 +167,7 @@
            * The user list model
            */
           protected DefaultListModel userListModel = new DefaultListModel();
  -        
  +
           /**
            * The language list model
            */
  @@ -203,14 +204,23 @@
           protected Map listeners = new HashMap();
   
           /**
  +         * The cached map for country icons (takes more than 2 secs.
  +         * to be computed).
  +         */
  +        private static Map iconMap = null;
  +
  +        /**
            * Creates a new Panel object.
            */
           public Panel() {
               super(new GridBagLayout());
  +
  +            initCountryIcons();
  +
               setBorder(BorderFactory.createTitledBorder
                         (BorderFactory.createEtchedBorder(),
                          resources.getString("Panel.title")));
  -            
  +
               listeners.put("AddLanguageButtonAction",
                             new AddLanguageButtonAction());
               listeners.put("RemoveLanguageButtonAction",
  @@ -310,6 +320,29 @@
           }
   
           /**
  +         * Allows to pre-initialize icons used by the <code>Panel</code>
  +         * constructor. It is not neccessary to call it and it should
  +         * be called only once.
  +         * This method is safe to be called by another thread than the
  +         * event thread as it doesn't manipulate Swing <code>JComponent</code>
  +         * instances.
  +         */
  +        public synchronized static void initCountryIcons()
  +        {
  +            // don't need to init several times...
  +            if (iconMap == null) {
  +                iconMap = new HashMap();
  +                StringTokenizer st;
  +                st = new StringTokenizer(resources.getString("Country.list"),
  +                                         " ");
  +                while (st.hasMoreTokens()) {
  +                    computeCountryIcon(LanguageDialog.Panel.class,
  +                                       st.nextToken());
  +                }
  +            }
  +        }
  +
  +        /**
            * Returns the selected user languages.
            */
           public String getLanguages() {
  @@ -362,7 +395,7 @@
               boolean selected     = i != -1;
               boolean zeroSelected = i == 0;
               boolean lastSelected = i == size - 1;
  -        
  +
               removeLanguageButton.setEnabled(!empty && selected);
               upLanguageButton.setEnabled(!empty && selected && !zeroSelected);
               downLanguageButton.setEnabled(!empty && selected && !lastSelected);
  @@ -388,15 +421,24 @@
            * returns the icon associated with a country code.
            */
           protected Icon getCountryIcon(String code) {
  +            return computeCountryIcon(getClass(), code);
  +        }
  +
  +        private static Icon computeCountryIcon(Class ref,
  +                                               String code) {
  +            ImageIcon icon = null;
               try {
  +                if ((icon = (ImageIcon)iconMap.get(code)) != null)
  +                    return icon;
                   String s = resources.getString(code + ".icon");
  -                URL url  = getClass().getResource(s);
  +                URL url  = ref.getResource(s);
                   if (url != null) {
  -                    return new ImageIcon(url);
  +                    iconMap.put(code, icon = new ImageIcon(url));
  +                    return icon;
                   }
               } catch (MissingResourceException e) {
               }
  -            return new ImageIcon(getClass().getResource("resources/blank.gif"));
  +            return new ImageIcon(ref.getResource("resources/blank.gif"));
           }
   
           // ActionMap implementation ///////////////////////////////////////
  @@ -508,7 +550,7 @@
                   userList.getSelectionModel().clearSelection();
                   languageList.setSelectedIndex(i);
                   updateButtons();
  -            }   
  +            }
           }
   
           /**
  @@ -521,7 +563,7 @@
                   languageList.getSelectionModel().clearSelection();
                   userList.setSelectedIndex(i);
                   updateButtons();
  -            }   
  +            }
           }
   
           /**