Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Drawing / PageHelper.java
blobcbf8d9cdcfd374688d7e56ef1e692026c4f3cc2c
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
35 // __________ Imports __________
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.lang.XComponent;
39 import com.sun.star.lang.XServiceInfo;
41 import com.sun.star.awt.Size;
43 import com.sun.star.beans.XPropertySet;
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;
51 import com.sun.star.presentation.XPresentationPage;
52 import com.sun.star.presentation.XHandoutMasterSupplier;
55 public class PageHelper
57 // __________ static helper methods __________
59 // __________ draw pages __________
61 /** get the page count for standard pages
63 static public int getDrawPageCount( XComponent xComponent )
65 XDrawPagesSupplier xDrawPagesSupplier =
66 UnoRuntime.queryInterface(
67 XDrawPagesSupplier.class, xComponent );
68 XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
69 return xDrawPages.getCount();
72 /** get draw page by index
74 static public XDrawPage getDrawPageByIndex( XComponent xComponent, int nIndex )
75 throws com.sun.star.lang.IndexOutOfBoundsException,
76 com.sun.star.lang.WrappedTargetException
78 XDrawPagesSupplier xDrawPagesSupplier =
79 UnoRuntime.queryInterface(
80 XDrawPagesSupplier.class, xComponent );
81 XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
82 return UnoRuntime.queryInterface(XDrawPage.class, xDrawPages.getByIndex( nIndex ));
85 /** creates and inserts a draw page into the giving position,
86 the method returns the new created page
88 static public XDrawPage insertNewDrawPageByIndex( XComponent xComponent, int nIndex )
89 throws Exception
91 XDrawPagesSupplier xDrawPagesSupplier =
92 UnoRuntime.queryInterface(
93 XDrawPagesSupplier.class, xComponent );
94 XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
95 return xDrawPages.insertNewByIndex( nIndex );
100 /** get size of the given page
102 static public Size getPageSize( XDrawPage xDrawPage )
103 throws com.sun.star.beans.UnknownPropertyException,
104 com.sun.star.lang.WrappedTargetException
106 XPropertySet xPageProperties = UnoRuntime.queryInterface( XPropertySet.class, xDrawPage );
107 return new Size(
108 ((Integer)xPageProperties.getPropertyValue( "Width" )).intValue(),
109 ((Integer)xPageProperties.getPropertyValue( "Height" )).intValue() );
112 // __________ master pages __________
114 /** get the page count for master pages
116 static public int getMasterPageCount( XComponent xComponent )
118 XMasterPagesSupplier xMasterPagesSupplier =
119 UnoRuntime.queryInterface(
120 XMasterPagesSupplier.class, xComponent );
121 XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
122 return xDrawPages.getCount();
125 /** get master page by index
127 static public XDrawPage getMasterPageByIndex( XComponent xComponent, int nIndex )
128 throws com.sun.star.lang.IndexOutOfBoundsException,
129 com.sun.star.lang.WrappedTargetException
131 XMasterPagesSupplier xMasterPagesSupplier =
132 UnoRuntime.queryInterface(
133 XMasterPagesSupplier.class, xComponent );
134 XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
135 return UnoRuntime.queryInterface(XDrawPage.class, xDrawPages.getByIndex( nIndex ));
138 /** creates and inserts a new master page into the giving position,
139 the method returns the new created page
141 static public XDrawPage insertNewMasterPageByIndex( XComponent xComponent, int nIndex )
143 XMasterPagesSupplier xMasterPagesSupplier =
144 UnoRuntime.queryInterface(
145 XMasterPagesSupplier.class, xComponent );
146 XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
147 return xDrawPages.insertNewByIndex( nIndex );
154 /** sets given masterpage at the drawpage
156 static public void setMasterPage( XDrawPage xDrawPage, XDrawPage xMasterPage )
158 XMasterPageTarget xMasterPageTarget =
159 UnoRuntime.queryInterface(
160 XMasterPageTarget.class, xDrawPage );
161 xMasterPageTarget.setMasterPage( xMasterPage );
164 // __________ presentation pages __________
166 /** test if a Presentation Document is supported.
167 This is important, because only presentation documents
168 have notes and handout pages
170 static public boolean isImpressDocument( XComponent xComponent )
172 XServiceInfo xInfo = UnoRuntime.queryInterface(
173 XServiceInfo.class, xComponent );
174 return xInfo.supportsService( "com.sun.star.presentation.PresentationDocument" );
177 /** in impress documents each normal draw page has a corresponding notes page
179 static public XDrawPage getNotesPage( XDrawPage xDrawPage )
181 XPresentationPage aPresentationPage =
182 UnoRuntime.queryInterface(
183 XPresentationPage.class, xDrawPage );
184 return aPresentationPage.getNotesPage();
187 /** in impress each documents has one handout page
189 static public XDrawPage getHandoutMasterPage( XComponent xComponent )
191 XHandoutMasterSupplier aHandoutMasterSupplier =
192 UnoRuntime.queryInterface(
193 XHandoutMasterSupplier.class, xComponent );
194 return aHandoutMasterSupplier.getHandoutMasterPage();