crashtesting: assert on reimport of docx export of ooo102874-2.doc
[LibreOffice.git] / odk / examples / DevelopersGuide / Drawing / DrawingDemo.java
blobf71197cf6d6149bc9e8b2acb56358baa5eee4b0c
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 // __________ Imports __________
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.lang.XComponent;
41 import com.sun.star.awt.Point;
42 import com.sun.star.awt.Size;
44 import com.sun.star.beans.PropertyValue;
45 import com.sun.star.beans.XPropertySet;
47 import com.sun.star.container.XNamed;
49 import com.sun.star.drawing.PolygonFlags;
50 import com.sun.star.drawing.PolyPolygonBezierCoords;
51 import com.sun.star.drawing.XShape;
52 import com.sun.star.drawing.XShapes;
53 import com.sun.star.drawing.XShapeGrouper;
54 import com.sun.star.drawing.XDrawPage;
56 import java.util.Random;
59 // __________ Implementation __________
61 // drawing demo
63 // This drawing demo will create/load a document, and show how to
64 // handle pages and shapes using the Office API,
66 // Calling this demo two parameter can be used. The first parameter
67 // describes if a document is to create or load: "draw" creates a
68 // draw document, "impress" creates an impress document, any other
69 // parameter is interpreted as URL and loads the corresponding
70 // document. ( example for a URL is: "file:///c:/test.odp" )
71 // The second parameter is the connection that is to use. If no parameter
72 // is given a standard impress document is created by using following
73 // connection: "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager";
75 public class DrawingDemo
77 public static void main( String args[] )
79 XComponent xDrawDoc = null;
80 try
82 // get the remote office context of a running office (a new office
83 // instance is started if necessary)
84 com.sun.star.uno.XComponentContext xOfficeContext = Helper.connect();
86 String sURL;
87 if ( args.length == 0 )
88 sURL = "impress";
89 else
90 sURL = args[ 0 ];
92 if ( sURL.equals( "draw" ) )
93 sURL = "private:factory/sdraw";
94 else if ( sURL.equals( "impress" ) )
95 sURL = "private:factory/simpress";
97 // suppress Presentation Autopilot when opening the document
98 // properties are the same as described for
99 // com.sun.star.document.MediaDescriptor
100 PropertyValue[] pPropValues = new PropertyValue[ 1 ];
101 pPropValues[ 0 ] = new PropertyValue();
102 pPropValues[ 0 ].Name = "Silent";
103 pPropValues[ 0 ].Value = Boolean.TRUE;
105 xDrawDoc = Helper.createDocument( xOfficeContext,
106 sURL, "_blank", 0, pPropValues );
108 catch( Exception ex )
110 System.out.println( ex );
111 System.exit( 0 );
115 Demo_PageCreation( xDrawDoc, 10 );
116 Demo_PageNaming( xDrawDoc, "this page is called: LastPage" );
117 Demo_ShapeCreation( xDrawDoc );
118 Demo_PolyPolygonBezier( xDrawDoc );
119 Demo_Group1( xDrawDoc );
120 Demo_Group2( xDrawDoc );
121 System.exit( 0 );
124 // This method appends draw pages to the document, so that a
125 // minimum of n draw pages will be available.
126 // For each second draw page also a new master page is created.
127 public static void Demo_PageCreation( XComponent xDrawDoc, int n )
131 // If the document has less than n draw pages, append them,
132 // a minimum of n draw pages will be available
133 int i, nDrawPages;
134 for ( nDrawPages = PageHelper.getDrawPageCount( xDrawDoc );
135 nDrawPages < n; nDrawPages++ )
136 PageHelper.insertNewDrawPageByIndex( xDrawDoc, nDrawPages );
137 // Create a master page for each second drawpage
138 int nMasterPages;
139 for ( nMasterPages = PageHelper.getMasterPageCount( xDrawDoc );
140 nMasterPages < ( ( nDrawPages + 1 ) / 2 ); nMasterPages++ )
141 PageHelper.insertNewMasterPageByIndex( xDrawDoc, nMasterPages );
143 // Now connect master page 1 to draw page 1 and 2,
144 // master page 2 to draw page 3 and 4 and so on.
145 for ( i = 0; i < nDrawPages; i++ )
147 XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, i );
148 XDrawPage xMasterPage = PageHelper.getMasterPageByIndex(
149 xDrawDoc, i / 2 );
150 PageHelper.setMasterPage( xDrawPage, xMasterPage );
153 catch( Exception ex )
155 System.out.println("Demo_PageCreation: I have a page creation problem");
159 // this method shows how to name a page, this is exemplary
160 // be done for the last draw page
161 public static void Demo_PageNaming(
162 XComponent xDrawDoc, String sLastPageName )
166 XDrawPage xLastPage = PageHelper.getDrawPageByIndex( xDrawDoc,
167 PageHelper.getDrawPageCount( xDrawDoc ) - 1 );
169 // each drawpage is supporting an XNamed interface
170 XNamed xNamed = UnoRuntime.queryInterface(
171 XNamed.class, xLastPage );
173 // beware, the page must have a unique name
174 xNamed.setName( sLastPageName );
176 catch( Exception ex )
178 System.out.println( "Demo_PageNaming: can't set page name" );
182 // This method will add one rectangle shape into the lower left quarter of
183 // every page that is available,
184 public static void Demo_ShapeCreation( XComponent xDrawDoc )
188 boolean bIsImpressDocument = PageHelper.isImpressDocument( xDrawDoc );
190 int nDrawingPages = PageHelper.getDrawPageCount( xDrawDoc );
191 int nMasterPages = PageHelper.getMasterPageCount( xDrawDoc );
192 int nGlobalPageCount = nDrawingPages + nMasterPages;
194 if ( bIsImpressDocument )
196 // in impress each draw page also has a notes page
197 nGlobalPageCount += nDrawingPages;
198 // for each drawing master is also a notes master available
199 nGlobalPageCount += nMasterPages;
200 // one handout is existing
201 nGlobalPageCount += 1;
204 // create and fill a container with all draw pages
205 XDrawPage[] pPages = new XDrawPage[ nGlobalPageCount ];
206 int i, nCurrentPageIndex = 0;
208 // insert handout page
209 if ( bIsImpressDocument )
210 pPages[ nCurrentPageIndex++ ] = PageHelper.getHandoutMasterPage(
211 xDrawDoc );
213 // inserting all master pages
214 for( i = 0; i < nMasterPages; i++ )
216 XDrawPage xMasterPage = PageHelper.getMasterPageByIndex(
217 xDrawDoc, i );
218 pPages[ nCurrentPageIndex++ ] = xMasterPage;
220 // if the document is an impress, get the corresponding notes
221 // master page
222 if ( bIsImpressDocument )
223 pPages[ nCurrentPageIndex++ ] = PageHelper.getNotesPage(
224 xMasterPage );
226 for ( i = 0; i < nDrawingPages; i++ )
228 XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, i );
229 pPages[ nCurrentPageIndex++ ] = xDrawPage;
231 // if the document is an impress, get the corresponding notes page
232 if ( bIsImpressDocument )
233 pPages[ nCurrentPageIndex++ ] = PageHelper.getNotesPage(
234 xDrawPage );
237 // Now a complete list of pages is available in pPages.
238 // The following code will insert a rectangle into each page.
239 for ( i = 0; i < nGlobalPageCount; i++ )
241 Size aPageSize = PageHelper.getPageSize( pPages[ i ] );
242 int nHalfWidth = aPageSize.Width / 2;
243 int nHalfHeight = aPageSize.Height / 2;
245 Random aRndGen = new Random();
246 int nRndObjWidth = aRndGen.nextInt( nHalfWidth );
247 int nRndObjHeight = aRndGen.nextInt( nHalfHeight );
249 int nRndObjPosX = aRndGen.nextInt( nHalfWidth - nRndObjWidth );
250 int nRndObjPosY = aRndGen.nextInt( nHalfHeight - nRndObjHeight )
251 + nHalfHeight;
253 XShapes xShapes = UnoRuntime.queryInterface( XShapes.class, pPages[ i ] );
254 ShapeHelper.createAndInsertShape( xDrawDoc, xShapes,
255 new Point( nRndObjPosX, nRndObjPosY ),
256 new Size( nRndObjWidth, nRndObjHeight ),
257 "com.sun.star.drawing.RectangleShape" );
260 catch( Exception ex )
262 System.out.println( "Demo_ShapeCreation:" + ex );
266 // This method will show how to create a PolyPolygonBezier that lies is in the
267 // topleft quarter of the page and positioned at the back
268 public static void Demo_PolyPolygonBezier( XComponent xDrawDoc )
272 XShape xPolyPolygonBezier = ShapeHelper.createShape( xDrawDoc,
273 new Point( 0, 0 ),
274 new Size( 0, 0 ),
275 "com.sun.star.drawing.ClosedBezierShape" );
277 // the fact that the shape must have been added to the page before
278 // it is possible to apply changes to the PropertySet, it is a good
279 // proceeding to add the shape as soon as possible
280 XDrawPage xDrawPage;
281 // if possible insert our new shape in the master page
282 if ( PageHelper.isImpressDocument( xDrawDoc ) )
283 xDrawPage = PageHelper.getMasterPageByIndex( xDrawDoc, 0 );
284 else
285 xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, 0 );
286 XShapes xShapes = UnoRuntime.queryInterface( XShapes.class, xDrawPage );
287 xShapes.add( xPolyPolygonBezier );
289 XPropertySet xShapeProperties = UnoRuntime.queryInterface( XPropertySet.class, xPolyPolygonBezier );
291 // get pagesize
292 XPropertySet xPageProperties = UnoRuntime.queryInterface( XPropertySet.class, xDrawPage );
293 int nPageWidth = ((Integer)xPageProperties.getPropertyValue( "Width" )).intValue() / 2;
294 int nPageHeight = ((Integer)xPageProperties.getPropertyValue( "Height" )).intValue() / 2;
296 PolyPolygonBezierCoords aCoords = new PolyPolygonBezierCoords();
297 // allocating the outer sequence
298 int nPolygonCount = 50;
299 aCoords.Coordinates = new Point[ nPolygonCount ][ ];
300 aCoords.Flags = new PolygonFlags[ nPolygonCount ][ ];
301 int i, n, nY;
302 // fill the inner point sequence now
303 for ( nY = 0, i = 0; i < nPolygonCount; i++, nY += nPageHeight / nPolygonCount )
305 // create a polygon using two normal and two control points
306 // allocating the inner sequence
307 int nPointCount = 8;
308 Point[] pPolyPoints = new Point[ nPointCount ];
309 PolygonFlags[] pPolyFlags = new PolygonFlags[ nPointCount ];
311 for ( n = 0; n < nPointCount; n++ )
312 pPolyPoints[ n ] = new Point();
314 pPolyPoints[ 0 ].X = 0;
315 pPolyPoints[ 0 ].Y = nY;
316 pPolyFlags[ 0 ] = PolygonFlags.NORMAL;
317 pPolyPoints[ 1 ].X = nPageWidth / 2;
318 pPolyPoints[ 1 ].Y = nPageHeight;
319 pPolyFlags[ 1 ] = PolygonFlags.CONTROL;
320 pPolyPoints[ 2 ].X = nPageWidth / 2;
321 pPolyPoints[ 2 ].Y = nPageHeight;
322 pPolyFlags[ 2 ] = PolygonFlags.CONTROL;
323 pPolyPoints[ 3 ].X = nPageWidth;
324 pPolyPoints[ 3 ].Y = nY;
325 pPolyFlags[ 3 ] = PolygonFlags.NORMAL;
327 pPolyPoints[ 4 ].X = nPageWidth - 1000;
328 pPolyPoints[ 4 ].Y = nY;
329 pPolyFlags[ 4 ] = PolygonFlags.NORMAL;
330 pPolyPoints[ 5 ].X = nPageWidth / 2;
331 pPolyPoints[ 5 ].Y = nPageHeight / 2;
332 pPolyFlags[ 5 ] = PolygonFlags.CONTROL;
333 pPolyPoints[ 6 ].X = nPageWidth / 2;
334 pPolyPoints[ 6 ].Y = nPageHeight / 2;
335 pPolyFlags[ 6 ] = PolygonFlags.CONTROL;
336 pPolyPoints[ 7 ].X = 1000;
337 pPolyPoints[ 7 ].Y = nY;
338 pPolyFlags[ 7 ] = PolygonFlags.NORMAL;
340 aCoords.Coordinates[ i ]= pPolyPoints;
341 aCoords.Flags[ i ] = pPolyFlags;
343 xShapeProperties.setPropertyValue( "PolyPolygonBezier", aCoords );
345 // move the shape to the back by changing the ZOrder
346 xShapeProperties.setPropertyValue( "ZOrder", Integer.valueOf( 1 ) );
348 catch ( Exception ex )
350 System.out.println( "Demo_PolyPolygonBezier:" + ex );
354 // This method will create a group containing two ellipses
355 // the shapes will be added into the top right corner of the first
356 // draw page
357 public static void Demo_Group1( XComponent xDrawDoc )
361 XShape xGroup = ShapeHelper.createShape( xDrawDoc,
362 new Point( 0, 0 ),
363 new Size( 0, 0 ),
364 "com.sun.star.drawing.GroupShape" );
366 // before it is possible to insert shapes,
367 // the group must have been added to the page
368 XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, 0 );
369 XShapes xShapes = UnoRuntime.queryInterface( XShapes.class, xDrawPage );
370 xShapes.add( xGroup );
372 XShapes xShapesGroup = UnoRuntime.queryInterface( XShapes.class, xGroup );
374 Size aPageSize = PageHelper.getPageSize( xDrawPage );
376 int nWidth = 4000;
377 int nHeight = 2000;
378 int nPosX = ( aPageSize.Width * 3 ) / 4 - nWidth / 2;
379 int nPosY1 = 2000;
380 int nPosY2 = aPageSize.Height / 2 - ( nPosY1 + nHeight );
381 XShape xRect1 = ShapeHelper.createShape( xDrawDoc,
382 new Point( nPosX, nPosY1 ),
383 new Size( nWidth, nHeight ),
384 "com.sun.star.drawing.EllipseShape" );
385 XShape xRect2 = ShapeHelper.createShape( xDrawDoc,
386 new Point( nPosX, nPosY2 ),
387 new Size( nWidth, nHeight ),
388 "com.sun.star.drawing.EllipseShape" );
390 xShapesGroup.add( xRect1 );
391 xShapesGroup.add( xRect2 );
393 catch ( Exception ex )
395 System.out.println( "Demo_Group1:" + ex );
399 // This method will group all available objects on the
400 // first page.
401 public static void Demo_Group2( XComponent xDrawDoc )
405 XDrawPage xDrawPage = PageHelper.getDrawPageByIndex( xDrawDoc, 0 );
406 XShapeGrouper xShapeGrouper = UnoRuntime.queryInterface( XShapeGrouper.class, xDrawPage );
408 XShapes xShapesPage = UnoRuntime.queryInterface( XShapes.class, xDrawPage );
410 xShapeGrouper.group( xShapesPage );
412 catch ( Exception ex )
414 System.out.println( "Demo_Group2:" + ex );
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */