home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 16.9 KB | 615 lines |
- /*
- * @(#ImageViewer.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
-
-
- package symantec.itools.db.awt;
-
- import java.applet.*;
- import java.io.*;
- import java.net.*;
- import java.awt.*;
- import java.util.*;
- import java.sql.*;
- import symantec.itools.net.*;
- import symantec.itools.db.pro.*;
- import symantec.itools.db.beans.binding.Name;
-
- /**
- * A dbAWARE component that displays an image.
- * <p>
- * This component can be "bound" to a projection within a relation view
- * so that it automatically gets the image to display from the database.
- * <p>
- */
- public class ImageViewer extends Canvas implements ProjectionBean
- {
- Object m_Value = null;
- boolean m_IsAdded = false;
- Image m_Image = null;
- int m_ImageType = StreamImageSource.JPEG;
- byte[] m_Data = null;
- ByteArrayInputStream m_InputStream = null;
- ByteArrayOutputStream m_OutputStream = null;
-
- private boolean m_displayWhileLoading = false;
- private boolean m_IsLoaded = false;
-
- private ProjectionBeanHelper m_Helper;
-
- /**
- * The exception message when the data stored in the data base can't be converted
- * into a image file
- */
- private final String INVALID_IMAGE_DATA= "There is not a valid image data";
-
- /**
- * Constructs a default ImageViewer.
- * By default this viewer will not display images while they are loading.
- */
- public ImageViewer() {
- setIsAdded(false);
- m_Helper = new ProjectionBeanHelper(this);
- }
-
- /**
- * Constructs an ImageViewer and specifies the default URL that security
- * checks are performed against.
- * By default this viewer will not display images while they are loading.
- *
- * @param base the default URL that security checks are performed against
- *
- * @see symantec.itools.db.awt.StreamImageSource
- */
- public ImageViewer (URL base) {
- this();
- setBaseUrl(base);
- }
-
-
- /**
- * Sets the default URL that security checks are performed against.
- * @param base the default URL that security checks are performed against
- *
- * @see symantec.itools.db.awt.StreamImageSource
- */
- public static void setBaseUrl(URL base)
- {
- StreamImageSource.setBaseUrl(base);
- }
-
- /**
- * Tells this component that it has been added to a container.
- * <p>
- * This is a standard Java AWT method which gets called by the AWT when
- * this component is added to a container. Typically, it is used to
- * create this component's peer.
- * <p>
- * It is overridden here to set the StreamImage base URL if within an applet.
- *
- * @see java.awt.Component#removeNotify
- */
- public void addNotify() {
- super.addNotify();
- Component parent = null;
- Applet app = null;
-
- parent = this.getParent();
-
- while (parent != null && parent.getParent() != null) {
- if (parent instanceof Applet) {
- break;
- }
-
- parent = parent.getParent();
- }
-
- if (parent instanceof Applet) {
- app = (Applet) parent;
- StreamImageSource.setBaseUrl(app.getCodeBase());
- }
- setIsAdded(true);
- if (m_Value != null) {
- setData(m_Value);
- }
- }
-
- /**
- * Gets this component's <code>Image</code>.
- * @return the image
- * @see #setImage
- */
- protected Image getImage()
- {
- return m_Image;
- }
-
- /**
- * Sets this component's <code>Image</code>.
- * @param image the new image
- * @see #getImage
- */
- protected void setImage(Image image)
- {
- m_Image = image;
- }
-
- /**
- * Gets the type of image loaded.
- * Currently only GIF and JPEG are supported.
- *
- * @return one of StreamImageSource.JPEG, or StreamImageSource.GIF
- *
- * @see #setImageType
- * @see symantec.itools.db.awt.StreamImageSource#JPEG
- * @see symantec.itools.db.awt.StreamImageSource#GIF
- */
- public int getImageType()
- {
- return m_ImageType;
- }
-
- /**
- * Sets the type of image loaded.
- * Currently only GIF and JPEG are supported.
- *
- * @param type one of StreamImageSource.JPEG, or StreamImageSource.GIF
- *
- * @see #setImageType(byte[])
- * @see #getImageType
- * @see symantec.itools.db.awt.StreamImageSource#JPEG
- * @see symantec.itools.db.awt.StreamImageSource#GIF
- */
- public void setImageType(int type)
- {
- m_ImageType = type;
- }
-
- /**
- * Sets the type of image loaded.
- * Currently only GIF and JPEG are supported.
- *
- * @param data one of "JPG" or "GIF"
- *
- * @see #setImageType(int)
- * @see #getImageType
- */
- protected void setImageType(byte[] data)
- {
- byte buf[] = new byte[3];
- System.arraycopy(data, 0, buf, 0, 3);
- String typeName = new String(buf, 0);
- int type = StreamImageSource.JPEG;
- if (typeName.equals("GIF")) {
- type = StreamImageSource.GIF;
- }
- setImageType(type);
- }
-
- /**
- * Determines whether the image is completely loaded or not.
- * @return true if the image is completely loaded
- */
- public boolean isLoaded()
- {
- return m_IsLoaded;
- }
-
- /**
- * Sets the "is loaded" flag.
- * @param isLoaded the new flag value
- * @see #isLoaded
- */
- protected void setIsLoaded(boolean isLoaded)
- {
- m_IsLoaded = isLoaded;
- }
-
- /**
- * Loads and builds the image at the specified URL then returns an
- * input stream of its image data.
- *
- * @param s the URL string
- * @return the image data stream
- * @exception MalformedURLException
- * if the URL string is invalid
- */
- public ByteArrayInputStream fetchImage(String s)
- throws MalformedURLException
- {
- URL url = new URL(s);
- return fetchImage(url);
- }
-
- /**
- * Helper function for fetchImage() which retrieves the data for an image
- * whose URL is in http format.
- * @param url the URL
- * @return the loaded image data
- * @exception MalformedURLException
- * if the URL is invalid
- * @see #fetchImage
- */
- public ByteArrayInputStream fetchHTTP(URL url)
- throws MalformedURLException
- {
- try {
- InputStream is = url.openStream();
- buildImage(is);
- } catch(Exception ex) {
- ex.printStackTrace();
- throw new MalformedURLException(ex.getMessage());
- }
-
- enable(false);
- m_Helper.notifyInputChanged(m_Data);
- enable(true);
- return getInputStream();
- }
-
- /**
- * Loads and builds the image at the specified URL then returns an
- * input stream of its image data.
- * Double checks to see if it
- * begins with ftp:// even though Java doesn't support that
- * right now.
- *
- * @param s the URL
- * @return the image data stream
- * @exception MalformedURLException
- * if the URL is invalid
- */
- public ByteArrayInputStream fetchImage(URL url)
- throws MalformedURLException
- {
- url.toExternalForm();
- return fetchHTTP(url);
- }
-
- /**
- * Sets whether the image will be displayed while it is loading.
- * Otherwise the image will not be displayed until it finishes loading.
- */
- public void setDisplayOption(boolean dOption)
- {
- if (dOption)
- {
- m_displayWhileLoading = true;
- }
- else
- {
- m_displayWhileLoading = false;
- }
- }
-
- /**
- * Gets the image input stream.
- * @return the input stream
- * @see #setInputStream
- */
- protected ByteArrayInputStream getInputStream()
- {
- return m_InputStream;
- }
-
- /**
- * Sets the image input stream.
- * @param stream the new input stream
- * @see #getInputStream
- */
- protected void setInputStream(ByteArrayInputStream stream)
- {
- m_InputStream = stream;
- }
-
- /**
- * Gets the image output stream.
- * @return the output stream
- */
- protected ByteArrayOutputStream getOutputStream()
- {
- return m_OutputStream;
- }
-
- /**
- * Converts the given input stream to an output stream.
- * @param input the input stream
- * @return the input stream converted to an output sream
- * @exception MalformedURLException if an exception occurs while converting
- */
- protected ByteArrayOutputStream convertInputStreamToOutputStream(InputStream input) throws MalformedURLException
- {
- BufferedInputStream bis = new BufferedInputStream(input);
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- int b;
- try {
- while((b = bis.read()) != -1) {
- bos.write(b);
- }
- }
- catch (Exception ex) {
- throw new MalformedURLException(ex.getMessage());
- }
- return bos;
- }
-
- /**
- * Eliminates any current image.
- */
- public void clearImage()
- {
- setIsLoaded(false);
- setImage(null);
- repaint();
- m_Helper.notifyInputChanged(m_Data);
- }
-
- private void buildImage(InputStream img) throws MalformedURLException
- {
- setIsLoaded(false);
- setImage(null);
-
- try {
- if (img != null) {
- ByteArrayOutputStream stream = convertInputStreamToOutputStream(img);
- byte[] data = stream.toByteArray();
- if (data.length > 0) {
- m_OutputStream = stream;
- m_Data = data;
- setInputStream(new ByteArrayInputStream(m_Data));
- setImageType(m_Data);
-
- InputStream imgCopy = new ByteArrayInputStream(m_Data);
- Toolkit tk = getToolkit();
- StreamImageSource sis = new StreamImageSource(imgCopy, getImageType());
- setImage(tk.createImage(data));//sis));
- if (!m_displayWhileLoading) {
- loadImage(getImage());
- }
- setIsLoaded(true);
- }
-
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- throw new MalformedURLException(e.getMessage());
- }
- finally {
- repaint();
- }
- }
-
- private void loadImage(Image image)
- {
- if (image != null)
- {
- MediaTracker mtracker;
-
- try
- {
- mtracker = new MediaTracker(this);
- mtracker.addImage(image, 0);
- mtracker.waitForID(0);
- }
- catch(InterruptedException e)
- {
- }
- }
- }
-
- /**
- * Paints this component using the given graphics context.
- * <p>
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its [0,0]
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint(Graphics g) {
- try {
- if (getImage() != null) {
- g.drawImage(getImage(), 0, 0, this);
- }
- else
- {
- g.clearRect(0,0,size().width,size().height);
- }
- } catch (Exception e) {
- e.printStackTrace();
- return;
- }
- }
-
- /**
- * Binds this component to a given projection within the specified
- * relation view.
- *
- * @param relView the relation view to bind with
- * @param projection the projection in relView to bind with
- */
- public void setBinding(RelationView relView, String projection)
- {
- m_Helper.setBinding(relView, projection);
- }
-
- /**
- * Returns the projection in the RelationView that this component is bound with.
- * @see #setBinding
- * @see #setProjection
- */
- public String getProjection() {
- return m_Helper.getProjection();
- }
-
- /**
- * Binds this component to the given projection within the RelationView
- * the component is currently bound with.
- * @see #setBinding
- * @see #getProjection
- * @see #getRelationView
- */
- public void setProjection(String projection) {
- m_Helper.setProjection(projection);
- }
-
- /**
- * Gets the RelationView that this component is bound with.
- * @return the RelationView currently bound with
- * @see #setRelationView
- * @see #setBinding
- * @see #getProjection
- */
- public RelationView getRelationView() {
- return m_Helper.getRelationView();
- }
-
- /**
- * Sets the RelationView that this component is bound with.
- * @param value the RelationView to bind with
- * @see #getRelationView
- * @see #setBinding
- * @see #setProjection
- */
- public void setRelationView(RelationView value) {
- m_Helper.setRelationView(value);
- }
-
- /**
- * Specifies how an empty string will be set when updating data on
- * the dbANYWHERE server.
- *
- * @param blank one of "DEFAULT", "NULL", or "BLANK"
- *
- * @see symantec.itools.db.pro.ProjBinder#setValueFromString(java.lang.String, int, int)
- */
- public void setTreatBlankAs(String value) {
- m_Helper.setTreatBlankAsString(value);
- }
-
- /**
- * Indicates when the component commits its changes.
- * @see #setDynamicUpdate
- */
- public boolean getDynamicUpdate() {
- return m_Helper.getDynamicUpdate();
- }
-
- /**
- * Sets when the component commits its changes.
- * @param value the new dynamic update mode value
- * @see #getDynamicUpdate
- */
- public void setDynamicUpdate(boolean value) {
- m_Helper.setDynamicUpdate(value);
- }
-
- /**
- * Sets whether the data value of this component may be modified.
- * @param value <code>true</code> if the value may not be modified,
- * <code>false</code>if the value may be modified
- */
- public void setReadOnly(boolean value) {
- }
-
- /**
- * Gets the value of this component.
- * @return the current component value
- * @see #setData
- */
- public Object getData() {
- return m_OutputStream;
- }
-
- /**
- * Loads the image data from the database this component has been bound to.
- * It then builds and displays the image.
- */
-
- /**
- * Sets the value of this component to the given value.
- * @param value the new component value
- * @see #getData
- */
- public void setData(Object value)
- {
- if (!isAdded()) {
- m_Value = value;
- return;
- }
-
- if (isEnabled()) {
- InputStream input = null;
- if (value != null && value instanceof InputStream) {
- input = (InputStream)value;
- }
- try {
- buildImage(input);
- }
- catch(Exception e){}
- }
- }
-
- /**
- * Gets whether this component saves its value as a text String.
- * @return <code>true</code> if the value is saved as text,
- * <code>false</code> otherwise
- */
- public boolean isTextBased() {
- return false;
- }
-
- /**
- * Gets the number of digits to the right of the decimal point for
- * this component's value.
- * @return the number of digits to the right of the decimal point
- */
- public int getScale() {
- return ProjBinder.DEFAULTSCALE;
- }
-
- /**
- * Registers the standard event listener(s) for this component.
- */
- public void registerListeners() {
- }
-
- /**
- * Sets the name of the data item to bind this component to.
- * @param name the data item name, like "MyTable@MyColumn"
- * @see #getDataBinding
- */
- public void setDataBinding(String name)
- {
- m_Helper.setDataBinding(new Name(name));
- }
-
- /**
- * Gets the name of the data item this component is bound to.
- * @returns the data item name, like "MyTable@MyColumn"
- * @see #setDataBinding
- */
- public String getDataBinding()
- {
- return m_Helper.getDataBinding().getFullName();
- }
-
- private synchronized boolean isAdded()
- {
- return m_IsAdded;
- }
-
- private synchronized void setIsAdded(boolean isAdded)
- {
- m_IsAdded = isAdded;
- }
- }
-