vhardy      02/05/02 02:24:16

  Modified:    sources/org/apache/batik/apps/svgbrowser
                        JSVGViewerFrame.java
               resources/org/apache/batik/apps/svgbrowser/resources
                        GUI.properties
  Added:       sources/org/apache/batik/apps/svgbrowser
                        WindowsAltFileSystemView.java
  Log:
  Implemented work around bug on Windows platform which caused an annoying dialog to 
be displayed when opening the JFileChooser and a SecurityManager was active
  
  Revision  Changes    Path
  1.76      +32 -2     
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.75
  retrieving revision 1.76
  diff -u -r1.75 -r1.76
  --- JSVGViewerFrame.java      30 Apr 2002 08:45:14 -0000      1.75
  +++ JSVGViewerFrame.java      2 May 2002 09:24:16 -0000       1.76
  @@ -165,7 +165,7 @@
    * This class represents a SVG viewer swing frame.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Stephane Hillion</a>
  - * @version $Id: JSVGViewerFrame.java,v 1.75 2002/04/30 08:45:14 vhardy Exp $
  + * @version $Id: JSVGViewerFrame.java,v 1.76 2002/05/02 09:24:16 vhardy Exp $
    */
   public class JSVGViewerFrame
       extends    JFrame
  @@ -228,6 +228,25 @@
           new Cursor(Cursor.DEFAULT_CURSOR);
   
       /**
  +     * Name for the os-name property
  +     */
  +    public final static String PROPERTY_OS_NAME 
  +        = Resources.getString("JSVGViewerFrame.property.os.name");
  +
  +    /**
  +     * Name for the os.name default
  +     */
  +    public final static String PROPERTY_OS_NAME_DEFAULT 
  +        = Resources.getString("JSVGViewerFrame.property.os.name.default");
  +
  +    /**
  +     * Name for the os.name property prefix we are looking
  +     * for in OpenAction to work around JFileChooser bug
  +     */
  +    public final static String PROPERTY_OS_WINDOWS_PREFIX 
  +        = Resources.getString("JSVGViewerFrame.property.os.windows.prefix");
  +
  +    /**
        * The resource bundle
        */
       protected static ResourceBundle bundle;
  @@ -702,7 +721,18 @@
           public OpenAction() {
           }
           public void actionPerformed(ActionEvent e) {
  -            JFileChooser fileChooser = new JFileChooser(makeAbsolute(currentPath));
  +            JFileChooser fileChooser = null;
  +
  +            // Apply work around Windows problem when security is enabled 
  +            String os = System.getProperty(PROPERTY_OS_NAME, 
PROPERTY_OS_NAME_DEFAULT);
  +            SecurityManager sm = System.getSecurityManager();
  +            if ( sm != null && os.indexOf(PROPERTY_OS_WINDOWS_PREFIX) != -1 ){
  +                fileChooser = new JFileChooser(makeAbsolute(currentPath),
  +                                               new WindowsAltFileSystemView());
  +            } else {
  +                fileChooser = new JFileChooser(makeAbsolute(currentPath));
  +            }
  +
               fileChooser.setFileHidingEnabled(false);
               fileChooser.setFileSelectionMode
                   (JFileChooser.FILES_ONLY);
  
  
  
  1.1                  
xml-batik/sources/org/apache/batik/apps/svgbrowser/WindowsAltFileSystemView.java
  
  Index: WindowsAltFileSystemView.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.io.File;
  import java.io.IOException;
  import java.lang.reflect.Method;
  import java.util.Vector;
  import javax.swing.filechooser.FileSystemView;
  
  /**
   * Work around FileSystemView implementation bug on the Windows 
   * platform. See:
   *
   * <a href="http://forums.java.sun.com/thread.jsp?forum=38&thread=71491";>
   * Using JFileChooser in WebStart-deployed application</a>
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Hardy</a>
   * @version $Id: WindowsAltFileSystemView.java,v 1.1 2002/05/02 09:24:16 vhardy Exp 
$
   */
  
  // This class is necessary due to an annoying bug on Windows NT where
  // instantiating a JFileChooser with the default FileSystemView will
  // cause a "drive A: not ready" error every time. I grabbed the
  // Windows FileSystemView impl from the 1.3 SDK and modified it so
  // as to not use java.io.File.listRoots() to get fileSystem roots.
  // java.io.File.listRoots() does a SecurityManager.checkRead() which
  // causes the OS to try to access drive A: even when there is no disk,
  // causing an annoying "abort, retry, ignore" popup message every time
  // we instantiate a JFileChooser!
  //
  // Instead of calling listRoots() we use a straightforward alternate
  // method of getting file system roots.
  
  class WindowsAltFileSystemView extends FileSystemView {
      public static final String EXCEPTION_CONTAINING_DIR_NULL
          = "AltFileSystemView.exception.containing.dir.null";
  
      public static final String EXCEPTION_DIRECTORY_ALREADY_EXISTS
          = "AltFileSystemView.exception.directory.already.exists";
  
      public static final String NEW_FOLDER_NAME = 
          " AltFileSystemView.new.folder.name";
  
      public static final String FLOPPY_DRIVE = 
          "AltFileSystemView.floppy.drive";
  
      private static final Object[] noArgs = {};
      private static final Class[] noArgTypes = {};
      
      private static Method listRootsMethod = null;
      private static boolean listRootsMethodChecked = false;
      
      /**
       * Returns true if the given file is a root.
       */
      public boolean isRoot(File f) {
          if(!f.isAbsolute()) {
              return false;
          }
          
          String parentPath = f.getParent();
          if(parentPath == null) {
              return true;
          } else {
              File parent = new File(parentPath);
              return parent.equals(f);
          }
      }
      
      /**
       * creates a new folder with a default folder name.
       */
      public File createNewFolder(File containingDir) throws
          IOException {
          if(containingDir == null) {
              throw new 
IOException(Resources.getString(EXCEPTION_CONTAINING_DIR_NULL));
          }
          File newFolder = null;
          // Using NT's default folder name
          newFolder = createFileObject(containingDir, 
                                       Resources.getString(NEW_FOLDER_NAME));
          int i = 2;
          while (newFolder.exists() && (i < 100)) {
              newFolder = createFileObject
                  (containingDir, Resources.getString(NEW_FOLDER_NAME) + " (" + i + 
")");
              i++;
          }
          
          if(newFolder.exists()) {
              throw new IOException
                  (Resources.formatMessage(EXCEPTION_DIRECTORY_ALREADY_EXISTS,
                                           new 
Object[]{newFolder.getAbsolutePath()}));
          } else {
              newFolder.mkdirs();
          }
          
          return newFolder;
      }
      
      /**
       * Returns whether a file is hidden or not. On Windows
       * there is currently no way to get this information from
       * io.File, therefore always return false.
       */
      public boolean isHiddenFile(File f) {
          return false;
      }
      
      /**
       * Returns all root partitians on this system. On Windows, this
       * will be the A: through Z: drives.
       */
      public File[] getRoots() {
          
          Vector rootsVector = new Vector();
          
          // Create the A: drive whether it is mounted or not
          FileSystemRoot floppy = new FileSystemRoot(Resources.getString(FLOPPY_DRIVE)
                                                     + "\\");
          rootsVector.addElement(floppy);
          
          // Run through all possible mount points and check
          // for their existance.
          for (char c = 'C'; c <= 'Z'; c++) {
              char device[] = {c, ':', '\\'};
              String deviceName = new String(device);
              File deviceFile = new FileSystemRoot(deviceName);
              if (deviceFile != null && deviceFile.exists()) {
                  rootsVector.addElement(deviceFile);
              }
          }
          File[] roots = new File[rootsVector.size()];
          rootsVector.copyInto(roots);
          return roots;
      }
      
      class FileSystemRoot extends File {
          public FileSystemRoot(File f) {
              super(f, "");
          }
          
          public FileSystemRoot(String s) {
              super(s);
          }
          
          public boolean isDirectory() {
              return true;
          }
      }
      
  }
  
  
  
  
  1.46      +20 -1     
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.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- GUI.properties    30 Apr 2002 08:45:14 -0000      1.45
  +++ GUI.properties    2 May 2002 09:24:16 -0000       1.46
  @@ -9,7 +9,7 @@
   # The viewer's GUI resources.
   #
   # Author: [EMAIL PROTECTED]
  -# $Id: GUI.properties,v 1.45 2002/04/30 08:45:14 vhardy Exp $
  +# $Id: GUI.properties,v 1.46 2002/05/02 09:24:16 vhardy Exp $
   #
   
   ViewSource.width = 750
  @@ -377,6 +377,12 @@
   ToolStop.action    = StopAction
   ToolStop.tooltip   = Stop the Processing of the SVG Document.
   
  +#
  +# JSVGViewerFrame constants
  +#
  +JSVGViewerFrame.property.os.name=os.name
  +JSVGViewerFrame.property.os.name.default=Solaris
  +JSVGViewerFrame.property.os.windows.prefix=Windows
   
   #
   # PreferenceDialog parameters
  @@ -488,6 +494,19 @@
   The user preference indicated that secure script execution should be enforced. \
   However, the Squiggle policy file could not be found, which causes this \
   exception.  
  +
  +#
  +# AltFileSystemView
  +#
  +AltFileSystemView.exception.containing.dir.null = \
  +Containing directory is null
  +
  +AltFileSystemView.exception.directory.already.exists = \
  +Directory already exists: {0}
  +
  +AltFileSystemView.new.folder.name = New Folder
  +
  +AltFileSystemView.floppy.drive = A:
   
   #
   # JPEGOptionPanel
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to