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 package testlib.uno;
22 
23 import com.sun.star.uno.UnoRuntime;
24 import com.sun.star.lang.XComponent;
25 import com.sun.star.lang.XServiceInfo;
26 
27 import com.sun.star.awt.Size;
28 
29 import com.sun.star.beans.XPropertySet;
30 
31 import com.sun.star.drawing.XDrawPage;
32 import com.sun.star.drawing.XDrawPages;
33 import com.sun.star.drawing.XDrawPagesSupplier;
34 import com.sun.star.drawing.XMasterPageTarget;
35 import com.sun.star.drawing.XMasterPagesSupplier;
36 
37 import com.sun.star.presentation.XPresentationPage;
38 import com.sun.star.presentation.XHandoutMasterSupplier;
39 
40 public class PageUtil {
41 	/**
42 	 * Get the page count for standard pages
43 	 *
44 	 * @param xComponent
45 	 *            : The presentation document
46 	 * @return slide count
47 	 */
48 	static public int getDrawPageCount(XComponent xComponent) {
49 		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
50 				.queryInterface(XDrawPagesSupplier.class, xComponent);
51 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
52 		return xDrawPages.getCount();
53 	}
54 
55 	/**
56 	 * Get draw page by index
57 	 *
58 	 * @param xComponent
59 	 *            : The presentation document
60 	 * @param nIndex
61 	 *            : index of slide pages, from 0 or 1?
62 	 * @return Page of corresponding index.
63 	 */
64 	static public XDrawPage getDrawPageByIndex(XComponent xComponent, int nIndex)
65 			throws com.sun.star.lang.IndexOutOfBoundsException,
66 			com.sun.star.lang.WrappedTargetException {
67 		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
68 				.queryInterface(XDrawPagesSupplier.class, xComponent);
69 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
70 		return (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class,
71 				xDrawPages.getByIndex(nIndex));
72 	}
73 
74 	/**
75 	 * Create and insert a draw page into the giving position,the method returns
76 	 * the new created page
77 	 *
78 	 * @param xComponent
79 	 *            :The Presentation Document
80 	 * @param nIndex
81 	 *            :The index at which page will be inserted.
82 	 * @return The newly created page.
83 	 */
84 	static public XDrawPage insertNewDrawPageByIndex(XComponent xComponent,
85 			int nIndex) throws Exception {
86 		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
87 				.queryInterface(XDrawPagesSupplier.class, xComponent);
88 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
89 		return xDrawPages.insertNewByIndex(nIndex);
90 	}
91 
92 	/**
93 	 * Remove the given page
94 	 *
95 	 * @param xComponent
96 	 *            : The Presentation Document
97 	 * @param xDrawPage
98 	 *            : The page want to be removed.
99 	 */
100 	static public void removeDrawPage(XComponent xComponent, XDrawPage xDrawPage) {
101 		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
102 				.queryInterface(XDrawPagesSupplier.class, xComponent);
103 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
104 		xDrawPages.remove(xDrawPage);
105 	}
106 
107 	/**
108 	 * Get size of the given page
109 	 *
110 	 * @param xDrawPage
111 	 *            : The specified target page
112 	 * @return specifies the 2-dimensional size of the page using width and
113 	 *         height.
114 	 */
115 	static public Size getPageSize(XDrawPage xDrawPage)
116 			throws com.sun.star.beans.UnknownPropertyException,
117 			com.sun.star.lang.WrappedTargetException {
118 		XPropertySet xPageProperties = (XPropertySet) UnoRuntime
119 				.queryInterface(XPropertySet.class, xDrawPage);
120 		return new Size(
121 				((Integer) xPageProperties.getPropertyValue("Width"))
122 						.intValue(),
123 				((Integer) xPageProperties.getPropertyValue("Height"))
124 						.intValue());
125 	}
126 
127 	/**
128 	 * Get the page count for master pages
129 	 *
130 	 * @param xComponent
131 	 *            : The presentation document
132 	 * @return Count of master pages.
133 	 */
134 	static public int getMasterPageCount(XComponent xComponent) {
135 		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
136 				.queryInterface(XMasterPagesSupplier.class, xComponent);
137 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
138 		return xDrawPages.getCount();
139 	}
140 
141 	/**
142 	 * Get master page by index
143 	 *
144 	 * @param xComponent
145 	 *            : The Presentation document
146 	 * @param nIndex
147 	 *            : Index of target master page.
148 	 * @return Page of
149 	 */
150 	static public XDrawPage getMasterPageByIndex(XComponent xComponent,
151 			int nIndex) throws com.sun.star.lang.IndexOutOfBoundsException,
152 			com.sun.star.lang.WrappedTargetException {
153 		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
154 				.queryInterface(XMasterPagesSupplier.class, xComponent);
155 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
156 		return (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class,
157 				xDrawPages.getByIndex(nIndex));
158 	}
159 
160 	/**
161 	 * creates and inserts a new master page into the giving position, the
162 	 * method returns the new created page
163 	 */
164 	static public XDrawPage insertNewMasterPageByIndex(XComponent xComponent,
165 			int nIndex) {
166 		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
167 				.queryInterface(XMasterPagesSupplier.class, xComponent);
168 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
169 		return xDrawPages.insertNewByIndex(nIndex);
170 	}
171 
172 	/**
173 	 * removes the given page
174 	 */
175 	static public void removeMasterPage(XComponent xComponent,
176 			XDrawPage xDrawPage) {
177 		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
178 				.queryInterface(XMasterPagesSupplier.class, xComponent);
179 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
180 		xDrawPages.remove(xDrawPage);
181 	}
182 
183 	/**
184 	 * return the corresponding masterpage for the giving drawpage
185 	 */
186 	static public XDrawPage getMasterPage(XDrawPage xDrawPage) {
187 		XMasterPageTarget xMasterPageTarget = (XMasterPageTarget) UnoRuntime
188 				.queryInterface(XMasterPageTarget.class, xDrawPage);
189 		return xMasterPageTarget.getMasterPage();
190 	}
191 
192 	/**
193 	 * sets given masterpage at the drawpage
194 	 */
195 	static public void setMasterPage(XDrawPage xDrawPage, XDrawPage xMasterPage) {
196 		XMasterPageTarget xMasterPageTarget = (XMasterPageTarget) UnoRuntime
197 				.queryInterface(XMasterPageTarget.class, xDrawPage);
198 		xMasterPageTarget.setMasterPage(xMasterPage);
199 	}
200 
201 	// __________ presentation pages __________
202 
203 	/**
204 	 * test if a Presentation Document is supported. This is important, because
205 	 * only presentation documents have notes and handout pages
206 	 */
207 	static public boolean isImpressDocument(XComponent xComponent) {
208 		XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface(
209 				XServiceInfo.class, xComponent);
210 		return xInfo
211 				.supportsService("com.sun.star.presentation.PresentationDocument");
212 	}
213 
214 	/**
215 	 * in impress documents each normal draw page has a corresponding notes page
216 	 */
217 	static public XDrawPage getNotesPage(XDrawPage xDrawPage) {
218 		XPresentationPage aPresentationPage = (XPresentationPage) UnoRuntime
219 				.queryInterface(XPresentationPage.class, xDrawPage);
220 		return aPresentationPage.getNotesPage();
221 	}
222 
223 	/**
224 	 * in impress each documents has one handout page
225 	 */
226 	static public XDrawPage getHandoutMasterPage(XComponent xComponent) {
227 		XHandoutMasterSupplier aHandoutMasterSupplier = (XHandoutMasterSupplier) UnoRuntime
228 				.queryInterface(XHandoutMasterSupplier.class, xComponent);
229 		return aHandoutMasterSupplier.getHandoutMasterPage();
230 	}
231 }
232