0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text;
0N/A
0N/Aimport java.util.Vector;
0N/Aimport java.util.Properties;
0N/Aimport java.awt.*;
0N/Aimport javax.swing.event.*;
0N/A
0N/A/**
0N/A * Implements View interface for a simple multi-line text view
0N/A * that has text in one font and color. The view represents each
0N/A * child element as a line of text.
0N/A *
0N/A * @author Timothy Prinzing
0N/A * @see View
0N/A */
0N/Apublic class PlainView extends View implements TabExpander {
0N/A
0N/A /**
0N/A * Constructs a new PlainView wrapped on an element.
0N/A *
0N/A * @param elem the element
0N/A */
0N/A public PlainView(Element elem) {
0N/A super(elem);
0N/A }
0N/A
0N/A /**
0N/A * Returns the tab size set for the document, defaulting to 8.
0N/A *
0N/A * @return the tab size
0N/A */
0N/A protected int getTabSize() {
0N/A Integer i = (Integer) getDocument().getProperty(PlainDocument.tabSizeAttribute);
0N/A int size = (i != null) ? i.intValue() : 8;
0N/A return size;
0N/A }
0N/A
0N/A /**
0N/A * Renders a line of text, suppressing whitespace at the end
0N/A * and expanding any tabs. This is implemented to make calls
0N/A * to the methods <code>drawUnselectedText</code> and
0N/A * <code>drawSelectedText</code> so that the way selected and
0N/A * unselected text are rendered can be customized.
0N/A *
0N/A * @param lineIndex the line to draw >= 0
0N/A * @param g the <code>Graphics</code> context
0N/A * @param x the starting X position >= 0
0N/A * @param y the starting Y position >= 0
0N/A * @see #drawUnselectedText
0N/A * @see #drawSelectedText
0N/A */
0N/A protected void drawLine(int lineIndex, Graphics g, int x, int y) {
0N/A Element line = getElement().getElement(lineIndex);
0N/A Element elem;
0N/A
0N/A try {
0N/A if (line.isLeaf()) {
0N/A drawElement(lineIndex, line, g, x, y);
0N/A } else {
0N/A // this line contains the composed text.
0N/A int count = line.getElementCount();
0N/A for(int i = 0; i < count; i++) {
0N/A elem = line.getElement(i);
0N/A x = drawElement(lineIndex, elem, g, x, y);
0N/A }
0N/A }
0N/A } catch (BadLocationException e) {
0N/A throw new StateInvariantError("Can't render line: " + lineIndex);
0N/A }
0N/A }
0N/A
0N/A private int drawElement(int lineIndex, Element elem, Graphics g, int x, int y) throws BadLocationException {
0N/A int p0 = elem.getStartOffset();
0N/A int p1 = elem.getEndOffset();
0N/A p1 = Math.min(getDocument().getLength(), p1);
0N/A
0N/A if (lineIndex == 0) {
0N/A x += firstLineOffset;
0N/A }
0N/A AttributeSet attr = elem.getAttributes();
0N/A if (Utilities.isComposedTextAttributeDefined(attr)) {
0N/A g.setColor(unselected);
0N/A x = Utilities.drawComposedText(this, attr, g, x, y,
0N/A p0-elem.getStartOffset(),
0N/A p1-elem.getStartOffset());
0N/A } else {
0N/A if (sel0 == sel1 || selected == unselected) {
0N/A // no selection, or it is invisible
0N/A x = drawUnselectedText(g, x, y, p0, p1);
0N/A } else if ((p0 >= sel0 && p0 <= sel1) && (p1 >= sel0 && p1 <= sel1)) {
0N/A x = drawSelectedText(g, x, y, p0, p1);
0N/A } else if (sel0 >= p0 && sel0 <= p1) {
0N/A if (sel1 >= p0 && sel1 <= p1) {
0N/A x = drawUnselectedText(g, x, y, p0, sel0);
0N/A x = drawSelectedText(g, x, y, sel0, sel1);
0N/A x = drawUnselectedText(g, x, y, sel1, p1);
0N/A } else {
0N/A x = drawUnselectedText(g, x, y, p0, sel0);
0N/A x = drawSelectedText(g, x, y, sel0, p1);
0N/A }
0N/A } else if (sel1 >= p0 && sel1 <= p1) {
0N/A x = drawSelectedText(g, x, y, p0, sel1);
0N/A x = drawUnselectedText(g, x, y, sel1, p1);
0N/A } else {
0N/A x = drawUnselectedText(g, x, y, p0, p1);
0N/A }
0N/A }
0N/A
0N/A return x;
0N/A }
0N/A
0N/A /**
0N/A * Renders the given range in the model as normal unselected
0N/A * text. Uses the foreground or disabled color to render the text.
0N/A *
0N/A * @param g the graphics context
0N/A * @param x the starting X coordinate >= 0
0N/A * @param y the starting Y coordinate >= 0
0N/A * @param p0 the beginning position in the model >= 0
0N/A * @param p1 the ending position in the model >= 0
0N/A * @return the X location of the end of the range >= 0
0N/A * @exception BadLocationException if the range is invalid
0N/A */
0N/A protected int drawUnselectedText(Graphics g, int x, int y,
0N/A int p0, int p1) throws BadLocationException {
0N/A g.setColor(unselected);
0N/A Document doc = getDocument();
0N/A Segment s = SegmentCache.getSharedSegment();
0N/A doc.getText(p0, p1 - p0, s);
0N/A int ret = Utilities.drawTabbedText(this, s, x, y, g, this, p0);
0N/A SegmentCache.releaseSharedSegment(s);
0N/A return ret;
0N/A }
0N/A
0N/A /**
0N/A * Renders the given range in the model as selected text. This
0N/A * is implemented to render the text in the color specified in
0N/A * the hosting component. It assumes the highlighter will render
0N/A * the selected background.
0N/A *
0N/A * @param g the graphics context
0N/A * @param x the starting X coordinate >= 0
0N/A * @param y the starting Y coordinate >= 0
0N/A * @param p0 the beginning position in the model >= 0
0N/A * @param p1 the ending position in the model >= 0
0N/A * @return the location of the end of the range
0N/A * @exception BadLocationException if the range is invalid
0N/A */
0N/A protected int drawSelectedText(Graphics g, int x,
0N/A int y, int p0, int p1) throws BadLocationException {
0N/A g.setColor(selected);
0N/A Document doc = getDocument();
0N/A Segment s = SegmentCache.getSharedSegment();
0N/A doc.getText(p0, p1 - p0, s);
0N/A int ret = Utilities.drawTabbedText(this, s, x, y, g, this, p0);
0N/A SegmentCache.releaseSharedSegment(s);
0N/A return ret;
0N/A }
0N/A
0N/A /**
0N/A * Gives access to a buffer that can be used to fetch
0N/A * text from the associated document.
0N/A *
0N/A * @return the buffer
0N/A */
0N/A protected final Segment getLineBuffer() {
0N/A if (lineBuffer == null) {
0N/A lineBuffer = new Segment();
0N/A }
0N/A return lineBuffer;
0N/A }
0N/A
0N/A /**
0N/A * Checks to see if the font metrics and longest line
0N/A * are up-to-date.
0N/A *
0N/A * @since 1.4
0N/A */
0N/A protected void updateMetrics() {
0N/A Component host = getContainer();
0N/A Font f = host.getFont();
0N/A if (font != f) {
0N/A // The font changed, we need to recalculate the
0N/A // longest line.
0N/A calculateLongestLine();
0N/A tabSize = getTabSize() * metrics.charWidth('m');
0N/A }
0N/A }
0N/A
0N/A // ---- View methods ----------------------------------------------------
0N/A
0N/A /**
0N/A * Determines the preferred span for this view along an
0N/A * axis.
0N/A *
0N/A * @param axis may be either View.X_AXIS or View.Y_AXIS
0N/A * @return the span the view would like to be rendered into >= 0.
0N/A * Typically the view is told to render into the span
0N/A * that is returned, although there is no guarantee.
0N/A * The parent may choose to resize or break the view.
0N/A * @exception IllegalArgumentException for an invalid axis
0N/A */
0N/A public float getPreferredSpan(int axis) {
0N/A updateMetrics();
0N/A switch (axis) {
0N/A case View.X_AXIS:
0N/A return getLineWidth(longLine);
0N/A case View.Y_AXIS:
0N/A return getElement().getElementCount() * metrics.getHeight();
0N/A default:
0N/A throw new IllegalArgumentException("Invalid axis: " + axis);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Renders using the given rendering surface and area on that surface.
0N/A * The view may need to do layout and create child views to enable
0N/A * itself to render into the given allocation.
0N/A *
0N/A * @param g the rendering surface to use
0N/A * @param a the allocated region to render into
0N/A *
0N/A * @see View#paint
0N/A */
0N/A public void paint(Graphics g, Shape a) {
0N/A Shape originalA = a;
0N/A a = adjustPaintRegion(a);
0N/A Rectangle alloc = (Rectangle) a;
0N/A tabBase = alloc.x;
0N/A JTextComponent host = (JTextComponent) getContainer();
0N/A Highlighter h = host.getHighlighter();
0N/A g.setFont(host.getFont());
0N/A sel0 = host.getSelectionStart();
0N/A sel1 = host.getSelectionEnd();
0N/A unselected = (host.isEnabled()) ?
0N/A host.getForeground() : host.getDisabledTextColor();
0N/A Caret c = host.getCaret();
0N/A selected = c.isSelectionVisible() && h != null ?
0N/A host.getSelectedTextColor() : unselected;
0N/A updateMetrics();
0N/A
0N/A // If the lines are clipped then we don't expend the effort to
0N/A // try and paint them. Since all of the lines are the same height
0N/A // with this object, determination of what lines need to be repainted
0N/A // is quick.
0N/A Rectangle clip = g.getClipBounds();
0N/A int fontHeight = metrics.getHeight();
0N/A int heightBelow = (alloc.y + alloc.height) - (clip.y + clip.height);
0N/A int heightAbove = clip.y - alloc.y;
0N/A int linesBelow, linesAbove, linesTotal;
0N/A
0N/A if (fontHeight > 0) {
0N/A linesBelow = Math.max(0, heightBelow / fontHeight);
0N/A linesAbove = Math.max(0, heightAbove / fontHeight);
0N/A linesTotal = alloc.height / fontHeight;
0N/A if (alloc.height % fontHeight != 0) {
0N/A linesTotal++;
0N/A }
0N/A } else {
0N/A linesBelow = linesAbove = linesTotal = 0;
0N/A }
0N/A
0N/A // update the visible lines
0N/A Rectangle lineArea = lineToRect(a, linesAbove);
0N/A int y = lineArea.y + metrics.getAscent();
0N/A int x = lineArea.x;
0N/A Element map = getElement();
0N/A int lineCount = map.getElementCount();
0N/A int endLine = Math.min(lineCount, linesTotal - linesBelow);
0N/A lineCount--;
0N/A LayeredHighlighter dh = (h instanceof LayeredHighlighter) ?
0N/A (LayeredHighlighter)h : null;
0N/A for (int line = linesAbove; line < endLine; line++) {
0N/A if (dh != null) {
0N/A Element lineElement = map.getElement(line);
0N/A if (line == lineCount) {
0N/A dh.paintLayeredHighlights(g, lineElement.getStartOffset(),
0N/A lineElement.getEndOffset(),
0N/A originalA, host, this);
0N/A }
0N/A else {
0N/A dh.paintLayeredHighlights(g, lineElement.getStartOffset(),
0N/A lineElement.getEndOffset() - 1,
0N/A originalA, host, this);
0N/A }
0N/A }
0N/A drawLine(line, g, x, y);
0N/A y += fontHeight;
0N/A if (line == 0) {
0N/A // This should never really happen, in so far as if
0N/A // firstLineOffset is non 0, there should only be one
0N/A // line of text.
0N/A x -= firstLineOffset;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Should return a shape ideal for painting based on the passed in
0N/A * Shape <code>a</code>. This is useful if painting in a different
0N/A * region. The default implementation returns <code>a</code>.
0N/A */
0N/A Shape adjustPaintRegion(Shape a) {
0N/A return a;
0N/A }
0N/A
0N/A /**
0N/A * Provides a mapping from the document model coordinate space
0N/A * to the coordinate space of the view mapped to it.
0N/A *
0N/A * @param pos the position to convert >= 0
0N/A * @param a the allocated region to render into
0N/A * @return the bounding box of the given position
0N/A * @exception BadLocationException if the given position does not
0N/A * represent a valid location in the associated document
0N/A * @see View#modelToView
0N/A */
0N/A public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
0N/A // line coordinates
0N/A Document doc = getDocument();
0N/A Element map = getElement();
0N/A int lineIndex = map.getElementIndex(pos);
0N/A if (lineIndex < 0) {
0N/A return lineToRect(a, 0);
0N/A }
0N/A Rectangle lineArea = lineToRect(a, lineIndex);
0N/A
0N/A // determine span from the start of the line
0N/A tabBase = lineArea.x;
0N/A Element line = map.getElement(lineIndex);
0N/A int p0 = line.getStartOffset();
0N/A Segment s = SegmentCache.getSharedSegment();
0N/A doc.getText(p0, pos - p0, s);
0N/A int xOffs = Utilities.getTabbedTextWidth(s, metrics, tabBase, this,p0);
0N/A SegmentCache.releaseSharedSegment(s);
0N/A
0N/A // fill in the results and return
0N/A lineArea.x += xOffs;
0N/A lineArea.width = 1;
0N/A lineArea.height = metrics.getHeight();
0N/A return lineArea;
0N/A }
0N/A
0N/A /**
0N/A * Provides a mapping from the view coordinate space to the logical
0N/A * coordinate space of the model.
0N/A *
0N/A * @param fx the X coordinate >= 0
0N/A * @param fy the Y coordinate >= 0
0N/A * @param a the allocated region to render into
0N/A * @return the location within the model that best represents the
0N/A * given point in the view >= 0
0N/A * @see View#viewToModel
0N/A */
0N/A public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
0N/A // PENDING(prinz) properly calculate bias
0N/A bias[0] = Position.Bias.Forward;
0N/A
0N/A Rectangle alloc = a.getBounds();
0N/A Document doc = getDocument();
0N/A int x = (int) fx;
0N/A int y = (int) fy;
0N/A if (y < alloc.y) {
0N/A // above the area covered by this icon, so the the position
0N/A // is assumed to be the start of the coverage for this view.
0N/A return getStartOffset();
0N/A } else if (y > alloc.y + alloc.height) {
0N/A // below the area covered by this icon, so the the position
0N/A // is assumed to be the end of the coverage for this view.
0N/A return getEndOffset() - 1;
0N/A } else {
0N/A // positioned within the coverage of this view vertically,
0N/A // so we figure out which line the point corresponds to.
0N/A // if the line is greater than the number of lines contained, then
0N/A // simply use the last line as it represents the last possible place
0N/A // we can position to.
0N/A Element map = doc.getDefaultRootElement();
0N/A int fontHeight = metrics.getHeight();
0N/A int lineIndex = (fontHeight > 0 ?
0N/A Math.abs((y - alloc.y) / fontHeight) :
0N/A map.getElementCount() - 1);
0N/A if (lineIndex >= map.getElementCount()) {
0N/A return getEndOffset() - 1;
0N/A }
0N/A Element line = map.getElement(lineIndex);
0N/A int dx = 0;
0N/A if (lineIndex == 0) {
0N/A alloc.x += firstLineOffset;
0N/A alloc.width -= firstLineOffset;
0N/A }
0N/A if (x < alloc.x) {
0N/A // point is to the left of the line
0N/A return line.getStartOffset();
0N/A } else if (x > alloc.x + alloc.width) {
0N/A // point is to the right of the line
0N/A return line.getEndOffset() - 1;
0N/A } else {
0N/A // Determine the offset into the text
0N/A try {
0N/A int p0 = line.getStartOffset();
0N/A int p1 = line.getEndOffset() - 1;
0N/A Segment s = SegmentCache.getSharedSegment();
0N/A doc.getText(p0, p1 - p0, s);
0N/A tabBase = alloc.x;
0N/A int offs = p0 + Utilities.getTabbedTextOffset(s, metrics,
0N/A tabBase, x, this, p0);
0N/A SegmentCache.releaseSharedSegment(s);
0N/A return offs;
0N/A } catch (BadLocationException e) {
0N/A // should not happen
0N/A return -1;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gives notification that something was inserted into the document
0N/A * in a location that this view is responsible for.
0N/A *
0N/A * @param changes the change information from the associated document
0N/A * @param a the current allocation of the view
0N/A * @param f the factory to use to rebuild if the view has children
0N/A * @see View#insertUpdate
0N/A */
0N/A public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
0N/A updateDamage(changes, a, f);
0N/A }
0N/A
0N/A /**
0N/A * Gives notification that something was removed from the document
0N/A * in a location that this view is responsible for.
0N/A *
0N/A * @param changes the change information from the associated document
0N/A * @param a the current allocation of the view
0N/A * @param f the factory to use to rebuild if the view has children
0N/A * @see View#removeUpdate
0N/A */
0N/A public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
0N/A updateDamage(changes, a, f);
0N/A }
0N/A
0N/A /**
0N/A * Gives notification from the document that attributes were changed
0N/A * in a location that this view is responsible for.
0N/A *
0N/A * @param changes the change information from the associated document
0N/A * @param a the current allocation of the view
0N/A * @param f the factory to use to rebuild if the view has children
0N/A * @see View#changedUpdate
0N/A */
0N/A public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
0N/A updateDamage(changes, a, f);
0N/A }
0N/A
0N/A /**
0N/A * Sets the size of the view. This should cause
0N/A * layout of the view along the given axis, if it
0N/A * has any layout duties.
0N/A *
0N/A * @param width the width >= 0
0N/A * @param height the height >= 0
0N/A */
0N/A public void setSize(float width, float height) {
0N/A super.setSize(width, height);
0N/A updateMetrics();
0N/A }
0N/A
0N/A // --- TabExpander methods ------------------------------------------
0N/A
0N/A /**
0N/A * Returns the next tab stop position after a given reference position.
0N/A * This implementation does not support things like centering so it
0N/A * ignores the tabOffset argument.
0N/A *
0N/A * @param x the current position >= 0
0N/A * @param tabOffset the position within the text stream
0N/A * that the tab occurred at >= 0.
0N/A * @return the tab stop, measured in points >= 0
0N/A */
0N/A public float nextTabStop(float x, int tabOffset) {
0N/A if (tabSize == 0) {
0N/A return x;
0N/A }
0N/A int ntabs = (((int) x) - tabBase) / tabSize;
0N/A return tabBase + ((ntabs + 1) * tabSize);
0N/A }
0N/A
0N/A // --- local methods ------------------------------------------------
0N/A
0N/A /**
0N/A * Repaint the region of change covered by the given document
0N/A * event. Damages the line that begins the range to cover
0N/A * the case when the insert/remove is only on one line.
0N/A * If lines are added or removed, damages the whole
0N/A * view. The longest line is checked to see if it has
0N/A * changed.
0N/A *
0N/A * @since 1.4
0N/A */
0N/A protected void updateDamage(DocumentEvent changes, Shape a, ViewFactory f) {
0N/A Component host = getContainer();
0N/A updateMetrics();
0N/A Element elem = getElement();
0N/A DocumentEvent.ElementChange ec = changes.getChange(elem);
0N/A
0N/A Element[] added = (ec != null) ? ec.getChildrenAdded() : null;
0N/A Element[] removed = (ec != null) ? ec.getChildrenRemoved() : null;
0N/A if (((added != null) && (added.length > 0)) ||
0N/A ((removed != null) && (removed.length > 0))) {
0N/A // lines were added or removed...
0N/A if (added != null) {
0N/A int currWide = getLineWidth(longLine);
0N/A for (int i = 0; i < added.length; i++) {
0N/A int w = getLineWidth(added[i]);
0N/A if (w > currWide) {
0N/A currWide = w;
0N/A longLine = added[i];
0N/A }
0N/A }
0N/A }
0N/A if (removed != null) {
0N/A for (int i = 0; i < removed.length; i++) {
0N/A if (removed[i] == longLine) {
0N/A calculateLongestLine();
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A preferenceChanged(null, true, true);
0N/A host.repaint();
0N/A } else {
0N/A Element map = getElement();
0N/A int line = map.getElementIndex(changes.getOffset());
0N/A damageLineRange(line, line, a, host);
0N/A if (changes.getType() == DocumentEvent.EventType.INSERT) {
0N/A // check to see if the line is longer than current
0N/A // longest line.
0N/A int w = getLineWidth(longLine);
0N/A Element e = map.getElement(line);
0N/A if (e == longLine) {
0N/A preferenceChanged(null, true, false);
0N/A } else if (getLineWidth(e) > w) {
0N/A longLine = e;
0N/A preferenceChanged(null, true, false);
0N/A }
0N/A } else if (changes.getType() == DocumentEvent.EventType.REMOVE) {
0N/A if (map.getElement(line) == longLine) {
0N/A // removed from longest line... recalc
0N/A calculateLongestLine();
0N/A preferenceChanged(null, true, false);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Repaint the given line range.
0N/A *
0N/A * @param host the component hosting the view (used to call repaint)
0N/A * @param a the region allocated for the view to render into
0N/A * @param line0 the starting line number to repaint. This must
0N/A * be a valid line number in the model.
0N/A * @param line1 the ending line number to repaint. This must
0N/A * be a valid line number in the model.
0N/A * @since 1.4
0N/A */
0N/A protected void damageLineRange(int line0, int line1, Shape a, Component host) {
0N/A if (a != null) {
0N/A Rectangle area0 = lineToRect(a, line0);
0N/A Rectangle area1 = lineToRect(a, line1);
0N/A if ((area0 != null) && (area1 != null)) {
0N/A Rectangle damage = area0.union(area1);
0N/A host.repaint(damage.x, damage.y, damage.width, damage.height);
0N/A } else {
0N/A host.repaint();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Determine the rectangle that represents the given line.
0N/A *
0N/A * @param a the region allocated for the view to render into
0N/A * @param line the line number to find the region of. This must
0N/A * be a valid line number in the model.
0N/A * @since 1.4
0N/A */
0N/A protected Rectangle lineToRect(Shape a, int line) {
0N/A Rectangle r = null;
0N/A updateMetrics();
0N/A if (metrics != null) {
0N/A Rectangle alloc = a.getBounds();
0N/A if (line == 0) {
0N/A alloc.x += firstLineOffset;
0N/A alloc.width -= firstLineOffset;
0N/A }
0N/A r = new Rectangle(alloc.x, alloc.y + (line * metrics.getHeight()),
0N/A alloc.width, metrics.getHeight());
0N/A }
0N/A return r;
0N/A }
0N/A
0N/A /**
0N/A * Iterate over the lines represented by the child elements
0N/A * of the element this view represents, looking for the line
0N/A * that is the longest. The <em>longLine</em> variable is updated to
0N/A * represent the longest line contained. The <em>font</em> variable
0N/A * is updated to indicate the font used to calculate the
0N/A * longest line.
0N/A */
0N/A private void calculateLongestLine() {
0N/A Component c = getContainer();
0N/A font = c.getFont();
0N/A metrics = c.getFontMetrics(font);
0N/A Document doc = getDocument();
0N/A Element lines = getElement();
0N/A int n = lines.getElementCount();
0N/A int maxWidth = -1;
0N/A for (int i = 0; i < n; i++) {
0N/A Element line = lines.getElement(i);
0N/A int w = getLineWidth(line);
0N/A if (w > maxWidth) {
0N/A maxWidth = w;
0N/A longLine = line;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Calculate the width of the line represented by
0N/A * the given element. It is assumed that the font
0N/A * and font metrics are up-to-date.
0N/A */
0N/A private int getLineWidth(Element line) {
0N/A if (line == null) {
0N/A return 0;
0N/A }
0N/A int p0 = line.getStartOffset();
0N/A int p1 = line.getEndOffset();
0N/A int w;
0N/A Segment s = SegmentCache.getSharedSegment();
0N/A try {
0N/A line.getDocument().getText(p0, p1 - p0, s);
0N/A w = Utilities.getTabbedTextWidth(s, metrics, tabBase, this, p0);
0N/A } catch (BadLocationException ble) {
0N/A w = 0;
0N/A }
0N/A SegmentCache.releaseSharedSegment(s);
0N/A return w;
0N/A }
0N/A
0N/A // --- member variables -----------------------------------------------
0N/A
0N/A /**
0N/A * Font metrics for the current font.
0N/A */
0N/A protected FontMetrics metrics;
0N/A
0N/A /**
0N/A * The current longest line. This is used to calculate
0N/A * the preferred width of the view. Since the calculation
0N/A * is potentially expensive we try to avoid it by stashing
0N/A * which line is currently the longest.
0N/A */
0N/A Element longLine;
0N/A
0N/A /**
0N/A * Font used to calculate the longest line... if this
0N/A * changes we need to recalculate the longest line
0N/A */
0N/A Font font;
0N/A
0N/A Segment lineBuffer;
0N/A int tabSize;
0N/A int tabBase;
0N/A
0N/A int sel0;
0N/A int sel1;
0N/A Color unselected;
0N/A Color selected;
0N/A
0N/A /**
0N/A * Offset of where to draw the first character on the first line.
0N/A * This is a hack and temporary until we can better address the problem
0N/A * of text measuring. This field is actually never set directly in
0N/A * PlainView, but by FieldView.
0N/A */
0N/A int firstLineOffset;
0N/A
0N/A}