Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / Drawing / ShapeHelper.java
blobbd21e7c828fcb2e9b0cb32c5042dee873c6dfa52
1 /*************************************************************************
3 * $RCSfile: ShapeHelper.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:24:53 $
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;
45 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.awt.Point;
48 import com.sun.star.awt.Size;
50 import com.sun.star.beans.XPropertySet;
52 import com.sun.star.container.XEnumeration;
53 import com.sun.star.container.XEnumerationAccess;
55 import com.sun.star.drawing.XShape;
56 import com.sun.star.drawing.XShapes;
58 import com.sun.star.text.ControlCharacter;
59 import com.sun.star.text.XText;
60 import com.sun.star.text.XTextCursor;
61 import com.sun.star.text.XTextContent;
62 import com.sun.star.text.XTextRange;
65 public class ShapeHelper
67 // __________ static helper methods __________
68 //
69 public static XPropertySet createAndInsertShape( XComponent xDrawDoc,
70 XShapes xShapes, Point aPos, Size aSize, String sShapeType )
71 throws java.lang.Exception
73 XShape xShape = createShape( xDrawDoc, aPos, aSize, sShapeType );
74 xShapes.add( xShape );
75 XPropertySet xPropSet = (XPropertySet)
76 UnoRuntime.queryInterface( XPropertySet.class, xShape );
77 return xPropSet;
80 /** create a Shape
82 public static XShape createShape( XComponent xDrawDoc,
83 Point aPos, Size aSize, String sShapeType )
84 throws java.lang.Exception
86 XShape xShape = null;
87 XMultiServiceFactory xFactory =
88 (XMultiServiceFactory )UnoRuntime.queryInterface(
89 XMultiServiceFactory.class, xDrawDoc );
90 Object xObj = xFactory.createInstance( sShapeType );
91 xShape = (XShape)UnoRuntime.queryInterface(
92 XShape.class, xObj );
93 xShape.setPosition( aPos );
94 xShape.setSize( aSize );
95 return xShape;
98 /**
99 add text to a shape. the return value is the PropertySet
100 of the text range that has been added
102 public static XPropertySet addPortion( XShape xShape, String sText, boolean bNewParagraph )
103 throws com.sun.star.lang.IllegalArgumentException
105 XText xText = (XText)
106 UnoRuntime.queryInterface( XText.class, xShape );
108 XTextCursor xTextCursor = xText.createTextCursor();
109 xTextCursor.gotoEnd( false );
110 if ( bNewParagraph == true )
112 xText.insertControlCharacter( xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false );
113 xTextCursor.gotoEnd( false );
115 XTextRange xTextRange = (XTextRange)
116 UnoRuntime.queryInterface( XTextRange.class, xTextCursor );
117 xTextRange.setString( sText );
118 xTextCursor.gotoEnd( true );
119 XPropertySet xPropSet = (XPropertySet)
120 UnoRuntime.queryInterface( XPropertySet.class, xTextRange );
121 return xPropSet;
124 public static void setPropertyForLastParagraph( XShape xText, String sPropName,
125 Object aValue )
126 throws com.sun.star.beans.UnknownPropertyException,
127 com.sun.star.beans.PropertyVetoException,
128 com.sun.star.lang.IllegalArgumentException,
129 com.sun.star.lang.WrappedTargetException,
130 com.sun.star.container.NoSuchElementException
132 XEnumerationAccess xEnumerationAccess = (XEnumerationAccess)
133 UnoRuntime.queryInterface( XEnumerationAccess.class, xText );
134 if ( xEnumerationAccess.hasElements() )
136 XEnumeration xEnumeration = xEnumerationAccess.createEnumeration();
137 while( xEnumeration.hasMoreElements () )
139 Object xObj = xEnumeration.nextElement();
140 if ( xEnumeration.hasMoreElements() == false )
142 XTextContent xTextContent = (XTextContent)UnoRuntime.queryInterface(
143 XTextContent.class, xObj );
144 XPropertySet xParaPropSet = (XPropertySet)
145 UnoRuntime.queryInterface( XPropertySet.class, xTextContent );
146 xParaPropSet.setPropertyValue( sPropName, aValue );