Hello Christian, Sebastian, *

On Thursday 26 March 2009, 06:12, Christian Lippka - Sun Microsystems GmbH - 
Hamburg wrote:
> Hi Sebastian,
>
> to my knowledge the writer does not support a group shape API. 

yes it does.

> In Draw or Impress you would use the XShapeGrouper interface 
> from the draw page. Maybe the draw page in writer
> also supports this interface. But I don't even know where to get the
> draw page in writer :-)

Writer's document model implenents com.sun.star.drawing.XDrawPageSupplier
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Text/Shape_Objects_in_Text#Drawing_Shapes

It seems someone deprecated 
http://api.openoffice.org/docs/common/ref/com/sun/star/drawing/XDrawPageSupplier.html
but that's wrong, this is the only way in Writer to access the DrawPage (the 
reason seems to be that there was a time when in Calc one accessed a single 
sheet's DrawPage via this interface - this seems to tell the fact that the 
only service supporting this interface is ::com::sun::star::sheet::Spreadsheet 
http://api.openoffice.org/docs/common/ref/com/sun/star/drawing/XDrawPageSupplier-
xref.html#SupportingServices)

> > How can I group some shapes and add them to a text document with Java?
> > I have a XTextDocument and a XTextRange.
> >
> > In the example at
> > http://api.openoffice.org/docs/DevelopersGuide/Drawing/Drawing.xhtml#1_3_
> >2_2_5_Grouping_2C_Combining_and_Binding 

@Sebastian: 
this is the "old" Develper's Guide. You must read the "new" version on the 
Wiki 
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Drawings/Grouping%2C_Combining_and_Binding

> >there is xDrawPage used to create
> > a GroupeShape. How can I handle the creation with the XTextDocument?

both method for grouping shapes described there work in Writer... well, only 
the first one does the job as expected. The code attached [CC you, as I'm not 
sure it will pass this list, in the API list it usually does] shows them both 
parallel in Writer and Draw.

Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina


"Aus der Kriegsschule des Lebens
                - Was mich nicht umbringt,
        macht mich härter."
                Nietzsche Götzendämmerung, Sprüche und Pfeile, 8.

package org.openoffice.sdk.drawing;

import com.sun.star.awt.Point;
import com.sun.star.awt.Size;
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.drawing.XDrawPage;
import com.sun.star.drawing.XDrawPageSupplier;
import com.sun.star.drawing.XDrawPages;
import com.sun.star.drawing.XDrawPagesSupplier;
import com.sun.star.drawing.XShape;
import com.sun.star.drawing.XShapeGroup;
import com.sun.star.drawing.XShapeGrouper;
import com.sun.star.drawing.XShapes;
import com.sun.star.frame.FrameSearchFlag;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.text.ControlCharacter;
import com.sun.star.text.TextContentAnchorType;
import com.sun.star.text.WrapTextMode;
import com.sun.star.text.XParagraphCursor;
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.uno.UnoRuntime;
import java.awt.Color;

/**
 *
 * @author ariel
 */
public class ShapeGroupDemo {

    public enum DocType {
        WRITER,
        DRAW;
    }

    private XComponentContext m_xContext;
    private DocType m_aType;
    private XDrawPage m_xDrawPage;
    private XMultiServiceFactory m_xDocFactory;
    private XText m_xDocText;
    private XTextCursor m_xDocCursor;
    
    /** Creates a new instance of ShapeGroupDemo */
    public ShapeGroupDemo(XComponentContext xContext, DocType type) {
        m_xContext = xContext;
        m_aType = type;
        initDoc();
    }
    
    private void initDoc(){
        XComponent xComponent = null;
        switch (m_aType) {
            case WRITER:
                try {
                    xComponent = createNewDoc(m_xContext, "writer");
                    XTextDocument xTextDoc = (XTextDocument) UnoRuntime.queryInterface(
                            XTextDocument.class, xComponent);

                    XDrawPageSupplier xDrawPageSupplier =
                            (XDrawPageSupplier) UnoRuntime.queryInterface(
                            XDrawPageSupplier.class, xComponent);
                    m_xDrawPage = xDrawPageSupplier.getDrawPage();

                    m_xDocText = xTextDoc.getText();
                    m_xDocCursor = m_xDocText.createTextCursorByRange(
                            m_xDocText.getStart());

                    m_xDocText.insertControlCharacter(m_xDocCursor,
                            ControlCharacter.PARAGRAPH_BREAK, false);

                    // Get the XParagraphCursor interface of our document cursor
                    XParagraphCursor xParaCursor =
                            (XParagraphCursor) UnoRuntime.queryInterface(
                            XParagraphCursor.class, m_xDocCursor);

                    xParaCursor.gotoPreviousParagraph(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case DRAW:
                try {
                    xComponent = createNewDoc(m_xContext, "draw");

                    XDrawPagesSupplier xDrawPagesSupplier =
                            (XDrawPagesSupplier) UnoRuntime.queryInterface(
                            XDrawPagesSupplier.class, xComponent);
                    XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();

                    m_xDrawPage =  xDrawPages.hasElements() ?
                        (XDrawPage) UnoRuntime.queryInterface(
                            XDrawPage.class, xDrawPages.getByIndex(0)) :
                        xDrawPages.insertNewByIndex(0);

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

        m_xDocFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(
                XMultiServiceFactory.class, xComponent);
    }

    
    protected void runShapeCollectionDemo() {
        try {

            // Create a RectangleShape using the document factory
            XShape xEllipse = createAndPosShape(m_xDocFactory, "Ellipse",
                    new Size(6000, 6000), new Point(0, 0));

            // Create an EllipseShape using the document factory
            XShape xRect = createAndPosShape(m_xDocFactory, "Rectangle",
                    new Size(7000, 4000), new Point(6100, 1500));

            // Get the XPropertySet interfaces of both shapes
            XPropertySet xRectProps = (XPropertySet) UnoRuntime.queryInterface(
                    XPropertySet.class, xRect);
            XPropertySet xEllipseProps = (XPropertySet) UnoRuntime.queryInterface(
                    XPropertySet.class, xEllipse);

            xRectProps.setPropertyValue("FillColor", new Integer(Color.MAGENTA.getRGB()));
            xEllipseProps.setPropertyValue("FillColor", new Integer(Color.RED.getRGB()));

            if (m_aType == DocType.WRITER) {
                // And set the AnchorTypes of both shapes to 'AT_PARAGRAPH'
                // Grouping of objects in text documents works
                // ONLY if none of the objects has an anchor of type AS_CHARACTER
                xRectProps.setPropertyValue(
                        "AnchorType", TextContentAnchorType.AT_PARAGRAPH);
                xEllipseProps.setPropertyValue(
                        "AnchorType", TextContentAnchorType.AT_PARAGRAPH);

                // Instead of adding the shapes to the draw page
                // we insert them as text contents
                // This way is easier to control the page layout of the text document
                XTextContent xRectTextContent = (XTextContent) UnoRuntime.queryInterface(
                        XTextContent.class, xRect);
                XTextContent xEllipseTextContent = (XTextContent) UnoRuntime.queryInterface(
                        XTextContent.class, xEllipse);

                m_xDocText.insertTextContent(m_xDocCursor, xRectTextContent, false);
                m_xDocText.insertTextContent(m_xDocCursor, xEllipseTextContent, false);
            } else {
                m_xDrawPage.add(xRect);
                m_xDrawPage.add(xEllipse);
            }
            //================================================================
            /*
             *                         GROUPING
             *
            * The example below creates a group using the 
            * com.sun.star.drawing.XShapeGrouper interface. 
            * For this purpose, the shapes that are to be grouped 
            * have to be added to a com.sun.star.drawing.ShapeCollection
            * that is created by the XMultiComponentFactory of the *global* 
            * service manager, NOT the document's XMultiServiceFactory.
            * It is a container of shapes that is accessed using the interface
            * com.sun.star.drawing.XShapes.
            */
            XShapes xShapesCollection = (XShapes) UnoRuntime.queryInterface(
                    XShapes.class,
                    m_xContext.getServiceManager().createInstanceWithContext(
                        "com.sun.star.drawing.ShapeCollection", m_xContext));
            xShapesCollection.add(xRect);
            xShapesCollection.add(xEllipse);

            XShapeGrouper xShapeGrouper = (XShapeGrouper) UnoRuntime.queryInterface(
                    XShapeGrouper.class, m_xDrawPage);

            XShapeGroup xShapeGroup = xShapeGrouper.group(xShapesCollection);

            if (m_aType == DocType.WRITER) {
                // Handle some text document layout
                XPropertySet xShapeGroupProps = (XPropertySet) UnoRuntime.queryInterface(
                        XPropertySet.class, xShapeGroup);
                xShapeGroupProps.setPropertyValue("Surround", WrapTextMode.NONE);
                xShapeGroupProps.setPropertyValue("BottomMargin", new Integer(500));
                xShapeGroupProps.setPropertyValue("TopMargin", new Integer(500));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    protected void runGroupShapeDemo() {
        try {

            // Create a RectangleShape using the document factory
            XShape xEllipse = createAndPosShape(m_xDocFactory, "Ellipse",
                    new Size(6000, 6000), new Point(2000, 2000));

            // Create an EllipseShape using the document factory
            XShape xRect = createAndPosShape(m_xDocFactory, "Rectangle",
                    new Size(7000, 4000), new Point(8100, 3500));

            //================================================================
            
            /**
             * 
             *                         GROUPING
             *
             * It is also possible to create GroupShapes directly
             * WITHOUT using the XDrawPage' XShapeGrouper interface.
             * The following code demonstrates the creation of a
             * com.sun.star.drawing.GroupShape that takes up other shapes.
             */
            // create a group shape first
            XShape xGroupShape = createAndPosShape(m_xDocFactory, "Group",
                    new Size(0, 0), new Point(8000, 3000));

            XPropertySet xGroupShapeProps = null;
            if (m_aType == DocType.WRITER) {
                // Handle some text document layout
                xGroupShapeProps = (XPropertySet) UnoRuntime.queryInterface(
                        XPropertySet.class, xGroupShape);
                xGroupShapeProps.setPropertyValue(
                        "AnchorType", TextContentAnchorType.AT_PARAGRAPH);

                XTextContent xGroupTextContent = (XTextContent) UnoRuntime.queryInterface(
                        XTextContent.class, xGroupShape);
                m_xDocText.insertTextContent(m_xDocCursor, xGroupTextContent, false);
            } else {
                m_xDrawPage.add(xGroupShape);
            }

            // query for the XShapes interface, which will take our new shapes
            XShapes xShapesGroup = (XShapes)UnoRuntime.queryInterface(
                    XShapes.class, xGroupShape);

            // new shapes can be inserted into the group shape collection DIRECTLY
            // NO need to add them first to the draw page
            xShapesGroup.add( xRect );
            xShapesGroup.add( xEllipse );

            if (m_aType == DocType.WRITER) {
                // Handle some text document layout
                xGroupShapeProps.setPropertyValue("Surround", WrapTextMode.NONE);
                xGroupShapeProps.setPropertyValue("BottomMargin", new Integer(500));
                xGroupShapeProps.setPropertyValue("TopMargin", new Integer(500));
            }

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

    private XShape createAndPosShape(XMultiServiceFactory xDocFactory,
            String sShape, Size aSize, Point aPos){
        XShape xShape = null;
        try {
            xShape = (XShape) UnoRuntime.queryInterface(
                    XShape.class, xDocFactory.createInstance(
                    "com.sun.star.drawing." + sShape + "Shape"));
            xShape.setSize(aSize);
            xShape.setPosition(aPos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return xShape;
        }
    }

    private XComponent createNewDoc(XComponentContext xContext, String docType) {
        XComponent xComponent = null;
        try {
            String loadUrl = "private:factory/s" + docType;
            XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(
                    XComponentLoader.class,
                    xContext.getServiceManager().createInstanceWithContext(
                    "com.sun.star.frame.Desktop", xContext));
            xComponent = xComponentLoader.loadComponentFromURL(
                    loadUrl, "_blank",FrameSearchFlag.ALL, new PropertyValue[0]);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return xComponent;
        }
    }
    
    /**
     * @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.");
            }

            ShapeGroupDemo writerDemo = new ShapeGroupDemo(xContext, DocType.WRITER);
            writerDemo.runShapeCollectionDemo();
            ShapeGroupDemo drawDemo = new ShapeGroupDemo(xContext, DocType.DRAW);
            drawDemo.runShapeCollectionDemo();


            writerDemo = new ShapeGroupDemo(xContext, DocType.WRITER);
            writerDemo.runGroupShapeDemo();
            drawDemo = new ShapeGroupDemo(xContext, DocType.DRAW);
            drawDemo.runGroupShapeDemo();

        }
        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