Hello Sebastian,

On Friday 27 March 2009, 09:31, Sebastian Patschorke wrote:
> Hello Ariel,
>
> with the attached source the grouping now works. Thanks.
> But there is still a problem with the position of the ellipse and the
> rectangle. They always have the position (0,0). What is the reason for
> this and how can I fix this?

I'm not sure what you're talking about, but I guess it's about the use of 
css.drawing.GroupShape in the Writer demo.

May I wasn't clear, and demo was to naive; I attach a more complete/complex 
one.

This demo shows that grouping using a css.drawing.GroupShape odes not work on 
Writer (at least not as it does in Draw; compare the resulting document in 
writerDemo.runGroupShapeDemo() with the one from drawDemo.runGroupShapeDemo() 
). So please use for grouping the method with the css.drawing.ShapeCollection, 
this one does work :-)


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.container.XNameAccess;
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.style.XStyleFamiliesSupplier;
import com.sun.star.text.ControlCharacter;
import com.sun.star.text.TextContentAnchorType;
import com.sun.star.text.WrapTextMode;
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 com.sun.star.uno.XInterface;
import java.awt.Color;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @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;
    private int PAGE_WIDHT = 0, PAGE_HEIGHT = 0,
            BORDER_LEFT = 0, BORDER_RIGHT = 0,
            BORDER_TOP = 0, BORDER_BOTTOM = 0;
    
    /** 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);

                    XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
                            XPropertySet.class, m_xDocCursor);
                    String sPageStyleName = AnyConverter.toString(
                            xCursorProps.getPropertyValue("PageStyleName"));

                    if (sPageStyleName != null && sPageStyleName.length() > 0){

                        XStyleFamiliesSupplier xStyleFamiliesSupplier =
                                (XStyleFamiliesSupplier) UnoRuntime.queryInterface(
                                XStyleFamiliesSupplier.class, xTextDoc);

                        XNameAccess xPageStyles =
                                (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,
                                xStyleFamiliesSupplier.getStyleFamilies().getByName("PageStyles"));

                        if (xPageStyles.hasByName(sPageStyleName)) {
                            XPropertySet xPageStyleProps = (XPropertySet) UnoRuntime.queryInterface(
                                    XPropertySet.class, xPageStyles.getByName(sPageStyleName));
                            PAGE_WIDHT = AnyConverter.toInt(
                                    xPageStyleProps.getPropertyValue("Width"));
                            PAGE_HEIGHT = AnyConverter.toInt(
                                    xPageStyleProps.getPropertyValue("Height"));
                            BORDER_LEFT = AnyConverter.toInt(
                                    xPageStyleProps.getPropertyValue("LeftMargin"));
                            BORDER_RIGHT = AnyConverter.toInt(
                                    xPageStyleProps.getPropertyValue("RightMargin"));
                            BORDER_TOP = AnyConverter.toInt(
                                    xPageStyleProps.getPropertyValue("TopMargin"));
                            BORDER_BOTTOM = AnyConverter.toInt(
                                    xPageStyleProps.getPropertyValue("BottomMargin"));
                        }
                    }
                } 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);

                    XPropertySet xDrawPageProps = (XPropertySet) UnoRuntime.queryInterface(
                            XPropertySet.class, m_xDrawPage);

                    PAGE_WIDHT = AnyConverter.toInt(
                            xDrawPageProps.getPropertyValue("Width"));
                    PAGE_HEIGHT = AnyConverter.toInt(
                            xDrawPageProps.getPropertyValue("Height"));
                    BORDER_LEFT = AnyConverter.toInt(
                            xDrawPageProps.getPropertyValue("BorderLeft"));
                    BORDER_RIGHT = AnyConverter.toInt(
                            xDrawPageProps.getPropertyValue("BorderRight"));
                    BORDER_TOP = AnyConverter.toInt(
                            xDrawPageProps.getPropertyValue("BorderTop"));
                    BORDER_BOTTOM = AnyConverter.toInt(
                            xDrawPageProps.getPropertyValue("BorderBottom"));

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

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

    
    protected void runShapeCollectionDemo() {
        try {
            Vector<XShape> aShapes = initShapes();

            if (m_aType == DocType.WRITER) {
                for (Iterator<XShape> it = aShapes.iterator(); it.hasNext();) {
                    XShape xShape = it.next();

                    // 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
                    setPropertyValue(xShape, "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
                    insertTextContent(m_xDocCursor, xShape, false);
                }
            } else {
                for (Iterator<XShape> it = aShapes.iterator(); it.hasNext();) {
                    m_xDrawPage.add(it.next());
                }
            }
            //================================================================
            /*
             *                         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));
            for (Iterator<XShape> it = aShapes.iterator(); it.hasNext();) {
                xShapesCollection.add(it.next());
            }

            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 {
            Vector<XShape> aShapes = initShapes();
            
            //================================================================
            
            /**
             * 
             *                         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(0, 0));

            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);

                insertTextContent(m_xDocCursor, xGroupShape, 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
            for (Iterator<XShape> it = aShapes.iterator(); it.hasNext();) {
                xShapesGroup.add( it.next() );
            }

            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 Vector<XShape> initShapes(){
        Vector<XShape> aRet = new Vector<XShape>();
        try {
            int nRect_Side = 8000, nRect_X = 0, nRect_Y = 0;

            if (m_aType == DocType.WRITER) {
                nRect_X = (PAGE_WIDHT - BORDER_LEFT - BORDER_RIGHT)/2 - nRect_Side/2;
                nRect_Y = (PAGE_HEIGHT - BORDER_TOP - BORDER_BOTTOM)/2 - nRect_Side/2;
            } else {
                nRect_X = PAGE_WIDHT/2 - nRect_Side/2;
                nRect_Y = PAGE_HEIGHT/2 - nRect_Side/2;
            }

            XShape xRect = createAndPosShape(m_xDocFactory, "Rectangle",
                    new Size(nRect_Side, nRect_Side),
                    new Point(nRect_X, nRect_Y));

            int nEll_Diam = 5000;

            XShape xEllipseRed = createAndPosShape(m_xDocFactory, "Ellipse",
                    new Size(nEll_Diam, nEll_Diam),
                    new Point(nRect_X + (nRect_Side/2 - nEll_Diam/2), nRect_Y));

            XShape xEllipseGreen = createAndPosShape(m_xDocFactory, "Ellipse",
                    new Size(nEll_Diam, nEll_Diam),
                    new Point(nRect_X, nRect_Y + nRect_Side - nEll_Diam));

            XShape xEllipseBlue = createAndPosShape(m_xDocFactory, "Ellipse",
                    new Size(nEll_Diam, nEll_Diam),
                    new Point(nRect_X + nRect_Side - nEll_Diam, nRect_Y + nRect_Side - nEll_Diam));


            // Get the XPropertySet interfaces of all shapes
            XPropertySet xRectProps = (XPropertySet) UnoRuntime.queryInterface(
                    XPropertySet.class, xRect);
            XPropertySet xEllipseRedProps = (XPropertySet) UnoRuntime.queryInterface(
                    XPropertySet.class, xEllipseRed);
            XPropertySet xEllipseGreenProps = (XPropertySet) UnoRuntime.queryInterface(
                    XPropertySet.class, xEllipseGreen);
            XPropertySet xEllipseBlueProps = (XPropertySet) UnoRuntime.queryInterface(
                    XPropertySet.class, xEllipseBlue);

            xRectProps.setPropertyValue("FillColor", new Integer(Color.WHITE.getRGB()));
            xEllipseRedProps.setPropertyValue("FillColor", new Integer(Color.RED.getRGB()));
            xEllipseGreenProps.setPropertyValue("FillColor", new Integer(Color.GREEN.getRGB()));
            xEllipseBlueProps.setPropertyValue("FillColor", new Integer(Color.BLUE.getRGB()));

            Short nTransparence = new Short((short)60);
            xEllipseRedProps.setPropertyValue("FillTransparence", nTransparence);
            xEllipseGreenProps.setPropertyValue("FillTransparence", nTransparence);
            xEllipseBlueProps.setPropertyValue("FillTransparence", nTransparence);

            aRet.add(xRect);
            aRet.add(xEllipseRed);
            aRet.add(xEllipseGreen);
            aRet.add(xEllipseBlue);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return aRet;
        }
    }

    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 XTextContent insertTextContent(
            XTextRange xTextRange, XInterface xTextContent, boolean bAbsorb){
        XTextContent _xTextContent = null;
        try {
            _xTextContent = (XTextContent) UnoRuntime.queryInterface(
                        XTextContent.class, xTextContent);
            xTextRange.getText().insertTextContent(
                    xTextRange, _xTextContent, bAbsorb);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return _xTextContent;
        }
    }

    private XPropertySet setPropertyValue(XInterface ifce, String sPropName, Object aPropValue){
        XPropertySet xPropertySet = null;
        try {
            xPropertySet = (XPropertySet) UnoRuntime.queryInterface(
                    XPropertySet.class, ifce);
            xPropertySet.setPropertyValue(sPropName, aPropValue);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return xPropertySet;
        }
    }

    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