Hi,

A long, long, time ago we had the following conversation:

On Mon, 2005-09-05 at 15:50 -0400, Thomas Fitzsimmons wrote:
> On Sat, 2005-09-03 at 21:56 +0200, Mark Wielaard wrote:
> > Is there a reason why the GtkToolkit createImage() methods only return
> > an ErrorImage on failure in the case of Graphics2D/BufferedImage
> > availability? This patch makes them behave similar:
> [...]
> Can you file a bug for the problem you're trying to fix?
> 
> I want to get rid of GtkErrorImage; rather it should just be a GtkImage
> with an error flag set.

Riccardo was so good to help me test some alternative patches. And this
is what we finally came up with:

2006-03-14  Mark Wielaard  <[EMAIL PROTECTED]>

    Fixes bug #23931
    * gnu/java/awt/peer/gtk/GtkImage.java (errorImage): New static
    field.
    (getErrorImage): New static method.
    * gnu/java/awt/peer/gtk/GtkToolkit.java (GtkErrorImage): Removed.
    (bufferedImageOrError): Renamed to ...
    (imageOrError): Renamed from bufferedImageOrError, takes Image.
    Returns GtkImage.getErrorImage() when argument null.
    (createImage(String)): Always use imageOrError.
    (createImage(URL)): Likewise.
    (createImage(ImageProducer)): Likewise.
    (createImage(byte[],int,int)): Likewise. 

See also the discussion in the bug report:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23931

OK to commit?

Cheers,

Mark
Index: gnu/java/awt/peer/gtk/GtkImage.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/awt/peer/gtk/GtkImage.java,v
retrieving revision 1.25
diff -u -r1.25 GtkImage.java
--- gnu/java/awt/peer/gtk/GtkImage.java	2 Sep 2005 06:36:57 -0000	1.25
+++ gnu/java/awt/peer/gtk/GtkImage.java	5 Feb 2006 16:53:37 -0000
@@ -1,5 +1,5 @@
 /* GtkImage.java
-   Copyright (C) 2005 Free Software Foundation, Inc.
+   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -329,6 +329,24 @@
     props = new Hashtable();
   }
 
+  // The singleton GtkImage that is returned on errors by GtkToolkit.
+  private static GtkImage errorImage;
+
+  /**
+   * Returns an empty GtkImage with the errorLoading flag set.
+   * Called from GtkToolKit when some error occured, but an image needs
+   * to be returned anyway.
+   */
+  static synchronized GtkImage getErrorImage()
+  {
+    if (errorImage == null)
+      {
+	errorImage = new GtkImage();
+	errorImage.errorLoading = true;
+      }
+    return errorImage;
+  }
+
   /**
    * Native helper function for constructor that takes a pixbuf Pointer.
    */
Index: gnu/java/awt/peer/gtk/GtkToolkit.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/awt/peer/gtk/GtkToolkit.java,v
retrieving revision 1.79
diff -u -r1.79 GtkToolkit.java
--- gnu/java/awt/peer/gtk/GtkToolkit.java	2 Sep 2005 09:15:22 -0000	1.79
+++ gnu/java/awt/peer/gtk/GtkToolkit.java	5 Feb 2006 16:53:37 -0000
@@ -1,5 +1,6 @@
 /* GtkToolkit.java -- Implements an AWT Toolkit using GTK for peers
-   Copyright (C) 1998, 1999, 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2002, 2003, 2004, 2005, 2006
+   Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -159,137 +160,93 @@
   }
 
   /** 
-   * A helper class to return to clients in cases where a BufferedImage is
-   * desired but its construction fails.
+   * Helper to return either a Image -- the argument -- or a
+   * GtkImage with the errorLoading flag set if the argument is null.
    */
-  private class GtkErrorImage extends Image
-  {
-    public GtkErrorImage()
-    {
-    }
-
-    public int getWidth(ImageObserver observer)
-    {
-      return -1;
-    }
-
-    public int getHeight(ImageObserver observer)
-    {
-      return -1;
-    }
-
-    public ImageProducer getSource()
-    {
-
-      return new ImageProducer() 
-        {          
-          HashSet consumers = new HashSet();          
-          public void addConsumer(ImageConsumer ic)
-          {
-            consumers.add(ic);
-          }
-
-          public boolean isConsumer(ImageConsumer ic)
-          {
-            return consumers.contains(ic);
-          }
-
-          public void removeConsumer(ImageConsumer ic)
-          {
-            consumers.remove(ic);
-          }
-
-          public void startProduction(ImageConsumer ic)
-          {
-            consumers.add(ic);
-            Iterator i = consumers.iterator();
-            while(i.hasNext())
-              {
-                ImageConsumer c = (ImageConsumer) i.next();
-                c.imageComplete(ImageConsumer.IMAGEERROR);
-              }
-          }
-          public void requestTopDownLeftRightResend(ImageConsumer ic)
-          {
-            startProduction(ic);
-          }        
-        };
-    }
-
-    public Graphics getGraphics() 
-    { 
-      return null; 
-    }
-
-    public Object getProperty(String name, ImageObserver observer)
-    {
-      return null;
-    }
-    public Image getScaledInstance(int width, int height, int flags)
-    {
-      return new GtkErrorImage();
-    }
-
-    public void flush() 
-    {
-    }
-  }
-
-
-  /** 
-   * Helper to return either a BufferedImage -- the argument -- or a
-   * GtkErrorImage if the argument is null.
-   */
-
-  private Image bufferedImageOrError(BufferedImage b)
+  private Image imageOrError(Image b)
   {
     if (b == null) 
-      return new GtkErrorImage();
+      return GtkImage.getErrorImage();
     else
       return b;
   }
 
-
   public Image createImage (String filename)
   {
     if (filename.length() == 0)
       return new GtkImage ();
-
-    if (useGraphics2D())
-      return bufferedImageOrError(GdkPixbufDecoder.createBufferedImage (filename));
-    else
-      return new GtkImage (filename);
+    
+    Image image;
+    try
+      {
+	if (useGraphics2D())
+	  image = GdkPixbufDecoder.createBufferedImage(filename);
+	else
+	  image = new GtkImage(filename);
+      }
+    catch (IllegalArgumentException iae)
+      {
+	image = null;
+      }
+    return imageOrError(image);
   }
 
   public Image createImage (URL url)
   {
-    if (useGraphics2D())
-      return bufferedImageOrError(GdkPixbufDecoder.createBufferedImage (url));
-    else
-      return new GtkImage (url);
+    Image image;
+    try
+      {
+	if (useGraphics2D())
+	  image = GdkPixbufDecoder.createBufferedImage(url);
+	else
+	  image = new GtkImage(url);
+      }
+    catch (IllegalArgumentException iae)
+      {
+	image = null;
+      }
+    return imageOrError(image);
   }
 
   public Image createImage (ImageProducer producer) 
   {
-    if (useGraphics2D())
-      return bufferedImageOrError(GdkPixbufDecoder.createBufferedImage (producer));
-    else
-      return new GtkImage (producer);
+    Image image;
+    try
+      {
+	if (useGraphics2D())
+	  image = GdkPixbufDecoder.createBufferedImage(producer);
+	else
+	  image = new GtkImage(producer);
+      }
+    catch (IllegalArgumentException iae)
+      {
+	image = null;
+      }
+    return imageOrError(image);
   }
 
   public Image createImage (byte[] imagedata, int imageoffset,
 			    int imagelength)
   {
-    if (useGraphics2D())
-      return bufferedImageOrError(GdkPixbufDecoder.createBufferedImage (imagedata,
-                                                   imageoffset, 
-                                                                        imagelength));
-    else
+    Image image;
+    try
+      {
+	if (useGraphics2D())
+	  image = GdkPixbufDecoder.createBufferedImage(imagedata,
+						       imageoffset, 
+						       imagelength);
+	else
+	  {
+	    byte[] datacopy = new byte[imagelength];
+	    System.arraycopy(imagedata, imageoffset, datacopy, 0, imagelength);
+	    return new GtkImage(datacopy);
+	  }
+      }
+    catch (IllegalArgumentException iae)
       {
-        byte[] datacopy = new byte[imagelength];
-        System.arraycopy (imagedata, imageoffset, datacopy, 0, imagelength);
-        return new GtkImage (datacopy);
+	image = null;
       }
+    return imageOrError(image);
   }
   
   /**

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to