diff dwt/graphics/ImageLoader.d @ 32:b9226997409c

Ported dwt.graphics.Image*
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Fri, 12 Sep 2008 13:44:30 +0200
parents e831403a80a9
children d8635bb48c7c
line wrap: on
line diff
--- a/dwt/graphics/ImageLoader.d	Fri Sep 12 12:36:09 2008 +0200
+++ b/dwt/graphics/ImageLoader.d	Fri Sep 12 13:44:30 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
@@ -7,22 +7,25 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *     Jacob Carlborg <jacob.carlborg@gmail.com>
  *******************************************************************************/
 module dwt.graphics.ImageLoader;
 
-import dwt.dwthelper.utils;
-
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.Vector;
 
 import dwt.DWT;
-import dwt.DWTException;
 import dwt.internal.Compatibility;
 import dwt.internal.image.FileFormat;
 
+import tango.core.Exception;
+import tango.core.Array;
+
+import dwt.dwthelper.utils;
+public import dwt.graphics.ImageData;
+public import dwt.graphics.ImageLoaderEvent;
+public import dwt.graphics.ImageLoaderListener;
+
 /**
  * Instances of this class are used to load images from,
  * and save images to, a file or stream.
@@ -44,17 +47,20 @@
  * <li>load interlaced GIF/PNG images</li>
  * <li>load progressive JPEG images</li>
  * </ul>
+ *
+ * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ImageAnalyzer</a>
+ * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
  */
- 
+
 public class ImageLoader {
-    
+
     /**
      * the array of ImageData objects in this ImageLoader.
      * This array is read in when the load method is called,
      * and it is written out when the save method is called
      */
     public ImageData[] data;
-    
+
     /**
      * the width of the logical screen on which the images
      * reside, in pixels (this corresponds to the GIF89a
@@ -70,10 +76,10 @@
     public int logicalScreenHeight;
 
     /**
-     * the background pixel for the logical screen (this 
+     * the background pixel for the logical screen (this
      * corresponds to the GIF89a Background Color Index value).
      * The default is -1 which means 'unspecified background'
-     * 
+     *
      */
     public int backgroundPixel;
 
@@ -84,11 +90,11 @@
      * The default is 1. A value of 0 means 'display repeatedly'
      */
     public int repeatCount;
-        
+
     /*
      * the set of ImageLoader event listeners, created on demand
      */
-    Vector imageLoaderListeners;
+    ImageLoaderListener[] imageLoaderListeners;
 
 /**
  * Construct a new empty ImageLoader.
@@ -155,17 +161,20 @@
 public ImageData[] load(String filename) {
     if (filename is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
     InputStream stream = null;
+    void close(){
+        try {
+            if( stream !is null ) stream.close();
+        } catch (IOException e) {
+            // Ignore error
+        }
+    }
     try {
         stream = Compatibility.newFileInputStream(filename);
+        scope(exit) close();
+
         return load(stream);
     } catch (IOException e) {
         DWT.error(DWT.ERROR_IO, e);
-    } finally {
-        try {
-            if (stream !is null) stream.close();
-        } catch (IOException e) {
-            // Ignore error
-        }
     }
     return null;
 }
@@ -250,52 +259,49 @@
     }
 }
 
-/**  
+/**
  * Adds the listener to the collection of listeners who will be
  * notified when image data is either partially or completely loaded.
  * <p>
  * An ImageLoaderListener should be added before invoking
- * one of the receiver's load methods. The listener's 
+ * one of the receiver's load methods. The listener's
  * <code>imageDataLoaded</code> method is called when image
  * data has been partially loaded, as is supported by interlaced
  * GIF/PNG or progressive JPEG images.
  *
  * @param listener the listener which should be notified
- * 
+ *
  * @exception IllegalArgumentException <ul>
  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
  * </ul>
- * 
+ *
  * @see ImageLoaderListener
  * @see ImageLoaderEvent
  */
 public void addImageLoaderListener(ImageLoaderListener listener) {
     if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
-    if (imageLoaderListeners is null) {
-        imageLoaderListeners = new Vector();
-    }
-    imageLoaderListeners.addElement(listener);
+    imageLoaderListeners ~= listener;
 }
 
-/**  
+/**
  * Removes the listener from the collection of listeners who will be
  * notified when image data is either partially or completely loaded.
  *
  * @param listener the listener which should no longer be notified
- * 
+ *
  * @exception IllegalArgumentException <ul>
  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
  * </ul>
- * 
+ *
  * @see #addImageLoaderListener(ImageLoaderListener)
  */
 public void removeImageLoaderListener(ImageLoaderListener listener) {
     if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
-    if (imageLoaderListeners is null) return;
-    imageLoaderListeners.removeElement(listener);
+    if (imageLoaderListeners.length is 0 ) return;
+    tango.core.Array.remove( imageLoaderListeners, listener, delegate bool(ImageLoaderListener l1, ImageLoaderListener l2 ){ return l1 is l2; });
 }
 
-/**  
+/**
  * Returns <code>true</code> if the receiver has image loader
  * listeners, and <code>false</code> otherwise.
  *
@@ -305,10 +311,10 @@
  * @see #removeImageLoaderListener(ImageLoaderListener)
  */
 public bool hasListeners() {
-    return imageLoaderListeners !is null && imageLoaderListeners.size() > 0;
+    return imageLoaderListeners.length > 0;
 }
 
-/**  
+/**
  * Notifies all image loader listeners that an image loader event
  * has occurred. Pass the specified event object to each listener.
  *
@@ -316,9 +322,7 @@
  */
 public void notifyListeners(ImageLoaderEvent event) {
     if (!hasListeners()) return;
-    int size = imageLoaderListeners.size();
-    for (int i = 0; i < size; i++) {
-        ImageLoaderListener listener = cast(ImageLoaderListener) imageLoaderListeners.elementAt(i);
+    foreach( listener; imageLoaderListeners ){
         listener.imageDataLoaded(event);
     }
 }