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