From ba143aff264ef5fa76ec48d1cdbbfffa17e4a527 Mon Sep 17 00:00:00 2001 From: david Date: Fri, 30 Jan 2004 07:43:01 +0000 Subject: [PATCH] 2003/12/17 04:13:27 darcs-hash:20040130074301-0c9ea-229587e3c0bb093604e08f116092f7f7431c0a7c.gz --- src/org/xwt/Box.java | 26 ++++++++------------------ src/org/xwt/Picture.java | 24 +++++++++++++++++++++++- src/org/xwt/XWT.java | 2 ++ 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/org/xwt/Box.java b/src/org/xwt/Box.java index d6bb5f5..6f96cc2 100644 --- a/src/org/xwt/Box.java +++ b/src/org/xwt/Box.java @@ -132,7 +132,7 @@ public final class Box extends JSScope implements Scheduler.Task { private String text = null; private Font font = DEFAULT_FONT; - private Picture texture; + private Picture texture = null; private short strokewidth = 1; private int fillcolor = 0x00000000; private int strokecolor = 0xFF000000; @@ -168,7 +168,13 @@ public final class Box extends JSScope implements Scheduler.Task { /** invoked when a resource needed to render ourselves finishes loading */ - public void perform() { + public void perform() throws JSExn { + // as external events have occured, check the state of box + if (texture != null) { + if (texture.isLoaded) { minwidth = texture.width; minheight = texture.height; } + else { Res res = texture.res; texture = null; throw new JSExn("image not found: "+res); } + } + MARK_REPACK; MARK_REFLOW; MARK_RESIZE; @@ -572,23 +578,7 @@ public final class Box extends JSScope implements Scheduler.Task { } if (!(value instanceof Res)) return; - - // FIXME texture = Picture.load((Res)value, this); - if (texture.isLoaded) { - minwidth = texture.width; - minheight = texture.height; - MARK_REFLOW; - dirty(); - return; - } - texture = Picture.load((Res)value, new Scheduler.Task() { public void perform() { - // FIXME pass this instead of a new Task? - minwidth = texture.width; - minheight = texture.height; - Box b = Box.this; MARK_REFLOW_b; - dirty(); - } }); } private void mouseEvent(String name, Object value) { diff --git a/src/org/xwt/Picture.java b/src/org/xwt/Picture.java index f0eafec..2bffb65 100644 --- a/src/org/xwt/Picture.java +++ b/src/org/xwt/Picture.java @@ -33,8 +33,29 @@ public class Picture { final Picture p = ret; if (!ret.isLoaded && callback != null) new java.lang.Thread() { public void run() { + // get the InputStream for the image + InputStream in = null; + try { in = r.getInputStream(); } + catch (IOException e) { + in = null; + if (r instanceof Res.Ref) { + // add extensions to the resource, looking for the image + Res.Ref ref = (Res.Ref)r; + Res newr; + + String[] exts = new String[] { ".png", ".jpg", ".jpeg", ".gif" }; + for (int i=0; i < exts.length && in == null; i++) { + newr = ref.addExtension(exts[i]); + try { in = newr.getInputStream(); } + catch (IOException f) { in = null; } + } + } + } + + if (in == null) return; // could not find image + try { - PushbackInputStream pbis = new PushbackInputStream(r.getInputStream()); + PushbackInputStream pbis = new PushbackInputStream(in); int firstByte = pbis.read(); if (firstByte == -1) throw new JSExn("empty stream reading image"); pbis.unread(firstByte); @@ -48,6 +69,7 @@ public class Picture { Log.log(this, e); } } }.start(); + return ret; } } diff --git a/src/org/xwt/XWT.java b/src/org/xwt/XWT.java index 3f4cc54..fe306af 100644 --- a/src/org/xwt/XWT.java +++ b/src/org/xwt/XWT.java @@ -178,6 +178,8 @@ public final class XWT extends JS { break; } } catch (RuntimeException e) { + // FIXME: maybe JSExn should take a second argument, Exception + Log.log(this, "xwt."+name+"() threw: "+e); throw new JSExn("invalid argument for xwt object method "+name+"()"); } -- 1.7.10.4