xref: /trunk/main/odk/examples/DevelopersGuide/Drawing/ShapeHelper.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1*34dd1e25SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // __________ Imports __________
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
27cdf0e10cSrcweir import com.sun.star.lang.XComponent;
28cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.awt.Point;
31cdf0e10cSrcweir import com.sun.star.awt.Size;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import com.sun.star.container.XEnumeration;
36cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import com.sun.star.drawing.XShape;
39cdf0e10cSrcweir import com.sun.star.drawing.XShapes;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir import com.sun.star.text.ControlCharacter;
42cdf0e10cSrcweir import com.sun.star.text.XText;
43cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
44cdf0e10cSrcweir import com.sun.star.text.XTextContent;
45cdf0e10cSrcweir import com.sun.star.text.XTextRange;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir public class ShapeHelper
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     // __________ static helper methods __________
51cdf0e10cSrcweir     //
createAndInsertShape( XComponent xDrawDoc, XShapes xShapes, Point aPos, Size aSize, String sShapeType )52cdf0e10cSrcweir     public static XPropertySet createAndInsertShape( XComponent xDrawDoc,
53cdf0e10cSrcweir             XShapes xShapes, Point aPos, Size aSize, String sShapeType )
54cdf0e10cSrcweir         throws java.lang.Exception
55cdf0e10cSrcweir     {
56cdf0e10cSrcweir         XShape xShape = createShape( xDrawDoc, aPos, aSize, sShapeType );
57cdf0e10cSrcweir         xShapes.add( xShape );
58cdf0e10cSrcweir         XPropertySet xPropSet = (XPropertySet)
59cdf0e10cSrcweir             UnoRuntime.queryInterface( XPropertySet.class, xShape );
60cdf0e10cSrcweir         return xPropSet;
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     /** create a Shape
64cdf0e10cSrcweir     */
createShape( XComponent xDrawDoc, Point aPos, Size aSize, String sShapeType )65cdf0e10cSrcweir     public static XShape createShape( XComponent xDrawDoc,
66cdf0e10cSrcweir             Point aPos, Size aSize, String sShapeType )
67cdf0e10cSrcweir         throws java.lang.Exception
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         XShape xShape = null;
70cdf0e10cSrcweir         XMultiServiceFactory xFactory =
71cdf0e10cSrcweir             (XMultiServiceFactory )UnoRuntime.queryInterface(
72cdf0e10cSrcweir                 XMultiServiceFactory.class, xDrawDoc );
73cdf0e10cSrcweir         Object xObj = xFactory.createInstance( sShapeType );
74cdf0e10cSrcweir         xShape = (XShape)UnoRuntime.queryInterface(
75cdf0e10cSrcweir             XShape.class, xObj );
76cdf0e10cSrcweir         xShape.setPosition( aPos );
77cdf0e10cSrcweir         xShape.setSize( aSize );
78cdf0e10cSrcweir         return xShape;
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     /**
82cdf0e10cSrcweir         add text to a shape. the return value is the PropertySet
83cdf0e10cSrcweir         of the text range that has been added
84cdf0e10cSrcweir     */
addPortion( XShape xShape, String sText, boolean bNewParagraph )85cdf0e10cSrcweir     public static XPropertySet addPortion( XShape xShape, String sText, boolean bNewParagraph )
86cdf0e10cSrcweir         throws com.sun.star.lang.IllegalArgumentException
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         XText xText = (XText)
89cdf0e10cSrcweir             UnoRuntime.queryInterface( XText.class, xShape );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         XTextCursor xTextCursor = xText.createTextCursor();
92cdf0e10cSrcweir         xTextCursor.gotoEnd( false );
93cdf0e10cSrcweir         if ( bNewParagraph == true )
94cdf0e10cSrcweir         {
95cdf0e10cSrcweir             xText.insertControlCharacter( xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false );
96cdf0e10cSrcweir             xTextCursor.gotoEnd( false );
97cdf0e10cSrcweir         }
98cdf0e10cSrcweir         XTextRange xTextRange = (XTextRange)
99cdf0e10cSrcweir             UnoRuntime.queryInterface( XTextRange.class, xTextCursor );
100cdf0e10cSrcweir         xTextRange.setString( sText );
101cdf0e10cSrcweir         xTextCursor.gotoEnd( true );
102cdf0e10cSrcweir         XPropertySet xPropSet = (XPropertySet)
103cdf0e10cSrcweir             UnoRuntime.queryInterface( XPropertySet.class, xTextRange );
104cdf0e10cSrcweir         return xPropSet;
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
setPropertyForLastParagraph( XShape xText, String sPropName, Object aValue )107cdf0e10cSrcweir     public static void setPropertyForLastParagraph( XShape xText, String sPropName,
108cdf0e10cSrcweir         Object aValue )
109cdf0e10cSrcweir             throws com.sun.star.beans.UnknownPropertyException,
110cdf0e10cSrcweir                 com.sun.star.beans.PropertyVetoException,
111cdf0e10cSrcweir                     com.sun.star.lang.IllegalArgumentException,
112cdf0e10cSrcweir                         com.sun.star.lang.WrappedTargetException,
113cdf0e10cSrcweir                             com.sun.star.container.NoSuchElementException
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         XEnumerationAccess xEnumerationAccess = (XEnumerationAccess)
116cdf0e10cSrcweir             UnoRuntime.queryInterface( XEnumerationAccess.class, xText );
117cdf0e10cSrcweir         if ( xEnumerationAccess.hasElements() )
118cdf0e10cSrcweir         {
119cdf0e10cSrcweir             XEnumeration xEnumeration = xEnumerationAccess.createEnumeration();
120cdf0e10cSrcweir             while( xEnumeration.hasMoreElements () )
121cdf0e10cSrcweir             {
122cdf0e10cSrcweir                 Object xObj = xEnumeration.nextElement();
123cdf0e10cSrcweir                 if ( xEnumeration.hasMoreElements() == false )
124cdf0e10cSrcweir                 {
125cdf0e10cSrcweir                     XTextContent xTextContent = (XTextContent)UnoRuntime.queryInterface(
126cdf0e10cSrcweir                         XTextContent.class, xObj );
127cdf0e10cSrcweir                     XPropertySet xParaPropSet = (XPropertySet)
128cdf0e10cSrcweir                         UnoRuntime.queryInterface( XPropertySet.class, xTextContent );
129cdf0e10cSrcweir                     xParaPropSet.setPropertyValue( sPropName, aValue );
130cdf0e10cSrcweir                 }
131cdf0e10cSrcweir             }
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir }
135