1519 |
*/ |
*/ |
1520 |
public int getElementIndex(int offset) |
public int getElementIndex(int offset) |
1521 |
{ |
{ |
1522 |
// If we have no children, return -1. |
// If offset is less than the start offset of our first child, |
1523 |
if (getElementCount() == 0) |
// return 0 |
1524 |
return - 1; |
if (offset < getStartOffset()) |
1525 |
|
return 0; |
1526 |
|
|
1527 |
// XXX: There is surely a better algorithm |
// XXX: There is surely a better algorithm |
1528 |
// as beginning from first element each time. |
// as beginning from first element each time. |
1529 |
for (int index = 0; index < children.length; ++index) |
for (int index = 0; index < children.length - 1; ++index) |
1530 |
{ |
{ |
1531 |
Element elem = children[index]; |
Element elem = children[index]; |
1532 |
|
|
1533 |
if ((elem.getStartOffset() <= offset) |
if ((elem.getStartOffset() <= offset) |
1534 |
&& (offset < elem.getEndOffset())) |
&& (offset < elem.getEndOffset())) |
1535 |
return index; |
return index; |
1536 |
|
// If the next element's start offset is greater than offset |
1537 |
|
// then we have to return the closest Element, since no Elements |
1538 |
|
// will contain the offset |
1539 |
|
if (children[index + 1].getStartOffset() > offset) |
1540 |
|
{ |
1541 |
|
if ((offset - elem.getEndOffset()) > (children[index + 1].getStartOffset() - offset)) |
1542 |
|
return index + 1; |
1543 |
|
else |
1544 |
|
return index; |
1545 |
|
} |
1546 |
} |
} |
1547 |
|
|
1548 |
// If offset is greater than the index of the last element, return |
// If offset is greater than the index of the last element, return |