• Getting the currently selected text
Replies:
3
Views:
46
Up one level
• Getting the currently selected text
Posted by
Yadis
at
2010-01-27 10:48:22
Hello, me again =)
I got NOA working so far inside my plugin, but now I stumbled across a few problems again. 1) Is it possible to retrieve the currently selected text? What I basically want to do is programmatically extract the selected text so I can use it and then format the text in the OO document. As far as I've understood that, I would like to have an ITextRange for that. But even just getting the current selection as a String would help, as I could just do a search for the returned String and then format. 2) Is there a way to retrieve the character background colour? I can access and change the character colour, but not the background colour. 3) This one is a bit more advanced. Is it possible to remove/change the right click menu inside an OO document? For example I want to select a text range, and in the right-click menu have an entry to set the character background colour to a specific colour (hence question 1 and 2). If the above questions can't be solved with NOA, a hint on how to solve them with UNO would also be very much appreciated. Thanks again. |
Yadis
Member
Posts:
8
|
• Re: Getting the currently selected text
Posted by
Markus Krueger
at
2010-01-27 11:31:23
Hi,
a bit more code, since NOA does not fully support this yet.
//1) for simple text selection
ITextRange foundTextRange = null;
String foundText = null;
XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, doc.getXComponent());
if (xModel != null) {
XController xController = xModel.getCurrentController();
XSelectionSupplier selectionSupplier = (XSelectionSupplier) UnoRuntime.queryInterface(XSelectionSupplier.class,
xController);
if (selectionSupplier != null) {
XServiceInfo xServInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class,
selectionSupplier.getSelection());
if (xServInfo.supportsService("com.sun.star.text.TextRanges")) {
XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,
selectionSupplier.getSelection());
for (int i = 0, n = xIndexAccess.getCount(); i < n; i++) {
XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(XTextRange.class,
xIndexAccess.getByIndex(i));
//here is the text
foundText = xTextRange.getString();
foundTextRange = new TextRange(doc, xTextRange);
System.out.println("foundText = " + foundText);
}
}
}
}
//2) set font color to be red
if (foundTextRange != null) {
CharacterProperties characterProperties = new CharacterProperties((XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
foundTextRange.getXTextRange()));
characterProperties.setFontColor(new Integer(0xff0000)); //red
}
//3
no solution to that one
Regards, Markus |
• Re: Getting the currently selected text
Posted by
Yadis
at
2010-01-27 11:52:58
Thank you again Markus.
Question #1 is perfectly answered, works great. I think I didn't express my needs in question #2 well enough. It's not the FontColor I need to change, but the background of it. Like take these forums for example, when you select any text, the Font color is white, but the background is a light blue. And I'd like to access that light blue selection color (if that's possible of course). That's just a minor thing though, getting the active selection is pretty much all I need, the rest is cosmetics =) I can also work around not being able to change the context menu. I will probably have a closer look into it, if I find the time for it. Once again, thank you for your swift and excellent response, it is great to be able to post questions and get answers, a lot of communities can't offer that. |
Yadis
Member
Posts:
8
|
• Re: Getting the currently selected text
Posted by
Yadis
at
2010-01-29 14:45:19
Ok, I got the background Color to work as well now.
<pre> CharacterProperties characterProperties = new CharacterProperties((XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, foundTextRange.getXTextRange())); characterProperties.getXPropertySet().setPropertyValue("CharBackColor", INT VALUE); } </pre> Maybe someone else will need this too at some point =) |
Yadis
Member
Posts:
8
|


