Hi Nicole,

Nicole Scholz escribió:
Hi Ariel!

I had a look at the BaseIndex service and I tried to add it to the code. But I don't know how I set the style to another font or font size.

sorry me if I was too cryptic. The BaseIndex services defines a set of
properties. In the case of styles, you can get the *name* of the header
style, or all the 10 levels. This is the name of *paragraph* style.



Object oContentIndex = xDocFactory.createInstance(
                     "com.sun.star.text.ContentIndex");

XPropertySet xContentIndexProps = (XPropertySet)
                     UnoRuntime.queryInterface( XPropertySet.class,
oContentIndex);

String sParaStyleHeading = AnyConverter.toString(

xContentIndexProps.getPropertyValue("ParaStyleHeading"));



So you can get that paragraph style from the document paragraph styles
family, and change its properties.

  XStyleFamiliesSupplier xStyleFamiliesSupplier =
                     (XStyleFamiliesSupplier) UnoRuntime.queryInterface(
                     XStyleFamiliesSupplier.class, xTextDocument);
  XNameAccess xStyleFamilies = xStyleFamiliesSupplier.getStyleFamilies();

  XNameContainer xParaStyleFamily = (XNameContainer)
                     UnoRuntime.queryInterface( XNameContainer.class,
                     xStyleFamilies.getByName("ParagraphStyles") );

  XPropertySet xHeadingStyleProps = (XPropertySet)
                     UnoRuntime.queryInterface( XPropertySet.class,
                     xParaStyleFamily.getByName(sParaStyleHeading) );

                 xHeadingStyleProps.setPropertyValue(
                         "ParaAdjust", ParagraphAdjust.CENTER);

                 xHeadingStyleProps.setPropertyValue(
                         "CharHeight", new Integer(20));



You can also get any paragraph style, change its properties, and set it
at the level you want, using its name.

Or you can also create a new custom style, and set it at the index level
you want.

Can you give me an example how I can use the BaseIndex?

I attach (and send to your private mail, as I'm not sure if it will pass
  to the list) a Java example.
Let me know if something isn't clear.


Regards
Ariel.



--
Ariel Constenla-Haile
La Plata, Argentina

[EMAIL PROTECTED]
[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.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.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.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;

/**
 *
 * @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();
            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();
        }
    }

    //**************************************************************************

    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