CompositeView.java 26.4 KB
Newer Older
Tom Tromey committed
1
/* CompositeView.java -- An abstract view that manages child views
2
   Copyright (C) 2005, 2006  Free Software Foundation, Inc.
Tom Tromey committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

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. */


package javax.swing.text;

import java.awt.Rectangle;
import java.awt.Shape;

import javax.swing.SwingConstants;

/**
 * An abstract base implementation of {@link View} that manages child
 * <code>View</code>s.
 *
 * @author Roman Kennke (roman@kennke.org)
 */
public abstract class CompositeView
  extends View
{

  /**
   * The child views of this <code>CompositeView</code>.
   */
59 60 61 62 63 64
  private View[] children;

  /**
   * The number of child views.
   */
  private int numChildren;
Tom Tromey committed
65 66 67 68

  /**
   * The allocation of this <code>View</code> minus its insets. This is
   * initialized in {@link #getInsideAllocation} and reused and modified in
69
   * {@link #childAllocation(int, Rectangle)}.
Tom Tromey committed
70
   */
71
  private final Rectangle insideAllocation = new Rectangle();
Tom Tromey committed
72 73 74 75 76

  /**
   * The insets of this <code>CompositeView</code>. This is initialized
   * in {@link #setInsets}.
   */
77 78 79 80
  private short top;
  private short bottom;
  private short left;
  private short right;
Tom Tromey committed
81 82 83 84 85 86 87 88 89 90 91

  /**
   * Creates a new <code>CompositeView</code> for the given
   * <code>Element</code>.
   *
   * @param element the element that is rendered by this CompositeView
   */
  public CompositeView(Element element)
  {
    super(element);
    children = new View[0];
92 93 94 95
    top = 0;
    bottom = 0;
    left = 0;
    right = 0;
Tom Tromey committed
96 97 98 99 100 101 102 103 104 105 106 107 108
  }

  /**
   * Loads the child views of this <code>CompositeView</code>. This method
   * is called from {@link #setParent} to initialize the child views of
   * this composite view.
   *
   * @param f the view factory to use for creating new child views
   *
   * @see #setParent
   */
  protected void loadChildren(ViewFactory f)
  {
109
    if (f != null)
Tom Tromey committed
110
      {
111 112 113 114 115 116 117 118 119 120 121 122 123
        Element el = getElement();
        int count = el.getElementCount();
        View[] newChildren = new View[count];
        for (int i = 0; i < count; ++i)
          {
            Element child = el.getElement(i);
            View view = f.create(child);
            newChildren[i] = view;
          }
        // I'd have called replace(0, getViewCount(), newChildren) here
        // in order to replace all existing views. However according to
        // Harmony's tests this is not what the RI does.
        replace(0, 0, newChildren);
Tom Tromey committed
124 125 126 127 128 129 130 131 132 133 134 135 136
      }
  }

  /**
   * Sets the parent of this <code>View</code>.
   * In addition to setting the parent, this calls {@link #loadChildren}, if
   * this <code>View</code> does not already have its children initialized.
   *
   * @param parent the parent to set
   */
  public void setParent(View parent)
  {
    super.setParent(parent);
137
    if (parent != null && numChildren == 0)
Tom Tromey committed
138 139 140 141 142 143 144 145 146 147
      loadChildren(getViewFactory());
  }

  /**
   * Returns the number of child views.
   *
   * @return the number of child views
   */
  public int getViewCount()
  {
148
    return numChildren;
Tom Tromey committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  }

  /**
   * Returns the child view at index <code>n</code>.
   *
   * @param n the index of the requested child view
   *
   * @return the child view at index <code>n</code>
   */
  public View getView(int n)
  {
    return children[n];
  }

  /**
   * Replaces child views by some other child views. If there are no views to
   * remove (<code>length == 0</code>), the result is a simple insert, if
   * there are no children to add (<code>view == null</code>) the result
   * is a simple removal.
   *
   * @param offset the start offset from where to remove children
   * @param length the number of children to remove
   * @param views the views that replace the removed children
   */
  public void replace(int offset, int length, View[] views)
  {
175 176 177 178
    // Make sure we have an array. The Harmony testsuite indicates that we
    // have to do something like this.
    if (views == null)
      views = new View[0];
Tom Tromey committed
179 180

    // First we set the parent of the removed children to null.
181
    int endOffset = offset + length;
Tom Tromey committed
182
    for (int i = offset; i < endOffset; ++i)
183 184 185 186 187
      {
        if (children[i].getParent() == this)
          children[i].setParent(null);
        children[i] = null;
      }
Tom Tromey committed
188

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    // Update the children array.
    int delta = views.length - length;
    int src = offset + length;
    int numMove = numChildren - src;
    int dst = src + delta;
    if (numChildren + delta > children.length)
      {
        // Grow array.
        int newLength = Math.max(2 * children.length, numChildren + delta);
        View[] newChildren = new View[newLength];
        System.arraycopy(children, 0, newChildren, 0, offset);
        System.arraycopy(views, 0, newChildren, offset, views.length);
        System.arraycopy(children, src, newChildren, dst, numMove);
        children = newChildren;
      }
    else
      {
        // Patch existing array.
        System.arraycopy(children, src, children, dst, numMove);
        System.arraycopy(views, 0, children, offset, views.length);
      }
    numChildren += delta;
Tom Tromey committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

    // Finally we set the parent of the added children to this.
    for (int i = 0; i < views.length; ++i)
      views[i].setParent(this);
  }

  /**
   * Returns the allocation for the specified child <code>View</code>.
   *
   * @param index the index of the child view
   * @param a the allocation for this view
   *
   * @return the allocation for the specified child <code>View</code>
   */
  public Shape getChildAllocation(int index, Shape a)
  {
    Rectangle r = getInsideAllocation(a);
    childAllocation(index, r);
    return r;
  }

  /**
   * 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.
   *
   * @param pos the position of the character in the model
   * @param a the area that is occupied by the view
   * @param bias either {@link Position.Bias#Forward} or
   *        {@link Position.Bias#Backward} depending on the preferred
   *        direction bias. If <code>null</code> this defaults to
   *        <code>Position.Bias.Forward</code>
   *
   * @return a rectangle that gives the location of the document position
   *         inside the view coordinate space
   *
   * @throws BadLocationException if <code>pos</code> is invalid
   * @throws IllegalArgumentException if b is not one of the above listed
   *         valid values
   */
  public Shape modelToView(int pos, Shape a, Position.Bias bias)
    throws BadLocationException
  {
254 255
    boolean backward = bias == Position.Bias.Backward;
    int testpos = backward ? Math.max(0, pos - 1) : pos;
256

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    Shape ret = null;
    if (! backward || testpos >= getStartOffset())
      {
        int childIndex = getViewIndexAtPosition(testpos);
        if (childIndex != -1 && childIndex < getViewCount())
          {
            View child = getView(childIndex);
            if (child != null && testpos >= child.getStartOffset()
                && testpos < child.getEndOffset())
              {
                Shape childAlloc = getChildAllocation(childIndex, a);
                if (childAlloc != null)
                  {
                    ret = child.modelToView(pos, childAlloc, bias);
                    // Handle corner case.
                    if (ret == null && child.getEndOffset() == pos)
                      {
                        childIndex++;
                        if (childIndex < getViewCount())
                          {
                            child = getView(childIndex);
                            childAlloc = getChildAllocation(childIndex, a);
                            ret = child.modelToView(pos, childAlloc, bias);
                          }
                      }
                  }
              }
          }
      }
286

287 288 289 290 291
    if (ret == null)
      throw new BadLocationException("Position " + pos
                                     + " is not represented by view.", pos);

    return ret;
Tom Tromey committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  }

  /**
   * Maps a region in the document into the coordinate space of the View.
   *
   * @param p1 the beginning position inside the document
   * @param b1 the direction bias for the beginning position
   * @param p2 the end position inside the document
   * @param b2 the direction bias for the end position
   * @param a the area that is occupied by the view
   *
   * @return a rectangle that gives the span of the document region
   *         inside the view coordinate space
   *
   * @throws BadLocationException if <code>p1</code> or <code>p2</code> are
   *         invalid
   * @throws IllegalArgumentException if b1 or b2 is not one of the above
   *         listed valid values
   */
  public Shape modelToView(int p1, Position.Bias b1,
312
                           int p2, Position.Bias b2, Shape a)
Tom Tromey committed
313 314 315 316 317 318 319 320 321 322 323
    throws BadLocationException
  {
    // TODO: This is most likely not 100% ok, figure out what else is to
    // do here.
    return super.modelToView(p1, b1, p2, b2, a);
  }

  /**
   * Maps coordinates from the <code>View</code>'s space into a position
   * in the document model.
   *
324 325
   * @param x the x coordinate in the view space, x >= 0
   * @param y the y coordinate in the view space, y >= 0
Tom Tromey committed
326 327 328 329
   * @param a the allocation of this <code>View</code>
   * @param b the bias to use
   *
   * @return the position in the document that corresponds to the screen
330
   *         coordinates <code>x, y</code> >= 0
Tom Tromey committed
331 332 333
   */
  public int viewToModel(float x, float y, Shape a, Position.Bias[] b)
  {
334 335 336 337
    if (x >= 0 && y >= 0)
      {
        Rectangle r = getInsideAllocation(a);
        View view = getViewAtPoint((int) x, (int) y, r);
338
        return view.viewToModel(x, y, r, b);
339 340
      }
    return 0;
Tom Tromey committed
341 342 343 344
  }

  /**
   * Returns the next model location that is visible in eiter north / south
345 346 347 348
   * direction or east / west direction. This is used to determine the placement
   * of the caret when navigating around the document with the arrow keys. This
   * is a convenience method for {@link #getNextNorthSouthVisualPositionFrom}
   * and {@link #getNextEastWestVisualPositionFrom}.
349
   *
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
   * @param pos
   *          the model position to start search from
   * @param b
   *          the bias for <code>pos</code>
   * @param a
   *          the allocated region for this view
   * @param direction
   *          the direction from the current position, can be one of the
   *          following:
   *          <ul>
   *          <li>{@link SwingConstants#WEST}</li>
   *          <li>{@link SwingConstants#EAST}</li>
   *          <li>{@link SwingConstants#NORTH}</li>
   *          <li>{@link SwingConstants#SOUTH}</li>
   *          </ul>
   * @param biasRet
   *          the bias of the return value gets stored here
Tom Tromey committed
367 368
   * @return the position inside the model that represents the next visual
   *         location
369 370 371 372 373
   * @throws BadLocationException
   *           if <code>pos</code> is not a valid location inside the document
   *           model
   * @throws IllegalArgumentException
   *           if <code>direction</code> is invalid
Tom Tromey committed
374 375 376
   */
  public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a,
                                       int direction, Position.Bias[] biasRet)
377
    throws BadLocationException
Tom Tromey committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  {
    int retVal = -1;
    switch (direction)
      {
      case SwingConstants.WEST:
      case SwingConstants.EAST:
        retVal = getNextEastWestVisualPositionFrom(pos, b, a, direction,
                                                   biasRet);
        break;
      case SwingConstants.NORTH:
      case SwingConstants.SOUTH:
        retVal = getNextNorthSouthVisualPositionFrom(pos, b, a, direction,
                                                     biasRet);
        break;
      default:
        throw new IllegalArgumentException("Illegal value for direction.");
      }
    return retVal;
  }

  /**
   * Returns the index of the child view that represents the specified
   * model location.
   *
   * @param pos the model location for which to determine the child view index
   * @param b the bias to be applied to <code>pos</code>
   *
   * @return the index of the child view that represents the specified
   *         model location
   */
  public int getViewIndex(int pos, Position.Bias b)
  {
410
    if (b == Position.Bias.Backward)
411
      pos -= 1;
412 413 414 415
    int i = -1;
    if (pos >= getStartOffset() && pos < getEndOffset())
      i = getViewIndexAtPosition(pos);
    return i;
Tom Tromey committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
  }

  /**
   * Returns <code>true</code> if the specified point lies before the
   * given <code>Rectangle</code>, <code>false</code> otherwise.
   *
   * &quot;Before&quot; is typically defined as being to the left or above.
   *
   * @param x the X coordinate of the point
   * @param y the Y coordinate of the point
   * @param r the rectangle to test the point against
   *
   * @return <code>true</code> if the specified point lies before the
   *         given <code>Rectangle</code>, <code>false</code> otherwise
   */
  protected abstract boolean isBefore(int x, int y, Rectangle r);

  /**
   * Returns <code>true</code> if the specified point lies after the
   * given <code>Rectangle</code>, <code>false</code> otherwise.
   *
   * &quot;After&quot; is typically defined as being to the right or below.
   *
   * @param x the X coordinate of the point
   * @param y the Y coordinate of the point
   * @param r the rectangle to test the point against
   *
   * @return <code>true</code> if the specified point lies after the
   *         given <code>Rectangle</code>, <code>false</code> otherwise
   */
  protected abstract boolean isAfter(int x, int y, Rectangle r);

  /**
   * Returns the child <code>View</code> at the specified location.
   *
   * @param x the X coordinate
   * @param y the Y coordinate
   * @param r the inner allocation of this <code>BoxView</code> on entry,
   *        the allocation of the found child on exit
   *
   * @return the child <code>View</code> at the specified location
   */
  protected abstract View getViewAtPoint(int x, int y, Rectangle r);

  /**
   * Computes the allocation for a child <code>View</code>. The parameter
   * <code>a</code> stores the allocation of this <code>CompositeView</code>
   * and is then adjusted to hold the allocation of the child view.
   *
   * @param index the index of the child <code>View</code>
   * @param a the allocation of this <code>CompositeView</code> before the
   *        call, the allocation of the child on exit
   */
  protected abstract void childAllocation(int index, Rectangle a);

  /**
   * Returns the child <code>View</code> that contains the given model
   * position. The given <code>Rectangle</code> gives the parent's allocation
   * and is changed to the child's allocation on exit.
   *
   * @param pos the model position to query the child <code>View</code> for
   * @param a the parent allocation on entry and the child allocation on exit
   *
   * @return the child view at the given model position
   */
  protected View getViewAtPosition(int pos, Rectangle a)
  {
483
    View view = null;
Tom Tromey committed
484
    int i = getViewIndexAtPosition(pos);
485 486 487 488 489
    if (i >= 0 && i < getViewCount() && a != null)
      {
        view = getView(i);
        childAllocation(i, a);
      }
Tom Tromey committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    return view;
  }

  /**
   * Returns the index of the child <code>View</code> for the given model
   * position.
   *
   * @param pos the model position for whicht the child <code>View</code> is
   *        queried
   *
   * @return the index of the child <code>View</code> for the given model
   *         position
   */
  protected int getViewIndexAtPosition(int pos)
  {
505 506 507 508
    // We have a 1:1 mapping of elements to views here, so we forward
    // this to the element.
    Element el = getElement();
    return el.getElementIndex(pos);
Tom Tromey committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
  }

  /**
   * Returns the allocation that is given to this <code>CompositeView</code>
   * minus this <code>CompositeView</code>'s insets.
   *
   * Also this translates from an immutable allocation to a mutable allocation
   * that is typically reused and further narrowed, like in
   * {@link #childAllocation}.
   *
   * @param a the allocation given to this <code>CompositeView</code>
   *
   * @return the allocation that is given to this <code>CompositeView</code>
   *         minus this <code>CompositeView</code>'s insets or
   *         <code>null</code> if a was <code>null</code>
   */
  protected Rectangle getInsideAllocation(Shape a)
  {
    if (a == null)
      return null;

530 531 532
    // Try to avoid allocation of Rectangle here.
    Rectangle alloc = a instanceof Rectangle ? (Rectangle) a : a.getBounds();

Tom Tromey committed
533 534 535
    // Initialize the inside allocation rectangle. This is done inside
    // a synchronized block in order to avoid multiple threads creating
    // this instance simultanously.
536 537 538 539 540
    Rectangle inside = insideAllocation;
    inside.x = alloc.x + getLeftInset();
    inside.y = alloc.y + getTopInset();
    inside.width = alloc.width - getLeftInset() - getRightInset();
    inside.height = alloc.height - getTopInset() - getBottomInset();
Tom Tromey committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554
    return inside;
  }

  /**
   * Sets the insets defined by attributes in <code>attributes</code>. This
   * queries the attribute keys {@link StyleConstants#SpaceAbove},
   * {@link StyleConstants#SpaceBelow}, {@link StyleConstants#LeftIndent} and
   * {@link StyleConstants#RightIndent} and calls {@link #setInsets} to
   * actually set the insets on this <code>CompositeView</code>.
   *
   * @param attributes the attributes from which to query the insets
   */
  protected void setParagraphInsets(AttributeSet attributes)
  {
555 556 557 558
    top = (short) StyleConstants.getSpaceAbove(attributes);
    bottom = (short) StyleConstants.getSpaceBelow(attributes);
    left = (short) StyleConstants.getLeftIndent(attributes);
    right = (short) StyleConstants.getRightIndent(attributes);
Tom Tromey committed
559 560 561 562 563
  }

  /**
   * Sets the insets of this <code>CompositeView</code>.
   *
564 565 566 567
   * @param t the top inset
   * @param l the left inset
   * @param b the bottom inset
   * @param r the right inset
Tom Tromey committed
568
   */
569
  protected void setInsets(short t, short l, short b, short r)
Tom Tromey committed
570
  {
571 572 573 574
    top = t;
    left = l;
    bottom = b;
    right = r;
Tom Tromey committed
575 576 577 578 579 580 581 582 583
  }

  /**
   * Returns the left inset of this <code>CompositeView</code>.
   *
   * @return the left inset of this <code>CompositeView</code>
   */
  protected short getLeftInset()
  {
584
    return left;
Tom Tromey committed
585 586 587 588 589 590 591 592 593
  }

  /**
   * Returns the right inset of this <code>CompositeView</code>.
   *
   * @return the right inset of this <code>CompositeView</code>
   */
  protected short getRightInset()
  {
594
    return right;
Tom Tromey committed
595 596 597 598 599 600 601 602 603
  }

  /**
   * Returns the top inset of this <code>CompositeView</code>.
   *
   * @return the top inset of this <code>CompositeView</code>
   */
  protected short getTopInset()
  {
604
    return top;
Tom Tromey committed
605 606 607 608 609 610 611 612 613
  }

  /**
   * Returns the bottom inset of this <code>CompositeView</code>.
   *
   * @return the bottom inset of this <code>CompositeView</code>
   */
  protected short getBottomInset()
  {
614
    return bottom;
Tom Tromey committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
  }

  /**
   * Returns the next model location that is visible in north or south
   * direction.
   * This is used to determine the
   * placement of the caret when navigating around the document with
   * the arrow keys.
   *
   * @param pos the model position to start search from
   * @param b the bias for <code>pos</code>
   * @param a the allocated region for this view
   * @param direction the direction from the current position, can be one of
   *        the following:
   *        <ul>
   *        <li>{@link SwingConstants#NORTH}</li>
   *        <li>{@link SwingConstants#SOUTH}</li>
   *        </ul>
   * @param biasRet the bias of the return value gets stored here
   *
   * @return the position inside the model that represents the next visual
   *         location
   *
   * @throws BadLocationException if <code>pos</code> is not a valid location
   *         inside the document model
   * @throws IllegalArgumentException if <code>direction</code> is invalid
   */
  protected int getNextNorthSouthVisualPositionFrom(int pos, Position.Bias b,
                                                    Shape a, int direction,
                                                    Position.Bias[] biasRet)
645
    throws BadLocationException
Tom Tromey committed
646
  {
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    // TODO: It is unknown to me how this method has to be implemented and
    // there is no specification telling me how to do it properly. Therefore
    // the implementation was done for cases that are known.
    //
    // If this method ever happens to act silly for your particular case then
    // it is likely that it is a cause of not knowing about your case when it
    // was implemented first. You are free to fix the behavior.
    //
    // Here are the assumptions that lead to the implementation:
    // If direction is NORTH chose the View preceding the one that contains the
    // offset 'pos' (imagine the views are stacked on top of each other where
    // the top is 0 and the bottom is getViewCount()-1.
    // Consecutively when the direction is SOUTH the View following the one
    // the offset 'pos' lies in is questioned.
    //
    // This limitation is described as PR 27345.
    int index = getViewIndex(pos, b);
    View v = null;
665

666 667 668 669 670 671 672 673 674 675
    if (index == -1)
      return pos;

    switch (direction)
    {
      case NORTH:
        // If we cannot calculate a proper offset return the one that was
        // provided.
        if (index <= 0)
          return pos;
676

677 678 679 680 681 682 683
        v = getView(index - 1);
        break;
      case SOUTH:
        // If we cannot calculate a proper offset return the one that was
        // provided.
        if (index >= getViewCount() - 1)
          return pos;
684

685 686 687 688 689
        v = getView(index + 1);
        break;
      default:
          throw new IllegalArgumentException();
    }
690

691
    return v.getNextVisualPositionFrom(pos, b, a, direction, biasRet);
Tom Tromey committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
  }

  /**
   * Returns the next model location that is visible in east or west
   * direction.
   * This is used to determine the
   * placement of the caret when navigating around the document with
   * the arrow keys.
   *
   * @param pos the model position to start search from
   * @param b the bias for <code>pos</code>
   * @param a the allocated region for this view
   * @param direction the direction from the current position, can be one of
   *        the following:
   *        <ul>
   *        <li>{@link SwingConstants#EAST}</li>
   *        <li>{@link SwingConstants#WEST}</li>
   *        </ul>
   * @param biasRet the bias of the return value gets stored here
   *
   * @return the position inside the model that represents the next visual
   *         location
   *
   * @throws BadLocationException if <code>pos</code> is not a valid location
   *         inside the document model
   * @throws IllegalArgumentException if <code>direction</code> is invalid
   */
  protected int getNextEastWestVisualPositionFrom(int pos, Position.Bias b,
                                                  Shape a, int direction,
                                                  Position.Bias[] biasRet)
722
    throws BadLocationException
Tom Tromey committed
723
  {
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
    // TODO: It is unknown to me how this method has to be implemented and
    // there is no specification telling me how to do it properly. Therefore
    // the implementation was done for cases that are known.
    //
    // If this method ever happens to act silly for your particular case then
    // it is likely that it is a cause of not knowing about your case when it
    // was implemented first. You are free to fix the behavior.
    //
    // Here are the assumptions that lead to the implementation:
    // If direction is EAST increase the offset by one and ask the View to
    // which that index belong to calculate the 'next visual position'.
    // If the direction is WEST do the same with offset 'pos' being decreased
    // by one.
    // This behavior will fail in a right-to-left or bidi environment!
    //
    // This limitation is described as PR 27346.
    int index;
741

742
    View v = null;
743

744 745 746 747 748 749 750 751
    switch (direction)
    {
      case EAST:
        index = getViewIndex(pos + 1, b);
        // If we cannot calculate a proper offset return the one that was
        // provided.
        if (index == -1)
          return pos;
752

753 754 755 756 757 758 759 760
        v  = getView(index);
        break;
      case WEST:
        index = getViewIndex(pos - 1, b);
        // If we cannot calculate a proper offset return the one that was
        // provided.
        if (index == -1)
          return pos;
761

762 763 764 765 766
        v  = getView(index);
        break;
      default:
        throw new IllegalArgumentException();
    }
767

768 769 770 771 772
    return v.getNextVisualPositionFrom(pos,
                                       b,
                                       a,
                                       direction,
                                       biasRet);
Tom Tromey committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
  }

  /**
   * Determines if the next view in horinzontal direction is located to
   * the east or west of the view at position <code>pos</code>. Usually
   * the <code>View</code>s are laid out from the east to the west, so
   * we unconditionally return <code>false</code> here. Subclasses that
   * support bidirectional text may wish to override this method.
   *
   * @param pos the position in the document
   * @param bias the bias to be applied to <code>pos</code>
   *
   * @return <code>true</code> if the next <code>View</code> is located
   *         to the EAST, <code>false</code> otherwise
   */
  protected boolean flipEastAndWestAtEnds(int pos, Position.Bias bias)
  {
    return false;
  }
}