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