1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 // __________ Imports __________
36 
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.lang.XComponent;
39 import com.sun.star.lang.XMultiServiceFactory;
40 
41 import com.sun.star.awt.Point;
42 import com.sun.star.awt.Size;
43 
44 import com.sun.star.beans.XPropertySet;
45 
46 import com.sun.star.container.XEnumeration;
47 import com.sun.star.container.XEnumerationAccess;
48 
49 import com.sun.star.drawing.XShape;
50 import com.sun.star.drawing.XShapes;
51 
52 import com.sun.star.text.ControlCharacter;
53 import com.sun.star.text.XText;
54 import com.sun.star.text.XTextCursor;
55 import com.sun.star.text.XTextContent;
56 import com.sun.star.text.XTextRange;
57 
58 
59 public class ShapeHelper
60 {
61     // __________ static helper methods __________
62 	//
63 	public static XPropertySet createAndInsertShape( XComponent xDrawDoc,
64 			XShapes xShapes, Point aPos, Size aSize, String sShapeType )
65 		throws java.lang.Exception
66 	{
67 		XShape xShape = createShape( xDrawDoc, aPos, aSize, sShapeType );
68 		xShapes.add( xShape );
69 		XPropertySet xPropSet = (XPropertySet)
70 			UnoRuntime.queryInterface( XPropertySet.class, xShape );
71 		return xPropSet;
72 	}
73 
74     /** create a Shape
75 	*/
76 	public static XShape createShape( XComponent xDrawDoc,
77 			Point aPos, Size aSize, String sShapeType )
78 		throws java.lang.Exception
79 	{
80 		XShape xShape = null;
81 		XMultiServiceFactory xFactory =
82 			(XMultiServiceFactory )UnoRuntime.queryInterface(
83 				XMultiServiceFactory.class, xDrawDoc );
84 		Object xObj = xFactory.createInstance( sShapeType );
85 		xShape = (XShape)UnoRuntime.queryInterface(
86 			XShape.class, xObj );
87 		xShape.setPosition( aPos );
88 		xShape.setSize( aSize );
89 		return xShape;
90 	}
91 
92 	/**
93         add text to a shape. the return value is the PropertySet
94         of the text range that has been added
95 	*/
96 	public static XPropertySet addPortion( XShape xShape, String sText, boolean bNewParagraph )
97 		throws com.sun.star.lang.IllegalArgumentException
98 	{
99 		XText xText = (XText)
100 			UnoRuntime.queryInterface( XText.class, xShape );
101 
102 		XTextCursor xTextCursor = xText.createTextCursor();
103 		xTextCursor.gotoEnd( false );
104 		if ( bNewParagraph == true )
105 		{
106 			xText.insertControlCharacter( xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false );
107 			xTextCursor.gotoEnd( false );
108 		}
109 		XTextRange xTextRange = (XTextRange)
110 			UnoRuntime.queryInterface( XTextRange.class, xTextCursor );
111 		xTextRange.setString( sText );
112 		xTextCursor.gotoEnd( true );
113 		XPropertySet xPropSet = (XPropertySet)
114 			UnoRuntime.queryInterface( XPropertySet.class, xTextRange );
115 		return xPropSet;
116 	}
117 
118 	public static void setPropertyForLastParagraph( XShape xText, String sPropName,
119 		Object aValue )
120 			throws com.sun.star.beans.UnknownPropertyException,
121 				com.sun.star.beans.PropertyVetoException,
122 					com.sun.star.lang.IllegalArgumentException,
123 						com.sun.star.lang.WrappedTargetException,
124 							com.sun.star.container.NoSuchElementException
125 	{
126 		XEnumerationAccess xEnumerationAccess = (XEnumerationAccess)
127 			UnoRuntime.queryInterface( XEnumerationAccess.class, xText );
128 		if ( xEnumerationAccess.hasElements() )
129 		{
130 			XEnumeration xEnumeration = xEnumerationAccess.createEnumeration();
131 			while( xEnumeration.hasMoreElements () )
132 			{
133 				Object xObj = xEnumeration.nextElement();
134 				if ( xEnumeration.hasMoreElements() == false )
135 				{
136 					XTextContent xTextContent = (XTextContent)UnoRuntime.queryInterface(
137 						XTextContent.class, xObj );
138 					XPropertySet xParaPropSet = (XPropertySet)
139 						UnoRuntime.queryInterface( XPropertySet.class, xTextContent );
140 					xParaPropSet.setPropertyValue( sPropName, aValue );
141 				}
142 			}
143 		}
144 	}
145 }
146