-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathParagraphsHighlighter.java
More file actions
71 lines (60 loc) · 1.94 KB
/
ParagraphsHighlighter.java
File metadata and controls
71 lines (60 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.oxygenxml;
import java.util.HashMap;
import java.util.Map;
import javax.swing.text.BadLocationException;
import ro.sync.ecss.extensions.api.AuthorAccess;
import ro.sync.ecss.extensions.api.AuthorDocumentController;
import ro.sync.ecss.extensions.api.AuthorOperationException;
import ro.sync.ecss.extensions.api.highlights.AuthorHighlighter;
import ro.sync.ecss.extensions.api.node.AuthorNode;
/**
* Adds highlights on each paragraph from the document
*/
public class ParagraphsHighlighter {
private AuthorHighlighter highlighter;
private AuthorDocumentController controller;
private static Map<String, String> HIGHLIGHTS_ATTRS = new HashMap<>();
{
HIGHLIGHTS_ATTRS.put("class", "grayBG");
HIGHLIGHTS_ATTRS.put("type", "grayBGH");
}
/**
* @param authorAccess Access to the non persistent highlighter.
*/
public ParagraphsHighlighter(AuthorAccess authorAccess) {
highlighter = authorAccess.getEditorAccess().getHighlighter();
controller = authorAccess.getDocumentController();
}
private void addHighlight(AuthorNode para) {
try {
highlighter.addHighlight(para.getStartOffset(), para.getEndOffset() + 1, null, HIGHLIGHTS_ATTRS);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* Recompute the highlights.
*/
public void recomputeHighlights() {
highlighter.removeAllHighlights();
AuthorNode[] allParas = findAllParas(controller);
for (AuthorNode para : allParas) {
this.addHighlight(para);
}
}
/**
* Find all paragraphs from the document.
*
* @param controller Author document controller.
* @return All paragraphs
*/
private AuthorNode[] findAllParas(AuthorDocumentController controller) {
AuthorNode[] allParas = new AuthorNode[0];
try {
allParas = controller.findNodesByXPath("//p", true, true, true);
} catch (AuthorOperationException e) {
e.printStackTrace();
}
return allParas;
}
}