OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Creating a table of content with java

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
freddiz
Newbie
Newbie


Joined: 16 Feb 2009
Posts: 2

PostPosted: Mon Feb 16, 2009 1:16 am    Post subject: Creating a table of content with java Reply with quote

Hi!

I'm new when working with OO and programming, and just got some java code from a colleague that quit. What I want to do is to insert a table of content to my document. At the moment the code does create a title for the table of content, but a get no text. I will insert parts of the code. It would be great if anyone has an opinion of what I should do. And please let me know if you need more code.

Code:

/**
     * Sets up a table of content at the beginning of the first page
     */
    private void setTableOfContent(){
        try {
         
           mxParagraphCursor.gotoStart(false);
           mxParagraphCursor.gotoNextParagraph(false);
           insertParaBreak(mxParagraphCursor);
           
           // New TOC
            Object oContentIndex = mxMultiServiceFactory.createInstance(
                    "com.sun.star.text.ContentIndex");

            // Set a name for the TOC
            XNamed xNamedIndex = (XNamed) UnoRuntime.queryInterface(
                    XNamed.class, oContentIndex);
            xNamedIndex.setName(TOC_INCEX_1);

            // access the set of properties
            XPropertySet xContentIndexProps = (XPropertySet)
                    UnoRuntime.queryInterface( XPropertySet.class, oContentIndex);

            // "Title" is a "the heading" of the TOC.
            xContentIndexProps.setPropertyValue("Title", "Inneh√•llsf√∂rteckning");
            // "IsProtected" is a property of every BaseIndex
            xContentIndexProps.setPropertyValue("IsProtected", Boolean.TRUE);
            // "CreateFromOutline" is a ContentIndex property
            xContentIndexProps.setPropertyValue("CreateFromOutline", Boolean.FALSE);
           
           


            // 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,
                    mxNameAccess.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 = mxMultiServiceFactory.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(mxDocument.getText().getStart(), xDocumentIndex);
           
           

            xDocumentIndex.update();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



private void insertTextContent(XTextRange xTextRange, XTextContent xContent) {
        try {
           mxParagraphCursor.gotoStart(false);
           mxParagraphCursor.gotoNextParagraph(false);
           
           //xPropertySet.setPropertyValue("BreakType", com.sun.star.style.BreakType.PAGE_BOTH);
           
           mxText.insertControlCharacter(mxTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
           xTextRange.getText().insertTextContent(xTextRange, xContent, false);
           
        } catch (com.sun.star.uno.Exception ex){
            ex.printStackTrace();
        }
    }

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group