/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* View of plain text (text with only one font and color)
* that does line-wrapping. This view expects that its
* associated element has child elements that represent
* the lines it should be wrapping. It is implemented
* as a vertical box that contains logical line views.
* The logical line views are nested classes that render
* the logical line as multiple physical line if the logical
* line is too wide to fit within the allocation. The
* line views draw upon the outer class for its state
* to reduce their memory requirements.
* <p>
* The line views do all of their rendering through the
* <code>drawLine</code> method which in turn does all of
* its rendering through the <code>drawSelectedText</code>
* and <code>drawUnselectedText</code> methods. This
* enables subclasses to easily specialize the rendering
* without concern for the layout aspects.
*
* @author Timothy Prinzing
* @see View
*/
/**
* Creates a new WrappedPlainView. Lines will be wrapped
* on character boundaries.
*
* @param elem the element underlying the view
*/
this(elem, false);
}
/**
* Creates a new WrappedPlainView. Lines can be wrapped on
* either character or word boundaries depending upon the
* setting of the wordWrap parameter.
*
* @param elem the element underlying the view
* @param wordWrap should lines be wrapped on word boundaries?
*/
}
/**
* Returns the tab size set for the document, defaulting to 8.
*
* @return the tab size
*/
protected int getTabSize() {
return size;
}
/**
* Renders a line of text, suppressing whitespace at the end
* and expanding any tabs. This is implemented to make calls
* to the methods <code>drawUnselectedText</code> and
* <code>drawSelectedText</code> so that the way selected and
* unselected text are rendered can be customized.
*
* @param p0 the starting document location to use >= 0
* @param p1 the ending document location to use >= p1
* @param g the graphics context
* @param x the starting X position >= 0
* @param y the starting Y position >= 0
* @see #drawUnselectedText
* @see #drawSelectedText
*/
try {
} else {
// this line contains the composed text.
}
}
} catch (BadLocationException e) {
}
}
private int drawText(Element elem, int p0, int p1, Graphics g, int x, int y) throws BadLocationException {
g.setColor(unselected);
} else {
// no selection, or it is invisible
} else {
}
} else {
}
}
return x;
}
/**
* Renders the given range in the model as normal unselected
* text.
*
* @param g the graphics context
* @param x the starting X coordinate >= 0
* @param y the starting Y coordinate >= 0
* @param p0 the beginning position in the model >= 0
* @param p1 the ending position in the model >= p0
* @return the X location of the end of the range >= 0
* @exception BadLocationException if the range is invalid
*/
g.setColor(unselected);
return ret;
}
/**
* Renders the given range in the model as selected text. This
* is implemented to render the text in the color specified in
* the hosting component. It assumes the highlighter will render
* the selected background.
*
* @param g the graphics context
* @param x the starting X coordinate >= 0
* @param y the starting Y coordinate >= 0
* @param p0 the beginning position in the model >= 0
* @param p1 the ending position in the model >= p0
* @return the location of the end of the range.
* @exception BadLocationException if the range is invalid
*/
return ret;
}
/**
* Gives access to a buffer that can be used to fetch
* text from the associated document.
*
* @return the buffer
*/
if (lineBuffer == null) {
lineBuffer = new Segment();
}
return lineBuffer;
}
/**
* This is called by the nested wrapped line
* views to determine the break location. This can
* be reimplemented to alter the breaking behavior.
* It will either break at word or character boundaries
* depending upon the break argument given at
* construction.
*/
int p;
int currentWidth = getWidth();
if (wordWrap) {
this, p0);
} else {
this, p0, false);
}
return p;
}
/**
* Loads all of the children to initialize the view.
* This is called by the <code>setParent</code> method.
* Subclasses can reimplement this to initialize their
* child views in a different manner. The default
* implementation creates a child view for each
* child element.
*
* @param f the view factory
*/
Element e = getElement();
int n = e.getElementCount();
if (n > 0) {
for (int i = 0; i < n; i++) {
}
}
}
/**
* Update the child views in response to a
* document event.
*/
// the structure of this element changed.
}
// should damge a little more intelligently.
if (a != null) {
preferenceChanged(null, true, true);
getContainer().repaint();
}
}
// update font metrics which may be used by the child views
}
/**
* Load the text buffer with the given range
* of text. This is used by the fragments
* broken off of this view as well as this
* view itself.
*/
try {
} catch (BadLocationException bl) {
throw new StateInvariantError("Can't get line text");
}
}
final void updateMetrics() {
}
// --- TabExpander methods ------------------------------------------
/**
* Returns the next tab stop position after a given reference position.
* This implementation does not support things like centering so it
* ignores the tabOffset argument.
*
* @param x the current position >= 0
* @param tabOffset the position within the text stream
* that the tab occurred at >= 0.
* @return the tab stop, measured in points >= 0
*/
if (tabSize == 0)
return x;
}
// --- View methods -------------------------------------
/**
* Renders using the given rendering surface and area
* on that surface. This is implemented to stash the
* selection positions, selection colors, and font
* metrics for the nested lines to use.
*
* @param g the rendering surface to use
* @param a the allocated region to render into
*
* @see View#paint
*/
// superclass paints the children
super.paint(g, a);
}
/**
* Sets the size of the view. This should cause
* layout of the view along the given axis, if it
* has any layout duties.
*
* @param width the width >= 0
* @param height the height >= 0
*/
// invalidate the view itself since the childrens
// desired widths will be based upon this views width.
preferenceChanged(null, true, true);
widthChanging = true;
}
widthChanging = false;
}
/**
* Determines the preferred span for this view along an
* axis. This is implemented to provide the superclass
* behavior after first making sure that the current font
* metrics are cached (for the nested lines which use
* the metrics to determine the height of the potentially
* wrapped lines).
*
* @param axis may be either View.X_AXIS or View.Y_AXIS
* @return 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.
* @see View#getPreferredSpan
*/
return super.getPreferredSpan(axis);
}
/**
* Determines the minimum span for this view along an
* axis. This is implemented to provide the superclass
* behavior after first making sure that the current font
* metrics are cached (for the nested lines which use
* the metrics to determine the height of the potentially
* wrapped lines).
*
* @param axis may be either View.X_AXIS or View.Y_AXIS
* @return 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.
* @see View#getMinimumSpan
*/
return super.getMinimumSpan(axis);
}
/**
* Determines the maximum span for this view along an
* axis. This is implemented to provide the superclass
* behavior after first making sure that the current font
* metrics are cached (for the nested lines which use
* the metrics to determine the height of the potentially
* wrapped lines).
*
* @param axis may be either View.X_AXIS or View.Y_AXIS
* @return 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.
* @see View#getMaximumSpan
*/
return super.getMaximumSpan(axis);
}
/**
* Gives notification that something was inserted into the
* document in a location that this view is responsible for.
* This is implemented to simply update the children.
*
* @param e the change information from the associated document
* @param a the current allocation of the view
* @param f the factory to use to rebuild if the view has children
* @see View#insertUpdate
*/
updateChildren(e, a);
getInsideAllocation(a) : null;
if (v != null) {
v.insertUpdate(e, alloc, f);
}
}
/**
* Gives notification that something was removed from the
* document in a location that this view is responsible for.
* This is implemented to simply update the children.
*
* @param e the change information from the associated document
* @param a the current allocation of the view
* @param f the factory to use to rebuild if the view has children
* @see View#removeUpdate
*/
updateChildren(e, a);
getInsideAllocation(a) : null;
if (v != null) {
v.removeUpdate(e, alloc, f);
}
}
/**
* Gives notification from the document that attributes were changed
* in a location that this view is responsible for.
*
* @param e the change information from the associated document
* @param a the current allocation of the view
* @param f the factory to use to rebuild if the view has children
* @see View#changedUpdate
*/
updateChildren(e, a);
}
// --- variables -------------------------------------------
boolean widthChanging;
int tabBase;
int tabSize;
boolean wordWrap;
int sel0;
int sel1;
/**
* Simple view of a line that wraps if it doesn't
* fit withing the horizontal space allocated.
* This class tries to be lightweight by carrying little
* state of it's own and sharing the state of the outer class
* with it's sibblings.
*/
super(elem);
lineCount = -1;
}
/**
* Determines the preferred span for this view along an
* axis.
*
* @param axis may be either X_AXIS or Y_AXIS
* @return 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.
* @see View#getPreferredSpan
*/
switch (axis) {
// We have been initially set to MAX_VALUE, but we don't
// want this as our preferred.
return 100f;
}
return width;
}
default:
}
}
/**
* Renders using the given rendering surface and area on that
* surface. The view may need to do layout and create child
* views to enable itself to render into the given allocation.
*
* @param g the rendering surface to use
* @param a the allocated region to render into
* @see View#paint
*/
int x = alloc.x;
(LayeredHighlighter)h : null;
int start = getStartOffset();
int end = getEndOffset();
int[] lineEnds = getLineEnds();
for (int i = 0; i < lineCount; i++) {
? (p1 - 1)
: p1;
}
}
}
/**
* Provides a mapping from the document model coordinate space
* to the coordinate space of the view mapped to it.
*
* @param pos the position to convert
* @param a the allocated region to render into
* @return the bounding box of the given position is returned
* @exception BadLocationException if the given position does not represent a
* valid location in the associated document
* @see View#modelToView
*/
throws BadLocationException {
int p0 = getStartOffset();
}
int line = 0;
int[] lineEnds = getLineEnds();
if (line > 0) {
}
}
}
return alloc;
}
/**
* Provides a mapping from the view coordinate space to the logical
* coordinate space of the model.
*
* @param fx the X coordinate
* @param fy the Y coordinate
* @param a the allocated region to render into
* @return the location within the model that best represents the
* given point in the view
* @see View#viewToModel
*/
// PENDING(prinz) implement bias properly
int x = (int) fx;
int y = (int) fy;
if (y < alloc.y) {
// above the area covered by this icon, so the the position
// is assumed to be the start of the coverage for this view.
return getStartOffset();
// below the area covered by this icon, so the the position
// is assumed to be the end of the coverage for this view.
return getEndOffset() - 1;
} else {
// positioned within the coverage of this view vertically,
// so we figure out which line the point corresponds to.
// if the line is greater than the number of lines contained, then
// simply use the last line as it represents the last possible place
// we can position to.
return getEndOffset() - 1;
} else {
int p0 = getStartOffset();
int p1;
if (lineCount == 1) {
p1 = getEndOffset();
} else {
int[] lineEnds = getLineEnds();
if (line > 0) {
}
}
if (x < alloc.x) {
// point is to the left of the line
return p0;
// point is to the right of the line
return p1 - 1;
} else {
// Determine the offset into the text
alloc.x, x,
WrappedPlainView.this, p0);
}
}
}
}
update(e, a);
}
update(e, a);
}
WrappedPlainView.this.preferenceChanged(this, false, true);
// have to repaint any views after the receiver.
getContainer().repaint();
} else if (a != null) {
Component c = getContainer();
}
}
/**
* Returns line cache. If the cache was GC'ed, recreates it.
* If there's no cache, returns null
*/
final int[] getLineEnds() {
return null;
} else {
// Cache was GC'ed, so rebuild it
return breakLines(getStartOffset());
} else {
return lineEnds;
}
}
}
/**
* Creates line cache if text breaks into more than one physical line.
* @param startPos position to start breaking from
* @return the cache created, ot null if text breaks into one line
*/
int[] oldLineEnds = lineEnds;
int start = getStartOffset();
int lineIndex = 0;
if (lineIndex > 0) {
lineIndex--;
}
}
int p1 = getEndOffset();
// do not use cache if there's only one line
lineIndex = 1;
break;
// we have 2+ lines, and the cache is not big enough
// we try to estimate total number of lines
}
}
}
if (lineCount > 1) {
// check if the cache is too big
int[] tmp = new int[maxCapacity];
}
}
}
return lineEnds;
}
/**
* Binary search in the cache for line containing specified offset
* (which is relative to the beginning of the view). This method
* assumes that cache exists.
*/
return 0;
return lineCount;
} else {
}
}
return max;
} else {
}
}
int lineCount;
}
}