xref: /trunk/main/odk/examples/DevelopersGuide/Drawing/DrawingDemo.java (revision d05a678705c51acd00d01d5f3bcd5d62d773da66)
134dd1e25SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
334dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
434dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
534dd1e25SAndrew Rist  * distributed with this work for additional information
634dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
734dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
834dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
934dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1134dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1334dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1434dd1e25SAndrew Rist  * software distributed under the License is distributed on an
1534dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1634dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
1734dd1e25SAndrew Rist  * specific language governing permissions and limitations
1834dd1e25SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2034dd1e25SAndrew Rist  *************************************************************/
2134dd1e25SAndrew Rist 
2234dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // __________ Imports __________
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
27cdf0e10cSrcweir import com.sun.star.lang.XComponent;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.awt.Point;
30cdf0e10cSrcweir import com.sun.star.awt.Size;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
33cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import com.sun.star.container.XNamed;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import com.sun.star.drawing.PolygonFlags;
38cdf0e10cSrcweir import com.sun.star.drawing.PolyPolygonBezierCoords;
39cdf0e10cSrcweir import com.sun.star.drawing.XShape;
40cdf0e10cSrcweir import com.sun.star.drawing.XShapes;
41cdf0e10cSrcweir import com.sun.star.drawing.XShapeGrouper;
42cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir import java.util.Random;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir // __________ Implementation __________
48cdf0e10cSrcweir 
49cdf0e10cSrcweir /** drawing demo
50cdf0e10cSrcweir     @author Sven Jacobi
51cdf0e10cSrcweir  */
52cdf0e10cSrcweir 
53cdf0e10cSrcweir // This drawing demo will create/load a document, and show how to
54cdf0e10cSrcweir // handle pages and shapes using the Office API,
55cdf0e10cSrcweir 
56cdf0e10cSrcweir // Calling this demo two parameter can be used. The first parameter
57cdf0e10cSrcweir // describes if a document is to create or load: "draw" creates a
58cdf0e10cSrcweir // draw document, "impress" creates an impress document, any other
59cdf0e10cSrcweir // parameter is interpreted as URL and loads the corresponding
60cdf0e10cSrcweir // document. ( example for a URL is: "file:///c:/test.odp" )
61cdf0e10cSrcweir // The second parameter is the connection that is to use. If no parameter
62cdf0e10cSrcweir // is given a standard impress document is created by using following
63cdf0e10cSrcweir // connection: "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager";
64cdf0e10cSrcweir 
65cdf0e10cSrcweir public class DrawingDemo
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     public static void main( String args[] )
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         XComponent xDrawDoc = null;
70cdf0e10cSrcweir         try
71cdf0e10cSrcweir         {
72cdf0e10cSrcweir             // get the remote office context of a running office (a new office
73cdf0e10cSrcweir             // instance is started if necessary)
74cdf0e10cSrcweir             com.sun.star.uno.XComponentContext xOfficeContext = Helper.connect();
75cdf0e10cSrcweir 
76cdf0e10cSrcweir             String sURL;
77cdf0e10cSrcweir             if ( args.length == 0 )
78cdf0e10cSrcweir                 sURL = "impress";
79cdf0e10cSrcweir             else
80cdf0e10cSrcweir                 sURL = args[ 0 ];
81cdf0e10cSrcweir 
82cdf0e10cSrcweir             if ( sURL.equals( "draw" ) )
83cdf0e10cSrcweir                 sURL = "private:factory/sdraw";
84cdf0e10cSrcweir             else if ( sURL.equals( "impress" ) )
85cdf0e10cSrcweir                 sURL = "private:factory/simpress";
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             // suppress Presentation Autopilot when opening the document
88cdf0e10cSrcweir             // properties are the same as described for
89cdf0e10cSrcweir             // com.sun.star.document.MediaDescriptor
90cdf0e10cSrcweir             PropertyValue[] pPropValues = new PropertyValue[ 1 ];
91cdf0e10cSrcweir             pPropValues[ 0 ] = new PropertyValue();
92cdf0e10cSrcweir             pPropValues[ 0 ].Name = "Silent";
93cdf0e10cSrcweir             pPropValues[ 0 ].Value = new Boolean( true );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir             xDrawDoc = Helper.createDocument( xOfficeContext,
96cdf0e10cSrcweir                 sURL, "_blank", 0, pPropValues );
97cdf0e10cSrcweir         }
98cdf0e10cSrcweir         catch( Exception ex )
99cdf0e10cSrcweir         {
100cdf0e10cSrcweir             System.out.println( ex );
101cdf0e10cSrcweir             System.exit( 0 );
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         Demo_PageCreation( xDrawDoc, 10 );
106cdf0e10cSrcweir         Demo_PageNaming( xDrawDoc, "this page is called: LastPage" );
107cdf0e10cSrcweir         Demo_ShapeCreation( xDrawDoc );
108cdf0e10cSrcweir         Demo_PolyPolygonBezier( xDrawDoc );
109cdf0e10cSrcweir         Demo_Group1( xDrawDoc );
110cdf0e10cSrcweir         Demo_Group2( xDrawDoc );
111cdf0e10cSrcweir         System.exit( 0 );
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     // This method appends draw pages to the document, so that a
115cdf0e10cSrcweir     // minimum of n draw pages will be available.
116cdf0e10cSrcweir     // For each second draw page also a new master page is created.
117cdf0e10cSrcweir     public static void Demo_PageCreation( XComponent xDrawDoc, int n )
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         try
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir             // If the document has less than n draw pages, append them,
122cdf0e10cSrcweir             // a minimum of n draw pages will be available
123cdf0e10cSrcweir             int i, nDrawPages;
124cdf0e10cSrcweir             for ( nDrawPages = PageHelper.getDrawPageCount( xDrawDoc );
125cdf0e10cSrcweir                 nDrawPages < n; nDrawPages++ )
126cdf0e10cSrcweir                 PageHelper.insertNewDrawPageByIndex( xDrawDoc, nDrawPages );
127cdf0e10cSrcweir             // Create a master page for each second drawpage
128cdf0e10cSrcweir             int nMasterPages;
129cdf0e10cSrcweir             for ( nMasterPages = PageHelper.getMasterPageCount( xDrawDoc );
130cdf0e10cSrcweir                 nMasterPages < ( ( nDrawPages + 1 ) / 2 ); nMasterPages++ )
131cdf0e10cSrcweir                 PageHelper.insertNewMasterPageByIndex( xDrawDoc, nMasterPages );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir             // Now connect master page 1 to draw page 1 and 2,
134cdf0e10cSrcweir             // master page 2 to draw page 3 and 4 and so on.
135cdf0e10cSrcweir             for ( i = 0; i < nDrawPages; i++ )
136cdf0e10cSrcweir             {
137cdf0e10cSrcweir                 XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, i );
138cdf0e10cSrcweir                 XDrawPage xMasterPage = PageHelper.getMasterPageByIndex(
139cdf0e10cSrcweir                     xDrawDoc, i / 2 );
140cdf0e10cSrcweir                 PageHelper.setMasterPage( xDrawPage, xMasterPage );
141cdf0e10cSrcweir             }
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir         catch( Exception ex )
144cdf0e10cSrcweir         {
145cdf0e10cSrcweir             System.out.println("Demo_PageCreation: I have a page creation problem");
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     // this method shows how to name a page, this is exemplary
150cdf0e10cSrcweir     // be done for the last draw page
151cdf0e10cSrcweir     public static void Demo_PageNaming(
152cdf0e10cSrcweir         XComponent xDrawDoc, String sLastPageName )
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         try
155cdf0e10cSrcweir         {
156cdf0e10cSrcweir             XDrawPage xLastPage = PageHelper.getDrawPageByIndex( xDrawDoc,
157cdf0e10cSrcweir                 PageHelper.getDrawPageCount( xDrawDoc ) - 1 );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir             // each drawpage is supporting an XNamed interface
160cdf0e10cSrcweir             XNamed xNamed = (XNamed)UnoRuntime.queryInterface(
161cdf0e10cSrcweir                 XNamed.class, xLastPage );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir             // beware, the page must have an unique name
164cdf0e10cSrcweir             xNamed.setName( sLastPageName );
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir         catch( Exception ex )
167cdf0e10cSrcweir         {
168cdf0e10cSrcweir             System.out.println( "Demo_PageNaming: can't set page name" );
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     // This method will add one rectangle shape into the lower left quarter of
173cdf0e10cSrcweir     // every page that is available,
174cdf0e10cSrcweir     public static void Demo_ShapeCreation( XComponent xDrawDoc )
175cdf0e10cSrcweir     {
176cdf0e10cSrcweir         try
177cdf0e10cSrcweir         {
178cdf0e10cSrcweir             boolean bIsImpressDocument = PageHelper.isImpressDocument( xDrawDoc );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir             int nDrawingPages = PageHelper.getDrawPageCount( xDrawDoc );
181cdf0e10cSrcweir             int nMasterPages =  PageHelper.getMasterPageCount( xDrawDoc );
182cdf0e10cSrcweir             int nGlobalPageCount = nDrawingPages + nMasterPages;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir             if ( bIsImpressDocument )
185cdf0e10cSrcweir             {
186cdf0e10cSrcweir                 // in impress each draw page also has a notes page
187cdf0e10cSrcweir                 nGlobalPageCount += nDrawingPages;
188cdf0e10cSrcweir                 // for each drawing master is also a notes master available
189cdf0e10cSrcweir                 nGlobalPageCount += nMasterPages;
190cdf0e10cSrcweir                 // one handout is existing
191cdf0e10cSrcweir                 nGlobalPageCount += 1;
192cdf0e10cSrcweir             }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir             // create and fill a container with all draw pages
195cdf0e10cSrcweir             XDrawPage[] pPages = new XDrawPage[ nGlobalPageCount ];
196cdf0e10cSrcweir             int i, nCurrentPageIndex = 0;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir             // insert handout page
199cdf0e10cSrcweir             if ( bIsImpressDocument )
200cdf0e10cSrcweir                 pPages[ nCurrentPageIndex++ ] = PageHelper.getHandoutMasterPage(
201cdf0e10cSrcweir                     xDrawDoc );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir             // inserting all master pages
204cdf0e10cSrcweir             for( i = 0; i < nMasterPages; i++ )
205cdf0e10cSrcweir             {
206cdf0e10cSrcweir                 XDrawPage xMasterPage = PageHelper.getMasterPageByIndex(
207cdf0e10cSrcweir                     xDrawDoc, i );
208cdf0e10cSrcweir                 pPages[ nCurrentPageIndex++ ] = xMasterPage;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir                 // if the document is an impress, get the corresponding notes
211cdf0e10cSrcweir                 // master page
212cdf0e10cSrcweir                 if ( bIsImpressDocument )
213cdf0e10cSrcweir                     pPages[ nCurrentPageIndex++ ] = PageHelper.getNotesPage(
214cdf0e10cSrcweir                         xMasterPage );
215cdf0e10cSrcweir             }
216cdf0e10cSrcweir             for ( i = 0; i < nDrawingPages; i++ )
217cdf0e10cSrcweir             {
218cdf0e10cSrcweir                 XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, i );
219cdf0e10cSrcweir                 pPages[ nCurrentPageIndex++ ] = xDrawPage;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir                 // if the document is an impress, get the corresponding notes page
222cdf0e10cSrcweir                 if ( bIsImpressDocument )
223cdf0e10cSrcweir                     pPages[ nCurrentPageIndex++ ] = PageHelper.getNotesPage(
224cdf0e10cSrcweir                         xDrawPage );
225cdf0e10cSrcweir             }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir             // Now a complete list of pages is available in pPages.
228cdf0e10cSrcweir             // The following code will insert a rectangle into each page.
229cdf0e10cSrcweir             for ( i = 0; i < nGlobalPageCount; i++ )
230cdf0e10cSrcweir             {
231cdf0e10cSrcweir                 Size aPageSize = PageHelper.getPageSize( pPages[ i ] );
232cdf0e10cSrcweir                 int nHalfWidth  = aPageSize.Width / 2;
233cdf0e10cSrcweir                 int nHalfHeight = aPageSize.Height / 2;
234cdf0e10cSrcweir 
235cdf0e10cSrcweir                 Random aRndGen = new Random();
236cdf0e10cSrcweir                 int nRndObjWidth  = aRndGen.nextInt( nHalfWidth );
237cdf0e10cSrcweir                 int nRndObjHeight = aRndGen.nextInt( nHalfHeight );
238cdf0e10cSrcweir 
239cdf0e10cSrcweir                 int nRndObjPosX = aRndGen.nextInt( nHalfWidth - nRndObjWidth );
240cdf0e10cSrcweir                 int nRndObjPosY = aRndGen.nextInt( nHalfHeight - nRndObjHeight )
241cdf0e10cSrcweir                     + nHalfHeight;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir                 XShapes xShapes = (XShapes)
244cdf0e10cSrcweir                     UnoRuntime.queryInterface( XShapes.class, pPages[ i ] );
245cdf0e10cSrcweir                 ShapeHelper.createAndInsertShape( xDrawDoc, xShapes,
246cdf0e10cSrcweir                     new Point( nRndObjPosX, nRndObjPosY ),
247cdf0e10cSrcweir                         new Size( nRndObjWidth, nRndObjHeight ),
248cdf0e10cSrcweir                             "com.sun.star.drawing.RectangleShape" );
249cdf0e10cSrcweir             }
250cdf0e10cSrcweir         }
251cdf0e10cSrcweir         catch( Exception ex )
252cdf0e10cSrcweir         {
253cdf0e10cSrcweir             System.out.println( "Demo_ShapeCreation:" + ex );
254cdf0e10cSrcweir         }
255cdf0e10cSrcweir     }
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     // This method will show how to create a PolyPolygonBezier that lies is in the
258cdf0e10cSrcweir     // topleft quarter of the page and positioned at the back
259cdf0e10cSrcweir     public static void Demo_PolyPolygonBezier( XComponent xDrawDoc )
260cdf0e10cSrcweir     {
261cdf0e10cSrcweir         try
262cdf0e10cSrcweir         {
263cdf0e10cSrcweir             XShape xPolyPolygonBezier = ShapeHelper.createShape( xDrawDoc,
264cdf0e10cSrcweir                 new Point( 0, 0 ),
265cdf0e10cSrcweir                     new Size( 0, 0 ),
266cdf0e10cSrcweir                         "com.sun.star.drawing.ClosedBezierShape" );
267cdf0e10cSrcweir 
268cdf0e10cSrcweir             // the fact that the shape must have been added to the page before
269cdf0e10cSrcweir             // it is possible to apply changes to the PropertySet, it is a good
270cdf0e10cSrcweir             // proceeding to add the shape as soon as possible
271cdf0e10cSrcweir             XDrawPage xDrawPage;
272cdf0e10cSrcweir             // if possible insert our new shape in the master page
273cdf0e10cSrcweir             if ( PageHelper.isImpressDocument( xDrawDoc ) )
274cdf0e10cSrcweir                 xDrawPage = PageHelper.getMasterPageByIndex( xDrawDoc, 0 );
275cdf0e10cSrcweir             else
276cdf0e10cSrcweir                 xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, 0 );
277cdf0e10cSrcweir             XShapes xShapes = (XShapes)
278cdf0e10cSrcweir                 UnoRuntime.queryInterface( XShapes.class, xDrawPage );
279cdf0e10cSrcweir             xShapes.add( xPolyPolygonBezier );
280cdf0e10cSrcweir 
281cdf0e10cSrcweir             XPropertySet xShapeProperties = (XPropertySet)
282cdf0e10cSrcweir                 UnoRuntime.queryInterface( XPropertySet.class, xPolyPolygonBezier );
283cdf0e10cSrcweir 
284cdf0e10cSrcweir             // get pagesize
285cdf0e10cSrcweir             XPropertySet xPageProperties = (XPropertySet)
286cdf0e10cSrcweir                 UnoRuntime.queryInterface( XPropertySet.class, xDrawPage );
287cdf0e10cSrcweir             int nPageWidth = ((Integer)xPageProperties.getPropertyValue( "Width" )).intValue() / 2;
288cdf0e10cSrcweir             int nPageHeight = ((Integer)xPageProperties.getPropertyValue( "Height" )).intValue() / 2;
289cdf0e10cSrcweir 
290cdf0e10cSrcweir             PolyPolygonBezierCoords aCoords = new PolyPolygonBezierCoords();
291cdf0e10cSrcweir             // allocating the outer sequence
292cdf0e10cSrcweir             int nPolygonCount = 50;
293cdf0e10cSrcweir             aCoords.Coordinates = new Point[ nPolygonCount ][ ];
294cdf0e10cSrcweir             aCoords.Flags = new PolygonFlags[ nPolygonCount ][ ];
295cdf0e10cSrcweir             int i, n, nY;
296cdf0e10cSrcweir             // fill the inner point sequence now
297cdf0e10cSrcweir             for ( nY = 0, i = 0; i < nPolygonCount; i++, nY += nPageHeight / nPolygonCount )
298cdf0e10cSrcweir             {
299cdf0e10cSrcweir                 // create a polygon using two normal and two control points
300cdf0e10cSrcweir                 // allocating the inner sequence
301cdf0e10cSrcweir                 int nPointCount = 8;
302cdf0e10cSrcweir                 Point[]         pPolyPoints = new Point[ nPointCount ];
303cdf0e10cSrcweir                 PolygonFlags[]  pPolyFlags  = new PolygonFlags[ nPointCount ];
304cdf0e10cSrcweir 
305cdf0e10cSrcweir                 for ( n = 0; n < nPointCount; n++ )
306cdf0e10cSrcweir                     pPolyPoints[ n ] = new Point();
307cdf0e10cSrcweir 
308cdf0e10cSrcweir                 pPolyPoints[ 0 ].X = 0;
309cdf0e10cSrcweir                 pPolyPoints[ 0 ].Y = nY;
310cdf0e10cSrcweir                 pPolyFlags[ 0 ] = PolygonFlags.NORMAL;
311cdf0e10cSrcweir                 pPolyPoints[ 1 ].X = nPageWidth / 2;
312cdf0e10cSrcweir                 pPolyPoints[ 1 ].Y = nPageHeight;
313cdf0e10cSrcweir                 pPolyFlags[ 1 ] = PolygonFlags.CONTROL;
314*d05a6787Smseidel                 pPolyPoints[ 2 ].X = nPageWidth / 2;
315cdf0e10cSrcweir                 pPolyPoints[ 2 ].Y = nPageHeight;
316cdf0e10cSrcweir                 pPolyFlags[ 2 ] = PolygonFlags.CONTROL;
317cdf0e10cSrcweir                 pPolyPoints[ 3 ].X = nPageWidth;
318cdf0e10cSrcweir                 pPolyPoints[ 3 ].Y = nY;
319cdf0e10cSrcweir                 pPolyFlags[ 3 ] = PolygonFlags.NORMAL;
320cdf0e10cSrcweir 
321cdf0e10cSrcweir                 pPolyPoints[ 4 ].X = nPageWidth - 1000;
322cdf0e10cSrcweir                 pPolyPoints[ 4 ].Y = nY;
323cdf0e10cSrcweir                 pPolyFlags[ 4 ] = PolygonFlags.NORMAL;
324cdf0e10cSrcweir                 pPolyPoints[ 5 ].X = nPageWidth / 2;
325cdf0e10cSrcweir                 pPolyPoints[ 5 ].Y = nPageHeight / 2;
326cdf0e10cSrcweir                 pPolyFlags[ 5 ] = PolygonFlags.CONTROL;
327*d05a6787Smseidel                 pPolyPoints[ 6 ].X = nPageWidth / 2;
328cdf0e10cSrcweir                 pPolyPoints[ 6 ].Y = nPageHeight / 2;
329cdf0e10cSrcweir                 pPolyFlags[ 6 ] = PolygonFlags.CONTROL;
330cdf0e10cSrcweir                 pPolyPoints[ 7 ].X = 1000;
331cdf0e10cSrcweir                 pPolyPoints[ 7 ].Y = nY;
332cdf0e10cSrcweir                 pPolyFlags[ 7 ] = PolygonFlags.NORMAL;
333cdf0e10cSrcweir 
334cdf0e10cSrcweir                 aCoords.Coordinates[ i ]= pPolyPoints;
335cdf0e10cSrcweir                 aCoords.Flags[ i ]      = pPolyFlags;
336cdf0e10cSrcweir             }
337cdf0e10cSrcweir             xShapeProperties.setPropertyValue( "PolyPolygonBezier", aCoords );
338cdf0e10cSrcweir 
339cdf0e10cSrcweir             // move the shape to the back by changing the ZOrder
340cdf0e10cSrcweir             xShapeProperties.setPropertyValue( "ZOrder", new Integer( 1 ) );
341cdf0e10cSrcweir         }
342cdf0e10cSrcweir         catch ( Exception ex )
343cdf0e10cSrcweir         {
344cdf0e10cSrcweir             System.out.println( "Demo_PolyPolygonBezier:" + ex );
345cdf0e10cSrcweir         }
346cdf0e10cSrcweir     }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir     // This method will create a group containing two ellipses
349cdf0e10cSrcweir     // the shapes will be added into the top right corner of the first
350cdf0e10cSrcweir     // draw page
351cdf0e10cSrcweir     public static void Demo_Group1( XComponent xDrawDoc )
352cdf0e10cSrcweir     {
353cdf0e10cSrcweir         try
354cdf0e10cSrcweir         {
355cdf0e10cSrcweir             XShape xGroup = ShapeHelper.createShape( xDrawDoc,
356cdf0e10cSrcweir                 new Point( 0, 0 ),
357cdf0e10cSrcweir                     new Size( 0, 0 ),
358cdf0e10cSrcweir                         "com.sun.star.drawing.GroupShape" );
359cdf0e10cSrcweir 
360cdf0e10cSrcweir             // before it is possible to insert shapes,
361cdf0e10cSrcweir             // the group must have been added to the page
362cdf0e10cSrcweir             XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, 0 );
363cdf0e10cSrcweir             XShapes xShapes = (XShapes)
364cdf0e10cSrcweir                 UnoRuntime.queryInterface( XShapes.class, xDrawPage );
365cdf0e10cSrcweir             xShapes.add( xGroup );
366cdf0e10cSrcweir 
367cdf0e10cSrcweir             XShapes xShapesGroup = (XShapes)
368cdf0e10cSrcweir                 UnoRuntime.queryInterface( XShapes.class, xGroup );
369cdf0e10cSrcweir 
370cdf0e10cSrcweir             Size aPageSize = PageHelper.getPageSize( xDrawPage );
371cdf0e10cSrcweir 
372cdf0e10cSrcweir             int nWidth  = 4000;
373cdf0e10cSrcweir             int nHeight = 2000;
374cdf0e10cSrcweir             int nPosX = ( aPageSize.Width * 3 ) / 4 - nWidth / 2;
375cdf0e10cSrcweir             int nPosY1 = 2000;
376cdf0e10cSrcweir             int nPosY2 = aPageSize.Height / 2 - ( nPosY1 + nHeight );
377cdf0e10cSrcweir             XShape xRect1 = ShapeHelper.createShape( xDrawDoc,
378cdf0e10cSrcweir                 new Point( nPosX, nPosY1 ),
379cdf0e10cSrcweir                     new Size( nWidth, nHeight ),
380cdf0e10cSrcweir                         "com.sun.star.drawing.EllipseShape" );
381cdf0e10cSrcweir             XShape xRect2 = ShapeHelper.createShape( xDrawDoc,
382cdf0e10cSrcweir                 new Point( nPosX, nPosY2 ),
383cdf0e10cSrcweir                     new Size( nWidth, nHeight ),
384cdf0e10cSrcweir                         "com.sun.star.drawing.EllipseShape" );
385cdf0e10cSrcweir 
386cdf0e10cSrcweir             xShapesGroup.add( xRect1 );
387cdf0e10cSrcweir             xShapesGroup.add( xRect2 );
388cdf0e10cSrcweir         }
389cdf0e10cSrcweir         catch ( Exception ex )
390cdf0e10cSrcweir         {
391cdf0e10cSrcweir             System.out.println( "Demo_Group1:" + ex );
392cdf0e10cSrcweir         }
393cdf0e10cSrcweir     }
394cdf0e10cSrcweir 
395cdf0e10cSrcweir     // This method will group all available objects on the
396cdf0e10cSrcweir     // first page.
397cdf0e10cSrcweir     public static void Demo_Group2( XComponent xDrawDoc )
398cdf0e10cSrcweir     {
399cdf0e10cSrcweir         try
400cdf0e10cSrcweir         {
401cdf0e10cSrcweir             XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, 0 );
402cdf0e10cSrcweir             XShapeGrouper xShapeGrouper = (XShapeGrouper)
403cdf0e10cSrcweir                 UnoRuntime.queryInterface( XShapeGrouper.class, xDrawPage );
404cdf0e10cSrcweir 
405cdf0e10cSrcweir             XShapes xShapesPage = (XShapes)
406cdf0e10cSrcweir                 UnoRuntime.queryInterface( XShapes.class, xDrawPage );
407cdf0e10cSrcweir 
408cdf0e10cSrcweir             xShapeGrouper.group( xShapesPage );
409cdf0e10cSrcweir         }
410cdf0e10cSrcweir         catch ( Exception ex )
411cdf0e10cSrcweir         {
412cdf0e10cSrcweir             System.out.println( "Demo_Group2:" + ex );
413cdf0e10cSrcweir         }
414cdf0e10cSrcweir     }
415cdf0e10cSrcweir }
416