1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // __________ Imports __________
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
27cdf0e10cSrcweir import com.sun.star.lang.XComponent;
28cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.awt.Size;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
35cdf0e10cSrcweir import com.sun.star.drawing.XDrawPages;
36cdf0e10cSrcweir import com.sun.star.drawing.XDrawPagesSupplier;
37cdf0e10cSrcweir import com.sun.star.drawing.XMasterPageTarget;
38cdf0e10cSrcweir import com.sun.star.drawing.XMasterPagesSupplier;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir import com.sun.star.presentation.XPresentationPage;
41cdf0e10cSrcweir import com.sun.star.presentation.XHandoutMasterSupplier;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 
44cdf0e10cSrcweir public class PageHelper
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     // __________ static helper methods __________
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     // __________ draw pages __________
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     /** get the page count for standard pages
51cdf0e10cSrcweir 	*/
getDrawPageCount( XComponent xComponent )52cdf0e10cSrcweir 	static public int getDrawPageCount( XComponent xComponent )
53cdf0e10cSrcweir 	{
54cdf0e10cSrcweir 		XDrawPagesSupplier xDrawPagesSupplier =
55cdf0e10cSrcweir 			(XDrawPagesSupplier)UnoRuntime.queryInterface(
56cdf0e10cSrcweir 				XDrawPagesSupplier.class, xComponent );
57cdf0e10cSrcweir 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
58cdf0e10cSrcweir 		return xDrawPages.getCount();
59cdf0e10cSrcweir 	}
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     /** get draw page by index
62cdf0e10cSrcweir 	*/
getDrawPageByIndex( XComponent xComponent, int nIndex )63cdf0e10cSrcweir 	static public XDrawPage getDrawPageByIndex( XComponent xComponent, int nIndex )
64cdf0e10cSrcweir 		throws com.sun.star.lang.IndexOutOfBoundsException,
65cdf0e10cSrcweir 			com.sun.star.lang.WrappedTargetException
66cdf0e10cSrcweir 	{
67cdf0e10cSrcweir 		XDrawPagesSupplier xDrawPagesSupplier =
68cdf0e10cSrcweir 			(XDrawPagesSupplier)UnoRuntime.queryInterface(
69cdf0e10cSrcweir 				XDrawPagesSupplier.class, xComponent );
70cdf0e10cSrcweir 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
71cdf0e10cSrcweir 		return (XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, xDrawPages.getByIndex( nIndex ));
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     /** creates and inserts a draw page into the giving position,
75cdf0e10cSrcweir 		the method returns the new created page
76cdf0e10cSrcweir 	*/
insertNewDrawPageByIndex( XComponent xComponent, int nIndex )77cdf0e10cSrcweir 	static public XDrawPage insertNewDrawPageByIndex( XComponent xComponent, int nIndex )
78cdf0e10cSrcweir 		throws Exception
79cdf0e10cSrcweir 	{
80cdf0e10cSrcweir 		XDrawPagesSupplier xDrawPagesSupplier =
81cdf0e10cSrcweir 			(XDrawPagesSupplier)UnoRuntime.queryInterface(
82cdf0e10cSrcweir 				XDrawPagesSupplier.class, xComponent );
83cdf0e10cSrcweir 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
84cdf0e10cSrcweir 		return xDrawPages.insertNewByIndex( nIndex );
85cdf0e10cSrcweir 	}
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     /** removes the given page
88cdf0e10cSrcweir 	*/
removeDrawPage( XComponent xComponent, XDrawPage xDrawPage )89cdf0e10cSrcweir 	static public void removeDrawPage( XComponent xComponent, XDrawPage xDrawPage )
90cdf0e10cSrcweir 	{
91cdf0e10cSrcweir 		XDrawPagesSupplier xDrawPagesSupplier =
92cdf0e10cSrcweir 			(XDrawPagesSupplier)UnoRuntime.queryInterface(
93cdf0e10cSrcweir 				XDrawPagesSupplier.class, xComponent );
94cdf0e10cSrcweir 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
95cdf0e10cSrcweir 		xDrawPages.remove( xDrawPage );
96cdf0e10cSrcweir 	}
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	/** get size of the given page
99cdf0e10cSrcweir 	*/
getPageSize( XDrawPage xDrawPage )100cdf0e10cSrcweir 	static public Size getPageSize( XDrawPage xDrawPage )
101cdf0e10cSrcweir 		throws com.sun.star.beans.UnknownPropertyException,
102cdf0e10cSrcweir 			com.sun.star.lang.WrappedTargetException
103cdf0e10cSrcweir 	{
104cdf0e10cSrcweir 		XPropertySet xPageProperties = (XPropertySet)
105cdf0e10cSrcweir 			UnoRuntime.queryInterface( XPropertySet.class, xDrawPage );
106cdf0e10cSrcweir 		return new Size(
107cdf0e10cSrcweir 			((Integer)xPageProperties.getPropertyValue( "Width" )).intValue(),
108cdf0e10cSrcweir 			((Integer)xPageProperties.getPropertyValue( "Height" )).intValue() );
109cdf0e10cSrcweir 	}
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     // __________ master pages __________
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     /** get the page count for master pages
114cdf0e10cSrcweir 	*/
getMasterPageCount( XComponent xComponent )115cdf0e10cSrcweir 	static public int getMasterPageCount( XComponent xComponent )
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir 		XMasterPagesSupplier xMasterPagesSupplier =
118cdf0e10cSrcweir 			(XMasterPagesSupplier)UnoRuntime.queryInterface(
119cdf0e10cSrcweir 				XMasterPagesSupplier.class, xComponent );
120cdf0e10cSrcweir 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
121cdf0e10cSrcweir 		return xDrawPages.getCount();
122cdf0e10cSrcweir 	}
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     /** get master page by index
125cdf0e10cSrcweir 	*/
getMasterPageByIndex( XComponent xComponent, int nIndex )126cdf0e10cSrcweir 	static public XDrawPage getMasterPageByIndex( XComponent xComponent, int nIndex )
127cdf0e10cSrcweir 		throws com.sun.star.lang.IndexOutOfBoundsException,
128cdf0e10cSrcweir 			com.sun.star.lang.WrappedTargetException
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		XMasterPagesSupplier xMasterPagesSupplier =
131cdf0e10cSrcweir 			(XMasterPagesSupplier)UnoRuntime.queryInterface(
132cdf0e10cSrcweir 				XMasterPagesSupplier.class, xComponent );
133cdf0e10cSrcweir 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
134cdf0e10cSrcweir 		return (XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, xDrawPages.getByIndex( nIndex ));
135cdf0e10cSrcweir 	}
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     /** creates and inserts a new master page into the giving position,
138cdf0e10cSrcweir 		the method returns the new created page
139cdf0e10cSrcweir 	*/
insertNewMasterPageByIndex( XComponent xComponent, int nIndex )140cdf0e10cSrcweir 	static public XDrawPage insertNewMasterPageByIndex( XComponent xComponent, int nIndex )
141cdf0e10cSrcweir 	{
142cdf0e10cSrcweir 		XMasterPagesSupplier xMasterPagesSupplier =
143cdf0e10cSrcweir 			(XMasterPagesSupplier)UnoRuntime.queryInterface(
144cdf0e10cSrcweir 				XMasterPagesSupplier.class, xComponent );
145cdf0e10cSrcweir 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
146cdf0e10cSrcweir 		return xDrawPages.insertNewByIndex( nIndex );
147cdf0e10cSrcweir 	}
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     /** removes the given page
150cdf0e10cSrcweir 	*/
removeMasterPage( XComponent xComponent, XDrawPage xDrawPage )151cdf0e10cSrcweir 	static public void removeMasterPage( XComponent xComponent, XDrawPage xDrawPage )
152cdf0e10cSrcweir 	{
153cdf0e10cSrcweir 		XMasterPagesSupplier xMasterPagesSupplier =
154cdf0e10cSrcweir 			(XMasterPagesSupplier)UnoRuntime.queryInterface(
155cdf0e10cSrcweir 				XMasterPagesSupplier.class, xComponent );
156cdf0e10cSrcweir 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
157cdf0e10cSrcweir 		xDrawPages.remove( xDrawPage );
158cdf0e10cSrcweir 	}
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     /** return the corresponding masterpage for the giving drawpage
161cdf0e10cSrcweir 	*/
getMasterPage( XDrawPage xDrawPage )162cdf0e10cSrcweir 	static public XDrawPage getMasterPage( XDrawPage xDrawPage )
163cdf0e10cSrcweir 	{
164cdf0e10cSrcweir 		XMasterPageTarget xMasterPageTarget =
165cdf0e10cSrcweir 			(XMasterPageTarget)UnoRuntime.queryInterface(
166cdf0e10cSrcweir 				XMasterPageTarget.class, xDrawPage );
167cdf0e10cSrcweir 		return xMasterPageTarget.getMasterPage();
168cdf0e10cSrcweir 	}
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     /** sets given masterpage at the drawpage
171cdf0e10cSrcweir 	*/
setMasterPage( XDrawPage xDrawPage, XDrawPage xMasterPage )172cdf0e10cSrcweir 	static public void setMasterPage( XDrawPage xDrawPage, XDrawPage xMasterPage )
173cdf0e10cSrcweir 	{
174cdf0e10cSrcweir 		XMasterPageTarget xMasterPageTarget =
175cdf0e10cSrcweir 			(XMasterPageTarget)UnoRuntime.queryInterface(
176cdf0e10cSrcweir 				XMasterPageTarget.class, xDrawPage );
177cdf0e10cSrcweir 		xMasterPageTarget.setMasterPage( xMasterPage );
178cdf0e10cSrcweir 	}
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     // __________ presentation pages __________
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 	/** test if a Presentation Document is supported.
183cdf0e10cSrcweir 		This is important, because only presentation documents
184cdf0e10cSrcweir 		have notes and handout pages
185cdf0e10cSrcweir 	*/
isImpressDocument( XComponent xComponent )186cdf0e10cSrcweir 	static public boolean isImpressDocument( XComponent xComponent )
187cdf0e10cSrcweir 	{
188cdf0e10cSrcweir 		XServiceInfo xInfo = (XServiceInfo)UnoRuntime.queryInterface(
189cdf0e10cSrcweir 				XServiceInfo.class, xComponent );
190cdf0e10cSrcweir 		return xInfo.supportsService( "com.sun.star.presentation.PresentationDocument" );
191cdf0e10cSrcweir 	}
192cdf0e10cSrcweir 
193cdf0e10cSrcweir     /** in impress documents each normal draw page has a corresponding notes page
194cdf0e10cSrcweir 	*/
getNotesPage( XDrawPage xDrawPage )195cdf0e10cSrcweir 	static public XDrawPage getNotesPage( XDrawPage xDrawPage )
196cdf0e10cSrcweir 	{
197cdf0e10cSrcweir 		XPresentationPage aPresentationPage =
198cdf0e10cSrcweir 			(XPresentationPage)UnoRuntime.queryInterface(
199cdf0e10cSrcweir 				XPresentationPage.class, xDrawPage );
200cdf0e10cSrcweir 		return aPresentationPage.getNotesPage();
201cdf0e10cSrcweir 	}
202cdf0e10cSrcweir 
203cdf0e10cSrcweir     /** in impress each documents has one handout page
204cdf0e10cSrcweir 	*/
getHandoutMasterPage( XComponent xComponent )205cdf0e10cSrcweir 	static public XDrawPage getHandoutMasterPage( XComponent xComponent )
206cdf0e10cSrcweir 	{
207cdf0e10cSrcweir 		XHandoutMasterSupplier aHandoutMasterSupplier =
208cdf0e10cSrcweir 			(XHandoutMasterSupplier)UnoRuntime.queryInterface(
209cdf0e10cSrcweir 				XHandoutMasterSupplier.class, xComponent );
210cdf0e10cSrcweir 		return aHandoutMasterSupplier.getHandoutMasterPage();
211cdf0e10cSrcweir 	}
212cdf0e10cSrcweir }
213