1645d376bSLiu Zhe /**************************************************************
2645d376bSLiu Zhe  *
3645d376bSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4645d376bSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5645d376bSLiu Zhe  * distributed with this work for additional information
6645d376bSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7645d376bSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8645d376bSLiu Zhe  * "License"); you may not use this file except in compliance
9645d376bSLiu Zhe  * with the License.  You may obtain a copy of the License at
10645d376bSLiu Zhe  *
11645d376bSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12645d376bSLiu Zhe  *
13645d376bSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14645d376bSLiu Zhe  * software distributed under the License is distributed on an
15645d376bSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16645d376bSLiu Zhe  * KIND, either express or implied.  See the License for the
17645d376bSLiu Zhe  * specific language governing permissions and limitations
18645d376bSLiu Zhe  * under the License.
19645d376bSLiu Zhe  *
20645d376bSLiu Zhe  *************************************************************/
21645d376bSLiu Zhe package testlib.uno;
22645d376bSLiu Zhe 
23645d376bSLiu Zhe import com.sun.star.uno.UnoRuntime;
24645d376bSLiu Zhe import com.sun.star.lang.XComponent;
25645d376bSLiu Zhe import com.sun.star.lang.XServiceInfo;
26645d376bSLiu Zhe 
27645d376bSLiu Zhe import com.sun.star.awt.Size;
28645d376bSLiu Zhe 
29645d376bSLiu Zhe import com.sun.star.beans.XPropertySet;
30645d376bSLiu Zhe 
31645d376bSLiu Zhe import com.sun.star.drawing.XDrawPage;
32645d376bSLiu Zhe import com.sun.star.drawing.XDrawPages;
33645d376bSLiu Zhe import com.sun.star.drawing.XDrawPagesSupplier;
34645d376bSLiu Zhe import com.sun.star.drawing.XMasterPageTarget;
35645d376bSLiu Zhe import com.sun.star.drawing.XMasterPagesSupplier;
36645d376bSLiu Zhe 
37645d376bSLiu Zhe import com.sun.star.presentation.XPresentationPage;
38645d376bSLiu Zhe import com.sun.star.presentation.XHandoutMasterSupplier;
39645d376bSLiu Zhe 
40645d376bSLiu Zhe public class PageUtil {
41645d376bSLiu Zhe 	/**
42645d376bSLiu Zhe 	 * Get the page count for standard pages
43645d376bSLiu Zhe 	 *
44645d376bSLiu Zhe 	 * @param xComponent
45645d376bSLiu Zhe 	 *            : The presentation document
46645d376bSLiu Zhe 	 * @return slide count
47645d376bSLiu Zhe 	 */
getDrawPageCount(XComponent xComponent)48645d376bSLiu Zhe 	static public int getDrawPageCount(XComponent xComponent) {
49645d376bSLiu Zhe 		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
50645d376bSLiu Zhe 				.queryInterface(XDrawPagesSupplier.class, xComponent);
51645d376bSLiu Zhe 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
52645d376bSLiu Zhe 		return xDrawPages.getCount();
53645d376bSLiu Zhe 	}
54645d376bSLiu Zhe 
55645d376bSLiu Zhe 	/**
56645d376bSLiu Zhe 	 * Get draw page by index
57645d376bSLiu Zhe 	 *
58645d376bSLiu Zhe 	 * @param xComponent
59645d376bSLiu Zhe 	 *            : The presentation document
60645d376bSLiu Zhe 	 * @param nIndex
61645d376bSLiu Zhe 	 *            : index of slide pages, from 0 or 1?
62645d376bSLiu Zhe 	 * @return Page of corresponding index.
63645d376bSLiu Zhe 	 */
getDrawPageByIndex(XComponent xComponent, int nIndex)64645d376bSLiu Zhe 	static public XDrawPage getDrawPageByIndex(XComponent xComponent, int nIndex)
65645d376bSLiu Zhe 			throws com.sun.star.lang.IndexOutOfBoundsException,
66645d376bSLiu Zhe 			com.sun.star.lang.WrappedTargetException {
67645d376bSLiu Zhe 		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
68645d376bSLiu Zhe 				.queryInterface(XDrawPagesSupplier.class, xComponent);
69645d376bSLiu Zhe 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
70645d376bSLiu Zhe 		return (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class,
71645d376bSLiu Zhe 				xDrawPages.getByIndex(nIndex));
72645d376bSLiu Zhe 	}
73645d376bSLiu Zhe 
74645d376bSLiu Zhe 	/**
75645d376bSLiu Zhe 	 * Create and insert a draw page into the giving position,the method returns
76645d376bSLiu Zhe 	 * the new created page
77645d376bSLiu Zhe 	 *
78645d376bSLiu Zhe 	 * @param xComponent
79645d376bSLiu Zhe 	 *            :The Presentation Document
80645d376bSLiu Zhe 	 * @param nIndex
81645d376bSLiu Zhe 	 *            :The index at which page will be inserted.
82645d376bSLiu Zhe 	 * @return The newly created page.
83645d376bSLiu Zhe 	 */
insertNewDrawPageByIndex(XComponent xComponent, int nIndex)84645d376bSLiu Zhe 	static public XDrawPage insertNewDrawPageByIndex(XComponent xComponent,
85645d376bSLiu Zhe 			int nIndex) throws Exception {
86645d376bSLiu Zhe 		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
87645d376bSLiu Zhe 				.queryInterface(XDrawPagesSupplier.class, xComponent);
88645d376bSLiu Zhe 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
89645d376bSLiu Zhe 		return xDrawPages.insertNewByIndex(nIndex);
90645d376bSLiu Zhe 	}
91645d376bSLiu Zhe 
92645d376bSLiu Zhe 	/**
93645d376bSLiu Zhe 	 * Remove the given page
94645d376bSLiu Zhe 	 *
95645d376bSLiu Zhe 	 * @param xComponent
96645d376bSLiu Zhe 	 *            : The Presentation Document
97645d376bSLiu Zhe 	 * @param xDrawPage
98645d376bSLiu Zhe 	 *            : The page want to be removed.
99645d376bSLiu Zhe 	 */
removeDrawPage(XComponent xComponent, XDrawPage xDrawPage)100645d376bSLiu Zhe 	static public void removeDrawPage(XComponent xComponent, XDrawPage xDrawPage) {
101645d376bSLiu Zhe 		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
102645d376bSLiu Zhe 				.queryInterface(XDrawPagesSupplier.class, xComponent);
103645d376bSLiu Zhe 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
104645d376bSLiu Zhe 		xDrawPages.remove(xDrawPage);
105645d376bSLiu Zhe 	}
106645d376bSLiu Zhe 
107645d376bSLiu Zhe 	/**
108645d376bSLiu Zhe 	 * Get size of the given page
109645d376bSLiu Zhe 	 *
110645d376bSLiu Zhe 	 * @param xDrawPage
111645d376bSLiu Zhe 	 *            : The specified target page
112645d376bSLiu Zhe 	 * @return specifies the 2-dimensional size of the page using width and
113645d376bSLiu Zhe 	 *         height.
114645d376bSLiu Zhe 	 */
getPageSize(XDrawPage xDrawPage)115645d376bSLiu Zhe 	static public Size getPageSize(XDrawPage xDrawPage)
116645d376bSLiu Zhe 			throws com.sun.star.beans.UnknownPropertyException,
117645d376bSLiu Zhe 			com.sun.star.lang.WrappedTargetException {
118645d376bSLiu Zhe 		XPropertySet xPageProperties = (XPropertySet) UnoRuntime
119645d376bSLiu Zhe 				.queryInterface(XPropertySet.class, xDrawPage);
120645d376bSLiu Zhe 		return new Size(
121645d376bSLiu Zhe 				((Integer) xPageProperties.getPropertyValue("Width"))
122645d376bSLiu Zhe 						.intValue(),
123645d376bSLiu Zhe 				((Integer) xPageProperties.getPropertyValue("Height"))
124645d376bSLiu Zhe 						.intValue());
125645d376bSLiu Zhe 	}
126645d376bSLiu Zhe 
127645d376bSLiu Zhe 	/**
128645d376bSLiu Zhe 	 * Get the page count for master pages
129645d376bSLiu Zhe 	 *
130645d376bSLiu Zhe 	 * @param xComponent
131645d376bSLiu Zhe 	 *            : The presentation document
132645d376bSLiu Zhe 	 * @return Count of master pages.
133645d376bSLiu Zhe 	 */
getMasterPageCount(XComponent xComponent)134645d376bSLiu Zhe 	static public int getMasterPageCount(XComponent xComponent) {
135645d376bSLiu Zhe 		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
136645d376bSLiu Zhe 				.queryInterface(XMasterPagesSupplier.class, xComponent);
137645d376bSLiu Zhe 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
138645d376bSLiu Zhe 		return xDrawPages.getCount();
139645d376bSLiu Zhe 	}
140645d376bSLiu Zhe 
141645d376bSLiu Zhe 	/**
142645d376bSLiu Zhe 	 * Get master page by index
143645d376bSLiu Zhe 	 *
144645d376bSLiu Zhe 	 * @param xComponent
145645d376bSLiu Zhe 	 *            : The Presentation document
146645d376bSLiu Zhe 	 * @param nIndex
147645d376bSLiu Zhe 	 *            : Index of target master page.
148645d376bSLiu Zhe 	 * @return Page of
149645d376bSLiu Zhe 	 */
getMasterPageByIndex(XComponent xComponent, int nIndex)150645d376bSLiu Zhe 	static public XDrawPage getMasterPageByIndex(XComponent xComponent,
151645d376bSLiu Zhe 			int nIndex) throws com.sun.star.lang.IndexOutOfBoundsException,
152645d376bSLiu Zhe 			com.sun.star.lang.WrappedTargetException {
153645d376bSLiu Zhe 		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
154645d376bSLiu Zhe 				.queryInterface(XMasterPagesSupplier.class, xComponent);
155645d376bSLiu Zhe 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
156645d376bSLiu Zhe 		return (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class,
157645d376bSLiu Zhe 				xDrawPages.getByIndex(nIndex));
158645d376bSLiu Zhe 	}
159645d376bSLiu Zhe 
160645d376bSLiu Zhe 	/**
161645d376bSLiu Zhe 	 * creates and inserts a new master page into the giving position, the
162645d376bSLiu Zhe 	 * method returns the new created page
163645d376bSLiu Zhe 	 */
insertNewMasterPageByIndex(XComponent xComponent, int nIndex)164645d376bSLiu Zhe 	static public XDrawPage insertNewMasterPageByIndex(XComponent xComponent,
165645d376bSLiu Zhe 			int nIndex) {
166645d376bSLiu Zhe 		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
167645d376bSLiu Zhe 				.queryInterface(XMasterPagesSupplier.class, xComponent);
168645d376bSLiu Zhe 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
169645d376bSLiu Zhe 		return xDrawPages.insertNewByIndex(nIndex);
170645d376bSLiu Zhe 	}
171645d376bSLiu Zhe 
172645d376bSLiu Zhe 	/**
173645d376bSLiu Zhe 	 * removes the given page
174645d376bSLiu Zhe 	 */
removeMasterPage(XComponent xComponent, XDrawPage xDrawPage)175645d376bSLiu Zhe 	static public void removeMasterPage(XComponent xComponent,
176645d376bSLiu Zhe 			XDrawPage xDrawPage) {
177645d376bSLiu Zhe 		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
178645d376bSLiu Zhe 				.queryInterface(XMasterPagesSupplier.class, xComponent);
179645d376bSLiu Zhe 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
180645d376bSLiu Zhe 		xDrawPages.remove(xDrawPage);
181645d376bSLiu Zhe 	}
182645d376bSLiu Zhe 
183645d376bSLiu Zhe 	/**
184645d376bSLiu Zhe 	 * return the corresponding masterpage for the giving drawpage
185645d376bSLiu Zhe 	 */
getMasterPage(XDrawPage xDrawPage)186645d376bSLiu Zhe 	static public XDrawPage getMasterPage(XDrawPage xDrawPage) {
187645d376bSLiu Zhe 		XMasterPageTarget xMasterPageTarget = (XMasterPageTarget) UnoRuntime
188645d376bSLiu Zhe 				.queryInterface(XMasterPageTarget.class, xDrawPage);
189645d376bSLiu Zhe 		return xMasterPageTarget.getMasterPage();
190645d376bSLiu Zhe 	}
191645d376bSLiu Zhe 
192645d376bSLiu Zhe 	/**
193645d376bSLiu Zhe 	 * sets given masterpage at the drawpage
194645d376bSLiu Zhe 	 */
setMasterPage(XDrawPage xDrawPage, XDrawPage xMasterPage)195645d376bSLiu Zhe 	static public void setMasterPage(XDrawPage xDrawPage, XDrawPage xMasterPage) {
196645d376bSLiu Zhe 		XMasterPageTarget xMasterPageTarget = (XMasterPageTarget) UnoRuntime
197645d376bSLiu Zhe 				.queryInterface(XMasterPageTarget.class, xDrawPage);
198645d376bSLiu Zhe 		xMasterPageTarget.setMasterPage(xMasterPage);
199645d376bSLiu Zhe 	}
200645d376bSLiu Zhe 
201645d376bSLiu Zhe 	// __________ presentation pages __________
202645d376bSLiu Zhe 
203645d376bSLiu Zhe 	/**
204645d376bSLiu Zhe 	 * test if a Presentation Document is supported. This is important, because
205645d376bSLiu Zhe 	 * only presentation documents have notes and handout pages
206645d376bSLiu Zhe 	 */
isImpressDocument(XComponent xComponent)207645d376bSLiu Zhe 	static public boolean isImpressDocument(XComponent xComponent) {
208645d376bSLiu Zhe 		XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface(
209645d376bSLiu Zhe 				XServiceInfo.class, xComponent);
210645d376bSLiu Zhe 		return xInfo
211645d376bSLiu Zhe 				.supportsService("com.sun.star.presentation.PresentationDocument");
212645d376bSLiu Zhe 	}
213645d376bSLiu Zhe 
214645d376bSLiu Zhe 	/**
215645d376bSLiu Zhe 	 * in impress documents each normal draw page has a corresponding notes page
216645d376bSLiu Zhe 	 */
getNotesPage(XDrawPage xDrawPage)217645d376bSLiu Zhe 	static public XDrawPage getNotesPage(XDrawPage xDrawPage) {
218645d376bSLiu Zhe 		XPresentationPage aPresentationPage = (XPresentationPage) UnoRuntime
219645d376bSLiu Zhe 				.queryInterface(XPresentationPage.class, xDrawPage);
220645d376bSLiu Zhe 		return aPresentationPage.getNotesPage();
221645d376bSLiu Zhe 	}
222645d376bSLiu Zhe 
223645d376bSLiu Zhe 	/**
224645d376bSLiu Zhe 	 * in impress each documents has one handout page
225645d376bSLiu Zhe 	 */
getHandoutMasterPage(XComponent xComponent)226645d376bSLiu Zhe 	static public XDrawPage getHandoutMasterPage(XComponent xComponent) {
227645d376bSLiu Zhe 		XHandoutMasterSupplier aHandoutMasterSupplier = (XHandoutMasterSupplier) UnoRuntime
228645d376bSLiu Zhe 				.queryInterface(XHandoutMasterSupplier.class, xComponent);
229645d376bSLiu Zhe 		return aHandoutMasterSupplier.getHandoutMasterPage();
230645d376bSLiu Zhe 	}
231645d376bSLiu Zhe }
232