javax.swing.text

Class GlyphView

Implemented Interfaces:
Cloneable, SwingConstants, TabableView
Known Direct Subclasses:
LabelView

public class GlyphView
extends View
implements TabableView, Cloneable

Renders a run of styled text. This View subclass paints the characters of the Element it is responsible for using the style information from that Element.

Nested Class Summary

static class
GlyphView.GlyphPainter
An abstract base implementation for a glyph painter for GlyphView.

Field Summary

Fields inherited from class javax.swing.text.View

BadBreakWeight, ExcellentBreakWeight, ForcedBreakWeight, GoodBreakWeight, X_AXIS, Y_AXIS

Fields inherited from interface javax.swing.SwingConstants

BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST

Constructor Summary

GlyphView(Element element)
Creates a new GlyphView for the given Element.

Method Summary

View
breakView(int axis, int p0, float pos, float len)
Tries to break the view near the specified view span len.
void
changedUpdate(DocumentEvent e, Shape a, ViewFactory vf)
Receives notification that some text attributes have changed within the text fragment that this view is responsible for.
protected void
checkPainter()
Checks if a GlyphPainer is installed.
protected Object
clone()
Creates and returns a shallow clone of this GlyphView.
View
createFragment(int p0, int p1)
Creates a fragment view of this view that starts at p0 and ends at p1.
float
getAlignment(int axis)
Returns the alignment of this view along the specified axis.
Color
getBackground()
Returns the background color which should be used to paint the text.
int
getBreakWeight(int axis, float pos, float len)
Determines how well the specified view location is suitable for inserting a line break.
int
getEndOffset()
Returns the end offset in the document model of the portion of text that this view is responsible for.
Font
getFont()
Returns the font for the text run for which this GlyphView is responsible.
Color
getForeground()
Returns the foreground color which should be used to paint the text.
GlyphView.GlyphPainter
getGlyphPainter()
Returns the GlyphPainter that is used by this GlyphView.
int
getNextVisualPositionFrom(int pos, Position.Bias bias, Shape a, int direction, Position.Bias[] biasRet)
Returns the model location that should be used to place a caret when moving the caret through the document.
float
getPartialSpan(int p0, int p1)
Returns the span of a portion of the view.
float
getPreferredSpan(int axis)
Returns the preferred span of the content managed by this View along the specified axis.
int
getStartOffset()
Returns the start offset in the document model of the portion of text that this view is responsible for.
TabExpander
getTabExpander()
Return the TabExpander to use.
float
getTabbedSpan(float x, TabExpander te)
Returns the preferred span of this view for tab expansion.
Segment
getText(int p0, int p1)
Returns the text segment that this view is responsible for.
void
insertUpdate(DocumentEvent e, Shape a, ViewFactory vf)
Receives notification that some text has been inserted within the text fragment that this view is responsible for.
boolean
isStrikeThrough()
Determines whether the text should be rendered strike-through or not.
boolean
isSubscript()
Determines whether the text should be rendered as subscript or not.
boolean
isSuperscript()
Determines whether the text should be rendered as superscript or not.
boolean
isUnderline()
Determines whether the text should be rendered as underlined or not.
Shape
modelToView(int pos, Shape a, Position.Bias b)
Maps a position in the document into the coordinate space of the View.
void
paint(Graphics g, Shape a)
Renders the Element that is associated with this View.
void
removeUpdate(DocumentEvent e, Shape a, ViewFactory vf)
Receives notification that some text has been removed within the text fragment that this view is responsible for.
void
setGlyphPainter(GlyphView.GlyphPainter painter)
Sets the GlyphView.GlyphPainter to be used for this GlyphView.
int
viewToModel(float x, float y, Shape a, Position.Bias[] b)
Maps coordinates from the View's space into a position in the document model.

Methods inherited from class javax.swing.text.View

append, breakView, changedUpdate, createFragment, dump, forwardUpdate, forwardUpdateToView, getAlignment, getAttributes, getBreakWeight, getChildAllocation, getContainer, getDocument, getElement, getEndOffset, getGraphics, getMaximumSpan, getMinimumSpan, getNextVisualPositionFrom, getParent, getPreferredSpan, getResizeWeight, getStartOffset, getToolTipText, getView, getViewCount, getViewFactory, getViewIndex, getViewIndex, insert, insertUpdate, isVisible, modelToView, modelToView, modelToView, paint, preferenceChanged, remove, removeAll, removeUpdate, replace, setParent, setSize, updateChildren, updateLayout, viewToModel, viewToModel

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

GlyphView

public GlyphView(Element element)
Creates a new GlyphView for the given Element.
Parameters:
element - the element that is rendered by this GlyphView

Method Details

breakView

public View breakView(int axis,
                      int p0,
                      float pos,
                      float len)
Tries to break the view near the specified view span len. The glyph view can only be broken in the X direction. For Y direction it returns itself.
Overrides:
breakView in interface View
Parameters:
axis - the axis for breaking, may be View.X_AXIS or View.Y_AXIS
p0 - the model location where the fragment should start
pos - the view position along the axis where the fragment starts
len - the desired length of the fragment view
Returns:
the fragment view, or this if breaking was not possible

changedUpdate

public void changedUpdate(DocumentEvent e,
                          Shape a,
                          ViewFactory vf)
Receives notification that some text attributes have changed within the text fragment that this view is responsible for. This calls View.preferenceChanged(View,boolean,boolean) on the parent for both width and height.
Overrides:
changedUpdate in interface View
Parameters:
e - the document event describing the change; not used here
a - the view allocation on screen; not used here
vf - the view factory; not used here

checkPainter

protected void checkPainter()
Checks if a GlyphPainer is installed. If this is not the case, a default painter is installed.

clone

protected final Object clone()
Creates and returns a shallow clone of this GlyphView. This is used by the createFragment(int,int) and breakView(int,int,float,float) methods.
Overrides:
clone in interface Object
Returns:
a shallow clone of this GlyphView

createFragment

public View createFragment(int p0,
                           int p1)
Creates a fragment view of this view that starts at p0 and ends at p1.
Overrides:
createFragment in interface View
Parameters:
p0 - the start location for the fragment view
p1 - the end location for the fragment view
Returns:
the fragment view

getAlignment

public float getAlignment(int axis)
Returns the alignment of this view along the specified axis. For the Y axis this is (height - descent) / height for the used font, so that it is aligned along the baseline. For the X axis the superclass is called.
Overrides:
getAlignment in interface View

getBackground

public Color getBackground()
Returns the background color which should be used to paint the text. This is fetched from the associated element's text attributes using StyleConstants.getBackground(AttributeSet).
Returns:
the background color which should be used to paint the text

getBreakWeight

public int getBreakWeight(int axis,
                          float pos,
                          float len)
Determines how well the specified view location is suitable for inserting a line break. If axis is View.Y_AXIS, then this method forwards to the superclass, if axis is View.X_AXIS then this method returns View.ExcellentBreakWeight if there is a suitable break location (usually whitespace) within the specified view span, or View.GoodBreakWeight if not.
Overrides:
getBreakWeight in interface View
Parameters:
axis - the axis along which the break weight is requested
pos - the starting view location
len - the length of the span at which the view should be broken
Returns:
the break weight

getEndOffset

public int getEndOffset()
Returns the end offset in the document model of the portion of text that this view is responsible for.
Overrides:
getEndOffset in interface View
Returns:
the end offset in the document model of the portion of text that this view is responsible for

getFont

public Font getFont()
Returns the font for the text run for which this GlyphView is responsible.
Returns:
the font for the text run for which this GlyphView is responsible

getForeground

public Color getForeground()
Returns the foreground color which should be used to paint the text. This is fetched from the associated element's text attributes using StyleConstants.getForeground(AttributeSet).
Returns:
the foreground color which should be used to paint the text

getGlyphPainter

public GlyphView.GlyphPainter getGlyphPainter()
Returns the GlyphPainter that is used by this GlyphView. If no GlyphPainer has been installed null is returned.
Returns:
the glyph painter that is used by this glyph view or null if no glyph painter has been installed

getNextVisualPositionFrom

public int getNextVisualPositionFrom(int pos,
                                     Position.Bias bias,
                                     Shape a,
                                     int direction,
                                     Position.Bias[] biasRet)
            throws BadLocationException
Returns the model location that should be used to place a caret when moving the caret through the document.
Overrides:
getNextVisualPositionFrom in interface View
Parameters:
pos - the current model location
bias - the bias for p
a - the allocated region for the glyph view
direction - the direction from the current position; Must be one of SwingConstants.EAST, SwingConstants.WEST, SwingConstants.NORTH or SwingConstants.SOUTH
biasRet - filled with the bias of the resulting location when method returns
Returns:
the location within the document that should be used to place the caret when moving the caret around the document
Throws:
BadLocationException - if pos is an invalid model location
IllegalArgumentException - if d is invalid

getPartialSpan

public float getPartialSpan(int p0,
                            int p1)
Returns the span of a portion of the view. This is used in TAB expansion for fragments that don't contain TABs.
Specified by:
getPartialSpan in interface TabableView
Parameters:
p0 - the start index
p1 - the end index
Returns:
the span of the specified portion of the view

getPreferredSpan

public float getPreferredSpan(int axis)
Returns the preferred span of the content managed by this View along the specified axis.
Overrides:
getPreferredSpan in interface View
Parameters:
axis - the axis
Returns:
the preferred span of this View.

getStartOffset

public int getStartOffset()
Returns the start offset in the document model of the portion of text that this view is responsible for.
Overrides:
getStartOffset in interface View
Returns:
the start offset in the document model of the portion of text that this view is responsible for

getTabExpander

public TabExpander getTabExpander()
Return the TabExpander to use.
Returns:
the TabExpander to use

getTabbedSpan

public float getTabbedSpan(float x,
                           TabExpander te)
Returns the preferred span of this view for tab expansion.
Specified by:
getTabbedSpan in interface TabableView
Parameters:
x - the location of the view
te - the tab expander to use
Returns:
the preferred span of this view for tab expansion

getText

public Segment getText(int p0,
                       int p1)
Returns the text segment that this view is responsible for.
Parameters:
p0 - the start index in the document model
p1 - the end index in the document model
Returns:
the text segment that this view is responsible for

insertUpdate

public void insertUpdate(DocumentEvent e,
                         Shape a,
                         ViewFactory vf)
Receives notification that some text has been inserted within the text fragment that this view is responsible for. This calls View.preferenceChanged(View,boolean,boolean) for the direction in which the glyphs are rendered.
Overrides:
insertUpdate in interface View
Parameters:
e - the document event describing the change; not used here
a - the view allocation on screen; not used here
vf - the view factory; not used here

isStrikeThrough

public boolean isStrikeThrough()
Determines whether the text should be rendered strike-through or not. This is determined using the method StyleConstants.isStrikeThrough(AttributeSet) on the element of this view.
Returns:
whether the text should be rendered strike-through or not

isSubscript

public boolean isSubscript()
Determines whether the text should be rendered as subscript or not. This is determined using the method StyleConstants.isSubscript(AttributeSet) on the element of this view.
Returns:
whether the text should be rendered as subscript or not

isSuperscript

public boolean isSuperscript()
Determines whether the text should be rendered as superscript or not. This is determined using the method StyleConstants.isSuperscript(AttributeSet) on the element of this view.
Returns:
whether the text should be rendered as superscript or not

isUnderline

public boolean isUnderline()
Determines whether the text should be rendered as underlined or not. This is determined using the method StyleConstants.isUnderline(AttributeSet) on the element of this view.
Returns:
whether the text should be rendered as underlined or not

modelToView

public Shape modelToView(int pos,
                         Shape a,
                         Position.Bias b)
            throws BadLocationException
Maps a position in the document into the coordinate space of the View. The output rectangle usually reflects the font height but has a width of zero.
Overrides:
modelToView in interface View
Parameters:
pos - the position of the character in the model
a - the area that is occupied by the view
b - either Position.Bias.Forward or Position.Bias.Backward depending on the preferred direction bias. If null this defaults to Position.Bias.Forward
Returns:
a rectangle that gives the location of the document position inside the view coordinate space
Throws:
BadLocationException - if pos is invalid
IllegalArgumentException - if b is not one of the above listed valid values

paint

public void paint(Graphics g,
                  Shape a)
Renders the Element that is associated with this View.
Overrides:
paint in interface View
Parameters:
g - the Graphics context to render to
a - the allocated region for the Element

removeUpdate

public void removeUpdate(DocumentEvent e,
                         Shape a,
                         ViewFactory vf)
Receives notification that some text has been removed within the text fragment that this view is responsible for. This calls View.preferenceChanged(View,boolean,boolean) on the parent for width.
Overrides:
removeUpdate in interface View
Parameters:
e - the document event describing the change; not used here
a - the view allocation on screen; not used here
vf - the view factory; not used here

setGlyphPainter

public void setGlyphPainter(GlyphView.GlyphPainter painter)
Sets the GlyphView.GlyphPainter to be used for this GlyphView.
Parameters:
painter - the glyph painter to be used for this glyph view

viewToModel

public int viewToModel(float x,
                       float y,
                       Shape a,
                       Position.Bias[] b)
Maps coordinates from the View's space into a position in the document model.
Overrides:
viewToModel in interface View
Parameters:
x - the x coordinate in the view space
y - the y coordinate in the view space
a - the allocation of this View
b - the bias to use
Returns:
the position in the document that corresponds to the screen coordinates x, y

GlyphView.java -- A view to render styled text Copyright (C) 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.