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