40 |
|
|
41 |
import java.awt.FontMetrics; |
import java.awt.FontMetrics; |
42 |
import java.awt.Graphics; |
import java.awt.Graphics; |
43 |
|
import java.awt.Point; |
44 |
|
import java.awt.Rectangle; |
45 |
import java.text.BreakIterator; |
import java.text.BreakIterator; |
46 |
|
|
47 |
|
import javax.swing.SwingConstants; |
48 |
|
import javax.swing.SwingUtilities; |
49 |
|
|
50 |
/** |
/** |
51 |
* A set of utilities to deal with text. This is used by several other classes |
* A set of utilities to deal with text. This is used by several other classes |
52 |
* inside this package. |
* inside this package. |
527 |
// just break it on the character boundary |
// just break it on the character boundary |
528 |
return mark; |
return mark; |
529 |
} |
} |
530 |
|
|
531 |
|
/** |
532 |
|
* Returns the paragraph element in the text component <code>c</code> at |
533 |
|
* the specified location <code>offset</code>. |
534 |
|
* |
535 |
|
* @param c the text component |
536 |
|
* @param offset the offset of the paragraph element to return |
537 |
|
* |
538 |
|
* @return the paragraph element at <code>offset</code> |
539 |
|
*/ |
540 |
|
public static final Element getParagraphElement(JTextComponent c, int offset) |
541 |
|
{ |
542 |
|
Document doc = c.getDocument(); |
543 |
|
Element par = null; |
544 |
|
if (doc instanceof StyledDocument) |
545 |
|
{ |
546 |
|
StyledDocument styledDoc = (StyledDocument) doc; |
547 |
|
par = styledDoc.getParagraphElement(offset); |
548 |
|
} |
549 |
|
else |
550 |
|
{ |
551 |
|
Element root = c.getDocument().getDefaultRootElement(); |
552 |
|
int parIndex = root.getElementIndex(offset); |
553 |
|
par = root.getElement(parIndex); |
554 |
|
} |
555 |
|
return par; |
556 |
|
} |
557 |
|
|
558 |
|
/** |
559 |
|
* Returns the document position that is closest above to the specified x |
560 |
|
* coordinate in the row containing <code>offset</code>. |
561 |
|
* |
562 |
|
* @param c the text component |
563 |
|
* @param offset the offset |
564 |
|
* @param x the x coordinate |
565 |
|
* |
566 |
|
* @return the document position that is closest above to the specified x |
567 |
|
* coordinate in the row containing <code>offset</code> |
568 |
|
* |
569 |
|
* @throws BadLocationException if <code>offset</code> is not a valid offset |
570 |
|
*/ |
571 |
|
public static final int getPositionAbove(JTextComponent c, int offset, int x) |
572 |
|
throws BadLocationException |
573 |
|
{ |
574 |
|
View rootView = c.getUI().getRootView(c); |
575 |
|
Rectangle r = c.modelToView(offset); |
576 |
|
int offs = c.viewToModel(new Point(x, r.y)); |
577 |
|
int pos = rootView.getNextVisualPositionFrom(offs, |
578 |
|
Position.Bias.Forward, |
579 |
|
SwingUtilities.calculateInnerArea(c, null), |
580 |
|
SwingConstants.NORTH, |
581 |
|
new Position.Bias[1]); |
582 |
|
return pos; |
583 |
|
} |
584 |
|
|
585 |
|
/** |
586 |
|
* Returns the document position that is closest below to the specified x |
587 |
|
* coordinate in the row containing <code>offset</code>. |
588 |
|
* |
589 |
|
* @param c the text component |
590 |
|
* @param offset the offset |
591 |
|
* @param x the x coordinate |
592 |
|
* |
593 |
|
* @return the document position that is closest above to the specified x |
594 |
|
* coordinate in the row containing <code>offset</code> |
595 |
|
* |
596 |
|
* @throws BadLocationException if <code>offset</code> is not a valid offset |
597 |
|
*/ |
598 |
|
public static final int getPositionBelow(JTextComponent c, int offset, int x) |
599 |
|
throws BadLocationException |
600 |
|
{ |
601 |
|
View rootView = c.getUI().getRootView(c); |
602 |
|
Rectangle r = c.modelToView(offset); |
603 |
|
int offs = c.viewToModel(new Point(x, r.y)); |
604 |
|
int pos = rootView.getNextVisualPositionFrom(offs, |
605 |
|
Position.Bias.Forward, |
606 |
|
SwingUtilities.calculateInnerArea(c, null), |
607 |
|
SwingConstants.SOUTH, |
608 |
|
new Position.Bias[1]); |
609 |
|
return pos; |
610 |
|
} |
611 |
} |
} |