jwo.utils.gui.html
Class ImprovedImageView

java.lang.Object
  extended by javax.swing.text.View
      extended by jwo.utils.gui.html.ImprovedImageView
All Implemented Interfaces:
MouseListener, MouseMotionListener, ImageObserver, EventListener, SwingConstants

public class ImprovedImageView
extends View
implements ImageObserver, MouseListener, MouseMotionListener

Customised image viewer for use by an HTML editor kit. This one handles the display of relative image URLs more flexibly than the default. Based on the JavaWorld Tip 109 by Rob Kenworthy.

Version:
2.3, 18th July, 2003.
Author:
Rob Kenworthy, modified by Jo Wood.

Field Summary
static String ABSMIDDLE
          Indicates absolute centre alignment of image.
static String BOTTOM
          Indicates bottom alignment of image.
static String CENTER
          Indicates centre alignment of image.
static String MIDDLE
          Indicates centre alignment of image.
static String TEXTTOP
          Indicates top alignment of image text.
static String TOP
          Indicates top alignment of image.
 
Fields inherited from class javax.swing.text.View
BadBreakWeight, ExcellentBreakWeight, ForcedBreakWeight, GoodBreakWeight, X_AXIS, Y_AXIS
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Fields inherited from interface javax.swing.SwingConstants
EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TRAILING, VERTICAL, WEST
 
Constructor Summary
ImprovedImageView(Element elem)
          Creates a new view that represents an IMG element.
 
Method Summary
 void changedUpdate(DocumentEvent e, Shape a, ViewFactory f)
          Responds to a change in the document.
 float getAlignment(int axis)
          Determines the desired alignment for this view along an axis.
 AttributeSet getAttributes()
          Fetches the attributes to use when rendering.
protected  Color getHighlightColor()
          Reports the text editor's highlight colour.
 float getPreferredSpan(int axis)
          Determines the preferred span for this view along an axis.
protected  int getSelectionState()
          Determines whether the image is selected, and if it's the only thing selected.
protected  StyleSheet getStyleSheet()
          Reports the style sheet used by the component for HTML viewing.
 boolean imageUpdate(Image img, int flags, int x, int y, int width, int height)
          Allows progressive display of images.
protected  boolean isEditable()
          Reports whether the view is editable.
 Shape modelToView(int pos, Shape a, Position.Bias b)
          Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.
 void mouseClicked(MouseEvent e)
          On double-click, open image properties dialogue.
 void mouseDragged(MouseEvent e)
          Resize image if initial click was in grow-box: Mouse event logging mouse drag.
 void mouseEntered(MouseEvent e)
          Ignores mouse entry events.
 void mouseExited(MouseEvent e)
          Ignores mouse exit events.
 void mouseMoved(MouseEvent e)
          Ignores mouse movement events.
 void mousePressed(MouseEvent e)
          Select or grow image when clicked.
 void mouseReleased(MouseEvent e)
          Stops image being resized when mouse released.
 void paint(Graphics g, Shape a)
          Draws the image.
protected  void repaint(long delay)
          Request that this view be repainted.
protected  void resize(int width, int height)
          Change the size of this image.
 void setParent(View parent)
          Establishes the parent view for this view.
 void setSize(float width, float height)
          Sets the size of the view, but ignored in this case.
 int viewToModel(float x, float y, Shape a, Position.Bias[] bias)
          Provides a mapping from the view coordinate space to the logical coordinate space of the model.
 
Methods inherited from class javax.swing.text.View
append, breakView, createFragment, forwardUpdate, forwardUpdateToView, getBreakWeight, getChildAllocation, getContainer, getDocument, getElement, getEndOffset, getGraphics, getMaximumSpan, getMinimumSpan, getNextVisualPositionFrom, getParent, getResizeWeight, getStartOffset, getToolTipText, getView, getViewCount, getViewFactory, getViewIndex, getViewIndex, insert, insertUpdate, isVisible, modelToView, modelToView, preferenceChanged, remove, removeAll, removeUpdate, replace, updateChildren, updateLayout, viewToModel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TOP

public static final String TOP
Indicates top alignment of image.

See Also:
Constant Field Values

TEXTTOP

public static final String TEXTTOP
Indicates top alignment of image text.

See Also:
Constant Field Values

MIDDLE

public static final String MIDDLE
Indicates centre alignment of image.

See Also:
Constant Field Values

ABSMIDDLE

public static final String ABSMIDDLE
Indicates absolute centre alignment of image.

See Also:
Constant Field Values

CENTER

public static final String CENTER
Indicates centre alignment of image.

See Also:
Constant Field Values

BOTTOM

public static final String BOTTOM
Indicates bottom alignment of image.

See Also:
Constant Field Values
Constructor Detail

ImprovedImageView

public ImprovedImageView(Element elem)
Creates a new view that represents an IMG element.

Parameters:
elem - The element to create a view for
Method Detail

getAttributes

public AttributeSet getAttributes()
Fetches the attributes to use when rendering. This is implemented to multiplex the attributes specified in the model with a StyleSheet.

Overrides:
getAttributes in class View
Returns:
Set of attributes to use when rendering.

setParent

public void setParent(View parent)
Establishes the parent view for this view. Also caches the AWT container that the view is within.

Overrides:
setParent in class View
Parameters:
parent - Parent view to use.

changedUpdate

public void changedUpdate(DocumentEvent e,
                          Shape a,
                          ViewFactory f)
Responds to a change in the document.

Overrides:
changedUpdate in class View
Parameters:
e - Document event logging change.
a - Shape holding new view.
f - View factory.

paint

public void paint(Graphics g,
                  Shape a)
Draws the image.

Specified by:
paint in class View
Parameters:
g - the rendering surface to use
a - the allocated region to render into
See Also:
View.paint(java.awt.Graphics, java.awt.Shape)

repaint

protected void repaint(long delay)
Request that this view be repainted. Assumes the view is still at its last-drawn location.

Parameters:
delay - before repainting (in milliseconds).

getSelectionState

protected int getSelectionState()
Determines whether the image is selected, and if it's the only thing selected.

Returns:
0 if not selected, 1 if selected, 2 if exclusively selected. "Exclusive" selection is only returned when editable.

isEditable

protected boolean isEditable()
Reports whether the view is editable.

Returns:
True if editable.

getHighlightColor

protected Color getHighlightColor()
Reports the text editor's highlight colour.

Returns:
Highlight colour.

imageUpdate

public boolean imageUpdate(Image img,
                           int flags,
                           int x,
                           int y,
                           int width,
                           int height)
Allows progressive display of images. This can come on any thread. If in the process of reloading the image and determining state (loading == true) preference change or repaint requests are not fired. Instead width and height are reset as necessary.

Specified by:
imageUpdate in interface ImageObserver
Parameters:
img - Image to update.
flags - Image loading flags.
x - x-location of image.
y - y-location of image.
width - Width of image.
height - Height of image.

getPreferredSpan

public float getPreferredSpan(int axis)
Determines the preferred span for this view along an axis.

Specified by:
getPreferredSpan in class View
Parameters:
axis - may be either X_AXIS or Y_AXIS
Returns:
The span the view would like to be rendered into. Typically the view is told to render into the span that is returned, although there is no guarantee. The parent may choose to resize or break the view.

getAlignment

public float getAlignment(int axis)
Determines the desired alignment for this view along an axis. This is implemented to give the alignment to the bottom of the icon along the y axis, and the default along the x axis.

Overrides:
getAlignment in class View
Parameters:
axis - may be either X_AXIS or Y_AXIS
Returns:
The desired alignment. This should be a value between 0.0 and 1.0 where 0 indicates alignment at the origin and 1.0 indicates alignment to the full span away from the origin. An alignment of 0.5 would be the centre of the view.

modelToView

public Shape modelToView(int pos,
                         Shape a,
                         Position.Bias b)
                  throws BadLocationException
Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.

Specified by:
modelToView in class View
Parameters:
pos - the position to convert
a - the allocated region to render into
Returns:
the bounding box of the given position
Throws:
BadLocationException - If the given position does not represent a valid location in the associated document

viewToModel

public int viewToModel(float x,
                       float y,
                       Shape a,
                       Position.Bias[] bias)
Provides a mapping from the view coordinate space to the logical coordinate space of the model.

Specified by:
viewToModel in class View
Parameters:
x - the X coordinate
y - the Y coordinate
a - the allocated region to render into
Returns:
the location within the model that best represents the given point of view

setSize

public void setSize(float width,
                    float height)
Sets the size of the view, but ignored in this case. Image size is determined by the tag attributes and the image itself, not the layout.

Overrides:
setSize in class View
Parameters:
width - the width
height - the height

resize

protected void resize(int width,
                      int height)
Change the size of this image. This alters the HEIGHT and WIDTH attributes of the Element and causes a re-layout.

Parameters:
width - New width of image.
height - New height of image.

mousePressed

public void mousePressed(MouseEvent e)
Select or grow image when clicked.

Specified by:
mousePressed in interface MouseListener
Parameters:
e - Mouse event logging mouse press.

mouseDragged

public void mouseDragged(MouseEvent e)
Resize image if initial click was in grow-box: Mouse event logging mouse drag.

Specified by:
mouseDragged in interface MouseMotionListener

mouseReleased

public void mouseReleased(MouseEvent e)
Stops image being resized when mouse released.

Specified by:
mouseReleased in interface MouseListener
Parameters:
e - Mouse release event.

mouseClicked

public void mouseClicked(MouseEvent e)
On double-click, open image properties dialogue. Not yet implemented.

Specified by:
mouseClicked in interface MouseListener
Parameters:
e - Mouse click event.

mouseEntered

public void mouseEntered(MouseEvent e)
Ignores mouse entry events.

Specified by:
mouseEntered in interface MouseListener
Parameters:
e - Mouse event (ignored).

mouseMoved

public void mouseMoved(MouseEvent e)
Ignores mouse movement events.

Specified by:
mouseMoved in interface MouseMotionListener
Parameters:
e - Mouse event (ignored).

mouseExited

public void mouseExited(MouseEvent e)
Ignores mouse exit events.

Specified by:
mouseExited in interface MouseListener
Parameters:
e - Mouse event (ignored).

getStyleSheet

protected StyleSheet getStyleSheet()
Reports the style sheet used by the component for HTML viewing.

Returns:
Stylesheet used by the component.


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009