/* SimpleImageScroll.java * * Copyright (c) 2001, Ted Nelson and Tuomas Lukka * * This file is part of Gzz. * * Gzz is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Gzz is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Gzz; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Tuomas Lukka */ package org.nongnu.alph.impl; import org.nongnu.alph.*; import org.nongnu.storm.*; import java.io.IOException; import java.awt.*; import java.awt.image.*; /** An image scrollblock */ public class SimpleImageScroll implements ScrollBlockManager.MediaserverScrollBlock { String rcsid = "$Id: SimpleImageScroll.java,v 1.1 2003/03/25 14:36:12 tjl Exp $"; final static String DIR="../mstmpimg/"; // XXX Platform dependent! Image im; int width, height; Mediaserver ms; Mediaserver.Id id; public SimpleImageScroll(Mediaserver.Id 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 ); } public SimpleImageScroll(Mediaserver ms, Mediaserver.Id id) { this.ms = ms; this.id = id; } public String getID() { return id.getString(); } public Mediaserver.Id saveOrGetId(Mediaserver ms) { return id; } public boolean equals(Object o) { if(!(o instanceof ScrollBlock)) return false; ScrollBlock sb = (ScrollBlock)o; return sb.getID().equals(getID()); } public int hashCode() { return getID().hashCode(); } public String imageFilename() { return new String(DIR+getID()); } private void loadImage() { // Background.getDefaultInstance().addTask( Runnable r = ( new Runnable() { public void run() { MediaserverBlock block ; String ct; try { block = ms.getDatum(id); ct = block.getContentType(); } catch(IOException e) { throw new Error("Couldn't load image block"); } if(!ct.substring(0,ct.indexOf('/')).equals("image")) throw new Error("Block isn't an image"); Image img = null; try { img = java.awt.Toolkit.getDefaultToolkit().createImage(block.getBytes()); } catch (IOException _) {} if(img == null) throw new Error("Unknown image type"); 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); } }); } class SimpleImageSpan extends ScrollBlockManager.ImageSpanBase implements Runnable{ SimpleImageSpan(int x, int y, int w, int h) { super(SimpleImageScroll.this, x, y, w, h); } protected ScrollBlockManager.ImageSpanBase createNew(int x, int y, int w, int h) { return new SimpleImageSpan(x, y, w, h); } 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)); } } public Span getCurrent() { if(im == null) loadImage(); return new SimpleImageSpan(0, 0, width, height); } public Span getSpan(int x, int y, int w, int h) { return new SimpleImageSpan(x, y, w, h); } public boolean isFinalized() { return true; } }