Hi Nicole,

Nicole Scholz escribió:
Hi!

I would like to install an automatic chapter numbering in my writer document. I 
did not find an example how to program this. Can anyone give me an example or a 
hint where I find one?

Regards
Nicole


if I understood clearly, your talking about what you can do in the menu "Tools" - "Outline Numbering...". If so, you'll get its API counterpart in the interface com.sun.star.text.XChapterNumberingSupplier, implemented by the model of every OOo Writer document.

See the Developer's Guide:
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Text/Line_Numbering_and_Outline_Numbering

And the method demoOutlineNumbering() in the example I attach
[by the way, which is in German the shortcut for the Dummy Text - DT AutoText?]

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 static final String CONTENT_INDEX2 = "API Content Index 2";

    private final XComponentContext m_xContext;
    private XTextDocument m_xTextDocument = null;
    private XNameAccess m_xStyleFamilies;
    private XAutoTextEntry m_xDummyText = null;

    /** Creates a new instance of DocumentIndexesDemo */
    public DocumentIndexesDemo( XComponentContext xContext ) {
        this.m_xContext = xContext;
    }

    protected void run(){
        initDoc();
        if (m_xTextDocument!=null){
            initData();
            demoOutlineNumbering();
            createIndexes();
        }
    }

    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 createIndexes(){
        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);

            ////////////////////////////////////////////////////////////////////

            XDocumentIndex xDocumentIndex = (XDocumentIndex)
                    UnoRuntime.queryInterface(
                    XDocumentIndex.class, oContentIndex);

            // insert the index at the start of the document
            insertTextContent(m_xTextDocument.getText().getStart(), xDocumentIndex);

            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() ] ));
            
        } 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 &lt; 0 or &gt; 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]

Reply via email to