xref: /trunk/test/testuno/source/testlib/uno/ShapeUtil.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1e6e6073dSLiu Zhe package testlib.uno;
2e6e6073dSLiu Zhe 
3e6e6073dSLiu Zhe /**************************************************************
4e6e6073dSLiu Zhe  *
5e6e6073dSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
6e6e6073dSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
7e6e6073dSLiu Zhe  * distributed with this work for additional information
8e6e6073dSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
9e6e6073dSLiu Zhe  * to you under the Apache License, Version 2.0 (the
10e6e6073dSLiu Zhe  * "License"); you may not use this file except in compliance
11e6e6073dSLiu Zhe  * with the License.  You may obtain a copy of the License at
12e6e6073dSLiu Zhe  *
13e6e6073dSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
14e6e6073dSLiu Zhe  *
15e6e6073dSLiu Zhe  * Unless required by applicable law or agreed to in writing,
16e6e6073dSLiu Zhe  * software distributed under the License is distributed on an
17e6e6073dSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18e6e6073dSLiu Zhe  * KIND, either express or implied.  See the License for the
19e6e6073dSLiu Zhe  * specific language governing permissions and limitations
20e6e6073dSLiu Zhe  * under the License.
21e6e6073dSLiu Zhe  *
22e6e6073dSLiu Zhe  *************************************************************/
23e6e6073dSLiu Zhe 
24e6e6073dSLiu Zhe // __________ Imports __________
25e6e6073dSLiu Zhe 
26e6e6073dSLiu Zhe import com.sun.star.uno.Exception;
27e6e6073dSLiu Zhe import com.sun.star.uno.UnoRuntime;
28e6e6073dSLiu Zhe import com.sun.star.lang.WrappedTargetException;
29e6e6073dSLiu Zhe import com.sun.star.lang.XComponent;
30e6e6073dSLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
31e6e6073dSLiu Zhe 
32e6e6073dSLiu Zhe import com.sun.star.awt.Point;
33e6e6073dSLiu Zhe import com.sun.star.awt.Size;
34e6e6073dSLiu Zhe 
35e6e6073dSLiu Zhe import com.sun.star.beans.XPropertySet;
36e6e6073dSLiu Zhe 
37e6e6073dSLiu Zhe import com.sun.star.container.NoSuchElementException;
38e6e6073dSLiu Zhe import com.sun.star.container.XEnumeration;
39e6e6073dSLiu Zhe import com.sun.star.container.XEnumerationAccess;
40*85b88695SLiu Zhe import com.sun.star.drawing.XDrawPage;
41*85b88695SLiu Zhe import com.sun.star.drawing.XDrawPages;
42*85b88695SLiu Zhe import com.sun.star.drawing.XDrawPagesSupplier;
43e6e6073dSLiu Zhe import com.sun.star.drawing.XShape;
44e6e6073dSLiu Zhe import com.sun.star.drawing.XShapes;
45e6e6073dSLiu Zhe 
46e6e6073dSLiu Zhe import com.sun.star.text.ControlCharacter;
47e6e6073dSLiu Zhe import com.sun.star.text.XText;
48e6e6073dSLiu Zhe import com.sun.star.text.XTextCursor;
49e6e6073dSLiu Zhe import com.sun.star.text.XTextContent;
50e6e6073dSLiu Zhe import com.sun.star.text.XTextRange;
51e6e6073dSLiu Zhe 
52e6e6073dSLiu Zhe public class ShapeUtil {
53e6e6073dSLiu Zhe     // __________ static helper methods __________
54e6e6073dSLiu Zhe     //
createAndInsertShape(XComponent xDrawDoc, XShapes xShapes, Point aPos, Size aSize, String sShapeType)55e6e6073dSLiu Zhe     public static XPropertySet createAndInsertShape(XComponent xDrawDoc,
56e6e6073dSLiu Zhe             XShapes xShapes, Point aPos, Size aSize, String sShapeType)
57e6e6073dSLiu Zhe             throws java.lang.Exception {
58e6e6073dSLiu Zhe         XShape xShape = createShape(xDrawDoc, aPos, aSize, sShapeType);
59e6e6073dSLiu Zhe         xShapes.add(xShape);
60e6e6073dSLiu Zhe         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
61e6e6073dSLiu Zhe                 XPropertySet.class, xShape);
62e6e6073dSLiu Zhe         return xPropSet;
63e6e6073dSLiu Zhe     }
64e6e6073dSLiu Zhe 
65e6e6073dSLiu Zhe     /**
66e6e6073dSLiu Zhe      * create a Shape
67e6e6073dSLiu Zhe      */
createShape(XComponent xDrawDoc, Point aPos, Size aSize, String sShapeType)68e6e6073dSLiu Zhe     public static XShape createShape(XComponent xDrawDoc, Point aPos,
69e6e6073dSLiu Zhe             Size aSize, String sShapeType) throws java.lang.Exception {
70e6e6073dSLiu Zhe         XShape xShape = null;
71e6e6073dSLiu Zhe         XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime
72e6e6073dSLiu Zhe                 .queryInterface(XMultiServiceFactory.class, xDrawDoc);
73e6e6073dSLiu Zhe         Object xObj = xFactory.createInstance(sShapeType);
74e6e6073dSLiu Zhe         xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xObj);
75e6e6073dSLiu Zhe         xShape.setPosition(aPos);
76e6e6073dSLiu Zhe         xShape.setSize(aSize);
77e6e6073dSLiu Zhe         return xShape;
78e6e6073dSLiu Zhe     }
79e6e6073dSLiu Zhe 
80e6e6073dSLiu Zhe 
81e6e6073dSLiu Zhe     /**
82e6e6073dSLiu Zhe      * add text to a shape. the return value is the PropertySet of the text
83e6e6073dSLiu Zhe      * range that has been added
84e6e6073dSLiu Zhe      */
addPortion(XShape xShape, String sText, boolean bNewParagraph)85e6e6073dSLiu Zhe     public static XPropertySet addPortion(XShape xShape, String sText,
86e6e6073dSLiu Zhe             boolean bNewParagraph)
87e6e6073dSLiu Zhe             throws com.sun.star.lang.IllegalArgumentException {
88e6e6073dSLiu Zhe         XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape);
89e6e6073dSLiu Zhe 
90e6e6073dSLiu Zhe         XTextCursor xTextCursor = xText.createTextCursor();
91e6e6073dSLiu Zhe         xTextCursor.gotoEnd(false);
92e6e6073dSLiu Zhe         if (bNewParagraph == true) {
93e6e6073dSLiu Zhe             xText.insertControlCharacter(xTextCursor,
94e6e6073dSLiu Zhe                     ControlCharacter.PARAGRAPH_BREAK, false);
95e6e6073dSLiu Zhe             xTextCursor.gotoEnd(false);
96e6e6073dSLiu Zhe         }
97e6e6073dSLiu Zhe         XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(
98e6e6073dSLiu Zhe                 XTextRange.class, xTextCursor);
99e6e6073dSLiu Zhe         xTextRange.setString(sText);
100e6e6073dSLiu Zhe         xTextCursor.gotoEnd(true);
101e6e6073dSLiu Zhe         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
102e6e6073dSLiu Zhe                 XPropertySet.class, xTextRange);
103e6e6073dSLiu Zhe         return xPropSet;
104e6e6073dSLiu Zhe     }
105e6e6073dSLiu Zhe 
106e6e6073dSLiu Zhe     /**
107e6e6073dSLiu Zhe      * get a paragraph in a shape. the return value is the PropertySet of the text
108e6e6073dSLiu Zhe      * range that specified by the index
109e6e6073dSLiu Zhe      */
getPortion(XShape xShape, int index)110e6e6073dSLiu Zhe     public static XPropertySet getPortion(XShape xShape, int index) throws NoSuchElementException, WrappedTargetException {
111e6e6073dSLiu Zhe         XEnumerationAccess m_paraAccess = (XEnumerationAccess)UnoRuntime.queryInterface(XEnumerationAccess.class, xShape);
112e6e6073dSLiu Zhe         XEnumeration xParaEnum = m_paraAccess.createEnumeration();
113e6e6073dSLiu Zhe         XPropertySet xPropSet = null;
114e6e6073dSLiu Zhe         int i=0;
115e6e6073dSLiu Zhe         while(xParaEnum.hasMoreElements())
116e6e6073dSLiu Zhe         {
117e6e6073dSLiu Zhe             if(i == index)
118e6e6073dSLiu Zhe             {
119e6e6073dSLiu Zhe                 Object aPortionObj = xParaEnum.nextElement();
120e6e6073dSLiu Zhe                 XTextRange xTextRange = (XTextRange)UnoRuntime.queryInterface(XTextRange.class, aPortionObj);
121e6e6073dSLiu Zhe //              System.out.println(xTextRange.getText().getString());
122e6e6073dSLiu Zhe                 xPropSet = (XPropertySet) UnoRuntime.queryInterface(
123e6e6073dSLiu Zhe                         XPropertySet.class, xTextRange);
124e6e6073dSLiu Zhe                 break;
125e6e6073dSLiu Zhe             }
126e6e6073dSLiu Zhe             else i++;
127e6e6073dSLiu Zhe         }
128e6e6073dSLiu Zhe         return xPropSet;
129e6e6073dSLiu Zhe     }
130e6e6073dSLiu Zhe 
131e6e6073dSLiu Zhe 
132e6e6073dSLiu Zhe     /**
133e6e6073dSLiu Zhe      * try to get text of a shape
134e6e6073dSLiu Zhe      *
135e6e6073dSLiu Zhe      * @return
136e6e6073dSLiu Zhe      */
getPortion(XShape xShape)137e6e6073dSLiu Zhe     public static String getPortion(XShape xShape) {
138e6e6073dSLiu Zhe         String text = null;
139e6e6073dSLiu Zhe         XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape);
140e6e6073dSLiu Zhe 
141e6e6073dSLiu Zhe         XTextCursor xTextCursor = xText.createTextCursor();
142e6e6073dSLiu Zhe         XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(
143e6e6073dSLiu Zhe                 XTextRange.class, xTextCursor);
144e6e6073dSLiu Zhe         text = xTextRange.getString();
145e6e6073dSLiu Zhe         return text;
146e6e6073dSLiu Zhe 
147e6e6073dSLiu Zhe     }
148e6e6073dSLiu Zhe 
setPropertyForLastParagraph(XShape xText, String sPropName, Object aValue)149e6e6073dSLiu Zhe     public static void setPropertyForLastParagraph(XShape xText,
150e6e6073dSLiu Zhe             String sPropName, Object aValue)
151e6e6073dSLiu Zhe             throws com.sun.star.beans.UnknownPropertyException,
152e6e6073dSLiu Zhe             com.sun.star.beans.PropertyVetoException,
153e6e6073dSLiu Zhe             com.sun.star.lang.IllegalArgumentException,
154e6e6073dSLiu Zhe             com.sun.star.lang.WrappedTargetException,
155e6e6073dSLiu Zhe             com.sun.star.container.NoSuchElementException {
156e6e6073dSLiu Zhe         XEnumerationAccess xEnumerationAccess = (XEnumerationAccess) UnoRuntime
157e6e6073dSLiu Zhe                 .queryInterface(XEnumerationAccess.class, xText);
158e6e6073dSLiu Zhe         if (xEnumerationAccess.hasElements()) {
159e6e6073dSLiu Zhe             XEnumeration xEnumeration = xEnumerationAccess.createEnumeration();
160e6e6073dSLiu Zhe             while (xEnumeration.hasMoreElements()) {
161e6e6073dSLiu Zhe                 Object xObj = xEnumeration.nextElement();
162e6e6073dSLiu Zhe                 if (xEnumeration.hasMoreElements() == false) {
163e6e6073dSLiu Zhe                     XTextContent xTextContent = (XTextContent) UnoRuntime
164e6e6073dSLiu Zhe                             .queryInterface(XTextContent.class, xObj);
165e6e6073dSLiu Zhe                     XPropertySet xParaPropSet = (XPropertySet) UnoRuntime
166e6e6073dSLiu Zhe                             .queryInterface(XPropertySet.class, xTextContent);
167e6e6073dSLiu Zhe                     xParaPropSet.setPropertyValue(sPropName, aValue);
168e6e6073dSLiu Zhe                 }
169e6e6073dSLiu Zhe             }
170e6e6073dSLiu Zhe         }
171e6e6073dSLiu Zhe     }
172*85b88695SLiu Zhe     /**
173*85b88695SLiu Zhe      * Get shapes in specific page
174*85b88695SLiu Zhe      * @param impressDocument
175*85b88695SLiu Zhe      * @param pageIndex
176*85b88695SLiu Zhe      * @return
177*85b88695SLiu Zhe      * @throws Exception
178*85b88695SLiu Zhe      */
getShapes(XComponent impressDocument, int pageIndex)179*85b88695SLiu Zhe     public static XShapes getShapes(XComponent impressDocument, int pageIndex) throws Exception{
180*85b88695SLiu Zhe 
181*85b88695SLiu Zhe         XDrawPagesSupplier drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
182*85b88695SLiu Zhe                 XDrawPagesSupplier.class, impressDocument);
183*85b88695SLiu Zhe         XDrawPages drawpages = drawsupplier.getDrawPages();
184*85b88695SLiu Zhe         XDrawPage xpage=(XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, drawpages.getByIndex(pageIndex));
185*85b88695SLiu Zhe         XShapes xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
186*85b88695SLiu Zhe         return xShapes;
187*85b88695SLiu Zhe 
188*85b88695SLiu Zhe     }
189*85b88695SLiu Zhe 
190*85b88695SLiu Zhe     /**
191*85b88695SLiu Zhe      * Remove the specific shape in specific page
192*85b88695SLiu Zhe      * @param impressDocument
193*85b88695SLiu Zhe      * @param pageIndex
194*85b88695SLiu Zhe      * @param shapeIndex
195*85b88695SLiu Zhe      * @throws Exception
196*85b88695SLiu Zhe      */
removeOneShape(XComponent impressDocument, int pageIndex, int shapeIndex)197*85b88695SLiu Zhe     public static void removeOneShape(XComponent impressDocument, int pageIndex, int shapeIndex) throws Exception{
198*85b88695SLiu Zhe         XDrawPagesSupplier drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
199*85b88695SLiu Zhe                 XDrawPagesSupplier.class, impressDocument);
200*85b88695SLiu Zhe         XDrawPages drawpages = drawsupplier.getDrawPages();
201*85b88695SLiu Zhe         XDrawPage xpage=(XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, drawpages.getByIndex(pageIndex));
202*85b88695SLiu Zhe         XShapes xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
203*85b88695SLiu Zhe         XShape xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xShapes.getByIndex(shapeIndex));
204*85b88695SLiu Zhe         xShapes.remove(xShape);
205*85b88695SLiu Zhe 
206*85b88695SLiu Zhe     }
207e6e6073dSLiu Zhe }
208