Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / Drawing / DrawingDemo.java
blobbb013010ce68d06fa38c7a1c4315a715a4551e82
1 /*************************************************************************
3 * $RCSfile: DrawingDemo.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:22:05 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 // __________ Imports __________
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.lang.XComponent;
46 import com.sun.star.awt.Point;
47 import com.sun.star.awt.Size;
49 import com.sun.star.beans.PropertyValue;
50 import com.sun.star.beans.XPropertySet;
52 import com.sun.star.container.XNamed;
54 import com.sun.star.drawing.PolygonFlags;
55 import com.sun.star.drawing.PolyPolygonBezierCoords;
56 import com.sun.star.drawing.XShape;
57 import com.sun.star.drawing.XShapes;
58 import com.sun.star.drawing.XShapeGrouper;
59 import com.sun.star.drawing.XDrawPage;
61 import java.util.Random;
64 // __________ Implementation __________
66 /** drawing demo
67 @author Sven Jacobi
70 // This drawing demo will create/load a document, and show how to
71 // handle pages and shapes using the Office API,
73 // Calling this demo two parameter can be used. The first parameter
74 // describes if a document is to create or load: "draw" creates a
75 // draw document, "impress" creates an impress document, any other
76 // parameter is interpreted as URL and loads the corresponding
77 // document. ( example for a URL is: "file:///c:/test.odp" )
78 // The second parameter is the connection that is to use. If no parameter
79 // is given a standard impress document is created by using following
80 // connection: "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager";
82 public class DrawingDemo
84 public static void main( String args[] )
86 XComponent xDrawDoc = null;
87 try
89 // get the remote office context of a running office (a new office
90 // instance is started if necessary)
91 com.sun.star.uno.XComponentContext xOfficeContext = Helper.connect();
93 String sURL;
94 if ( args.length == 0 )
95 sURL = "impress";
96 else
97 sURL = args[ 0 ];
99 if ( sURL.equals( "draw" ) )
100 sURL = "private:factory/sdraw";
101 else if ( sURL.equals( "impress" ) )
102 sURL = "private:factory/simpress";
104 // suppress Presentation Autopilot when opening the document
105 // properties are the same as described for
106 // com.sun.star.document.MediaDescriptor
107 PropertyValue[] pPropValues = new PropertyValue[ 1 ];
108 pPropValues[ 0 ] = new PropertyValue();
109 pPropValues[ 0 ].Name = "Silent";
110 pPropValues[ 0 ].Value = new Boolean( true );
112 xDrawDoc = Helper.createDocument( xOfficeContext,
113 sURL, "_blank", 0, pPropValues );
115 catch( Exception ex )
117 System.out.println( ex );
118 System.exit( 0 );
122 Demo_PageCreation( xDrawDoc, 10 );
123 Demo_PageNaming( xDrawDoc, "this page is called: LastPage" );
124 Demo_ShapeCreation( xDrawDoc );
125 Demo_PolyPolygonBezier( xDrawDoc );
126 Demo_Group1( xDrawDoc );
127 Demo_Group2( xDrawDoc );
128 System.exit( 0 );
131 // This method appends draw pages to the document, so that a
132 // minimum of n draw pages will be available.
133 // For each second draw page also a new master page is created.
134 public static void Demo_PageCreation( XComponent xDrawDoc, int n )
138 // If the document has less than n draw pages, append them,
139 // a minimum of n draw pages will be available
140 int i, nDrawPages;
141 for ( nDrawPages = PageHelper.getDrawPageCount( xDrawDoc );
142 nDrawPages < n; nDrawPages++ )
143 PageHelper.insertNewDrawPageByIndex( xDrawDoc, nDrawPages );
144 // Create a master page for each second drawpage
145 int nMasterPages;
146 for ( nMasterPages = PageHelper.getMasterPageCount( xDrawDoc );
147 nMasterPages < ( ( nDrawPages + 1 ) / 2 ); nMasterPages++ )
148 PageHelper.insertNewMasterPageByIndex( xDrawDoc, nMasterPages );
150 // Now connect master page 1 to draw page 1 and 2,
151 // master page 2 to draw page 3 and 4 and so on.
152 for ( i = 0; i < nDrawPages; i++ )
154 XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, i );
155 XDrawPage xMasterPage = PageHelper.getMasterPageByIndex(
156 xDrawDoc, i / 2 );
157 PageHelper.setMasterPage( xDrawPage, xMasterPage );
160 catch( Exception ex )
162 System.out.println("Demo_PageCreation: I have a page creation problem");
166 // this method shows how to name a page, this is exemplary
167 // be done for the last draw page
168 public static void Demo_PageNaming(
169 XComponent xDrawDoc, String sLastPageName )
173 XDrawPage xLastPage = PageHelper.getDrawPageByIndex( xDrawDoc,
174 PageHelper.getDrawPageCount( xDrawDoc ) - 1 );
176 // each drawpage is supporting an XNamed interface
177 XNamed xNamed = (XNamed)UnoRuntime.queryInterface(
178 XNamed.class, xLastPage );
180 // beware, the page must have an unique name
181 xNamed.setName( sLastPageName );
183 catch( Exception ex )
185 System.out.println( "Demo_PageNaming: can't set page name" );
189 // This method will add one rectangle shape into the lower left quarter of
190 // every page that is available,
191 public static void Demo_ShapeCreation( XComponent xDrawDoc )
195 boolean bIsImpressDocument = PageHelper.isImpressDocument( xDrawDoc );
197 int nDrawingPages = PageHelper.getDrawPageCount( xDrawDoc );
198 int nMasterPages = PageHelper.getMasterPageCount( xDrawDoc );
199 int nGlobalPageCount = nDrawingPages + nMasterPages;
201 if ( bIsImpressDocument )
203 // in impress each draw page also has a notes page
204 nGlobalPageCount += nDrawingPages;
205 // for each drawing master is also a notes master available
206 nGlobalPageCount += nMasterPages;
207 // one handout is existing
208 nGlobalPageCount += 1;
211 // create and fill a container with all draw pages
212 XDrawPage[] pPages = new XDrawPage[ nGlobalPageCount ];
213 int i, nCurrentPageIndex = 0;
215 // insert handout page
216 if ( bIsImpressDocument )
217 pPages[ nCurrentPageIndex++ ] = PageHelper.getHandoutMasterPage(
218 xDrawDoc );
220 // inserting all master pages
221 for( i = 0; i < nMasterPages; i++ )
223 XDrawPage xMasterPage = PageHelper.getMasterPageByIndex(
224 xDrawDoc, i );
225 pPages[ nCurrentPageIndex++ ] = xMasterPage;
227 // if the document is an impress, get the corresponding notes
228 // master page
229 if ( bIsImpressDocument )
230 pPages[ nCurrentPageIndex++ ] = PageHelper.getNotesPage(
231 xMasterPage );
233 for ( i = 0; i < nDrawingPages; i++ )
235 XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, i );
236 pPages[ nCurrentPageIndex++ ] = xDrawPage;
238 // if the document is an impress, get the corresponding notes page
239 if ( bIsImpressDocument )
240 pPages[ nCurrentPageIndex++ ] = PageHelper.getNotesPage(
241 xDrawPage );
244 // Now a complete list of pages is available in pPages.
245 // The following code will insert a rectangle into each page.
246 for ( i = 0; i < nGlobalPageCount; i++ )
248 Size aPageSize = PageHelper.getPageSize( pPages[ i ] );
249 int nHalfWidth = aPageSize.Width / 2;
250 int nHalfHeight = aPageSize.Height / 2;
252 Random aRndGen = new Random();
253 int nRndObjWidth = aRndGen.nextInt( nHalfWidth );
254 int nRndObjHeight = aRndGen.nextInt( nHalfHeight );
256 int nRndObjPosX = aRndGen.nextInt( nHalfWidth - nRndObjWidth );
257 int nRndObjPosY = aRndGen.nextInt( nHalfHeight - nRndObjHeight )
258 + nHalfHeight;
260 XShapes xShapes = (XShapes)
261 UnoRuntime.queryInterface( XShapes.class, pPages[ i ] );
262 ShapeHelper.createAndInsertShape( xDrawDoc, xShapes,
263 new Point( nRndObjPosX, nRndObjPosY ),
264 new Size( nRndObjWidth, nRndObjHeight ),
265 "com.sun.star.drawing.RectangleShape" );
268 catch( Exception ex )
270 System.out.println( "Demo_ShapeCreation:" + ex );
274 // This method will show how to create a PolyPolygonBezier that lies is in the
275 // topleft quarter of the page and positioned at the back
276 public static void Demo_PolyPolygonBezier( XComponent xDrawDoc )
280 XShape xPolyPolygonBezier = ShapeHelper.createShape( xDrawDoc,
281 new Point( 0, 0 ),
282 new Size( 0, 0 ),
283 "com.sun.star.drawing.ClosedBezierShape" );
285 // the fact that the shape must have been added to the page before
286 // it is possible to apply changes to the PropertySet, it is a good
287 // proceeding to add the shape as soon as possible
288 XDrawPage xDrawPage;
289 // if possible insert our new shape in the master page
290 if ( PageHelper.isImpressDocument( xDrawDoc ) )
291 xDrawPage = PageHelper.getMasterPageByIndex( xDrawDoc, 0 );
292 else
293 xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, 0 );
294 XShapes xShapes = (XShapes)
295 UnoRuntime.queryInterface( XShapes.class, xDrawPage );
296 xShapes.add( xPolyPolygonBezier );
298 XPropertySet xShapeProperties = (XPropertySet)
299 UnoRuntime.queryInterface( XPropertySet.class, xPolyPolygonBezier );
301 // get pagesize
302 XPropertySet xPageProperties = (XPropertySet)
303 UnoRuntime.queryInterface( XPropertySet.class, xDrawPage );
304 int nPageWidth = ((Integer)xPageProperties.getPropertyValue( "Width" )).intValue() / 2;
305 int nPageHeight = ((Integer)xPageProperties.getPropertyValue( "Height" )).intValue() / 2;
307 PolyPolygonBezierCoords aCoords = new PolyPolygonBezierCoords();
308 // allocating the outer sequence
309 int nPolygonCount = 50;
310 aCoords.Coordinates = new Point[ nPolygonCount ][ ];
311 aCoords.Flags = new PolygonFlags[ nPolygonCount ][ ];
312 int i, n, nY;
313 // fill the inner point sequence now
314 for ( nY = 0, i = 0; i < nPolygonCount; i++, nY += nPageHeight / nPolygonCount )
316 // create a polygon using two normal and two control points
317 // allocating the inner sequence
318 int nPointCount = 8;
319 Point[] pPolyPoints = new Point[ nPointCount ];
320 PolygonFlags[] pPolyFlags = new PolygonFlags[ nPointCount ];
322 for ( n = 0; n < nPointCount; n++ )
323 pPolyPoints[ n ] = new Point();
325 pPolyPoints[ 0 ].X = 0;
326 pPolyPoints[ 0 ].Y = nY;
327 pPolyFlags[ 0 ] = PolygonFlags.NORMAL;
328 pPolyPoints[ 1 ].X = nPageWidth / 2;
329 pPolyPoints[ 1 ].Y = nPageHeight;
330 pPolyFlags[ 1 ] = PolygonFlags.CONTROL;
331 pPolyPoints[ 2 ].X = nPageWidth / 2;;
332 pPolyPoints[ 2 ].Y = nPageHeight;
333 pPolyFlags[ 2 ] = PolygonFlags.CONTROL;
334 pPolyPoints[ 3 ].X = nPageWidth;
335 pPolyPoints[ 3 ].Y = nY;
336 pPolyFlags[ 3 ] = PolygonFlags.NORMAL;
338 pPolyPoints[ 4 ].X = nPageWidth - 1000;
339 pPolyPoints[ 4 ].Y = nY;
340 pPolyFlags[ 4 ] = PolygonFlags.NORMAL;
341 pPolyPoints[ 5 ].X = nPageWidth / 2;
342 pPolyPoints[ 5 ].Y = nPageHeight / 2;
343 pPolyFlags[ 5 ] = PolygonFlags.CONTROL;
344 pPolyPoints[ 6 ].X = nPageWidth / 2;;
345 pPolyPoints[ 6 ].Y = nPageHeight / 2;
346 pPolyFlags[ 6 ] = PolygonFlags.CONTROL;
347 pPolyPoints[ 7 ].X = 1000;
348 pPolyPoints[ 7 ].Y = nY;
349 pPolyFlags[ 7 ] = PolygonFlags.NORMAL;
351 aCoords.Coordinates[ i ]= pPolyPoints;
352 aCoords.Flags[ i ] = pPolyFlags;
354 xShapeProperties.setPropertyValue( "PolyPolygonBezier", aCoords );
356 // move the shape to the back by changing the ZOrder
357 xShapeProperties.setPropertyValue( "ZOrder", new Integer( 1 ) );
359 catch ( Exception ex )
361 System.out.println( "Demo_PolyPolygonBezier:" + ex );
365 // This method will create a group containing two ellipses
366 // the shapes will be added into the top right corner of the first
367 // draw page
368 public static void Demo_Group1( XComponent xDrawDoc )
372 XShape xGroup = ShapeHelper.createShape( xDrawDoc,
373 new Point( 0, 0 ),
374 new Size( 0, 0 ),
375 "com.sun.star.drawing.GroupShape" );
377 // before it is possible to insert shapes,
378 // the group must have been added to the page
379 XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, 0 );
380 XShapes xShapes = (XShapes)
381 UnoRuntime.queryInterface( XShapes.class, xDrawPage );
382 xShapes.add( xGroup );
384 XShapes xShapesGroup = (XShapes)
385 UnoRuntime.queryInterface( XShapes.class, xGroup );
387 Size aPageSize = PageHelper.getPageSize( xDrawPage );
389 int nWidth = 4000;
390 int nHeight = 2000;
391 int nPosX = ( aPageSize.Width * 3 ) / 4 - nWidth / 2;
392 int nPosY1 = 2000;
393 int nPosY2 = aPageSize.Height / 2 - ( nPosY1 + nHeight );
394 XShape xRect1 = ShapeHelper.createShape( xDrawDoc,
395 new Point( nPosX, nPosY1 ),
396 new Size( nWidth, nHeight ),
397 "com.sun.star.drawing.EllipseShape" );
398 XShape xRect2 = ShapeHelper.createShape( xDrawDoc,
399 new Point( nPosX, nPosY2 ),
400 new Size( nWidth, nHeight ),
401 "com.sun.star.drawing.EllipseShape" );
403 xShapesGroup.add( xRect1 );
404 xShapesGroup.add( xRect2 );
406 catch ( Exception ex )
408 System.out.println( "Demo_Group1:" + ex );
412 // This method will group all available objects on the
413 // first page.
414 public static void Demo_Group2( XComponent xDrawDoc )
418 XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, 0 );
419 XShapeGrouper xShapeGrouper = (XShapeGrouper)
420 UnoRuntime.queryInterface( XShapeGrouper.class, xDrawPage );
422 XShapes xShapesPage = (XShapes)
423 UnoRuntime.queryInterface( XShapes.class, xDrawPage );
425 xShapeGrouper.group( xShapesPage );
427 catch ( Exception ex )
429 System.out.println( "Demo_Group2:" + ex );