2011/7/4 Anton Starcev <[email protected]>:
> Hi Dan,
>
> Maybe I explained not correctly: I'm trying to insert text directly in
> <caption/>
>
>> Try this: document.insertText(range.getFocus(), "Some text")
>
> Tried to use this, but "Some text" has been inserted before image
> doodad, not in <caption/>
> Then I tried to get location from ContentElement:
>
>            final ContentElement caption =
> document.createChildElement(image, "caption", new HashMap<String,
> String>());
>            final int location = document.getLocation(caption);
>            document.insertText(location, "Some text");
>
> Last line produces exception:
>
> editor (1309769162.559): Invalid LOCAL operation: {__8; ++"Some text";
> } Violation: schema violation: element type image does not allow text

That's because the index you gave is the one for the <caption>
element, so inserting text at that location would insert it *before*
the <caption>. You have to add 1 to move after the <caption> start
tag.
To make things clearer (easier to read/maintain, at the expense of
being slightly less performant, according to Dan), you can use Point
static methods, such as Point.start, Point inElement or
Point.textOrElementStart.

>> (getFocus() is fine, why do you want Math.min?
>
> I looked to the comments of methods:
>
>  /**
>   * @return anchor location, may or may not be before the focus
>   */
>  public int getAnchor() {
>    return anchor;
>  }
>
>  /**
>   * @return focus location, may or may not be before the anchor
>   */
>  public int getFocus() {
>    return focus;
>  }
>
> and decided that I need min value from the focus and the anchor.

Then use getOrderedSelectionRange instead of getSelectionRange, and
then you'll have what you want with getFirst, without the need for a
Math.min (but of course, Math.min also works ;-) )
And if you need points instead of indices, you can use
getOrderedSelectionPoints.

Reply via email to