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 |
|
|
49 |
/** |
/** |
50 |
* 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 |
51 |
* inside this package. |
* inside this package. |
536 |
* |
* |
537 |
* @return the paragraph element at <code>offset</code> |
* @return the paragraph element at <code>offset</code> |
538 |
*/ |
*/ |
539 |
public static Element getParagraphElement(JTextComponent c, int offset) |
public static final Element getParagraphElement(JTextComponent c, int offset) |
540 |
|
{ |
541 |
|
Document doc = c.getDocument(); |
542 |
|
Element par = null; |
543 |
|
if (doc instanceof StyledDocument) |
544 |
|
{ |
545 |
|
StyledDocument styledDoc = (StyledDocument) doc; |
546 |
|
par = styledDoc.getParagraphElement(offset); |
547 |
|
} |
548 |
|
else |
549 |
|
{ |
550 |
|
Element root = c.getDocument().getDefaultRootElement(); |
551 |
|
int parIndex = root.getElementIndex(offset); |
552 |
|
par = root.getElement(parIndex); |
553 |
|
} |
554 |
|
return par; |
555 |
|
} |
556 |
|
|
557 |
|
/** |
558 |
|
* Returns the document position that is closest above to the specified x |
559 |
|
* coordinate in the row containing <code>offset</code>. |
560 |
|
* |
561 |
|
* @param c the text component |
562 |
|
* @param offset the offset |
563 |
|
* @param x the x coordinate |
564 |
|
* |
565 |
|
* @return the document position that is closest above to the specified x |
566 |
|
* coordinate in the row containing <code>offset</code> |
567 |
|
* |
568 |
|
* @throws BadLocationException if <code>offset</code> is not a valid offset |
569 |
|
*/ |
570 |
|
public static final int getPositionAbove(JTextComponent c, int offset, int x) |
571 |
|
throws BadLocationException |
572 |
{ |
{ |
573 |
Element root = c.getDocument().getDefaultRootElement(); |
View rootView = c.getUI().getRootView(c); |
574 |
int parIndex = root.getElementIndex(offset); |
Rectangle r = c.modelToView(offset); |
575 |
return root.getElement(parIndex); |
int offs = c.viewToModel(new Point(x, r.y)); |
576 |
|
int pos = rootView.getNextVisualPositionFrom(c, offs, |
577 |
|
Position.Bias.Forward, |
578 |
|
SwingConstants.NORTH, |
579 |
|
new Position.Bias[1]); |
580 |
|
return pos; |
581 |
|
} |
582 |
|
|
583 |
|
/** |
584 |
|
* Returns the document position that is closest below to the specified x |
585 |
|
* coordinate in the row containing <code>offset</code>. |
586 |
|
* |
587 |
|
* @param c the text component |
588 |
|
* @param offset the offset |
589 |
|
* @param x the x coordinate |
590 |
|
* |
591 |
|
* @return the document position that is closest above to the specified x |
592 |
|
* coordinate in the row containing <code>offset</code> |
593 |
|
* |
594 |
|
* @throws BadLocationException if <code>offset</code> is not a valid offset |
595 |
|
*/ |
596 |
|
public static final int getPositionBelow(JTextComponent c, int offset, int x) |
597 |
|
throws BadLocationException |
598 |
|
{ |
599 |
|
View rootView = c.getUI().getRootView(c); |
600 |
|
Rectangle r = c.modelToView(offset); |
601 |
|
int offs = c.viewToModel(new Point(x, r.y)); |
602 |
|
int pos = rootView.getNextVisualPositionFrom(c, offs, |
603 |
|
Position.Bias.Forward, |
604 |
|
SwingConstants.SOUTH, |
605 |
|
new Position.Bias[1]); |
606 |
|
return pos; |
607 |
} |
} |
608 |
} |
} |