You are viewing a plain text version of this content. The canonical link for it is here.
Posted to api@openoffice.apache.org by "Rony G. Flatscher" <Ro...@wu-wien.ac.at> on 2012/11/10 18:55:49 UTC

The Java transcription ... (Re: Question ad Impress: inserting text using the content's layout formatting, howto

Hi Ariel,

On 08.11.2012 20:37, Ariel Constenla-Haile wrote:
> On Thu, Nov 08, 2012 at 02:02:08PM +0100, Rony G. Flatscher wrote:
>> O.K., it is at the end of this e-mail. Correction: the kudos for
>> finding it goes to Christoph *Jopp* still from Munich and a great AOO
>> hacker! :)
>>>
>>> Wish I was there!
>> Yes, that would have been nice to meet in person!
>>
>> ---
>>
>> The following was created yesterday and added to todays presentation
>> "Scripting Apache OpenOffice", which should be uploaded by ASF (no URL
>> yet).
>>
>> The programming language is ooRexx (http://www.ooRexx.org) which is
>> powerful yet its syntax is very easy and looks like pseudo code. It
>> has a proper message operator (tilde: ~), left of it is the receiving
>> object right of it is the message name (sometimes with arguments in
>> round parenthesis).
>>
>> The logic needed can be found in the routine addItem(), dumpItem()
>> demonstrates how to iterate over an impress XText and learn the
>> outline level in addition:
>>
>> xDesktop=uno.createDesktop() -- bootstrap & get access to
>> XDesktop xcl=xDesktop~XComponentLoader -- get
>> XComponentLoader interface
>>
>> uri="private:factory/simpress" -- new simpress document
>> doc=xcl~loadComponentFromURL(uri,"_blank",0,.uno~noProps)
>>
>> xDrawPages = doc~XDrawPagesSupplier~getDrawPages -- get
>> DrawPages
>>
>> xDrawPage=xDrawPages~getByIndex(0) -- get first (empty) page
>> xDrawPage~XPropertySet~setPropertyValue("Layout", box("short",0))
>> -- "Title Slide" xShapes=xDrawPage~XShapes -- get access
>> to its shapes xShapes~getByIndex(0)~XText~setString("ApacheCon
>> Europe 2012") xShapes~getByIndex(1)~XText~setString("Scripting
>> Apache OpenOffice")
>>
>> xDrawPage=xDrawPages~~insertNewByIndex(1)~getByIndex(1) -- insert
>> at end, get access
>> xDrawPage~XPropertySet~setPropertyValue("Layout", box("short",1))
>> -- "Title Content" xShapes=xDrawPage~XShapes -- get
>> access to its shapes
>> xShapes~getByIndex(0)~XText~setString("Scripting Apache
>> OpenOffice")
>>
>> xText=xShapes~getByIndex(1)~XText -- content's XText call
>> addItem xText, "First", 0 -- add string, determine
>> level call addItem xText, "Explored by many", 0 call addItem
>> xText, "Kudos! go to", 1 call addItem xText, "Christoph
>> Jopp!", 1 call addItem xText, "On 2012-11-07", 0, .false
>>
>> doc~XModifiable~setModified(.false)
>> doc~XPresentationSupplier~getPresentation~~bsf.dispatch("start")
>> -- start presentation
>>
>> say "now dumping infos about second page's content XText:" call
>> dumpItems xText
>>
>> ::requires UNO.CLS -- get UNO support
>>
>> ::routine addItem -- adds string at the given
>> (0-based outline) level use arg xText, string, level,
>> bNewParagraph=.true
>>
>> xTR=xText~XTextRange~getEnd -- get end, a XTextRange
>> xTR~XPropertySet~setPropertyValue("NumberingLevel",level) -- set
>> XTextRange level
>>
>> xTR~setString(string) -- set string
>>
>> if bNewParagraph=.true then -- add new paragraph
>> xTR~getEnd~setString("0a"x) -- add linefeed character -> new
>> paragraph
>>
>>
>> ::routine dumpItems -- show level and string from
>> XText use arg xText
>>
>> enum=xText~XEnumerationAccess~createEnumeration -- enumerate
>> paragraphs do i=1 while enum~hasMoreElements
>> xtr=enum~nextElement~XTextRange -- we need XTextRange's string
>> & properties
>> nl=xtr~XPropertySet~getPropertyValue("NumberingLevel") say
>> " item #" i": NumberingLevel="pp(nl) pp(xtr~getString) end
>>
>> Should anyone have questions, please come forward!
>
> I was digging into this the other days, to give a sample, but I've found
> it somehow broken. Could you insert more than one paragraph in the same
> shape, and set each paragraph to a different numbering level?
>
> In my test, only the first paragraph gets the numbering level, it does
> not work for subsequent paragraphs (added by inserting a para. break
> control character). And, the numbering level inserted via API is not
> reflected on the Outline view.
>
> Another issue: how to set an animation for each paragraph?
>
> Yet another one, setting the draw page Layout has effect, but is not
> reflected in the UI: when you select a page with a specific layout, the
> "Layout" taks pane should reflect this by selecting the respective
> level; that's not the case.
>
> Impress API seems broken at several levels.
in the case that you need a pure Java version of the above ooRexx code, here is a transcription.

In order to run the following program you need to compile them having access to the AOO jar-files
juh.jar and jurt.jar (both in URE/java) and ridl.jar and unoil.jar (both in Basis/program/classes).

    import com.sun.star.uno.UnoRuntime;

    import com.sun.star.beans.PropertyValue;
    import com.sun.star.beans.XPropertySet;

    import com.sun.star.comp.helper.Bootstrap;

    import com.sun.star.frame.XDesktop;

    import com.sun.star.text.XText;
    // import com.sun.star.text.XTextCursor;
    import com.sun.star.text.XTextRange;

    import com.sun.star.container.XEnumerationAccess;
    import com.sun.star.container.XEnumeration;
    import com.sun.star.frame.XComponentLoader;

    import com.sun.star.drawing.XDrawPage;
    import com.sun.star.drawing.XDrawPages;
    import com.sun.star.drawing.XDrawPagesSupplier;

    import com.sun.star.uno.XComponentContext;
    import com.sun.star.lang.XComponent;

    import com.sun.star.drawing.XShape;
    import com.sun.star.drawing.XShapes;

    import com.sun.star.util.XModifiable;
    import com.sun.star.presentation.XPresentationSupplier;

    import com.sun.star.comp.helper.BootstrapException;
    import com.sun.star.uno.Exception;
    import com.sun.star.beans.UnknownPropertyException;
    import com.sun.star.beans.PropertyVetoException;
    import com.sun.star.lang.IllegalArgumentException;
    import com.sun.star.lang.WrappedTargetException;
    import com.sun.star.container.NoSuchElementException;

    class Simpress3
    {
        public static void main (String args[]) throws BootstrapException, Exception
        {
            XComponentContext xContext=Bootstrap.bootstrap();   // create a fully initialized local
XComponentContext
            String service   ="com.sun.star.frame.Desktop";     // OOo Desktop UNO component service
            Object  desktop  =xContext.getServiceManager().createInstanceWithContext(service,xContext);
            XDesktop xDesktop=(XDesktop) UnoRuntime.queryInterface(XDesktop.class,desktop);

            XComponentLoader xcl=(XComponentLoader)
UnoRuntime.queryInterface(XComponentLoader.class,xDesktop);
            String uri="private:factory/simpress";              // new simpress document
            XComponent doc=xcl.loadComponentFromURL(uri,"_blank",0, new PropertyValue[0]);

                // get XDrawPages
            XDrawPages xDrawPages=((XDrawPagesSupplier)
UnoRuntime.queryInterface(XDrawPagesSupplier.class,doc)).getDrawPages();

                // work on first XDrawPage (first slide)
            XDrawPage xDrawPage=(XDrawPage)
UnoRuntime.queryInterface(XDrawPage.class,xDrawPages.getByIndex(0));   // get first (empty) page

            ((XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class,xDrawPage)).setPropertyValue("Layout",0); // "Title Slide"
            XShapes xShapes=(XShapes) UnoRuntime.queryInterface(XShapes.class,xDrawPage);

            XShape xs=(XShape) UnoRuntime.queryInterface(XShape.class,xShapes.getByIndex(0));
            ((XText) UnoRuntime.queryInterface(XText.class,xs)).setString("ApacheCon Europe 2012");
            xs=(XShape) UnoRuntime.queryInterface(XShape.class,xShapes.getByIndex(1));
            ((XText) UnoRuntime.queryInterface(XText.class,xs)).setString("Scripting Apache
OpenOffice");

                // create and work on a second XDrawPage (second slide)
            xDrawPages.insertNewByIndex(1);
            xDrawPage=(XDrawPage)
UnoRuntime.queryInterface(XDrawPage.class,xDrawPages.getByIndex(1)); // insert at end, get access
            ((XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class,xDrawPage)).setPropertyValue("Layout",1); // "Title
Content"

            xShapes=(XShapes) UnoRuntime.queryInterface(XShapes.class,xDrawPage);
            xs=(XShape) UnoRuntime.queryInterface(XShape.class,xShapes.getByIndex(0));
            ((XText) UnoRuntime.queryInterface(XText.class,xs)).setString("Scripting Apache
OpenOffice");

            XText xText=(XText) UnoRuntime.queryInterface(XText.class,xShapes.getByIndex(1));   //
content's XText

            addItem(xText, "First",            0, true);
            addItem(xText, "Explored by many", 0, true);
            addItem(xText, "Kudos! go to",     1, true);
            addItem(xText, "Christoph Jopp!",  1, true);
            addItem(xText, "On 2012-11-07",    0, false);

            ((XModifiable) UnoRuntime.queryInterface(XModifiable.class,doc)).setModified(false);
                // start presentation
            ((XPresentationSupplier)
UnoRuntime.queryInterface(XPresentationSupplier.class,doc)).getPresentation().start();

            System.out.println("now dumping infos about second page's content XText:");
            dumpItems(xText);

            System.exit(0);
        }

        static void addItem(XText xText, String string, int level, boolean bNewParagraph) throws
UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException
        {
            XTextRange xTR=((XTextRange)
UnoRuntime.queryInterface(XTextRange.class,xText)).getEnd(); // get end, a XTextRange
                // set TextRange level
            ((XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class,xTR)).setPropertyValue("NumberingLevel",new
Short((short)level));

            xTR.setString(string);                  // set string

            if (bNewParagraph==true)                // add new paragraph
            {
                xTR.getEnd().setString("\n");       // add linefeed character -> new paragraph
            }
        }

        static void dumpItems(XText xText) throws NoSuchElementException, UnknownPropertyException,
WrappedTargetException
        {
                // enumerate paragraphs
            XEnumeration enuMM=(XEnumeration) ((XEnumerationAccess) 
UnoRuntime.queryInterface(XEnumerationAccess.class,xText)).createEnumeration();
            int i=0;
            while (enuMM.hasMoreElements())
            {
                    // we need XTextRange's string & properties
                XTextRange
xtr=(XTextRange)UnoRuntime.queryInterface(XTextRange.class,enuMM.nextElement());
                Object nl=((XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class,xtr)).getPropertyValue("NumberingLevel");
                System.out.println("     item # "+(++i)+": NumberingLevel=["+nl+"]
["+xtr.getString()+"]");
            }
        }
    }

HTH,

---rony