Oxygen Author Component (Applet) text mode: elements highlig

Having trouble installing Oxygen? Got a bug to report? Post it all here.
yury.eroshenkov
Posts: 34
Joined: Mon Jun 03, 2013 2:17 pm

Oxygen Author Component (Applet) text mode: elements highlig

Post by yury.eroshenkov »

Hi, Oxygen guys!
As you may know, we are using Oxygen web Component in our Ditaworks Cloud application as a main topic editor. We are implementing a conditional processing feature now and we need to highlight a tagged content as a part of this functionality. In the Author mode there are no questions and all are done, but in the text component we faced with problems:
1. How to get a current selected element? In the Author mode we used next:

Code: Select all


	int currentOffset = authorAccess.getEditorAccess().getCaretOffset();
AuthorNode node = authorAccess.getDocumentController().getNodeAtOffset(currentOffset);
2. How to get a current selected attribute value? Author:

Code: Select all


	AuthorElement element = (AuthorElement) node;
AttrValue attrValue = element.getAttribute(tagName);
3. How to add attribute and set/change an attribute value? Author example:

Code: Select all


authorAccess.getDocumentController().beginCompoundEdit();
// insert tag attribute
authorAccess.getDocumentController().removeAttribute(tag.getName(), element);
authorAccess.getDocumentController().setAttribute(tag.getName(),
new AttrValue(ConditionalProcessingUtils.getTagValue(conditionalValues)),element);
authorAccess.getDocumentController().insertXMLFragment("", currentOffset);
authorAccess.getDocumentController().endCompoundEdit();
4. XML elements highlighting in ext mode? Author example:

Code: Select all


ColorHighlightPainter painter = new ColorHighlightPainter();
ColorRGB mergeColor = ColorGenerator.mergeHexColors(tagColors);
Color color = new Color(mergeColor.red, mergeColor.green, mergeColor.blue);
painter.setBgColor(color); authorAccess.getDocumentController().beginCompoundEdit();
authorAccess.getEditorAccess().getHighlighter().addHighlight(element.getStartOffset(), element.getEndOffset(), painter, new AuthorConditionalProcessingHighlight(element)); authorAccess.getDocumentController().endCompoundEdit();
We tried next:

Code: Select all


WSTextEditorPage currentPage = (WSTextEditorPage)editorComponent.getWSEditorAccess().getCurrentPage();
Object textComponent = currentPage.getTextComponent();
if (textComponent instanceof JTextArea) {
JTextArea textArea = (JTextArea) textComponent;
Highlighter highlighther = textArea.getHighlighter();
highlighther.addHighlight(p0, p1, p)
}
but we don't know how to get a (p0, p1, p) coordinates? And in general, how to build an object model of the XML in the text mode?

Thank you!
Radu
Posts: 9051
Joined: Fri Jul 09, 2004 5:18 pm

Re: Oxygen Author Component (Applet) text mode: elements hig

Post by Radu »

Hi Yuri,

The XML text page does not have an internal node structure, the XML is just presented in a Java JTextArea with syntax highlight. The Outline view from the Text page tries to present some hierarchical node structure over the XML content but there is no API to access that structure.
Here are some possible approaches for your questions:
1. How to get a current selected element? In the Author mode we used next:
The text page API for an opened XML document is actually a ro.sync.exml.workspace.api.editor.page.text.xml.WSXMLTextEditorPage which extends WSTextEditorPage.
The WSXMLTextEditorPage has extra API like WSXMLTextEditorPage.findElementsByXPath(String) which can be used to obtain the range (start/end line/column) of a certain XML element.
So you could call WSXMLTextEditorPage.findElementsByXPath(".") to obtain the range of the current element in which the caret is placed.
2. How to get a current selected attribute value? Author:
You could call this API WSXMLTextEditorPage.evaluateXPath(".") and obtain a DOM node representing the current element, then query this node as to what attributes are set on it.
3. How to add attribute and set/change an attribute value? Author example:
You would need to insert/modify text directly in the XML content. You can use WSTextEditorPage.beginCompoundUndoableEdit() and WSTextEditorPage.endCompoundUndoableEdit() to create a compound undoable edit event.
Then use the WSTextEditorPage.getDocument() API to read/insert/remove XML content.
4. XML elements highlighting in ext mode? Author example:
Probably you can run an XPath WSXMLTextEditorPage.findElementsByXPath(String) and find out the elements which interest you (start/end line/column ranges).
You can convert line start/end ranges to offsets in the JTextArea using the API WSTextEditorPage.getOffsetOfLineStart(int) and then use the usual Swing support for highlights in a JTextArea, like you already tried.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
yury.eroshenkov
Posts: 34
Joined: Mon Jun 03, 2013 2:17 pm

Re: Oxygen Author Component (Applet) text mode: elements hig

Post by yury.eroshenkov »

Thank you, Radu, very much!

We will try to use your suggestions.

Best, Yury.
Radu
Posts: 9051
Joined: Fri Jul 09, 2004 5:18 pm

Re: Oxygen Author Component (Applet) text mode: elements hig

Post by Radu »

Hi Yuri,

Just take care that all the XPath executions should probably be done on a different thread as they might block the graphical user interface AWT thread if the document is large and the XPath executions are done often.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
alexandr.golovach
Posts: 19
Joined: Tue Mar 11, 2014 11:56 am

Re: Oxygen Author Component (Applet) text mode: elements hig

Post by alexandr.golovach »

Hi Radu,
I tried to use your suggestions and I have some questions.
1. When I use WSXMLTextEditorPage.evaluateXPath(".") I don't have a DOM node representing the current element.

Code: Select all


Object object = currentPage.evaluateXPath(".");
if (object instanceof Node) {
Node node = (Node) object;
}
Maybe I made mistake? You said about w3c DOM model or Java Swing DOM model?

2. When I use WSXMLTextEditorPage.findElementsByXPath(".") I have the WSXMLTextNodeRange range (start/end line/column). Then I use WSTextEditorPage.getOffsetOfLineStart(int) and I have start line offset and end line offset. How do I convert start/end column number to offsets? How do I join all 4 offsets to start/end offsets for highlights in a JTextArea?

Thank you!
Radu
Posts: 9051
Joined: Fri Jul 09, 2004 5:18 pm

Re: Oxygen Author Component (Applet) text mode: elements hig

Post by Radu »

Hi Alexandr,
1. When I use WSXMLTextEditorPage.evaluateXPath(".") I don't have a DOM node representing the current element.
The method returns an array of DOM nodes, please see the API Javadoc:

http://www.oxygenxml.com/InstData/Edito ... .String%29

So would work with it like:

Code: Select all

Object[] nodes = currentPage.evaluateXPath(".");
if (nodes.length > 0) {
Node currentNode = (Node) nodes[0];
}
2. When I use WSXMLTextEditorPage.findElementsByXPath(".") I have the WSXMLTextNodeRange range (start/end line/column). Then I use WSTextEditorPage.getOffsetOfLineStart(int) and I have start line offset and end line offset. How do I convert start/end column number to offsets? How do I join all 4 offsets to start/end offsets for highlights in a JTextArea?
You first call getOffsetOfLineStart(int startLine) and then you add to it the startColumn to find out the offset corresponding to the (startLine, startColumn) pairs.
Then you call getOffsetOfLineStart(int endLine) and then you add to it the endColumn to find out the offset corresponding to the (endLine, endColumn) pairs.
Then you obtain a start and and end offset which are used to add the custom painter to the JTextArea highlighter.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
alexandr.golovach
Posts: 19
Joined: Tue Mar 11, 2014 11:56 am

Re: Oxygen Author Component (Applet) text mode: elements hig

Post by alexandr.golovach »

Thank you, Radu, very much!

We used your suggestions for previous problem.
Now I have a question about Attributes View.

How can I change attribute possible values? For example, I want to change possible values for <audience> attribute ("expert, notice" - default value). Can I change possible values in Java code?

Best, Alexandr.
Radu
Posts: 9051
Joined: Fri Jul 09, 2004 5:18 pm

Re: Oxygen Author Component (Applet) text mode: elements hig

Post by Radu »

Hi Alexandr,

Those values are usually configured in a standalone version of Oxygen in the Preferences dialog but in the component you have API to force set a certain preference key to a certain value.
So in this case you could do something like:

Code: Select all

		//new ProfileConditionInfoPO(String attributeName, String attributeRenderName, boolean allowsMultipleValues,ProfileConditionValuePO[] allowedValues, String valuesSeparator, String documentTypePattern);
ProfileConditionInfoPO cond = new ProfileConditionInfoPO("audience", "Audience", true,
new ProfileConditionValuePO[] {
new ProfileConditionValuePO("value1", "Description1"),
new ProfileConditionValuePO("value2", "Description2")
}, " ", "DITA*");
AuthorComponentFactory.getInstance().setObjectProperty("profiling.conditions.list", new ProfileConditionInfoPO[] {cond});
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
alexandr.golovach
Posts: 19
Joined: Tue Mar 11, 2014 11:56 am

Re: Oxygen Author Component (Applet) text mode: elements hig

Post by alexandr.golovach »

Thank you, Radu, very much!

I used your suggestions - it works great.

Best, Alex.
Post Reply