Hi Nicole,
Nicole Scholz escribió:
Hi!
Thank you for your answer. I tried to update the toc but this does not solve
the problem. I add my code to this mail perhaps I do something else wrong.
[ooRexx code]
as my ooRexx knowledge is null, I can't get if you're doing something
wrong; but you can try with the code I attach, which does almost the
same [I added the LevelFormat demo]; maybe stepping into the code, you
can find if you're missing something.
Regards
Ariel.
--
Ariel Constenla-Haile
La Plata, Argentina
[EMAIL PROTECTED]
http://www.ArielConstenlaHaile.com.ar/ooo/
"Aus der Kriegsschule des Lebens
- Was mich nicht umbringt,
macht mich härter."
Nietzsche Götzendämmerung, Sprüche und Pfeile, 8.
/*
* DocumentIndexesDemo.java
*
* Created on 2008.08.31 - 18:21:29
*
*/
package org.openoffice.sdk.writer;
import com.sun.star.awt.FontSlant;
import com.sun.star.awt.FontWeight;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.uno.XComponentContext;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.container.XIndexReplace;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.XNameContainer;
import com.sun.star.container.XNamed;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.Locale;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.style.NumberingType;
import com.sun.star.style.ParagraphAdjust;
import com.sun.star.style.XStyleFamiliesSupplier;
import com.sun.star.text.ControlCharacter;
import com.sun.star.text.XAutoTextContainer;
import com.sun.star.text.XAutoTextEntry;
import com.sun.star.text.XAutoTextGroup;
import com.sun.star.text.XChapterNumberingSupplier;
import com.sun.star.text.XDocumentIndex;
import com.sun.star.text.XText;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import java.util.ArrayList;
/**
*
* @author ariel
*/
public class DocumentIndexesDemo {
private static final String CONTENT_INDEX1 = "API Content Index 1";
private final XComponentContext m_xContext;
private XTextDocument m_xTextDocument = null;
private XNameAccess m_xStyleFamilies;
private XAutoTextEntry m_xDummyText = null;
private XDocumentIndex m_xDocumentIndex = null;
/** Creates a new instance of DocumentIndexesDemo */
public DocumentIndexesDemo( XComponentContext xContext ) {
this.m_xContext = xContext;
}
protected void run(){
initDoc();
if (m_xTextDocument!=null){
initData();
createIndex();
demoOutlineNumbering();
}
}
private void initDoc(){
XTextDocument xTextDocument = createNewTextDoc(m_xContext);
if (xTextDocument!=null) {
m_xTextDocument = xTextDocument;
XStyleFamiliesSupplier xStyleFamiliesSupplier =
(XStyleFamiliesSupplier) UnoRuntime.queryInterface(
XStyleFamiliesSupplier.class, m_xTextDocument);
m_xStyleFamilies = xStyleFamiliesSupplier.getStyleFamilies();
m_xDummyText = getDummyText(m_xContext);
}
}
private void initData(){
try {
XText xText = m_xTextDocument.getText();
XTextCursor xTextCursor = xText.createTextCursor();
XPropertySet xTextCursorProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xTextCursor);
insertParaBreak(xTextCursor.getEnd());
insertParaBreak(xTextCursor.getEnd());
for (int n = 1; n < 5; n++) {
// Heading Level 1
xTextCursorProps.setPropertyValue("ParaStyleName", "Heading 1");
insertText(xTextCursor, "Heading Level 1");
// dummy text
insertParaBreak(xTextCursor.getEnd());
xTextCursorProps.setPropertyValue("ParaStyleName", "Default");
insertDummyText(xTextCursor);
insertParaBreak(xTextCursor.getEnd());
// Heading Level 2
xTextCursorProps.setPropertyValue("ParaStyleName", "Heading 2");
insertText(xTextCursor, "Heading Level 2");
insertParaBreak(xTextCursor.getEnd());
xTextCursorProps.setPropertyValue("ParaStyleName", "Default");
insertDummyText(xTextCursor);
insertParaBreak(xTextCursor.getEnd());
// Heading Level 3
xTextCursorProps.setPropertyValue("ParaStyleName", "Heading 3");
insertText(xTextCursor, "Heading Level 3");
insertParaBreak(xTextCursor.getEnd());
xTextCursorProps.setPropertyValue("ParaStyleName", "Default");
insertDummyText(xTextCursor);
insertParaBreak(xTextCursor.getEnd());
}
} catch (Exception e) {
e.printStackTrace();
}
}
synchronized void insertDummyText(XTextRange xTextRange) {
if (m_xDummyText != null) {
m_xDummyText.applyTo(xTextRange);
} else {
insertText(xTextRange, "Bla, bla, bla.");
}
}
private void createIndex(){
try {
XMultiServiceFactory xDocFactory = (XMultiServiceFactory)
UnoRuntime.queryInterface(
XMultiServiceFactory.class, m_xTextDocument);
Object oContentIndex = xDocFactory.createInstance(
"com.sun.star.text.ContentIndex");
// a unique name may help for later access
XNamed xNamedIndex = (XNamed) UnoRuntime.queryInterface(
XNamed.class, oContentIndex);
xNamedIndex.setName(CONTENT_INDEX1);
// access the set of properties
XPropertySet xContentIndexProps = (XPropertySet)
UnoRuntime.queryInterface( XPropertySet.class, oContentIndex);
// "Title" is a property of every BaseIndex
xContentIndexProps.setPropertyValue("Title", "API Generated ContentIndex");
// "IsProtected" is a property of every BaseIndex
xContentIndexProps.setPropertyValue("IsProtected", Boolean.TRUE);
// "CreateFromOutline" is a ContentIndex property
xContentIndexProps.setPropertyValue("CreateFromOutline", Boolean.TRUE);
// HOW TO CHANGE THE STYLES?
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//
// GETTER METHOD
// 1. get the name of the style applied to the level we want
// 2. get that style from the paragraph styles family
// 3. modify it
//
XNameContainer xParaStyleFamily = (XNameContainer)
UnoRuntime.queryInterface( XNameContainer.class,
m_xStyleFamilies.getByName("ParagraphStyles") );
// the service BaseIndex has properties to access the styles:
// ParaStyleHeading
// ParaStyleLevel1, ParaStyleLevel2 ... ParaStyleLevel10
// change the heading style
String sParaStyleHeading = AnyConverter.toString(
xContentIndexProps.getPropertyValue("ParaStyleHeading"));
if (xParaStyleFamily.hasByName(sParaStyleHeading)) {
XPropertySet xHeadingStyleProps = (XPropertySet)
UnoRuntime.queryInterface( XPropertySet.class,
xParaStyleFamily.getByName(sParaStyleHeading) );
xHeadingStyleProps.setPropertyValue(
"ParaAdjust", ParagraphAdjust.CENTER);
xHeadingStyleProps.setPropertyValue(
"CharHeight", new Integer(20));
xHeadingStyleProps.setPropertyValue(
"CharColor", getRGBColor( 0, 0, 125));
xHeadingStyleProps.setPropertyValue(
"CharFontName", "DejaVu Serif;Galatia SIL");
}
// change the style for level 1
String sParaStyleLevel1 = AnyConverter.toString(
xContentIndexProps.getPropertyValue("ParaStyleLevel1"));
if (xParaStyleFamily.hasByName(sParaStyleLevel1)) {
XPropertySet xParaStyleLevel1 = (XPropertySet)
UnoRuntime.queryInterface( XPropertySet.class,
xParaStyleFamily.getByName(sParaStyleLevel1) );
xParaStyleLevel1.setPropertyValue(
"CharPosture", FontSlant.ITALIC);
}
// change the style for level 3
String sParaStyleLevel3 = AnyConverter.toString(
xContentIndexProps.getPropertyValue("ParaStyleLevel3"));
if (xParaStyleFamily.hasByName(sParaStyleLevel3)) {
XPropertySet xParaStyleLevel3 = (XPropertySet)
UnoRuntime.queryInterface( XPropertySet.class,
xParaStyleFamily.getByName(sParaStyleLevel3) );
xParaStyleLevel3.setPropertyValue(
"CharColor", getRGBColor(255, 0, 255));
}
///////////////////////////////////////////////////////////////////
//
// SETTER METHOD
//
// 1. create a new custom style
// 2. set that style at the index level you want
//
// create a new style for level 2
String sMyParaStyleName = "My Custom ContentIndex Style Level 2";
Object oNewParaStyleLevel2;
if (xParaStyleFamily.hasByName(sMyParaStyleName)) {
oNewParaStyleLevel2 = xParaStyleFamily.getByName(sMyParaStyleName);
} else {
oNewParaStyleLevel2 = xDocFactory.createInstance(
"com.sun.star.style.ParagraphStyle");
xParaStyleFamily.insertByName(sMyParaStyleName, oNewParaStyleLevel2);
}
XPropertySet xParaStyleLevel2 = (XPropertySet)
UnoRuntime.queryInterface( XPropertySet.class,
oNewParaStyleLevel2 );
xParaStyleLevel2.setPropertyValue(
"ParaLeftMargin", new Integer(500));
xParaStyleLevel2.setPropertyValue(
"CharWeight", FontWeight.BOLD);
// SET IT AT THE INDEX LEVEL
xContentIndexProps.setPropertyValue("ParaStyleLevel2", sMyParaStyleName);
////////////////////////////////////////////////////////////////////
// hold a reference to update the document later
m_xDocumentIndex = (XDocumentIndex)
UnoRuntime.queryInterface(
XDocumentIndex.class, oContentIndex);
// insert the index at the start of the document
insertTextContent(m_xTextDocument.getText().getStart(), m_xDocumentIndex);
m_xDocumentIndex.update();
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
/**
* DocumentIndexLevelFormat
*
* BaseIndex - prop. "LevelFormat"
*
* returns the interface of the level format of the index.
* The description of the format of the levels depends
* on the type of the document index.
*/
XIndexReplace xLevelFormat = (XIndexReplace) UnoRuntime.queryInterface(
XIndexReplace.class, xContentIndexProps.getPropertyValue("LevelFormat"));
ArrayList< PropertyValue[] > aFormatLevels1_3 = new ArrayList< PropertyValue[] >();
PropertyValue[] aChapterNumber = new PropertyValue[1];
aChapterNumber[0] = new PropertyValue();
aChapterNumber[0].Name = "TokenType";
aChapterNumber[0].Value = "TokenEntryNumber";
aFormatLevels1_3.add(aChapterNumber);
PropertyValue[] aChapterName = new PropertyValue[1];
aChapterName[0] = new PropertyValue();
aChapterName[0].Name = "TokenType";
aChapterName[0].Value = "TokenEntryText";
aFormatLevels1_3.add(aChapterName);
PropertyValue[] aTabStop = new PropertyValue[4];
aTabStop[0] = new PropertyValue();
aTabStop[0].Name = "TokenType";
aTabStop[0].Value = "TokenTabStop";
aTabStop[1] = new PropertyValue();
aTabStop[1].Name = "TabStopRightAligned";
aTabStop[1].Value = Boolean.TRUE;
aTabStop[2] = new PropertyValue();
aTabStop[2].Name = "TabStopFillCharacter";
aTabStop[2].Value = ".";
aTabStop[3] = new PropertyValue();
aTabStop[3].Name = "WithTab";
aTabStop[3].Value = Boolean.TRUE;
aFormatLevels1_3.add(aTabStop);
PropertyValue[] aHyperlinkStart = new PropertyValue[2];
aHyperlinkStart[0] = new PropertyValue();
aHyperlinkStart[0].Name = "TokenType";
aHyperlinkStart[0].Value = "TokenHyperlinkStart";
aHyperlinkStart[1] = new PropertyValue();
aHyperlinkStart[1].Name = "CharacterStyleName";
aHyperlinkStart[1].Value = "Internet Link";
aFormatLevels1_3.add(aHyperlinkStart);
PropertyValue[] aPageNumber = new PropertyValue[1];
aPageNumber[0] = new PropertyValue();
aPageNumber[0].Name = "TokenType";
aPageNumber[0].Value = "TokenPageNumber";
aFormatLevels1_3.add(aPageNumber);
PropertyValue[] aHyperlinkEnd = new PropertyValue[1];
aHyperlinkEnd[0] = new PropertyValue();
aHyperlinkEnd[0].Name = "TokenType";
aHyperlinkEnd[0].Value = "TokenHyperlinkEnd";
aFormatLevels1_3.add(aHyperlinkEnd);
int dim = aFormatLevels1_3.size();
PropertyValue[][] aPropVals = aFormatLevels1_3.toArray(
new PropertyValue[dim][]);
// apply format to levels 1 - 3
xLevelFormat.replaceByIndex(1, aPropVals);
xLevelFormat.replaceByIndex(2, aPropVals);
xLevelFormat.replaceByIndex(3, aPropVals);
m_xDocumentIndex.update();
} catch (Exception e) {
e.printStackTrace();
}
}
private void demoOutlineNumbering(){
try {
XChapterNumberingSupplier xChapterNumberingSupplier =
(XChapterNumberingSupplier) UnoRuntime.queryInterface(
XChapterNumberingSupplier.class, m_xTextDocument);
if ( xChapterNumberingSupplier == null )
return;
XIndexReplace xChapterNumberingRules =
xChapterNumberingSupplier.getChapterNumberingRules();
ArrayList<PropertyValue> aChapterNumberingList = new ArrayList<PropertyValue>();
////////////////////////////////////////////////////////////////////
/**
* HeadingStyleName
*
* Contains the name of the paragraph style
* that marks a paragraph as a chapter heading.
*/
PropertyValue aHeadingStyleName = new PropertyValue();
aHeadingStyleName.Name = "HeadingStyleName";
aHeadingStyleName.Value = "Heading 1";
aChapterNumberingList.add(aHeadingStyleName);
/**
* NumberingType
*
* Determines the type of the numbering defined
* in com.sun.star.style.NumberingType.
*/
PropertyValue aNumberingType = new PropertyValue();
aNumberingType.Name = "NumberingType";
aNumberingType.Value = NumberingType.ROMAN_UPPER;
aChapterNumberingList.add(aNumberingType);
/**
* Suffix
*
* the suffix of the numbering symbol.
*/
PropertyValue aSuffix = new PropertyValue();
aSuffix.Name = "Suffix";
aSuffix.Value = ". ";
aChapterNumberingList.add(aSuffix);
xChapterNumberingRules.replaceByIndex( 0,
aChapterNumberingList.toArray(
new PropertyValue[ aChapterNumberingList.size() ] ));
// we could reuse it! but for the sake of clarity, we clear it
aChapterNumberingList.clear();
///////////////////////////////////////////////////////////////////
// HeadingStyleName
aHeadingStyleName.Value = "Heading 2";
aChapterNumberingList.add(aHeadingStyleName);
// NumberingType
aNumberingType.Value = NumberingType.ARABIC;
aChapterNumberingList.add(aNumberingType);
// Suffix
aSuffix.Value = " ";
aChapterNumberingList.add(aSuffix);
/**
* ParentNumbering
*
* Determines if higher numbering levels are included in the numbering,
* for example, 2.3.1.2.
*/
PropertyValue aParentNumbering = new PropertyValue();
aParentNumbering.Name = "ParentNumbering";
aParentNumbering.Value = new Short((short)2);
aChapterNumberingList.add(aParentNumbering);
xChapterNumberingRules.replaceByIndex( 1,
aChapterNumberingList.toArray(
new PropertyValue[ aChapterNumberingList.size() ] ));
// we could reuse it! but for the sake of clarity, we clear it
aChapterNumberingList.clear();
///////////////////////////////////////////////////////////////////
// HeadingStyleName
aHeadingStyleName.Value = "Heading 3";
aChapterNumberingList.add(aHeadingStyleName);
// NumberingType
aNumberingType.Value = NumberingType.ROMAN_LOWER;
aChapterNumberingList.add(aNumberingType);
// Suffix
aSuffix.Value = ") ";
aChapterNumberingList.add(aSuffix);
// ParentNumbering
aParentNumbering.Value = new Short((short)3);
aChapterNumberingList.add(aParentNumbering);
xChapterNumberingRules.replaceByIndex( 2,
aChapterNumberingList.toArray(
new PropertyValue[ aChapterNumberingList.size() ] ));
// update the index of contents in order to
// reflect the new outline numbering
if ( m_xDocumentIndex != null)
m_xDocumentIndex.update();
} catch (Exception e) {
e.printStackTrace();
}
}
//**************************************************************************
public static XTextDocument createNewTextDoc( XComponentContext xContext ){
XTextDocument xTextDocument = null;
try {
XComponentLoader xComponentLoader =
(XComponentLoader) UnoRuntime.queryInterface(
XComponentLoader.class,
xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext));
if (xComponentLoader!=null) {
xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class,
xComponentLoader.loadComponentFromURL(
"private:factory/swriter",
"_default", 0, new PropertyValue[]{}));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return xTextDocument;
}
}
public static void insertText(XTextRange xTextRange, String string) {
xTextRange.getText().insertString(xTextRange, string, false);
}
public static void insertParaBreak(XTextRange xTextRange) {
try {
xTextRange.getText().insertControlCharacter(
xTextRange, ControlCharacter.PARAGRAPH_BREAK, false);
} catch (com.sun.star.lang.IllegalArgumentException ex) {
ex.printStackTrace();
}
}
public static void insertTextContent(XTextRange xTextRange, XTextContent xContent) {
try {
xTextRange.getText().insertTextContent(xTextRange, xContent, false);
} catch (com.sun.star.uno.Exception ex){
ex.printStackTrace();
}
}
public static XAutoTextEntry getDummyText( XComponentContext xContext){
XAutoTextEntry xAutoTextEntry = null;
try {
XAutoTextContainer xAutoTextContainer =
(XAutoTextContainer) UnoRuntime.queryInterface(
XAutoTextContainer.class,
xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.text.AutoTextContainer", xContext));
if ( xAutoTextContainer.hasByName("standard")) {
XAutoTextGroup xAutoTextGroup = (XAutoTextGroup)
UnoRuntime.queryInterface(
XAutoTextGroup.class,
xAutoTextContainer.getByName("standard"));
/**
* Default AutoText's are locale sensitive!
*/
Locale aOOoLocale = getOOoLocale(xContext);
String sLang = aOOoLocale.Language;
Object oAutoTextEntry = null;
if (sLang.equals("en")) {
if (xAutoTextGroup.hasByName("DT")) {
oAutoTextEntry = xAutoTextGroup.getByName("DT");
}
} else if(sLang.equals("de")) {
if (xAutoTextGroup.hasByName("BT")) {
oAutoTextEntry = xAutoTextGroup.getByName("BT");
}
} // else if ...
xAutoTextEntry = (XAutoTextEntry) UnoRuntime.queryInterface(
XAutoTextEntry.class, oAutoTextEntry);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return xAutoTextEntry;
}
}
public static Locale getOOoLocale( XComponentContext xContext ) {
Locale aLocale = new Locale();
try {
Object oConfigurationProvider =
xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.configuration.ConfigurationProvider", xContext);
XMultiServiceFactory xConfigurationProvider =
(XMultiServiceFactory) UnoRuntime.queryInterface(
XMultiServiceFactory.class,
oConfigurationProvider);
PropertyValue aPropValue = new PropertyValue();
aPropValue.Name = "nodepath";
aPropValue.Value = "/org.openoffice.Setup/L10N";
XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(
XNameAccess.class, xConfigurationProvider.createInstanceWithArguments(
"com.sun.star.configuration.ConfigurationAccess",
new PropertyValue[]{aPropValue}));
String sLocale = AnyConverter.toString(
xNameAccess.getByName("ooLocale"));
if ( sLocale.length() > 0) {
if (sLocale.contains("-")){
String[] sLocaleInfo = sLocale.split("-");
aLocale.Language = sLocaleInfo[0];
if (sLocaleInfo.length > 1) {
aLocale.Country = sLocaleInfo[1];
}
} else {
aLocale.Language = sLocale;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
return aLocale;
}
}
/**
* Returns the value for setting a color property of an object, as an Integer.
*
* @param r the <b>red</b> component
* @param g the <b>green</b> component
* @param b the <b>blue</b> component
* @return an Integer with the color value
* @throws com.sun.star.lang.IllegalArgumentException
* if any of the color components is < 0 or > 255
*/
public static Integer getRGBColor(int r, int g, int b)
throws com.sun.star.lang.IllegalArgumentException
{
boolean isRangeBad = false;
String sErrorMessage = "";
if ( r < 0 || r > 255 ){
isRangeBad = true;
sErrorMessage += " red component";
}
if ( g < 0 || g > 255 ){
isRangeBad = true;
sErrorMessage += " green component";
}
if ( b < 0 || b > 255 ){
isRangeBad = true;
sErrorMessage += " blue component";
}
if( isRangeBad ){
throw new com.sun.star.lang.IllegalArgumentException(
"Color component outside the legal range: " + sErrorMessage);
} else {
int intValue = (b | g << 8) | (r << 16);
return new Integer( intValue );
}
}
//**************************************************************************
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
// get the remote office component context
XComponentContext xContext = Bootstrap.bootstrap();
if (xContext == null) {
System.err.println("ERROR: Could not bootstrap default Office.");
}
DocumentIndexesDemo demo = new DocumentIndexesDemo(xContext);
demo.run();
}
catch (java.lang.Exception e){
e.printStackTrace();
}
finally {
System.exit( 0 );
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]