diff dwtx/jface/resource/FileImageDescriptor.d @ 70:46a6e0e6ccd4

Merge with d-fied sources of 3.4M7
author Frank Benoit <benoit@tionex.de>
date Thu, 22 May 2008 01:36:46 +0200
parents ea8ff534f622
children 4878bef4a38e
line wrap: on
line diff
--- a/dwtx/jface/resource/FileImageDescriptor.d	Mon May 19 13:41:06 2008 +0200
+++ b/dwtx/jface/resource/FileImageDescriptor.d	Thu May 22 01:36:46 2008 +0200
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * Copyright (c) 2000, 2008 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -22,7 +22,10 @@
 
 import dwt.DWT;
 import dwt.DWTException;
+import dwt.graphics.Device;
+import dwt.graphics.Image;
 import dwt.graphics.ImageData;
+import dwtx.core.runtime.Path;
 
 import dwt.dwthelper.utils;
 import dwt.dwthelper.InputStream;
@@ -31,16 +34,16 @@
 import dwt.dwthelper.ByteArrayInputStream;
 
 import tango.util.log.Trace;
+import tango.text.convert.Format;
 
 /**
- * An image descriptor that loads its image information
- * from a file.
+ * An image descriptor that loads its image information from a file.
  */
 class FileImageDescriptor : ImageDescriptor {
 
     /**
-     * The class whose resource directory contain the file,
-     * or <code>null</code> if none.
+     * The class whose resource directory contain the file, or <code>null</code>
+     * if none.
      */
 //     private ClassInfo location;
 
@@ -51,18 +54,18 @@
     private void[] importdata;
 
     /**
-     * Creates a new file image descriptor.
-     * The file has the given file name and is located
-     * in the given class's resource directory. If the given
-     * class is <code>null</code>, the file name must be absolute.
+     * Creates a new file image descriptor. The file has the given file name and
+     * is located in the given class's resource directory. If the given class is
+     * <code>null</code>, the file name must be absolute.
      * <p>
-     * Note that the file is not accessed until its
-     * <code>getImageDate</code> method is called.
+     * Note that the file is not accessed until its <code>getImageDate</code>
+     * method is called.
      * </p>
-     *
-     * @param clazz class for resource directory, or
-     *   <code>null</code>
-     * @param filename the name of the file
+     * 
+     * @param clazz
+     *            class for resource directory, or <code>null</code>
+     * @param filename
+     *            the name of the file
      */
     this(ImportData importdata) {
 //         this.location = clazz;
@@ -70,8 +73,8 @@
         this.importdata = importdata.data;
     }
 
-    /* (non-Javadoc)
-     * Method declared on Object.
+    /*
+     * (non-Javadoc) Method declared on Object.
      */
     public override int opEquals(Object o) {
         if (!( cast(FileImageDescriptor)o )) {
@@ -90,9 +93,11 @@
         return importdata == other.importdata;
     }
 
-    /* (non-Javadoc)
-     * Method declared on ImageDesciptor.
-     * Returns null if the image data cannot be read.
+    /**
+     * @see dwtx.jface.resource.ImageDescriptor#getImageData() The
+     *      FileImageDescriptor implementation of this method is not used by
+     *      {@link ImageDescriptor#createImage(bool, Device)} as of version
+     *      3.4 so that the DWT OS optimised loading can be used.
      */
     public override ImageData getImageData() {
         InputStream in_ = getStream();
@@ -104,7 +109,7 @@
                 if (e.code !is DWT.ERROR_INVALID_IMAGE /+&& e.code !is DWT.ERROR_UNSUPPORTED_FORMAT+/) {
                     Trace.formatln( "FileImageDescriptor getImageData DWTException for name={}", name );
                     throw e;
-                // fall through otherwise
+                    // fall through otherwise
                 }
             } finally {
                 in_.close();
@@ -114,11 +119,11 @@
     }
 
     /**
-     * Returns a stream on the image contents.  Returns
-     * null if a stream could not be opened.
+     * Returns a stream on the image contents. Returns null if a stream could
+     * not be opened.
      *
-     * @return the buffered stream on the file or <code>null</code>
-     * if the file cannot be found
+     * @return the buffered stream on the file or <code>null</code> if the
+     *         file cannot be found
      */
     private InputStream getStream() {
         InputStream is_ = null;
@@ -137,13 +142,13 @@
 //         }
         if (is_ is null) {
             return null;
-        } else {
-            return new BufferedInputStream(is_);
         }
+        return new BufferedInputStream(is);
+
     }
 
-    /* (non-Javadoc)
-     * Method declared on Object.
+    /*
+     * (non-Javadoc) Method declared on Object.
      */
     public override hash_t toHash() {
         int code = dwt.dwthelper.utils.toHash(cast(char[])importdata/+name+/);
@@ -153,15 +158,77 @@
         return code;
     }
 
-    /* (non-Javadoc)
-     * Method declared on Object.
+    /*
+     * (non-Javadoc) Method declared on Object.
      */
     /**
-     * The <code>FileImageDescriptor</code> implementation of this <code>Object</code> method
-     * returns a string representation of this object which is suitable only for debugging.
+     * The <code>FileImageDescriptor</code> implementation of this
+     * <code>Object</code> method returns a string representation of this
+     * object which is suitable only for debugging.
      */
     public override String toString() {
-//         return "FileImageDescriptor(location=" ~ location.toString() ~ ", name=" ~ name ~ ")";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
-        return "FileImageDescriptor()";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
+        return Format("FileImageDescriptor(location={}, name={})", location, name );//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see dwtx.jface.resource.ImageDescriptor#createImage(bool,
+     *      dwt.graphics.Device)
+     */
+    public Image createImage(bool returnMissingImageOnError, Device device) {
+        String path = getFilePath();
+        if (path is null)
+            return createDefaultImage(returnMissingImageOnError, device);
+        try {           
+            return new Image(device, path);
+        } catch (DWTException exception) {
+            //if we fail try the default way using a stream
+        }
+        return super.createImage(returnMissingImageOnError, device);
+    }
+
+    /**
+     * Return default image if returnMissingImageOnError is true.
+     * 
+     * @param device
+     * @return Image or <code>null</code>
+     */
+    private Image createDefaultImage(bool returnMissingImageOnError,
+            Device device) {
+        try {
+            if (returnMissingImageOnError)
+                return new Image(device, DEFAULT_IMAGE_DATA);
+        } catch (DWTException nextException) {
+            return null;
+        }
+        return null;
+    }
+
+    /**
+     * Returns the filename for the ImageData.
+     * 
+     * @return {@link String} or <code>null</code> if the file cannot be found
+     */
+    private String getFilePath() {
+
+        if (location is null)
+            return new Path(name).toOSString();
+
+        URL resource = location.getResource(name);
+
+        if (resource is null)
+            return null;
+//      try {
+//          if (JFaceActivator.getBundleContext() is null) {// Stand-alone case
+//
+//              return new Path(resource.getFile()).toOSString();
+//          }
+//          return new Path(FileLocator.toFileURL(resource).getPath()).toOSString();
+            return null;
+//      } catch (IOException e) {
+//          Policy.logException(e);
+//          return null;
+//      }
     }
 }