com.nwoods.jgo
Class JGoImage

java.lang.Object
  extended bycom.nwoods.jgo.JGoObject
      extended bycom.nwoods.jgo.JGoImage
All Implemented Interfaces:
java.util.EventListener, org.eclipse.swt.graphics.ImageLoaderListener, JGoXMLSaveRestore, java.io.Serializable, org.eclipse.swt.internal.SWTEventListener

public class JGoImage
extends JGoObject
implements org.eclipse.swt.graphics.ImageLoaderListener

JGoImage is a JGoObject that represents a graphic image stored in some graphics file or at some location on the network.

See Also:
Serialized Form

Field Summary
static int ChangedFilename
          a CHANGED JGoDocumentEvent or JGoViewEvent hint: the String filename has changed
static int ChangedImage
          a CHANGED JGoDocumentEvent or JGoViewEvent hint: the image may have been changed
static int ChangedTransparentColor
          a CHANGED JGoDocumentEvent or JGoViewEvent hint: the transparent color has changed
static int ChangedURL
          a CHANGED JGoDocumentEvent or JGoViewEvent hint: the URL has changed
 
Fields inherited from class com.nwoods.jgo.JGoObject
Bottom, BottomCenter, BottomLeft, BottomMiddle, BottomRight, Center, CenterLeft, CenterRight, Changed4ResizeHandles, ChangedAutoRescale, ChangedBrush, ChangedDraggable, ChangedDragsNode, ChangedGeometry, ChangedGrabChildSelection, ChangedInitializing, ChangedPen, ChangedResizable, ChangedSelectable, ChangedUpdatePartner, ChangedVisible, ChangedZOrder, LastChangedHint, Left, LeftCenter, NoHandle, NoSpot, NumReservedHandles, RepaintAll, Right, RightCenter, SideLeft, SideRight, Top, TopCenter, TopLeft, TopMiddle, TopRight
 
Constructor Summary
JGoImage()
          Construct an object displaying an unspecified image with no particular dimensions or location.
JGoImage(org.eclipse.swt.graphics.Point location, Dimension size)
          Construct a new image of the given size.
JGoImage(org.eclipse.swt.graphics.Rectangle rect)
          Construct a new image of the given size.
 
Method Summary
 void changeValue(JGoDocumentChangedEdit e, boolean undo)
          This method is called by the JGoDocumentChangedEdit.undo and redo methods to actually perform the property value change for a JGoObject.
 void copyNewValueForRedo(JGoDocumentChangedEdit e)
          This method is called when a JGoDocumentChangedEdit is created for a JGoDocumentEvent.CHANGED event, when a JGoObject has been changed.
 JGoObject copyObject(JGoCopyEnvironment env)
          Create a new instance of this object with the same field values.
static java.net.URL getDefaultBase()
          Return a default base URL for images.
 java.lang.String getFilename()
          Return the filename from which this JGoImage will load an image.
 org.eclipse.swt.graphics.ImageData getImage()
          Return the SWT ImageData that this JGoImage will display.
 org.eclipse.swt.graphics.ImageData getImage(java.lang.String path)
          Return an ImageData for the given filename path.
 org.eclipse.swt.graphics.ImageData getImage(java.net.URL url)
          Return an ImageData for the given URL.
static java.util.HashMap getImageMap()
          Get a HashMap mapping the filenames and URLs to the cached image data.
 Dimension getNaturalSize()
          Returns the unstretched dimensions of the image.
 org.eclipse.swt.graphics.RGB getTransparentColor()
          Return the color that is to be drawn instead of a transparent background for the image.
 java.net.URL getURL()
          Return the URL from which this JGoImage will load an image.
 void imageDataLoaded(org.eclipse.swt.graphics.ImageLoaderEvent e)
          Called by the ImageLoader, initiated from getImage(URL) or getImage(String).
 boolean loadImage(java.lang.String filename, boolean wait)
          Load an image given a filename.
 boolean loadImage(java.net.URL url, boolean wait)
          Load an image given a URL.
 void paint(Graphics2D g, JGoView view)
          Draws this image on the given graphics context.
static void resetAllImages()
          Remove all cached image data that JGo is maintaining.
static void resetImage(java.lang.String path)
          Remove any cached image data for the given filename path.
static void resetImage(java.net.URL url)
          Remove any cached image data for the given URL.
static void setDefaultBase(java.net.URL u)
          Set the default base URL used to determine the location from which to load images specified by a string filename.
 void setImage(org.eclipse.swt.graphics.ImageData img)
          Set the Image property of this JGoImage to refer to a new SWT ImageData.
 void setTransparentColor(org.eclipse.swt.graphics.RGB trans)
          Change the color that is used for transparent pixels in the image.
 void SVGReadAttributes(DomElement svgElement)
          This method will read attributes from standard SVG elements.
 DomNode SVGReadObject(DomDoc svgDoc, JGoDocument jGoDoc, DomElement svgElement, DomElement jGoChildElement)
          Read this object in from a DomDoc representing a Scalable Vector Graphics (SVG) XML document.
 void SVGWriteAttributes(DomElement svgElement)
          This method will add attributes to standard SVG elements.
 void SVGWriteObject(DomDoc svgDoc, DomElement jGoElementGroup)
          Write this object out to a DomDoc representing a Scalable Vector Graphics (SVG) XML document.
 
Methods inherited from class com.nwoods.jgo.JGoObject
canView, computeBoundingRect, computeMove, computeResize, copy, copyObjectDelayed, copyOldValueForUndo, copyRect, doMouseClick, doMouseDblClick, doUncapturedMouseMove, expandRectByPenWidth, findCommonParent, foredate, gainedSelection, geometryChange, geometryChangeChild, getBoundingRect, getDocument, getDraggingObject, getFlags, getHeight, getLayer, getLeft, getLocation, getLocation, getNearestIntersectionPoint, getParent, getParentJGoNode, getParentNode, getPartner, getRectangleSpotLocation, getSize, getSize, getSpotLocation, getSpotLocation, getToolTipText, getTop, getTopLeft, getTopLeft, getTopLevelObject, getView, getWidth, growRect, handleMove, handleResize, hideSelectionHandles, is4ResizeHandles, isAutoRescale, isBoundingRectInvalid, isChildOf, isDraggable, isDragsNode, isInitializing, isPointInObj, isResizable, isSelectable, isSkipsUndoManager, isSuspendUpdates, isTopLevel, isUpdatePartner, isVisible, lostSelection, ownerChange, partnerUpdate, pick, redirectSelection, remove, set4ResizeHandles, setAutoRescale, setBoundingRect, setBoundingRect, setBoundingRect, setBoundingRectInvalid, setBoundsRect, setDraggable, setDragsNode, setFlags, setHeight, setInitializing, setLeft, setLocation, setLocation, setLocationOffset, setLocationOffset, setParent, setPartner, setRectangleSpotLocation, setResizable, setSelectable, setSize, setSize, setSizeKeepingLocation, setSkipsUndoManager, setSpotLocation, setSpotLocation, setSpotLocation, setSpotLocationOffset, setSuspendUpdates, setTop, setTopLeft, setTopLeft, setUpdatePartner, setVisible, setWidth, showSelectionHandles, spotOpposite, SVGUpdateReference, update, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ChangedImage

public static final int ChangedImage
a CHANGED JGoDocumentEvent or JGoViewEvent hint: the image may have been changed

See Also:
Constant Field Values

ChangedTransparentColor

public static final int ChangedTransparentColor
a CHANGED JGoDocumentEvent or JGoViewEvent hint: the transparent color has changed

See Also:
Constant Field Values

ChangedURL

public static final int ChangedURL
a CHANGED JGoDocumentEvent or JGoViewEvent hint: the URL has changed

See Also:
Constant Field Values

ChangedFilename

public static final int ChangedFilename
a CHANGED JGoDocumentEvent or JGoViewEvent hint: the String filename has changed

See Also:
Constant Field Values
Constructor Detail

JGoImage

public JGoImage()
Construct an object displaying an unspecified image with no particular dimensions or location.


JGoImage

public JGoImage(org.eclipse.swt.graphics.Point location,
                Dimension size)
Construct a new image of the given size.

Parameters:
location - the top-left position, in document coordinates
size - the width and height, in document coordinates

JGoImage

public JGoImage(org.eclipse.swt.graphics.Rectangle rect)
Construct a new image of the given size.

Parameters:
rect - the initial bounding rect of the image.
Method Detail

copyObject

public JGoObject copyObject(JGoCopyEnvironment env)
Create a new instance of this object with the same field values. The actual image data is not copied, but will be shared. Since the image data reference is also transient, it may be re-loaded on demand when needed.

Overrides:
copyObject in class JGoObject
Parameters:
env - the environment for the copy operation - keeps track of copied objects.
Returns:
a newly allocated copy of this object, or null

getImage

public org.eclipse.swt.graphics.ImageData getImage(java.net.URL url)
Return an ImageData for the given URL. The default implementation of this method returns a cached image for the URL, if any. If none is available, it uses an ImageLoader to get, remember and return an ImageData for the URL. This method will return null if it cannot load the image, perhaps due to an exception.


getImage

public org.eclipse.swt.graphics.ImageData getImage(java.lang.String path)
Return an ImageData for the given filename path. The default implementation of this method returns a cached image for the filename, if any. If none is available, it uses an ImageLoader to get, remember and return an ImageData for the filename. This method will return null if it cannot load the image, perhaps due to an exception.


resetImage

public static void resetImage(java.net.URL url)
Remove any cached image data for the given URL. Any future calls to the static getImage or loadImage on a JGoImage instance using the same URL will cause the image to be reloaded.


resetImage

public static void resetImage(java.lang.String path)
Remove any cached image data for the given filename path. Any future calls to the static getImage or loadImage on a JGoImage instance using the same filename will cause the image to be reloaded.


resetAllImages

public static void resetAllImages()
Remove all cached image data that JGo is maintaining.

Note that this method will not, by itself, cause any image files to be reloaded, whether by file pathname or by URL. However, they will be reloaded on demand as JGoImage objects are repainted.


loadImage

public boolean loadImage(java.net.URL url,
                         boolean wait)
Load an image given a URL. If wait is true, this method will return only after the image has been completely loaded; otherwise, it will return before the image is fully loaded.

Parameters:
url - the URL for the image
wait - if true, wait for the image to load
Returns:
true when waiting if loading was successful

loadImage

public boolean loadImage(java.lang.String filename,
                         boolean wait)
Load an image given a filename. If wait is true, this method will return only after the image has been completely loaded; otherwise, it will return before the image is fully loaded.

This also depends on the value of getDefaultBase(). If that URL is non-null, it is combined with the given filename to specify a URL from which to load image data.

Parameters:
filename - the image file
wait - if true, wait for the image to load
Returns:
true when waiting if loading was successful

setImage

public void setImage(org.eclipse.swt.graphics.ImageData img)
Set the Image property of this JGoImage to refer to a new SWT ImageData.

Use this method instead of calling loadImage(URL, boolean) or loadImage(String, boolean), when you construct your own image instead of loading it from a file.


getImage

public org.eclipse.swt.graphics.ImageData getImage()
Return the SWT ImageData that this JGoImage will display.

This value may be null because this JGoImage has not yet finished loading (or is unable to load) an Image.

Calling loadImage(Image, boolean) will set this property.


getURL

public java.net.URL getURL()
Return the URL from which this JGoImage will load an image. This value will be null if the source is actually a file.

Calling loadImage(URL, boolean) will set the URL property.


getFilename

public java.lang.String getFilename()
Return the filename from which this JGoImage will load an image. This value will be null if the source is actually a URL.

Calling loadImage(String, boolean) will set the Filename property, unless getDefaultBase() returns a URL.


getNaturalSize

public Dimension getNaturalSize()
Returns the unstretched dimensions of the image.

Returns:
a Dimension, in document coordinates; the width and/or height may be negative if the size is not known or if the image has not yet been loaded

getTransparentColor

public org.eclipse.swt.graphics.RGB getTransparentColor()
Return the color that is to be drawn instead of a transparent background for the image.

This value will be null if transparent pixels are actually not drawn. By default this value is null; i.e., the image does not appear to have a background.


setTransparentColor

public void setTransparentColor(org.eclipse.swt.graphics.RGB trans)
Change the color that is used for transparent pixels in the image. Specify null if transparent pixels (typically used as the background) should not be drawn.

Some image files do not support transparent pixels; for such files this method has no effect.

Parameters:
trans - the new background color, or null for a transparent background

copyNewValueForRedo

public void copyNewValueForRedo(JGoDocumentChangedEdit e)
Description copied from class: JGoObject
This method is called when a JGoDocumentChangedEdit is created for a JGoDocumentEvent.CHANGED event, when a JGoObject has been changed. In order for the JGoDocumentChangedEdit to be able to perform a redo, it needs to remember the new property value or state. However, the JGoObject.update method does not provide such a new value. The value must be taken from this object and copied into the JGoDocumentChangedEdit by calling setNewValue and/or setNewValueInt.

Just as the previous/old value/state information is stored as one or both of an integer and an Object, the new value/state information may be stored as either or both of an integer or an Object.

The responsibility for copying rests with the document event listener rather than the code calling JGoObject.update for efficiency when there is no undo/redo manager.

Overrides:
copyNewValueForRedo in class JGoObject
Parameters:
e - the UndoableEdit that also remembers the kind of change and any appropriate new state that should be copied in

changeValue

public void changeValue(JGoDocumentChangedEdit e,
                        boolean undo)
Description copied from class: JGoObject
This method is called by the JGoDocumentChangedEdit.undo and redo methods to actually perform the property value change for a JGoObject.

You will want to override this method to handle changing the additional state of your object subclasses.

Overrides:
changeValue in class JGoObject
Parameters:
e - the UndoableEdit that also remembers the kind of change and any appropriate old and new state for performing an undo or redo
undo - if true, this method should restore the old state/value, otherwise this method should restore the new state/value

SVGWriteObject

public void SVGWriteObject(DomDoc svgDoc,
                           DomElement jGoElementGroup)
Description copied from interface: JGoXMLSaveRestore
Write this object out to a DomDoc representing a Scalable Vector Graphics (SVG) XML document.

This method will be called by com.nwoods.jgo.svg.DefaultDocument.SVGWriteDoc() for every JGoObject contained within a JGoDocument. The following example uses a JGoRectangle to illustrate the format of a generated SVG JGoObject:

<g>
<JGoClass class="com.nwoods.jgo.JGoRectangle"\>
<rect height="75" style="stroke:black;stroke-width:1;fill:rgb(255,0,0);" width="75" x="65" y="71"/>
<JGoClass class="com.nwoods.jgo.JGoDrawable" drawablebrush="jgoid1" drawablepen="jgoid2" embeddedpenbrush="false"/>
<JGoClass class="com.nwoods.jgo.JGoObject" obj_flags="1054"/>
</g>

Note that the entire JGoObject is enclosed in a group (<g>). Each subclass of the JGoObject is described by a <JGoClass> element, starting with the most specific class and moving to the more general. Each <JGoClass> element has a "class" attribute which defines the class name. Each <JGoClass> element may also have several other attributes which uniquely describe that state of that class. Following the <JGoClass> element, each class may also genereate any other elements that are required, including representations of contained objects and standard SVG elements such as the <rect> element shown in the above example. The generation of SVG elements in the output makes it possible for the output to be read by SVG viewers and other SVG aware applications. The information contained in the <JGoClass> elements allows us to accurately save and restore all the information in a particular JGoObject subclass.

If this class contains a reference to another object, call DomDoc.registerReferencingNode() to register that reference. This will cause the element representing the referenced object to be identified with a unique id attribute, and cause the element representing the referring object to have a user defined attribute containing a reference to that id. See DomDoc.registerReferencingNode() for more information.

Typically, an implementation of this method will generate a <JGoClass> element using the DomDoc.createJGoClassElement() method, adding whatever attributes are necessary to describe the class. It will then call its superclass so that the superclass can add its own <JGoClass> element and other information to jGoElementGroup within the DomDoc.

Refer to com.nwoods.jgo.examples.flower.ActivityNode.SVGWriteObject() and com.nwoods.jgo.examples.SimpleNode.SVGWriteObject() for examples using this method.

Refer to the file com.nwoods.jgo.svg\xsvg.dtd for a formal description of JGo extensions to the SVG document type.

Specified by:
SVGWriteObject in interface JGoXMLSaveRestore
Overrides:
SVGWriteObject in class JGoObject

SVGReadObject

public DomNode SVGReadObject(DomDoc svgDoc,
                             JGoDocument jGoDoc,
                             DomElement svgElement,
                             DomElement jGoChildElement)
Description copied from interface: JGoXMLSaveRestore
Read this object in from a DomDoc representing a Scalable Vector Graphics (SVG) XML document.

This method will be called by com.nwoods.jgo.svg.DefaultDocument.SVGReadDoc() for every <JGoClass> element encountered in a DomDoc. First, an object of the type specified by the class attribute will be created and then the SVGReadObject() method will be called on that newly created object. Refer to SVGWriteObject() for a description of the expected format of the SVG document.

This method may also be called by com.nwoods.jgo.svg.DefaultDocument.SVGReadDoc() when reading any SVG document, including those not generated by JGo and therefore not containing any <JGoClass> elements. In such cases, the DefaultDocument.SVGReadElement() method may be subclassed to examine the SVG element and cause the appropriate JGoObject to be created to represent that element and call SVGReadObject() on that newly created object. By default, standard JGoObjects such as JGoRectangles, JGoStrokes, etc will be created to represent these objects.

If this class contains a reference to another object, call DomDoc.registerReferencingObject() to register that reference. This will cause the JGoObject.SVGUpdateReference() to be called on the referencing object after all the objects have been created, passing in the object befing referenced. See DomDoc.registerReferencingObject() for more information.

Typically, an implementation of this method starts by verifying that the element being read is a <JGoClass> element (which will be true if jGoChildElement is not null). This will always be true unless you have subclassed the DefaultDocument.SVGReadElement() method as described above. It will then read the expected attributes of the <JGoClass> element (as written by SVGReadObject()), call SVGReadObject() on its superclass, and finally return the next element by calling svgElement.getNextSibling().

Refer to com.nwoods.jgo.examples.flower.ActivityNode.SVGReadObject() and com.nwoods.jgo.examples.SimpleNode.SVGReadObject() for examples using this method.

Specified by:
SVGReadObject in interface JGoXMLSaveRestore
Overrides:
SVGReadObject in class JGoObject

SVGWriteAttributes

public void SVGWriteAttributes(DomElement svgElement)
Description copied from class: JGoObject
This method will add attributes to standard SVG elements.

Call this method from SVGWriteObject() if standard SVG elements are generated by this class or if this class may be subclassed and it should contribute arguments to SVG elements. For example, the JGoDrawable class implements this method to add standard SVG attributes for stroke, fill, and width. The JGoRectangle class calls this method to add all appropriate attributes to the <rect> element it generates. It is not necessary to implement this method if this class does not contribute any attributes to standard SVG elements. It is not necessary to call this method from SVGWriteObject() if this class does not generate any standard SVG elements.

Typically, an implementation of this method will first call the superclass and then call DomElement.setAttribute() to add attributes for this class.

Overrides:
SVGWriteAttributes in class JGoObject
Parameters:
svgElement - the DomElement to which to add attributes
See Also:
JGoObject.SVGWriteObject(com.nwoods.jgo.DomDoc, com.nwoods.jgo.DomElement), JGoObject.SVGReadObject(com.nwoods.jgo.DomDoc, com.nwoods.jgo.JGoDocument, com.nwoods.jgo.DomElement, com.nwoods.jgo.DomElement), JGoObject.SVGWriteAttributes(com.nwoods.jgo.DomElement), JGoObject.SVGReadAttributes(com.nwoods.jgo.DomElement), JGoObject.SVGUpdateReference(java.lang.String, java.lang.Object), DomDoc, DomNode, DomElement, DomText

SVGReadAttributes

public void SVGReadAttributes(DomElement svgElement)
Description copied from class: JGoObject
This method will read attributes from standard SVG elements.

Call this method from SVGReadObject() if standard SVG elements are being read by this class or if this class may be subclassed and it should read arguments from SVG elements. For example, the JGoDrawable class implements this method to read standard SVG attributes for stroke, fill, and width. The JGoRectangle class calls this method to read all appropriate attributes from the <rect> element it is reading. It is not necessary to implement this method if this class does not read any attributes from standard SVG elements. It is not necessary to call this method from SVGReadObject() if this class does not read any standard SVG elements.

Typically, an implementation of this method will first call the superclass and then call DomElement.getAttribute() to read the attributes for this class.

Overrides:
SVGReadAttributes in class JGoObject
Parameters:
svgElement - the DomElement from which to read attributes
See Also:
JGoObject.SVGWriteObject(com.nwoods.jgo.DomDoc, com.nwoods.jgo.DomElement), JGoObject.SVGReadObject(com.nwoods.jgo.DomDoc, com.nwoods.jgo.JGoDocument, com.nwoods.jgo.DomElement, com.nwoods.jgo.DomElement), JGoObject.SVGWriteAttributes(com.nwoods.jgo.DomElement), JGoObject.SVGReadAttributes(com.nwoods.jgo.DomElement), JGoObject.SVGUpdateReference(java.lang.String, java.lang.Object), DomDoc, DomNode, DomElement, DomText

paint

public void paint(Graphics2D g,
                  JGoView view)
Draws this image on the given graphics context. If no image has been loaded yet, nothing is drawn. If only part of the image has been loaded, the part that is loaded is drawn.

Overrides:
paint in class JGoObject
Parameters:
g - the graphics context on which to draw.
view - the view we're drawing in

imageDataLoaded

public void imageDataLoaded(org.eclipse.swt.graphics.ImageLoaderEvent e)
Called by the ImageLoader, initiated from getImage(URL) or getImage(String).

This calls setImage(ImageData) when ImageLoaderEvent.endOfImage is true, and remembers the mapping in the static getImageMap() hash table.

Specified by:
imageDataLoaded in interface org.eclipse.swt.graphics.ImageLoaderListener

getDefaultBase

public static java.net.URL getDefaultBase()
Return a default base URL for images.

This value is normally null, but is typically set to the value of Applet.getCodeBase() or Applet.getDocumentBase() or some URL derived from those values.


setDefaultBase

public static void setDefaultBase(java.net.URL u)
Set the default base URL used to determine the location from which to load images specified by a string filename.


getImageMap

public static java.util.HashMap getImageMap()
Get a HashMap mapping the filenames and URLs to the cached image data.

Returns:
the HashMap mapping filenames and URLs to SWT ImageDatas