Logger.getLogger(TextDocument.class.getName()).log(Level.SEVERE,
"Failed to create Default TOC, The refParagraph where the TOC be inserted is null");
throw new RuntimeException("Failed to create Default TOC, The refParagraph where the TOC be inserted is null");
}
if (additionalStyle == null) {
TextTableOfContentElement textTableOfContentElement = createDefaultTOC(refParagraph, before);
return textTableOfContentElement;
}
HashMap<Integer, String> tocstyleList = additionalStyle.getStyle();
if (tocstyleList.isEmpty()) {
TextTableOfContentElement textTableOfContentElement =createDefaultTOC(refParagraph, before);
return textTableOfContentElement;
}
Collection<String> tocvalues = tocstyleList.values();
OdfOfficeStyles docstyles = getOrCreateDocumentStyles();
Iterable<OdfStyle> paragraphStyles = docstyles
.getStylesForFamily(OdfStyleFamily.Paragraph);
ArrayList<String> pstyle = new ArrayList<String>();
Iterator<OdfStyle> iterator = paragraphStyles.iterator();
while (iterator.hasNext()) {
OdfStyle style = iterator.next();
String name = style.getStyleNameAttribute();
pstyle.add(name);
}
if (!pstyle.containsAll(tocvalues)) {
Logger.getLogger(TextDocument.class.getName())
.log(Level.SEVERE,
"Failed to create TOC with Styles. Some of addtional Styles that you want to include in the TOC can't be found in the document.\n"
+ tocvalues.toString());
throw new RuntimeException(
"Failed to create TOC Styles.Some of addtional Styles that you want to include in the TOC can't be found in the document.");
}
TextTableOfContentElement textTableOfContent =null;
// Additional Styles support
HashMap<String, Integer> tmptocstyleList = new HashMap<String, Integer>();
if (!tocstyleList.isEmpty()) {
Set<Integer> key = tocstyleList.keySet();
Iterator<Integer> it = key.iterator();
while (it.hasNext()) {
Integer textOutlineLevelValue = it.next();
String textStyleNameValue = tocstyleList
.get(textOutlineLevelValue);
tmptocstyleList.put(textStyleNameValue, textOutlineLevelValue);
}
}
// end of Additional Styles support
Node refparagraphNode = refParagraph.getOdfElement();
Node rootNode = refparagraphNode.getParentNode();
try {
OdfContentDom content = getContentDom();
textTableOfContent = content
.newOdfElement(TextTableOfContentElement.class);
textTableOfContent.setTextNameAttribute("Table of Contents");
textTableOfContent.setTextProtectedAttribute(true);
TextTableOfContentSourceElement textTableOfContentSource = textTableOfContent
.newTextTableOfContentSourceElement();
textTableOfContentSource.setTextOutlineLevelAttribute(10);
textTableOfContentSource.setTextUseIndexMarksAttribute(true);
textTableOfContentSource.setTextUseIndexSourceStylesAttribute(true);
TextIndexTitleTemplateElement textIndexTitleTemplate = textTableOfContentSource
.newTextIndexTitleTemplateElement();
textIndexTitleTemplate
.setTextStyleNameAttribute("Contents_20_Heading");
textIndexTitleTemplate.setTextContent("Table of Contents");
for (int i = 1; i <= 10; i++) {
TextTableOfContentEntryTemplateElement textTableOfContentEntryTemplate = textTableOfContentSource
.newTextTableOfContentEntryTemplateElement(i,
"Contents_20_" + i);
TextIndexEntryLinkStartElement TextIndexEntryLinkStart = textTableOfContentEntryTemplate
.newTextIndexEntryLinkStartElement();
TextIndexEntryLinkStart
.setTextStyleNameAttribute("Index_20_Link");
textTableOfContentEntryTemplate
.newTextIndexEntryChapterElement();
textTableOfContentEntryTemplate.newTextIndexEntryTextElement();
TextIndexEntryTabStopElement TextIndexEntryTabStop = textTableOfContentEntryTemplate
.newTextIndexEntryTabStopElement("right");
TextIndexEntryTabStop.setStyleLeaderCharAttribute(".");
textTableOfContentEntryTemplate
.newTextIndexEntryPageNumberElement();
textTableOfContentEntryTemplate
.newTextIndexEntryLinkEndElement();
}
if (!tocstyleList.isEmpty()) {
Set<Integer> key = tocstyleList.keySet();
Iterator<Integer> it = key.iterator();
while (it.hasNext()) {
Integer textOutlineLevelValue = it.next();
String textStyleNameValue = tocstyleList
.get(textOutlineLevelValue);
TextIndexSourceStylesElement textIndexSourceStyles = textTableOfContentSource
.newTextIndexSourceStylesElement(textOutlineLevelValue);
textIndexSourceStyles
.newTextIndexSourceStyleElement(textStyleNameValue);
}
}
TextIndexBodyElement textIndexBody = textTableOfContent
.newTextIndexBodyElement();
TextIndexTitleElement TextIndexTitle = textIndexBody
.newTextIndexTitleElement("Table of Contents_Head");
TextPElement texp = TextIndexTitle.newTextPElement();
texp.setTextStyleNameAttribute("Contents_20_Heading");