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