38 |
/** An image scrollblock |
/** An image scrollblock |
39 |
*/ |
*/ |
40 |
|
|
41 |
public class SimpleImageScroll |
public class SimpleImageScroll extends AbstractScrollBlock { |
|
implements ScrollBlockManager.MediaserverScrollBlock { |
|
42 |
String rcsid = "$Id$"; |
String rcsid = "$Id$"; |
43 |
|
|
44 |
final static String DIR="../mstmpimg/"; // XXX Platform dependent! |
int width=-1, height=-1; |
|
|
|
|
Image im; |
|
|
int width, height; |
|
|
|
|
|
StormPool ms; |
|
|
BlockId id; |
|
|
|
|
|
public SimpleImageScroll(BlockId id, Image im, int width, int height) { |
|
|
this.id = id; |
|
|
this.im = im; |
|
|
this.width = width; |
|
|
this.height = height; |
|
|
} |
|
|
|
|
|
public SimpleImageScroll(Image im, int width, int height) { |
|
|
this( null, im, width, height ); |
|
|
} |
|
45 |
|
|
46 |
public SimpleImageScroll(StormPool ms, BlockId id) { |
public SimpleImageScroll(StormPool ms, BlockId id) { |
47 |
this.ms = ms; |
super(ms, id); |
48 |
this.id = id; |
try { |
49 |
|
ms.request(id, null); |
50 |
|
} catch(IOException e) { |
51 |
|
throw new Error("Couldn't load image block"); |
52 |
|
} |
53 |
} |
} |
54 |
|
|
|
public String getID() { return id.toString(); } |
|
|
public BlockId saveOrGetId(StormPool ms) { return id; } |
|
|
|
|
55 |
public boolean equals(Object o) { |
public boolean equals(Object o) { |
56 |
if(!(o instanceof ScrollBlock)) return false; |
if(!(o instanceof ScrollBlock)) return false; |
57 |
ScrollBlock sb = (ScrollBlock)o; |
ScrollBlock sb = (ScrollBlock)o; |
61 |
return getID().hashCode(); |
return getID().hashCode(); |
62 |
} |
} |
63 |
|
|
64 |
public String imageFilename() { |
private void loadImageSize() { |
|
return new String(DIR+getID()); |
|
|
} |
|
|
|
|
|
private void loadImage() { |
|
|
// Background.getDefaultInstance().addTask( |
|
65 |
|
|
66 |
Runnable r = ( new Runnable() { |
String ct = msid.getContentType(); |
67 |
public void run() { |
|
68 |
Block block ; |
if(!ct.substring(0,ct.indexOf('/')).equals("image")) |
69 |
String ct; |
throw new Error("Block isn't an image"); |
70 |
try { |
|
71 |
block = ms.request(id, null); |
Block block ; |
72 |
ct = id.getContentType(); |
Dimension d; |
73 |
} catch(IOException e) { |
try { |
74 |
throw new Error("Couldn't load image block"); |
block = ms.get(msid); |
75 |
} |
java.io.InputStream is = block.getInputStream(); |
76 |
|
d = org.nongnu.navidoc.util.ImageSize.readSize(is); |
77 |
if(!ct.substring(0,ct.indexOf('/')).equals("image")) |
if(d == null) |
78 |
throw new Error("Block isn't an image"); |
throw new Error("Couldn't get size of image block"); |
79 |
|
is.close(); |
80 |
Image img = null; |
|
81 |
try { |
} catch(Exception e) { |
82 |
img = java.awt.Toolkit.getDefaultToolkit().createImage( |
throw new Error("Couldn't load image block"); |
83 |
SlurpStream.slurp(block.getInputStream())); |
} |
84 |
} catch (IOException _) {} |
|
85 |
if(img == null) |
this.width = d.width; |
86 |
throw new Error("Unknown image type"); |
this.height = d.height; |
|
int count = 0; |
|
|
while(img.getWidth(null) < 0 || img.getHeight(null) < 0) { |
|
|
try { |
|
|
count++; |
|
|
if(count > 100) |
|
|
throw new Error("Timeout"); |
|
|
Thread.sleep(200); |
|
|
} catch(InterruptedException e) { |
|
|
throw new Error("Interrupted"); |
|
|
} |
|
|
} |
|
|
SimpleImageScroll.this.im = img; |
|
|
SimpleImageScroll.this.width = img.getWidth(null); |
|
|
SimpleImageScroll.this.height = img.getHeight(null); |
|
|
} |
|
|
}); |
|
87 |
} |
} |
88 |
|
|
89 |
class SimpleImageSpan extends ScrollBlockManager.ImageSpanBase implements Runnable{ |
class SimpleImageSpan extends ScrollBlockManager.ImageSpanBase { |
90 |
SimpleImageSpan(int x, int y, int w, int h) { |
SimpleImageSpan(int x, int y, int w, int h) { |
91 |
super(SimpleImageScroll.this, x, y, w, h); |
super(SimpleImageScroll.this, x, y, w, h); |
92 |
} |
} |
96 |
return new SimpleImageSpan(x, y, w, h); |
return new SimpleImageSpan(x, y, w, h); |
97 |
} |
} |
98 |
|
|
|
Image cached; |
|
|
|
|
|
public Image getImage() { |
|
|
if(cached != null) return cached; |
|
|
if(im == null) { |
|
|
loadImage(); |
|
|
return null; |
|
|
} |
|
|
if(x==0 && y==0 && w==width && h==height) |
|
|
return im; |
|
|
|
|
|
// Background.getDefaultInstance().addTask(this); |
|
|
return null; |
|
|
} |
|
|
|
|
|
public void run() { |
|
|
if(cached != null) return; |
|
|
|
|
|
ImageFilter filter = new CropImageFilter(x,y,w,h); |
|
|
cached = java.awt.Toolkit.getDefaultToolkit().createImage |
|
|
(new FilteredImageSource(im.getSource(), filter)); |
|
|
} |
|
99 |
} |
} |
100 |
|
|
101 |
public Span getCurrent() { |
public Span getCurrent() { |
102 |
if(im == null) loadImage(); |
if(width < 0) loadImageSize(); |
103 |
return new SimpleImageSpan(0, 0, width, height); |
return new SimpleImageSpan(0, 0, width, height); |
104 |
} |
} |
105 |
|
|
108 |
} |
} |
109 |
|
|
110 |
|
|
|
public boolean isFinalized() { return true; } |
|
111 |
} |
} |
112 |
|
|
113 |
|
|