1459 newIndex = constrainOffset(target, newIndex); |
1459 newIndex = constrainOffset(target, newIndex); |
1460 if (newIndex != selectedIndex) { |
1460 if (newIndex != selectedIndex) { |
1461 // Make sure the new visible location contains |
1461 // Make sure the new visible location contains |
1462 // the location of dot, otherwise Caret will |
1462 // the location of dot, otherwise Caret will |
1463 // cause an additional scroll. |
1463 // cause an additional scroll. |
1464 adjustScrollIfNecessary(target, newVis, initialY, |
1464 int newY = getAdjustedY(target, newVis, newIndex); |
1465 newIndex); |
1465 |
1466 if (select) { |
1466 if (direction == -1 && newY <= initialY || direction == 1 && newY >= initialY) { |
1467 target.moveCaretPosition(newIndex); |
1467 // Change index and correct newVis.y only if won't cause scrolling upward |
1468 } |
1468 newVis.y = newY; |
1469 else { |
1469 |
1470 target.setCaretPosition(newIndex); |
1470 if (select) { |
|
1471 target.moveCaretPosition(newIndex); |
|
1472 } else { |
|
1473 target.setCaretPosition(newIndex); |
|
1474 } |
1471 } |
1475 } |
1472 } |
1476 } |
1473 } catch (BadLocationException ble) { } |
1477 } catch (BadLocationException ble) { } |
1474 } else { |
1478 } else { |
1475 newVis.y = constrainY(target, |
1479 newVis.y = constrainY(target, |
1511 } |
1515 } |
1512 return offset; |
1516 return offset; |
1513 } |
1517 } |
1514 |
1518 |
1515 /** |
1519 /** |
1516 * Adjusts the rectangle that indicates the location to scroll to |
1520 * Returns adjustsed {@code y} position that indicates the location to scroll to |
1517 * after selecting <code>index</code>. |
1521 * after selecting <code>index</code>. |
1518 */ |
1522 */ |
1519 private void adjustScrollIfNecessary(JTextComponent text, |
1523 private int getAdjustedY(JTextComponent text, Rectangle visible, int index) { |
1520 Rectangle visible, int initialY, |
1524 int result = visible.y; |
1521 int index) { |
1525 |
1522 try { |
1526 try { |
1523 Rectangle dotBounds = text.modelToView(index); |
1527 Rectangle dotBounds = text.modelToView(index); |
1524 |
1528 |
1525 if (dotBounds.y < visible.y || |
1529 if (dotBounds.y < visible.y) { |
1526 (dotBounds.y > (visible.y + visible.height)) || |
1530 result = dotBounds.y; |
1527 (dotBounds.y + dotBounds.height) > |
1531 } else { |
1528 (visible.y + visible.height)) { |
1532 if ((dotBounds.y > visible.y + visible.height) || |
1529 int y; |
1533 (dotBounds.y + dotBounds.height > visible.y + visible.height)) { |
1530 |
1534 result = dotBounds.y + dotBounds.height - visible.height; |
1531 if (dotBounds.y < visible.y) { |
1535 } |
1532 y = dotBounds.y; |
1536 } |
1533 } |
1537 } catch (BadLocationException ble) { |
1534 else { |
1538 } |
1535 y = dotBounds.y + dotBounds.height - visible.height; |
1539 |
1536 } |
1540 return result; |
1537 if ((direction == -1 && y < initialY) || |
|
1538 (direction == 1 && y > initialY)) { |
|
1539 // Only adjust if won't cause scrolling upward. |
|
1540 visible.y = y; |
|
1541 } |
|
1542 } |
|
1543 } catch (BadLocationException ble) {} |
|
1544 } |
1541 } |
1545 |
1542 |
1546 /** |
1543 /** |
1547 * Adjusts the Rectangle to contain the bounds of the character at |
1544 * Adjusts the Rectangle to contain the bounds of the character at |
1548 * <code>index</code> in response to a page up. |
1545 * <code>index</code> in response to a page up. |