下面列出了javax.swing.plaf.TextUI#modelToView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private int getPosFromY(JTextComponent component, TextUI textUI, int y) throws BadLocationException {
if(textUI instanceof BaseTextUI) {
return ((BaseTextUI) textUI).getPosFromY(y);
} else {
// fallback to ( less otimized than ((BaseTextUI) textUI).getPosFromY(y) )
return textUI.modelToView(component, textUI.viewToModel(component, new Point(0, y))).y;
}
}
/**
* Returns a string anchor if the passed in element has a
* USEMAP that contains the passed in location.
*/
private String getMapHREF(JEditorPane html, HTMLDocument hdoc,
Element elem, AttributeSet attr, int offset,
int x, int y) {
Object useMap = attr.getAttribute(HTML.Attribute.USEMAP);
if (useMap != null && (useMap instanceof String)) {
Map m = hdoc.getMap((String)useMap);
if (m != null && offset < hdoc.getLength()) {
Rectangle bounds;
TextUI ui = html.getUI();
try {
Shape lBounds = ui.modelToView(html, offset,
Position.Bias.Forward);
Shape rBounds = ui.modelToView(html, offset + 1,
Position.Bias.Backward);
bounds = lBounds.getBounds();
bounds.add((rBounds instanceof Rectangle) ?
(Rectangle)rBounds : rBounds.getBounds());
} catch (BadLocationException ble) {
bounds = null;
}
if (bounds != null) {
AttributeSet area = m.getArea(x - bounds.x,
y - bounds.y,
bounds.width,
bounds.height);
if (area != null) {
return (String)area.getAttribute(HTML.Attribute.
HREF);
}
}
}
}
return null;
}
/**
* Returns a string anchor if the passed in element has a
* USEMAP that contains the passed in location.
*/
private String getMapHREF(JEditorPane html, HTMLDocument hdoc,
Element elem, AttributeSet attr, int offset,
int x, int y) {
Object useMap = attr.getAttribute(HTML.Attribute.USEMAP);
if (useMap != null && (useMap instanceof String)) {
Map m = hdoc.getMap((String)useMap);
if (m != null && offset < hdoc.getLength()) {
Rectangle bounds;
TextUI ui = html.getUI();
try {
Shape lBounds = ui.modelToView(html, offset,
Position.Bias.Forward);
Shape rBounds = ui.modelToView(html, offset + 1,
Position.Bias.Backward);
bounds = lBounds.getBounds();
bounds.add((rBounds instanceof Rectangle) ?
(Rectangle)rBounds : rBounds.getBounds());
} catch (BadLocationException ble) {
bounds = null;
}
if (bounds != null) {
AttributeSet area = m.getArea(x - bounds.x,
y - bounds.y,
bounds.width,
bounds.height);
if (area != null) {
return (String)area.getAttribute(HTML.Attribute.
HREF);
}
}
}
}
return null;
}
/**
* Returns true if the View representing <code>e</code> contains
* the location <code>x</code>, <code>y</code>. <code>offset</code>
* gives the offset into the Document to check for.
*/
private boolean doesElementContainLocation(JEditorPane editor,
Element e, int offset,
int x, int y) {
if (e != null && offset > 0 && e.getStartOffset() == offset) {
try {
TextUI ui = editor.getUI();
Shape s1 = ui.modelToView(editor, offset,
Position.Bias.Forward);
if (s1 == null) {
return false;
}
Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle)s1 :
s1.getBounds();
Shape s2 = ui.modelToView(editor, e.getEndOffset(),
Position.Bias.Backward);
if (s2 != null) {
Rectangle r2 = (s2 instanceof Rectangle) ? (Rectangle)s2 :
s2.getBounds();
r1.add(r2);
}
return r1.contains(x, y);
} catch (BadLocationException ble) {
}
}
return true;
}
/**
* Returns true if the View representing <code>e</code> contains
* the location <code>x</code>, <code>y</code>. <code>offset</code>
* gives the offset into the Document to check for.
*/
private boolean doesElementContainLocation(JEditorPane editor,
Element e, int offset,
int x, int y) {
if (e != null && offset > 0 && e.getStartOffset() == offset) {
try {
TextUI ui = editor.getUI();
Shape s1 = ui.modelToView(editor, offset,
Position.Bias.Forward);
if (s1 == null) {
return false;
}
Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle)s1 :
s1.getBounds();
Shape s2 = ui.modelToView(editor, e.getEndOffset(),
Position.Bias.Backward);
if (s2 != null) {
Rectangle r2 = (s2 instanceof Rectangle) ? (Rectangle)s2 :
s2.getBounds();
r1.add(r2);
}
return r1.contains(x, y);
} catch (BadLocationException ble) {
}
}
return true;
}
/**
* Returns true if the View representing <code>e</code> contains
* the location <code>x</code>, <code>y</code>. <code>offset</code>
* gives the offset into the Document to check for.
*/
private boolean doesElementContainLocation(JEditorPane editor,
Element e, int offset,
int x, int y) {
if (e != null && offset > 0 && e.getStartOffset() == offset) {
try {
TextUI ui = editor.getUI();
Shape s1 = ui.modelToView(editor, offset,
Position.Bias.Forward);
if (s1 == null) {
return false;
}
Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle)s1 :
s1.getBounds();
Shape s2 = ui.modelToView(editor, e.getEndOffset(),
Position.Bias.Backward);
if (s2 != null) {
Rectangle r2 = (s2 instanceof Rectangle) ? (Rectangle)s2 :
s2.getBounds();
r1.add(r2);
}
return r1.contains(x, y);
} catch (BadLocationException ble) {
}
}
return true;
}
/**
* Paints a highlight.
*
* @param g the graphics context
* @param offs0 the starting model offset >= 0
* @param offs1 the ending model offset >= offs1
* @param bounds the bounding box for the highlight
* @param c the editor
*/
public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
Rectangle alloc = bounds.getBounds();
try {
// --- determine locations ---
TextUI mapper = c.getUI();
Rectangle p0 = mapper.modelToView(c, offs0);
Rectangle p1 = mapper.modelToView(c, offs1);
// --- render ---
Color color = getColor();
if (color == null) {
g.setColor(c.getSelectionColor());
}
else {
g.setColor(color);
}
if (p0.y == p1.y) {
// same line, render a rectangle
Rectangle r = p0.union(p1);
g.fillRect(r.x, r.y, r.width, r.height);
} else {
// different lines
int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
if ((p0.y + p0.height) != p1.y) {
g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
p1.y - (p0.y + p0.height));
}
g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
}
} catch (BadLocationException e) {
// can't render
}
}
/**
* Returns a string anchor if the passed in element has a
* USEMAP that contains the passed in location.
*/
private String getMapHREF(JEditorPane html, HTMLDocument hdoc,
Element elem, AttributeSet attr, int offset,
int x, int y) {
Object useMap = attr.getAttribute(HTML.Attribute.USEMAP);
if (useMap != null && (useMap instanceof String)) {
Map m = hdoc.getMap((String)useMap);
if (m != null && offset < hdoc.getLength()) {
Rectangle bounds;
TextUI ui = html.getUI();
try {
Shape lBounds = ui.modelToView(html, offset,
Position.Bias.Forward);
Shape rBounds = ui.modelToView(html, offset + 1,
Position.Bias.Backward);
bounds = lBounds.getBounds();
bounds.add((rBounds instanceof Rectangle) ?
(Rectangle)rBounds : rBounds.getBounds());
} catch (BadLocationException ble) {
bounds = null;
}
if (bounds != null) {
AttributeSet area = m.getArea(x - bounds.x,
y - bounds.y,
bounds.width,
bounds.height);
if (area != null) {
return (String)area.getAttribute(HTML.Attribute.
HREF);
}
}
}
}
return null;
}
/**
* Returns a string anchor if the passed in element has a
* USEMAP that contains the passed in location.
*/
@SuppressWarnings("deprecation")
private String getMapHREF(JEditorPane html, HTMLDocument hdoc,
Element elem, AttributeSet attr, int offset,
int x, int y) {
Object useMap = attr.getAttribute(HTML.Attribute.USEMAP);
if (useMap != null && (useMap instanceof String)) {
Map m = hdoc.getMap((String)useMap);
if (m != null && offset < hdoc.getLength()) {
Rectangle bounds;
TextUI ui = html.getUI();
try {
Shape lBounds = ui.modelToView(html, offset,
Position.Bias.Forward);
Shape rBounds = ui.modelToView(html, offset + 1,
Position.Bias.Backward);
bounds = lBounds.getBounds();
bounds.add((rBounds instanceof Rectangle) ?
(Rectangle)rBounds : rBounds.getBounds());
} catch (BadLocationException ble) {
bounds = null;
}
if (bounds != null) {
AttributeSet area = m.getArea(x - bounds.x,
y - bounds.y,
bounds.width,
bounds.height);
if (area != null) {
return (String)area.getAttribute(HTML.Attribute.
HREF);
}
}
}
}
return null;
}
/**
* Returns true if the View representing <code>e</code> contains
* the location <code>x</code>, <code>y</code>. <code>offset</code>
* gives the offset into the Document to check for.
*/
private boolean doesElementContainLocation(JEditorPane editor,
Element e, int offset,
int x, int y) {
if (e != null && offset > 0 && e.getStartOffset() == offset) {
try {
TextUI ui = editor.getUI();
Shape s1 = ui.modelToView(editor, offset,
Position.Bias.Forward);
if (s1 == null) {
return false;
}
Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle)s1 :
s1.getBounds();
Shape s2 = ui.modelToView(editor, e.getEndOffset(),
Position.Bias.Backward);
if (s2 != null) {
Rectangle r2 = (s2 instanceof Rectangle) ? (Rectangle)s2 :
s2.getBounds();
r1.add(r2);
}
return r1.contains(x, y);
} catch (BadLocationException ble) {
}
}
return true;
}
/**
* Returns a string anchor if the passed in element has a
* USEMAP that contains the passed in location.
*/
private String getMapHREF(JEditorPane html, HTMLDocument hdoc,
Element elem, AttributeSet attr, int offset,
int x, int y) {
Object useMap = attr.getAttribute(HTML.Attribute.USEMAP);
if (useMap != null && (useMap instanceof String)) {
Map m = hdoc.getMap((String)useMap);
if (m != null && offset < hdoc.getLength()) {
Rectangle bounds;
TextUI ui = html.getUI();
try {
Shape lBounds = ui.modelToView(html, offset,
Position.Bias.Forward);
Shape rBounds = ui.modelToView(html, offset + 1,
Position.Bias.Backward);
bounds = lBounds.getBounds();
bounds.add((rBounds instanceof Rectangle) ?
(Rectangle)rBounds : rBounds.getBounds());
} catch (BadLocationException ble) {
bounds = null;
}
if (bounds != null) {
AttributeSet area = m.getArea(x - bounds.x,
y - bounds.y,
bounds.width,
bounds.height);
if (area != null) {
return (String)area.getAttribute(HTML.Attribute.
HREF);
}
}
}
}
return null;
}
/**
* Returns a string anchor if the passed in element has a
* USEMAP that contains the passed in location.
*/
private String getMapHREF(JEditorPane html, HTMLDocument hdoc,
Element elem, AttributeSet attr, int offset,
int x, int y) {
Object useMap = attr.getAttribute(HTML.Attribute.USEMAP);
if (useMap != null && (useMap instanceof String)) {
Map m = hdoc.getMap((String)useMap);
if (m != null && offset < hdoc.getLength()) {
Rectangle bounds;
TextUI ui = html.getUI();
try {
Shape lBounds = ui.modelToView(html, offset,
Position.Bias.Forward);
Shape rBounds = ui.modelToView(html, offset + 1,
Position.Bias.Backward);
bounds = lBounds.getBounds();
bounds.add((rBounds instanceof Rectangle) ?
(Rectangle)rBounds : rBounds.getBounds());
} catch (BadLocationException ble) {
bounds = null;
}
if (bounds != null) {
AttributeSet area = m.getArea(x - bounds.x,
y - bounds.y,
bounds.width,
bounds.height);
if (area != null) {
return (String)area.getAttribute(HTML.Attribute.
HREF);
}
}
}
}
return null;
}
/**
* Returns a string anchor if the passed in element has a
* USEMAP that contains the passed in location.
*/
private String getMapHREF(JEditorPane html, HTMLDocument hdoc,
Element elem, AttributeSet attr, int offset,
int x, int y) {
Object useMap = attr.getAttribute(HTML.Attribute.USEMAP);
if (useMap != null && (useMap instanceof String)) {
Map m = hdoc.getMap((String)useMap);
if (m != null && offset < hdoc.getLength()) {
Rectangle bounds;
TextUI ui = html.getUI();
try {
Shape lBounds = ui.modelToView(html, offset,
Position.Bias.Forward);
Shape rBounds = ui.modelToView(html, offset + 1,
Position.Bias.Backward);
bounds = lBounds.getBounds();
bounds.add((rBounds instanceof Rectangle) ?
(Rectangle)rBounds : rBounds.getBounds());
} catch (BadLocationException ble) {
bounds = null;
}
if (bounds != null) {
AttributeSet area = m.getArea(x - bounds.x,
y - bounds.y,
bounds.width,
bounds.height);
if (area != null) {
return (String)area.getAttribute(HTML.Attribute.
HREF);
}
}
}
}
return null;
}
/**
* Returns true if the View representing <code>e</code> contains
* the location <code>x</code>, <code>y</code>. <code>offset</code>
* gives the offset into the Document to check for.
*/
@SuppressWarnings("deprecation")
private boolean doesElementContainLocation(JEditorPane editor,
Element e, int offset,
int x, int y) {
if (e != null && offset > 0 && e.getStartOffset() == offset) {
try {
TextUI ui = editor.getUI();
Shape s1 = ui.modelToView(editor, offset,
Position.Bias.Forward);
if (s1 == null) {
return false;
}
Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle)s1 :
s1.getBounds();
Shape s2 = ui.modelToView(editor, e.getEndOffset(),
Position.Bias.Backward);
if (s2 != null) {
Rectangle r2 = (s2 instanceof Rectangle) ? (Rectangle)s2 :
s2.getBounds();
r1.add(r2);
}
return r1.contains(x, y);
} catch (BadLocationException ble) {
}
}
return true;
}
/**
* Paints a highlight.
*
* @param g the graphics context
* @param offs0 the starting model offset >= 0
* @param offs1 the ending model offset >= offs1
* @param bounds the bounding box for the highlight
* @param c the editor
*/
public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
Rectangle alloc = bounds.getBounds();
try {
// --- determine locations ---
TextUI mapper = c.getUI();
Rectangle p0 = mapper.modelToView(c, offs0);
Rectangle p1 = mapper.modelToView(c, offs1);
// --- render ---
Color color = getColor();
if (color == null) {
g.setColor(c.getSelectionColor());
}
else {
g.setColor(color);
}
boolean firstIsDot = false;
boolean secondIsDot = false;
if (c.isEditable()) {
int dot = c.getCaretPosition();
firstIsDot = (offs0 == dot);
secondIsDot = (offs1 == dot);
}
if (p0.y == p1.y) {
// same line, render a rectangle
Rectangle r = p0.union(p1);
if (r.width > 0) {
if (firstIsDot) {
r.x++;
r.width--;
}
else if (secondIsDot) {
r.width--;
}
}
g.fillRect(r.x, r.y, r.width, r.height);
} else {
// different lines
int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
if (firstIsDot && p0ToMarginWidth > 0) {
p0.x++;
p0ToMarginWidth--;
}
g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
if ((p0.y + p0.height) != p1.y) {
g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
p1.y - (p0.y + p0.height));
}
if (secondIsDot && p1.x > alloc.x) {
p1.x--;
}
g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
}
} catch (BadLocationException e) {
// can't render
}
}
/**
* Paints a highlight.
*
* @param g the graphics context
* @param offs0 the starting model offset >= 0
* @param offs1 the ending model offset >= offs1
* @param bounds the bounding box for the highlight
* @param c the editor
*/
public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
Rectangle alloc = bounds.getBounds();
try {
// --- determine locations ---
TextUI mapper = c.getUI();
Rectangle p0 = mapper.modelToView(c, offs0);
Rectangle p1 = mapper.modelToView(c, offs1);
// --- render ---
Color color = getColor();
if (color == null) {
g.setColor(c.getSelectionColor());
}
else {
g.setColor(color);
}
boolean firstIsDot = false;
boolean secondIsDot = false;
if (c.isEditable()) {
int dot = c.getCaretPosition();
firstIsDot = (offs0 == dot);
secondIsDot = (offs1 == dot);
}
if (p0.y == p1.y) {
// same line, render a rectangle
Rectangle r = p0.union(p1);
if (r.width > 0) {
if (firstIsDot) {
r.x++;
r.width--;
}
else if (secondIsDot) {
r.width--;
}
}
g.fillRect(r.x, r.y, r.width, r.height);
} else {
// different lines
int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
if (firstIsDot && p0ToMarginWidth > 0) {
p0.x++;
p0ToMarginWidth--;
}
g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
if ((p0.y + p0.height) != p1.y) {
g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
p1.y - (p0.y + p0.height));
}
if (secondIsDot && p1.x > alloc.x) {
p1.x--;
}
g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
}
} catch (BadLocationException e) {
// can't render
}
}
/**
* Paints a highlight.
*
* @param g the graphics context
* @param offs0 the starting model offset >= 0
* @param offs1 the ending model offset >= offs1
* @param bounds the bounding box for the highlight
* @param c the editor
*/
public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
Rectangle alloc = bounds.getBounds();
try {
// --- determine locations ---
TextUI mapper = c.getUI();
Rectangle p0 = mapper.modelToView(c, offs0);
Rectangle p1 = mapper.modelToView(c, offs1);
// --- render ---
Color color = getColor();
if (color == null) {
g.setColor(c.getSelectionColor());
}
else {
g.setColor(color);
}
boolean firstIsDot = false;
boolean secondIsDot = false;
if (c.isEditable()) {
int dot = c.getCaretPosition();
firstIsDot = (offs0 == dot);
secondIsDot = (offs1 == dot);
}
if (p0.y == p1.y) {
// same line, render a rectangle
Rectangle r = p0.union(p1);
if (r.width > 0) {
if (firstIsDot) {
r.x++;
r.width--;
}
else if (secondIsDot) {
r.width--;
}
}
g.fillRect(r.x, r.y, r.width, r.height);
} else {
// different lines
int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
if (firstIsDot && p0ToMarginWidth > 0) {
p0.x++;
p0ToMarginWidth--;
}
g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
if ((p0.y + p0.height) != p1.y) {
g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
p1.y - (p0.y + p0.height));
}
if (secondIsDot && p1.x > alloc.x) {
p1.x--;
}
g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
}
} catch (BadLocationException e) {
// can't render
}
}
/**
* Paints a highlight.
*
* @param g the graphics context
* @param offs0 the starting model offset >= 0
* @param offs1 the ending model offset >= offs1
* @param bounds the bounding box for the highlight
* @param c the editor
*/
public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
Rectangle alloc = bounds.getBounds();
try {
// --- determine locations ---
TextUI mapper = c.getUI();
Rectangle p0 = mapper.modelToView(c, offs0);
Rectangle p1 = mapper.modelToView(c, offs1);
// --- render ---
Color color = getColor();
if (color == null) {
g.setColor(c.getSelectionColor());
}
else {
g.setColor(color);
}
boolean firstIsDot = false;
boolean secondIsDot = false;
if (c.isEditable()) {
int dot = c.getCaretPosition();
firstIsDot = (offs0 == dot);
secondIsDot = (offs1 == dot);
}
if (p0.y == p1.y) {
// same line, render a rectangle
Rectangle r = p0.union(p1);
if (r.width > 0) {
if (firstIsDot) {
r.x++;
r.width--;
}
else if (secondIsDot) {
r.width--;
}
}
g.fillRect(r.x, r.y, r.width, r.height);
} else {
// different lines
int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
if (firstIsDot && p0ToMarginWidth > 0) {
p0.x++;
p0ToMarginWidth--;
}
g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
if ((p0.y + p0.height) != p1.y) {
g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
p1.y - (p0.y + p0.height));
}
if (secondIsDot && p1.x > alloc.x) {
p1.x--;
}
g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
}
} catch (BadLocationException e) {
// can't render
}
}
/**
* Paints a highlight.
*
* @param g the graphics context
* @param offs0 the starting model offset >= 0
* @param offs1 the ending model offset >= offs1
* @param bounds the bounding box for the highlight
* @param c the editor
*/
public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
Rectangle alloc = bounds.getBounds();
try {
// --- determine locations ---
TextUI mapper = c.getUI();
Rectangle p0 = mapper.modelToView(c, offs0);
Rectangle p1 = mapper.modelToView(c, offs1);
// --- render ---
Color color = getColor();
if (color == null) {
g.setColor(c.getSelectionColor());
}
else {
g.setColor(color);
}
boolean firstIsDot = false;
boolean secondIsDot = false;
if (c.isEditable()) {
int dot = c.getCaretPosition();
firstIsDot = (offs0 == dot);
secondIsDot = (offs1 == dot);
}
if (p0.y == p1.y) {
// same line, render a rectangle
Rectangle r = p0.union(p1);
if (r.width > 0) {
if (firstIsDot) {
r.x++;
r.width--;
}
else if (secondIsDot) {
r.width--;
}
}
g.fillRect(r.x, r.y, r.width, r.height);
} else {
// different lines
int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
if (firstIsDot && p0ToMarginWidth > 0) {
p0.x++;
p0ToMarginWidth--;
}
g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
if ((p0.y + p0.height) != p1.y) {
g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
p1.y - (p0.y + p0.height));
}
if (secondIsDot && p1.x > alloc.x) {
p1.x--;
}
g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
}
} catch (BadLocationException e) {
// can't render
}
}
/**
* Paints a highlight.
*
* @param g the graphics context
* @param offs0 the starting model offset >= 0
* @param offs1 the ending model offset >= offs1
* @param bounds the bounding box for the highlight
* @param c the editor
*/
public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
Rectangle alloc = bounds.getBounds();
try {
// --- determine locations ---
TextUI mapper = c.getUI();
Rectangle p0 = mapper.modelToView(c, offs0);
Rectangle p1 = mapper.modelToView(c, offs1);
// --- render ---
Color color = getColor();
if (color == null) {
g.setColor(c.getSelectionColor());
}
else {
g.setColor(color);
}
boolean firstIsDot = false;
boolean secondIsDot = false;
if (c.isEditable()) {
int dot = c.getCaretPosition();
firstIsDot = (offs0 == dot);
secondIsDot = (offs1 == dot);
}
if (p0.y == p1.y) {
// same line, render a rectangle
Rectangle r = p0.union(p1);
if (r.width > 0) {
if (firstIsDot) {
r.x++;
r.width--;
}
else if (secondIsDot) {
r.width--;
}
}
g.fillRect(r.x, r.y, r.width, r.height);
} else {
// different lines
int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
if (firstIsDot && p0ToMarginWidth > 0) {
p0.x++;
p0ToMarginWidth--;
}
g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
if ((p0.y + p0.height) != p1.y) {
g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
p1.y - (p0.y + p0.height));
}
if (secondIsDot && p1.x > alloc.x) {
p1.x--;
}
g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
}
} catch (BadLocationException e) {
// can't render
}
}