APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing.text.html

public Class ImageView

extends View
Class Inheritance
Imports
java.awt.Rectangle, .Image, .Dimension, .Container, .Color, .Shape, .Graphics, .Toolkit, java.awt.image.ImageObserver, java.net.URL, .MalformedURLException, java.util.Dictionary, javax.swing.GrayFilter, .ImageIcon, .Icon, .UIManager, .SwingUtilities, javax.swing.text.JTextComponent, .StyledDocument, .View, .AttributeSet, .Element, .ViewFactory, .Position, .Segment, .Highlighter, .LayeredHighlighter, .AbstractDocument, .Document, .BadLocationException, javax.swing.event.DocumentEvent

View of an Image, intended to support the HTML <IMG> tag. Supports scaling via the HEIGHT and WIDTH attributes of the tag. If the image is unable to be loaded any text specified via the ALT attribute will be rendered.

While this class has been part of swing for a while now, it is public as of 1.4.

Author
Scott Violet
Since
1.4
See Also
javax.swing.text.IconView

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
ImageView.ImageHandler

ImageHandler implements the ImageObserver to correctly update the display as new parts of the image become available.

private static class
ImageView.ImageLabelView

ImageLabelView is used if the image can't be loaded, and the attribute specified an alt attribute.

Field Summary

Modifier and TypeField and Description
private View
altView

Used for alt text.

private AttributeSet
private Color
private short
private short
private Container
private static final int
DEFAULT_BORDER

Default border to use if one is not specified.

private static final int
private static final int
private Image
private Rectangle
private int
private static final int
private Image
private static final String
IMAGE_CACHE_PROPERTY

Document property for image cache.

private ImageObserver
imageObserver

We don't directly implement ImageObserver, instead we use an instance that calls back to us.

private short
private static final int
private static final String
MISSING_IMAGE

Property name for missing image icon

private static final String
PENDING_IMAGE

Property name for pending image icon

private static final int
private static final int
private short
private static int
sIncRate

Repaint delay when some of the bits are available.

private static boolean
sIsInc

If true, when some of the bits are available a repaint is done.

private int
state

Bitmask containing some of the above bitmask values.

private static final int
private short
private float
vAlign

Alignment along the vertical (Y) axis.

private int
private static final int
Inherited from javax.swing.text.View:
BadBreakWeightExcellentBreakWeightForcedBreakWeightGoodBreakWeightX_AXISY_AXIS

Constructor Summary

AccessConstructor and Description
public
ImageView(Element
the element to create a view for
elem
)

Creates a new view that represents an IMG element.

Method Summary

Modifier and TypeMethod and Description
private Dimension
adjustWidthHeight(int newWidth, int newHeight)

public void
changedUpdate(DocumentEvent e, Shape a, ViewFactory f)

Overrides javax.swing.text.View.changedUpdate.

Invoked when the Elements attributes have changed.

public float

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 center of the view
getAlignment
(int
may be either X_AXIS or Y_AXIS
axis
)

Overrides javax.swing.text.View.getAlignment.

Determines the desired alignment for this view along an axis.

public String

Returns:

the test to display if the image cannot be loaded.
getAltText
()

Returns the text to display if the image cannot be loaded.

private View
getAltView()

Returns the view to use for alternate text.

public AttributeSet
getAttributes()

Overrides javax.swing.text.View.getAttributes.

Fetches the attributes to use when rendering.

public Image

Returns:

the image to render.
getImage
()

Returns the image to render.

private Image
getImage(boolean enabled)

public URL

Returns:

the URL for the image source, or null if it could not be determined.
getImageURL
()

Return a URL for the image source, or null if it could not be determined.

private int
getIntAttr(HTML.Attribute name, int deflt)

Convenient method for getting an integer attribute from the elements AttributeSet.

public Icon

Returns:

the icon to use while in the process of loading the image.
getLoadingImageIcon
()

Returns the icon to use while in the process of loading the image.

public boolean

Returns:

true if the image should be loaded when first asked for.
getLoadsSynchronously
()

Returns true if the image should be loaded when first asked for.

public Icon

Returns:

the icon to use if the image could not be found.
getNoImageIcon
()

Returns the icon to use if the image could not be found.

public float

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
getPreferredSpan
(int
may be either X_AXIS or Y_AXIS
axis
)

Implements abstract javax.swing.text.View.getPreferredSpan.

Determines the preferred span for this view along an axis.

private float
getPreferredSpanFromAltView(int axis)

Returns the preferred span of the View used to display the alt text, or 0 if the view does not exist.

protected StyleSheet

Returns:

the StyleSheet
getStyleSheet
()

Convenient method to get the StyleSheet.

public String
getToolTipText(float x, float y, Shape allocation)

Overrides javax.swing.text.View.getToolTipText.

For images the tooltip text comes from text specified with the ALT attribute.

private boolean
hasPixels(Image image)

Returns true if the passed in image has a non-zero width and height.

private void
loadImage()

Loads the image from the URL getImageURL.

public Shape

Returns:

the bounding box of the given position
modelToView
(int
the position to convert
pos
,
Shape
the allocated region to render into
a
,
Position.Bias b)

Implements abstract javax.swing.text.View.modelToView.

Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.

public void
paint(Graphics
the rendering surface to use
g
,
Shape
the allocated region to render into
a
)

Implements abstract javax.swing.text.View.paint.

Paints the View.

private void
private void
private void
refreshImage()

Loads the image and updates the size accordingly.

private void
repaint(long delay)

Request that this view be repainted.

private void
safePreferenceChanged()

Invokes preferenceChanged on the event dispatching thread.

public void
setLoadsSynchronously(boolean
if true the image will be loaded when first asked for, otherwise it will be asynchronously.
newValue
)

Sets how the image is loaded.

public void
setParent(View parent)

Overrides javax.swing.text.View.setParent.

Establishes the parent view for this view.

protected void
setPropertiesFromAttributes()

Update any cached values that come from attributes.

public void
setSize(float
the width >= 0
width
,
float
the height >= 0
height
)

Overrides javax.swing.text.View.setSize.

Sets the size of the view.

private void
sync()

Makes sure the necessary properties and image is loaded.

private void
updateAltTextView()

Updates the view representing the alt text.

private void
updateImageSize()

Recreates and reloads the image.

public int

Returns:

the location within the model that best represents the given point of view
viewToModel
(float
the X coordinate
x
,
float
the Y coordinate
y
,
Shape
the allocated region to render into
a
,
Position.Bias[] bias)

Implements abstract javax.swing.text.View.viewToModel.

Provides a mapping from the view coordinate space to the logical coordinate space of the model.

Inherited from javax.swing.text.View:
appendbreakViewcreateFragmentforwardUpdateforwardUpdateToViewgetBreakWeightgetChildAllocationgetContainergetDocumentgetElementgetEndOffsetgetGraphicsgetMaximumSpangetMinimumSpangetNextVisualPositionFromgetParentgetResizeWeightgetStartOffsetgetViewgetViewCountgetViewFactorygetViewIndexgetViewIndexinsertinsertUpdateisVisiblemodelToViewmodelToViewpreferenceChangedremoveremoveAllremoveUpdatereplaceupdateChildrenupdateLayoutviewToModel

Field Detail

altViewback to summary
private View altView

Used for alt text. Will be non-null if the image couldn't be found, and there is valid alt text.

attrback to summary
private AttributeSet attr
borderColorback to summary
private Color borderColor
borderSizeback to summary
private short borderSize
bottomInsetback to summary
private short bottomInset
containerback to summary
private Container container
DEFAULT_BORDERback to summary
private static final int DEFAULT_BORDER

Default border to use if one is not specified.

DEFAULT_HEIGHTback to summary
private static final int DEFAULT_HEIGHT
DEFAULT_WIDTHback to summary
private static final int DEFAULT_WIDTH
disabledImageback to summary
private Image disabledImage
fBoundsback to summary
private Rectangle fBounds
heightback to summary
private int height
HEIGHT_FLAGback to summary
private static final int HEIGHT_FLAG
imageback to summary
private Image image
IMAGE_CACHE_PROPERTYback to summary
private static final String IMAGE_CACHE_PROPERTY

Document property for image cache.

imageObserverback to summary
private ImageObserver imageObserver

We don't directly implement ImageObserver, instead we use an instance that calls back to us.

leftInsetback to summary
private short leftInset
LOADING_FLAGback to summary
private static final int LOADING_FLAG
MISSING_IMAGEback to summary
private static final String MISSING_IMAGE

Property name for missing image icon

PENDING_IMAGEback to summary
private static final String PENDING_IMAGE

Property name for pending image icon

RELOAD_FLAGback to summary
private static final int RELOAD_FLAG
RELOAD_IMAGE_FLAGback to summary
private static final int RELOAD_IMAGE_FLAG
rightInsetback to summary
private short rightInset
sIncRateback to summary
private static int sIncRate

Repaint delay when some of the bits are available.

sIsIncback to summary
private static boolean sIsInc

If true, when some of the bits are available a repaint is done.

This is set to false as swing does not offer a repaint that takes a delay. If this were true, a bunch of immediate repaints would get generated that end up significantly delaying the loading of the image (or anything else going on for that matter).

stateback to summary
private int state

Bitmask containing some of the above bitmask values. Because the image loading notification can happen on another thread access to this is synchronized (at least for modifying it).

SYNC_LOAD_FLAGback to summary
private static final int SYNC_LOAD_FLAG
topInsetback to summary
private short topInset
vAlignback to summary
private float vAlign

Alignment along the vertical (Y) axis.

widthback to summary
private int width
WIDTH_FLAGback to summary
private static final int WIDTH_FLAG

Constructor Detail

ImageViewback to summary
public ImageView(Element elem)

Creates a new view that represents an IMG element.

Parameters
elem:Element

the element to create a view for

Method Detail

adjustWidthHeightback to summary
private Dimension adjustWidthHeight(int newWidth, int newHeight)
changedUpdateback to summary
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f)

Overrides javax.swing.text.View.changedUpdate.

Invoked when the Elements attributes have changed. Recreates the image.

getAlignmentback to summary
public float getAlignment(int axis)

Overrides javax.swing.text.View.getAlignment.

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.

Parameters
axis:int

may be either X_AXIS or Y_AXIS

Returns:float

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 center of the view

getAltTextback to summary
public String getAltText()

Returns the text to display if the image cannot be loaded. This is obtained from the Elements attribute set with the attribute name HTML.Attribute.ALT.

Returns:String

the test to display if the image cannot be loaded.

getAltViewback to summary
private View getAltView()

Returns the view to use for alternate text. This may be null.

getAttributesback to summary
public AttributeSet getAttributes()

Overrides javax.swing.text.View.getAttributes.

Fetches the attributes to use when rendering. This is implemented to multiplex the attributes specified in the model with a StyleSheet.

getImageback to summary
public Image getImage()

Returns the image to render.

Returns:Image

the image to render.

getImageback to summary
private Image getImage(boolean enabled)
getImageURLback to summary
public URL getImageURL()

Return a URL for the image source, or null if it could not be determined.

Returns:URL

the URL for the image source, or null if it could not be determined.

getIntAttrback to summary
private int getIntAttr(HTML.Attribute name, int deflt)

Convenient method for getting an integer attribute from the elements AttributeSet.

getLoadingImageIconback to summary
public Icon getLoadingImageIcon()

Returns the icon to use while in the process of loading the image.

Returns:Icon

the icon to use while in the process of loading the image.

getLoadsSynchronouslyback to summary
public boolean getLoadsSynchronously()

Returns true if the image should be loaded when first asked for.

Returns:boolean

true if the image should be loaded when first asked for.

getNoImageIconback to summary
public Icon getNoImageIcon()

Returns the icon to use if the image could not be found.

Returns:Icon

the icon to use if the image could not be found.

getPreferredSpanback to summary
public float getPreferredSpan(int axis)

Implements abstract javax.swing.text.View.getPreferredSpan.

Determines the preferred span for this view along an axis.

Parameters
axis:int

may be either X_AXIS or Y_AXIS

Returns:float

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

getPreferredSpanFromAltViewback to summary
private float getPreferredSpanFromAltView(int axis)

Returns the preferred span of the View used to display the alt text, or 0 if the view does not exist.

getStyleSheetback to summary
protected StyleSheet getStyleSheet()

Convenient method to get the StyleSheet.

Returns:StyleSheet

the StyleSheet

getToolTipTextback to summary
public String getToolTipText(float x, float y, Shape allocation)

Overrides javax.swing.text.View.getToolTipText.

For images the tooltip text comes from text specified with the ALT attribute. This is overridden to return getAltText.

See Also
JTextComponent#getToolTipText
hasPixelsback to summary
private boolean hasPixels(Image image)

Returns true if the passed in image has a non-zero width and height.

loadImageback to summary
private void loadImage()

Loads the image from the URL getImageURL. This should only be invoked from refreshImage.

modelToViewback to summary
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException

Implements abstract javax.swing.text.View.modelToView.

Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.

Parameters
pos:int

the position to convert

a:Shape

the allocated region to render into

Returns:Shape

the bounding box of the given position

Exceptions
BadLocationException:
if the given position does not represent a valid location in the associated document
See Also
View#modelToView
paintback to summary
public void paint(Graphics g, Shape a)

Implements abstract javax.swing.text.View.paint.

Paints the View.

Parameters
g:Graphics

the rendering surface to use

a:Shape

the allocated region to render into

See Also
View#paint
paintBorderback to summary
private void paintBorder(Graphics g, Rectangle rect)
paintHighlightsback to summary
private void paintHighlights(Graphics g, Shape shape)
refreshImageback to summary
private void refreshImage()

Loads the image and updates the size accordingly. This should be invoked instead of invoking loadImage or updateImageSize directly.

repaintback to summary
private void repaint(long delay)

Request that this view be repainted. Assumes the view is still at its last-drawn location.

safePreferenceChangedback to summary
private void safePreferenceChanged()

Invokes preferenceChanged on the event dispatching thread.

setLoadsSynchronouslyback to summary
public void setLoadsSynchronously(boolean newValue)

Sets how the image is loaded. If newValue is true, the image will be loaded when first asked for, otherwise it will be loaded asynchronously. The default is to not load synchronously, that is to load the image asynchronously.

Parameters
newValue:boolean

if true the image will be loaded when first asked for, otherwise it will be asynchronously.

setParentback to summary
public void setParent(View parent)

Overrides javax.swing.text.View.setParent.

Establishes the parent view for this view. Seize this moment to cache the AWT Container I'm in.

setPropertiesFromAttributesback to summary
protected void setPropertiesFromAttributes()

Update any cached values that come from attributes.

setSizeback to summary
public void setSize(float width, float height)

Overrides javax.swing.text.View.setSize.

Sets the size of the view. This should cause layout of the view if it has any layout duties.

Parameters
width:float

the width >= 0

height:float

the height >= 0

syncback to summary
private void sync()

Makes sure the necessary properties and image is loaded.

updateAltTextViewback to summary
private void updateAltTextView()

Updates the view representing the alt text.

updateImageSizeback to summary
private void updateImageSize()

Recreates and reloads the image. This should only be invoked from refreshImage.

viewToModelback to summary
public int viewToModel(float x, float y, Shape a, Position.Bias[] bias)

Implements abstract javax.swing.text.View.viewToModel.

Provides a mapping from the view coordinate space to the logical coordinate space of the model.

Parameters
x:float

the X coordinate

y:float

the Y coordinate

a:Shape

the allocated region to render into

Returns:int

the location within the model that best represents the given point of view

See Also
View#viewToModel
javax.swing.text.html back to summary

private Class ImageView.ImageHandler

extends Object
implements ImageObserver
Class Inheritance
All Implemented Interfaces
java.awt.image.ImageObserver

ImageHandler implements the ImageObserver to correctly update the display as new parts of the image become available.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
imageUpdate(Image img, int flags, int x, int y, int newWidth, int newHeight)

Implements java.awt.image.ImageObserver.imageUpdate.

This method is called when information about an image which was previously requested using an asynchronous interface becomes available.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ImageHandlerback to summary
private ImageHandler()

Method Detail

imageUpdateback to summary
public boolean imageUpdate(Image img, int flags, int x, int y, int newWidth, int newHeight)

Implements java.awt.image.ImageObserver.imageUpdate.

Doc from java.awt.image.ImageObserver.imageUpdate.

This method is called when information about an image which was previously requested using an asynchronous interface becomes available. Asynchronous interfaces are method calls such as getWidth(ImageObserver) and drawImage(img, x, y, ImageObserver) which take an ImageObserver object as an argument. Those methods register the caller as interested either in information about the overall image itself (in the case of getWidth(ImageObserver)) or about an output version of an image (in the case of the drawImage(img, x, y, [w, h,] ImageObserver) call).

This method should return true if further updates are needed or false if the required information has been acquired. The image which was being tracked is passed in using the img argument. Various constants are combined to form the infoflags argument which indicates what information about the image is now available. The interpretation of the x, y, width, and height arguments depends on the contents of the infoflags argument.

The infoflags argument should be the bitwise inclusive OR of the following flags: WIDTH, HEIGHT, PROPERTIES, SOMEBITS, FRAMEBITS, ALLBITS, ERROR, ABORT.

javax.swing.text.html back to summary

private Class ImageView.ImageLabelView

extends InlineView
Class Inheritance

ImageLabelView is used if the image can't be loaded, and the attribute specified an alt attribute. It overridden a handle of methods as the text is hardcoded and does not come from the document.

Field Summary

Modifier and TypeField and Description
private Color
private Segment

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public View
breakView(int axis, int p0, float pos, float len)

Overrides javax.swing.text.html.InlineView.breakView.

Tries to break this view on the given axis.

public int
getEndOffset()

Overrides javax.swing.text.GlyphView.getEndOffset.

Fetches the portion of the model that this view is responsible for.

public Color
getForeground()

Overrides javax.swing.text.LabelView.getForeground.

Fetches the foreground color to use to render the glyphs.

public int
getStartOffset()

Overrides javax.swing.text.GlyphView.getStartOffset.

Fetches the portion of the model that this view is responsible for.

public Segment
getText(int p0, int p1)

Overrides javax.swing.text.GlyphView.getText.

Fetch a reference to the text that occupies the given range.

public void
paint(Graphics g, Shape a)

Overrides javax.swing.text.GlyphView.paint.

Renders a portion of a text style run.

public void
reset(String text)

Inherited from javax.swing.text.html.InlineView:
changedUpdategetAttributesgetBreakWeightgetStyleSheetinsertUpdateremoveUpdatesetPropertiesFromAttributes

Field Detail

fgback to summary
private Color fg

Hides javax.swing.text.LabelView.fg.

segmentback to summary
private Segment segment

Constructor Detail

ImageLabelViewback to summary
pack-priv ImageLabelView(Element e, String text)

Method Detail

breakViewback to summary
public View breakView(int axis, int p0, float pos, float len)

Overrides javax.swing.text.html.InlineView.breakView.

Doc from javax.swing.text.html.InlineView.breakView.

Tries to break this view on the given axis. Refer to javax.swing.text.View#breakView for a complete description of this method.

Behavior of this method is unspecified in case axis is neither View.X_AXIS nor View.Y_AXIS, and in case offset, pos, or len is null.

getEndOffsetback to summary
public int getEndOffset()

Overrides javax.swing.text.GlyphView.getEndOffset.

Doc from javax.swing.text.GlyphView.getEndOffset.

Fetches the portion of the model that this view is responsible for.

getForegroundback to summary
public Color getForeground()

Overrides javax.swing.text.LabelView.getForeground.

Doc from javax.swing.text.LabelView.getForeground.

Fetches the foreground color to use to render the glyphs. This is implemented to return a cached foreground color, which defaults to null.

getStartOffsetback to summary
public int getStartOffset()

Overrides javax.swing.text.GlyphView.getStartOffset.

Doc from javax.swing.text.GlyphView.getStartOffset.

Fetches the portion of the model that this view is responsible for.

getTextback to summary
public Segment getText(int p0, int p1)

Overrides javax.swing.text.GlyphView.getText.

Doc from javax.swing.text.GlyphView.getText.

Fetch a reference to the text that occupies the given range. This is normally used by the GlyphPainter to determine what characters it should render glyphs for.

paintback to summary
public void paint(Graphics g, Shape a)

Overrides javax.swing.text.GlyphView.paint.

Doc from javax.swing.text.GlyphView.paint.

Renders a portion of a text style run.

resetback to summary
public void reset(String text)