1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir // __________ Imports __________
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
39*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir import com.sun.star.awt.Size;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
46*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPages;
47*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPagesSupplier;
48*cdf0e10cSrcweir import com.sun.star.drawing.XMasterPageTarget;
49*cdf0e10cSrcweir import com.sun.star.drawing.XMasterPagesSupplier;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir import com.sun.star.presentation.XPresentationPage;
52*cdf0e10cSrcweir import com.sun.star.presentation.XHandoutMasterSupplier;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir public class PageHelper
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir     // __________ static helper methods __________
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     // __________ draw pages __________
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     /** get the page count for standard pages
62*cdf0e10cSrcweir 	*/
63*cdf0e10cSrcweir 	static public int getDrawPageCount( XComponent xComponent )
64*cdf0e10cSrcweir 	{
65*cdf0e10cSrcweir 		XDrawPagesSupplier xDrawPagesSupplier =
66*cdf0e10cSrcweir 			(XDrawPagesSupplier)UnoRuntime.queryInterface(
67*cdf0e10cSrcweir 				XDrawPagesSupplier.class, xComponent );
68*cdf0e10cSrcweir 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
69*cdf0e10cSrcweir 		return xDrawPages.getCount();
70*cdf0e10cSrcweir 	}
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     /** get draw page by index
73*cdf0e10cSrcweir 	*/
74*cdf0e10cSrcweir 	static public XDrawPage getDrawPageByIndex( XComponent xComponent, int nIndex )
75*cdf0e10cSrcweir 		throws com.sun.star.lang.IndexOutOfBoundsException,
76*cdf0e10cSrcweir 			com.sun.star.lang.WrappedTargetException
77*cdf0e10cSrcweir 	{
78*cdf0e10cSrcweir 		XDrawPagesSupplier xDrawPagesSupplier =
79*cdf0e10cSrcweir 			(XDrawPagesSupplier)UnoRuntime.queryInterface(
80*cdf0e10cSrcweir 				XDrawPagesSupplier.class, xComponent );
81*cdf0e10cSrcweir 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
82*cdf0e10cSrcweir 		return (XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, xDrawPages.getByIndex( nIndex ));
83*cdf0e10cSrcweir 	}
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     /** creates and inserts a draw page into the giving position,
86*cdf0e10cSrcweir 		the method returns the new created page
87*cdf0e10cSrcweir 	*/
88*cdf0e10cSrcweir 	static public XDrawPage insertNewDrawPageByIndex( XComponent xComponent, int nIndex )
89*cdf0e10cSrcweir 		throws Exception
90*cdf0e10cSrcweir 	{
91*cdf0e10cSrcweir 		XDrawPagesSupplier xDrawPagesSupplier =
92*cdf0e10cSrcweir 			(XDrawPagesSupplier)UnoRuntime.queryInterface(
93*cdf0e10cSrcweir 				XDrawPagesSupplier.class, xComponent );
94*cdf0e10cSrcweir 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
95*cdf0e10cSrcweir 		return xDrawPages.insertNewByIndex( nIndex );
96*cdf0e10cSrcweir 	}
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     /** removes the given page
99*cdf0e10cSrcweir 	*/
100*cdf0e10cSrcweir 	static public void removeDrawPage( XComponent xComponent, XDrawPage xDrawPage )
101*cdf0e10cSrcweir 	{
102*cdf0e10cSrcweir 		XDrawPagesSupplier xDrawPagesSupplier =
103*cdf0e10cSrcweir 			(XDrawPagesSupplier)UnoRuntime.queryInterface(
104*cdf0e10cSrcweir 				XDrawPagesSupplier.class, xComponent );
105*cdf0e10cSrcweir 		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
106*cdf0e10cSrcweir 		xDrawPages.remove( xDrawPage );
107*cdf0e10cSrcweir 	}
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	/** get size of the given page
110*cdf0e10cSrcweir 	*/
111*cdf0e10cSrcweir 	static public Size getPageSize( XDrawPage xDrawPage )
112*cdf0e10cSrcweir 		throws com.sun.star.beans.UnknownPropertyException,
113*cdf0e10cSrcweir 			com.sun.star.lang.WrappedTargetException
114*cdf0e10cSrcweir 	{
115*cdf0e10cSrcweir 		XPropertySet xPageProperties = (XPropertySet)
116*cdf0e10cSrcweir 			UnoRuntime.queryInterface( XPropertySet.class, xDrawPage );
117*cdf0e10cSrcweir 		return new Size(
118*cdf0e10cSrcweir 			((Integer)xPageProperties.getPropertyValue( "Width" )).intValue(),
119*cdf0e10cSrcweir 			((Integer)xPageProperties.getPropertyValue( "Height" )).intValue() );
120*cdf0e10cSrcweir 	}
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     // __________ master pages __________
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     /** get the page count for master pages
125*cdf0e10cSrcweir 	*/
126*cdf0e10cSrcweir 	static public int getMasterPageCount( XComponent xComponent )
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		XMasterPagesSupplier xMasterPagesSupplier =
129*cdf0e10cSrcweir 			(XMasterPagesSupplier)UnoRuntime.queryInterface(
130*cdf0e10cSrcweir 				XMasterPagesSupplier.class, xComponent );
131*cdf0e10cSrcweir 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
132*cdf0e10cSrcweir 		return xDrawPages.getCount();
133*cdf0e10cSrcweir 	}
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     /** get master page by index
136*cdf0e10cSrcweir 	*/
137*cdf0e10cSrcweir 	static public XDrawPage getMasterPageByIndex( XComponent xComponent, int nIndex )
138*cdf0e10cSrcweir 		throws com.sun.star.lang.IndexOutOfBoundsException,
139*cdf0e10cSrcweir 			com.sun.star.lang.WrappedTargetException
140*cdf0e10cSrcweir 	{
141*cdf0e10cSrcweir 		XMasterPagesSupplier xMasterPagesSupplier =
142*cdf0e10cSrcweir 			(XMasterPagesSupplier)UnoRuntime.queryInterface(
143*cdf0e10cSrcweir 				XMasterPagesSupplier.class, xComponent );
144*cdf0e10cSrcweir 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
145*cdf0e10cSrcweir 		return (XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, xDrawPages.getByIndex( nIndex ));
146*cdf0e10cSrcweir 	}
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     /** creates and inserts a new master page into the giving position,
149*cdf0e10cSrcweir 		the method returns the new created page
150*cdf0e10cSrcweir 	*/
151*cdf0e10cSrcweir 	static public XDrawPage insertNewMasterPageByIndex( XComponent xComponent, int nIndex )
152*cdf0e10cSrcweir 	{
153*cdf0e10cSrcweir 		XMasterPagesSupplier xMasterPagesSupplier =
154*cdf0e10cSrcweir 			(XMasterPagesSupplier)UnoRuntime.queryInterface(
155*cdf0e10cSrcweir 				XMasterPagesSupplier.class, xComponent );
156*cdf0e10cSrcweir 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
157*cdf0e10cSrcweir 		return xDrawPages.insertNewByIndex( nIndex );
158*cdf0e10cSrcweir 	}
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     /** removes the given page
161*cdf0e10cSrcweir 	*/
162*cdf0e10cSrcweir 	static public void removeMasterPage( XComponent xComponent, XDrawPage xDrawPage )
163*cdf0e10cSrcweir 	{
164*cdf0e10cSrcweir 		XMasterPagesSupplier xMasterPagesSupplier =
165*cdf0e10cSrcweir 			(XMasterPagesSupplier)UnoRuntime.queryInterface(
166*cdf0e10cSrcweir 				XMasterPagesSupplier.class, xComponent );
167*cdf0e10cSrcweir 		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
168*cdf0e10cSrcweir 		xDrawPages.remove( xDrawPage );
169*cdf0e10cSrcweir 	}
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     /** return the corresponding masterpage for the giving drawpage
172*cdf0e10cSrcweir 	*/
173*cdf0e10cSrcweir 	static public XDrawPage getMasterPage( XDrawPage xDrawPage )
174*cdf0e10cSrcweir 	{
175*cdf0e10cSrcweir 		XMasterPageTarget xMasterPageTarget =
176*cdf0e10cSrcweir 			(XMasterPageTarget)UnoRuntime.queryInterface(
177*cdf0e10cSrcweir 				XMasterPageTarget.class, xDrawPage );
178*cdf0e10cSrcweir 		return xMasterPageTarget.getMasterPage();
179*cdf0e10cSrcweir 	}
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     /** sets given masterpage at the drawpage
182*cdf0e10cSrcweir 	*/
183*cdf0e10cSrcweir 	static public void setMasterPage( XDrawPage xDrawPage, XDrawPage xMasterPage )
184*cdf0e10cSrcweir 	{
185*cdf0e10cSrcweir 		XMasterPageTarget xMasterPageTarget =
186*cdf0e10cSrcweir 			(XMasterPageTarget)UnoRuntime.queryInterface(
187*cdf0e10cSrcweir 				XMasterPageTarget.class, xDrawPage );
188*cdf0e10cSrcweir 		xMasterPageTarget.setMasterPage( xMasterPage );
189*cdf0e10cSrcweir 	}
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     // __________ presentation pages __________
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 	/** test if a Presentation Document is supported.
194*cdf0e10cSrcweir 		This is important, because only presentation documents
195*cdf0e10cSrcweir 		have notes and handout pages
196*cdf0e10cSrcweir 	*/
197*cdf0e10cSrcweir 	static public boolean isImpressDocument( XComponent xComponent )
198*cdf0e10cSrcweir 	{
199*cdf0e10cSrcweir 		XServiceInfo xInfo = (XServiceInfo)UnoRuntime.queryInterface(
200*cdf0e10cSrcweir 				XServiceInfo.class, xComponent );
201*cdf0e10cSrcweir 		return xInfo.supportsService( "com.sun.star.presentation.PresentationDocument" );
202*cdf0e10cSrcweir 	}
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     /** in impress documents each normal draw page has a corresponding notes page
205*cdf0e10cSrcweir 	*/
206*cdf0e10cSrcweir 	static public XDrawPage getNotesPage( XDrawPage xDrawPage )
207*cdf0e10cSrcweir 	{
208*cdf0e10cSrcweir 		XPresentationPage aPresentationPage =
209*cdf0e10cSrcweir 			(XPresentationPage)UnoRuntime.queryInterface(
210*cdf0e10cSrcweir 				XPresentationPage.class, xDrawPage );
211*cdf0e10cSrcweir 		return aPresentationPage.getNotesPage();
212*cdf0e10cSrcweir 	}
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     /** in impress each documents has one handout page
215*cdf0e10cSrcweir 	*/
216*cdf0e10cSrcweir 	static public XDrawPage getHandoutMasterPage( XComponent xComponent )
217*cdf0e10cSrcweir 	{
218*cdf0e10cSrcweir 		XHandoutMasterSupplier aHandoutMasterSupplier =
219*cdf0e10cSrcweir 			(XHandoutMasterSupplier)UnoRuntime.queryInterface(
220*cdf0e10cSrcweir 				XHandoutMasterSupplier.class, xComponent );
221*cdf0e10cSrcweir 		return aHandoutMasterSupplier.getHandoutMasterPage();
222*cdf0e10cSrcweir 	}
223*cdf0e10cSrcweir }
224