FileDocCategorySizeDatePackage
ImageViewer.javaAPI DocGlassfish v2 API4217Mon May 14 15:29:52 BST 2007com.sun.activation.viewers

ImageViewer

public class ImageViewer extends Panel implements CommandObject

Fields Summary
private ImageViewerCanvas
canvas
private Image
image
private DataHandler
_dh
private boolean
DEBUG
Constructors Summary
public ImageViewer()
Constructor

          
     
	
	// create the ImageViewerCanvas
	canvas = new ImageViewerCanvas();
	add(canvas);
    
Methods Summary
public voidaddNotify()

	super.addNotify(); // call the real one first...
	this.invalidate();
	this.validate();
	this.doLayout();
    
public java.awt.DimensiongetPreferredSize()

	return canvas.getPreferredSize();
    
public voidsetCommandContext(java.lang.String verb, javax.activation.DataHandler dh)
Set the DataHandler for this CommandObject

param
DataHandler the DataHandler

	_dh = dh;
	this.setInputStream( _dh.getInputStream() );
    
private voidsetInputStream(java.io.InputStream ins)
Set the data stream, component to assume it is ready to be read.

	MediaTracker mt = new MediaTracker(this);
	int bytes_read = 0;
	byte data[] = new byte[1024];
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	
	while((bytes_read = ins.read(data)) >0)
	    baos.write(data, 0, bytes_read);
	ins.close();
	
	// convert the buffer into an image
	image = getToolkit().createImage(baos.toByteArray());
	
	mt.addImage(image, 0);
	
	try {
	    mt.waitForID(0);
	    mt.waitForAll();
	    if(mt.statusID(0, true ) != MediaTracker.COMPLETE){
		System.out.println("Error occured in image loading = " +
				   mt.getErrorsID(0));
		
	    }
	    
	}
	catch(InterruptedException e) {
	    throw new IOException("Error reading image data");
	}
	
	canvas.setImage(image);
	if(DEBUG)
	    System.out.println("calling invalidate");