bump product version to 7.2.5.1
[LibreOffice.git] / odk / examples / DevelopersGuide / Drawing / ShapeHelper.java
blob44b9b28a74fe5abaee779299285dc6d17b5bb78a
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;
40 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.awt.Point;
43 import com.sun.star.awt.Size;
45 import com.sun.star.beans.XPropertySet;
47 import com.sun.star.container.XEnumeration;
48 import com.sun.star.container.XEnumerationAccess;
50 import com.sun.star.drawing.XShape;
51 import com.sun.star.drawing.XShapes;
53 import com.sun.star.text.ControlCharacter;
54 import com.sun.star.text.XText;
55 import com.sun.star.text.XTextCursor;
56 import com.sun.star.text.XTextContent;
57 import com.sun.star.text.XTextRange;
60 public class ShapeHelper
62 // __________ static helper methods __________
64 public static XPropertySet createAndInsertShape( XComponent xDrawDoc,
65 XShapes xShapes, Point aPos, Size aSize, String sShapeType )
66 throws java.lang.Exception
68 XShape xShape = createShape( xDrawDoc, aPos, aSize, sShapeType );
69 xShapes.add( xShape );
70 XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, xShape );
71 return xPropSet;
74 /** create a Shape
76 public static XShape createShape( XComponent xDrawDoc,
77 Point aPos, Size aSize, String sShapeType )
78 throws java.lang.Exception
80 XShape xShape = null;
81 XMultiServiceFactory xFactory =
82 UnoRuntime.queryInterface(
83 XMultiServiceFactory.class, xDrawDoc );
84 Object xObj = xFactory.createInstance( sShapeType );
85 xShape = UnoRuntime.queryInterface(
86 XShape.class, xObj );
87 xShape.setPosition( aPos );
88 xShape.setSize( aSize );
89 return xShape;
92 /**
93 add text to a shape. the return value is the PropertySet
94 of the text range that has been added
96 public static XPropertySet addPortion( XShape xShape, String sText, boolean bNewParagraph )
97 throws com.sun.star.lang.IllegalArgumentException
99 XText xText = UnoRuntime.queryInterface( XText.class, xShape );
101 XTextCursor xTextCursor = xText.createTextCursor();
102 xTextCursor.gotoEnd( false );
103 if ( bNewParagraph == true )
105 xText.insertControlCharacter( xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false );
106 xTextCursor.gotoEnd( false );
108 XTextRange xTextRange = UnoRuntime.queryInterface( XTextRange.class, xTextCursor );
109 xTextRange.setString( sText );
110 xTextCursor.gotoEnd( true );
111 XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, xTextRange );
112 return xPropSet;
115 public static void setPropertyForLastParagraph( XShape xText, String sPropName,
116 Object aValue )
117 throws com.sun.star.beans.UnknownPropertyException,
118 com.sun.star.beans.PropertyVetoException,
119 com.sun.star.lang.IllegalArgumentException,
120 com.sun.star.lang.WrappedTargetException,
121 com.sun.star.container.NoSuchElementException
123 XEnumerationAccess xEnumerationAccess = UnoRuntime.queryInterface( XEnumerationAccess.class, xText );
124 if ( xEnumerationAccess.hasElements() )
126 XEnumeration xEnumeration = xEnumerationAccess.createEnumeration();
127 while( xEnumeration.hasMoreElements () )
129 Object xObj = xEnumeration.nextElement();
130 if ( xEnumeration.hasMoreElements() == false )
132 XTextContent xTextContent = UnoRuntime.queryInterface(
133 XTextContent.class, xObj );
134 XPropertySet xParaPropSet = UnoRuntime.queryInterface( XPropertySet.class, xTextContent );
135 xParaPropSet.setPropertyValue( sPropName, aValue );
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */