xref: /AOO41X/main/odk/examples/DevelopersGuide/Forms/DocumentHelper.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir /**************************************************************************/
29*cdf0e10cSrcweir import com.sun.star.uno.*;
30*cdf0e10cSrcweir import com.sun.star.lang.*;
31*cdf0e10cSrcweir import com.sun.star.util.*;
32*cdf0e10cSrcweir import com.sun.star.awt.*;
33*cdf0e10cSrcweir import com.sun.star.drawing.*;
34*cdf0e10cSrcweir import com.sun.star.frame.*;
35*cdf0e10cSrcweir import com.sun.star.form.*;
36*cdf0e10cSrcweir import com.sun.star.beans.*;
37*cdf0e10cSrcweir import com.sun.star.container.*;
38*cdf0e10cSrcweir import com.sun.star.container.*;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir /**************************************************************************/
41*cdf0e10cSrcweir /** provides a small wrapper around a document
42*cdf0e10cSrcweir */
43*cdf0e10cSrcweir public class DocumentHelper
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir     /// the remote office context
46*cdf0e10cSrcweir     protected XComponentContext         m_remoteContext;
47*cdf0e10cSrcweir     /// the remote service manager
48*cdf0e10cSrcweir     protected XMultiServiceFactory      m_orb;
49*cdf0e10cSrcweir     protected XComponent                m_documentComponent;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
52*cdf0e10cSrcweir     public XComponent getDocument( )
53*cdf0e10cSrcweir     {
54*cdf0e10cSrcweir         return m_documentComponent;
55*cdf0e10cSrcweir     }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
58*cdf0e10cSrcweir     public XComponentContext getContext( )
59*cdf0e10cSrcweir     {
60*cdf0e10cSrcweir         return m_remoteContext;
61*cdf0e10cSrcweir     }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
64*cdf0e10cSrcweir     public XMultiServiceFactory getOrb( )
65*cdf0e10cSrcweir     {
66*cdf0e10cSrcweir         return m_orb;
67*cdf0e10cSrcweir     }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
70*cdf0e10cSrcweir     public DocumentHelper( XComponentContext xContext, XComponent document )
71*cdf0e10cSrcweir     {
72*cdf0e10cSrcweir         m_remoteContext = xContext;
73*cdf0e10cSrcweir         m_orb = (XMultiServiceFactory)UnoRuntime.queryInterface(
74*cdf0e10cSrcweir             XMultiServiceFactory.class, m_remoteContext.getServiceManager());
75*cdf0e10cSrcweir         m_documentComponent = document;
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
79*cdf0e10cSrcweir     protected static XComponent implCreateBlankDocument( XComponentContext xCtx, String factoryURL ) throws com.sun.star.uno.Exception
80*cdf0e10cSrcweir     {
81*cdf0e10cSrcweir         XComponentLoader aLoader = (XComponentLoader)UnoRuntime.queryInterface(
82*cdf0e10cSrcweir             XComponentLoader.class,
83*cdf0e10cSrcweir             xCtx.getServiceManager().createInstanceWithContext(
84*cdf0e10cSrcweir                 "com.sun.star.frame.Desktop", xCtx ));
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir         return UNO.queryComponent(
87*cdf0e10cSrcweir             aLoader.loadComponentFromURL( factoryURL, "_blank", 0, new PropertyValue[ 0 ] )
88*cdf0e10cSrcweir         );
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
92*cdf0e10cSrcweir     public static DocumentHelper blankTextDocument( XComponentContext xCtx ) throws com.sun.star.uno.Exception
93*cdf0e10cSrcweir     {
94*cdf0e10cSrcweir         return blankDocument( xCtx, DocumentType.WRITER );
95*cdf0e10cSrcweir     }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
98*cdf0e10cSrcweir     public static DocumentHelper blankDocument( XComponentContext xCtx, DocumentType eType ) throws com.sun.star.uno.Exception
99*cdf0e10cSrcweir     {
100*cdf0e10cSrcweir         XComponent document = implCreateBlankDocument( xCtx, getDocumentFactoryURL( eType ) );
101*cdf0e10cSrcweir         if ( eType == DocumentType.CALC )
102*cdf0e10cSrcweir             return new SpreadsheetDocument( xCtx, document );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         return new DocumentHelper( xCtx, document );
105*cdf0e10cSrcweir     }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
108*cdf0e10cSrcweir     /** retrieves the current view of the document
109*cdf0e10cSrcweir         @return
110*cdf0e10cSrcweir             the view component, queried for the interface described by aInterfaceClass
111*cdf0e10cSrcweir     */
112*cdf0e10cSrcweir     public DocumentViewHelper getCurrentView( )
113*cdf0e10cSrcweir     {
114*cdf0e10cSrcweir         // get the model interface for the document
115*cdf0e10cSrcweir         XModel xDocModel = (XModel)UnoRuntime.queryInterface(XModel.class, m_documentComponent );
116*cdf0e10cSrcweir         // get the current controller for the document - as a controller is tied to a view,
117*cdf0e10cSrcweir         // this gives us the currently active view for the document.
118*cdf0e10cSrcweir         XController xController = xDocModel.getCurrentController();
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         if ( classify() == DocumentType.CALC )
121*cdf0e10cSrcweir             return new SpreadsheetView( m_orb, this, xController );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         return new DocumentViewHelper( m_orb, this, xController );
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
127*cdf0e10cSrcweir     /** creates a new form which is a child of the given form components container
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         @param xParentContainer
130*cdf0e10cSrcweir             The parent container for the new form
131*cdf0e10cSrcweir         @param sInitialName
132*cdf0e10cSrcweir             The initial name of the form. May be null, in this case the default (which
133*cdf0e10cSrcweir             is an implementation detail) applies.
134*cdf0e10cSrcweir     */
135*cdf0e10cSrcweir     protected XIndexContainer createSubForm( XIndexContainer xParentContainer, String sInitialName )
136*cdf0e10cSrcweir             throws com.sun.star.uno.Exception
137*cdf0e10cSrcweir     {
138*cdf0e10cSrcweir         // create a new form
139*cdf0e10cSrcweir         Object xNewForm = m_orb.createInstance( "com.sun.star.form.component.DataForm" );
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir         // insert
142*cdf0e10cSrcweir         xParentContainer.insertByIndex( xParentContainer.getCount(), xNewForm );
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir         // set the name if necessary
145*cdf0e10cSrcweir         if ( null != sInitialName )
146*cdf0e10cSrcweir         {
147*cdf0e10cSrcweir             XPropertySet xFormProps = UNO.queryPropertySet( xNewForm );
148*cdf0e10cSrcweir             xFormProps.setPropertyValue( "Name", sInitialName );
149*cdf0e10cSrcweir         }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir         // outta here
152*cdf0e10cSrcweir         return (XIndexContainer)UnoRuntime.queryInterface( XIndexContainer.class, xNewForm );
153*cdf0e10cSrcweir     }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
156*cdf0e10cSrcweir     /** creates a new form which is a child of the given form components container
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir         @param aParentContainer
159*cdf0e10cSrcweir             The parent container for the new form
160*cdf0e10cSrcweir         @param sInitialName
161*cdf0e10cSrcweir             The initial name of the form. May be null, in this case the default (which
162*cdf0e10cSrcweir             is an implementation detail) applies.
163*cdf0e10cSrcweir     */
164*cdf0e10cSrcweir     public XIndexContainer createSubForm( Object aParentContainer, String sInitialName )
165*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
166*cdf0e10cSrcweir     {
167*cdf0e10cSrcweir         XIndexContainer xParentContainer = (XIndexContainer)UnoRuntime.queryInterface(
168*cdf0e10cSrcweir             XIndexContainer.class, aParentContainer );
169*cdf0e10cSrcweir         return createSubForm( xParentContainer, sInitialName );
170*cdf0e10cSrcweir     }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
173*cdf0e10cSrcweir     /** creates a form which is a sibling of the given form
174*cdf0e10cSrcweir         @param aForm
175*cdf0e10cSrcweir             A sinbling of the to be created form.
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir         @param sInitialName
178*cdf0e10cSrcweir             The initial name of the form. May be null, in this case the default (which
179*cdf0e10cSrcweir             is an implementation detail) applies.
180*cdf0e10cSrcweir     */
181*cdf0e10cSrcweir     public XIndexContainer createSiblingForm( Object aForm, String sInitialName ) throws com.sun.star.uno.Exception
182*cdf0e10cSrcweir     {
183*cdf0e10cSrcweir         // get the parent
184*cdf0e10cSrcweir         XChild xAsChild = (XChild)UnoRuntime.queryInterface( XChild.class, aForm );
185*cdf0e10cSrcweir         XIndexContainer xContainer = (XIndexContainer)UnoRuntime.queryInterface(
186*cdf0e10cSrcweir             XIndexContainer.class, xAsChild.getParent() );;
187*cdf0e10cSrcweir         // append a new form to this parent container
188*cdf0e10cSrcweir         return createSubForm( xContainer, sInitialName );
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
192*cdf0e10cSrcweir     /** retrieves the document model which a given form component belongs to
193*cdf0e10cSrcweir     */
194*cdf0e10cSrcweir     static public DocumentHelper getDocumentForComponent( Object aFormComponent, XComponentContext xCtx )
195*cdf0e10cSrcweir     {
196*cdf0e10cSrcweir         XChild xChild = (XChild)UnoRuntime.queryInterface( XChild.class, aFormComponent );
197*cdf0e10cSrcweir         XModel xModel = null;
198*cdf0e10cSrcweir         while ( ( null != xChild ) && ( null == xModel ) )
199*cdf0e10cSrcweir         {
200*cdf0e10cSrcweir             XInterface xParent = (XInterface)xChild.getParent();
201*cdf0e10cSrcweir             xModel = (XModel)UnoRuntime.queryInterface( XModel.class, xParent );
202*cdf0e10cSrcweir             xChild = (XChild)UnoRuntime.queryInterface( XChild.class, xParent );
203*cdf0e10cSrcweir         }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir         return new DocumentHelper( xCtx, xModel );
206*cdf0e10cSrcweir     }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
209*cdf0e10cSrcweir     /** returns a URL which can be used to create a document of a certain type
210*cdf0e10cSrcweir     */
211*cdf0e10cSrcweir     public static String getDocumentFactoryURL( DocumentType eType )
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir         if ( eType == DocumentType.WRITER )
214*cdf0e10cSrcweir             return "private:factory/swriter";
215*cdf0e10cSrcweir         if ( eType == DocumentType.CALC )
216*cdf0e10cSrcweir             return "private:factory/scalc";
217*cdf0e10cSrcweir         if ( eType == DocumentType.DRAWING )
218*cdf0e10cSrcweir             return "private:factory/sdraw";
219*cdf0e10cSrcweir         return "private:factory/swriter";
220*cdf0e10cSrcweir     }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
223*cdf0e10cSrcweir     /** classifies a document
224*cdf0e10cSrcweir     */
225*cdf0e10cSrcweir     public DocumentType classify( )
226*cdf0e10cSrcweir     {
227*cdf0e10cSrcweir         XServiceInfo xSI = (XServiceInfo)UnoRuntime.queryInterface(
228*cdf0e10cSrcweir             XServiceInfo.class, m_documentComponent );
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir         if ( xSI.supportsService( "com.sun.star.text.TextDocument" ) )
231*cdf0e10cSrcweir             return DocumentType.WRITER;
232*cdf0e10cSrcweir         else if ( xSI.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
233*cdf0e10cSrcweir             return DocumentType.CALC;
234*cdf0e10cSrcweir         else if ( xSI.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
235*cdf0e10cSrcweir             return DocumentType.DRAWING;
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir         return DocumentType.UNKNOWN;
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
240*cdf0e10cSrcweir     /** retrieves a com.sun.star.drawing.DrawPage of the document, denoted by index
241*cdf0e10cSrcweir      *  @param index
242*cdf0e10cSrcweir      *      the index of the draw page<br/>
243*cdf0e10cSrcweir      *  @throws
244*cdf0e10cSrcweir      *      com.sun.star.lang.IndexOutOfBoundsException
245*cdf0e10cSrcweir      *      com.sun.star.lang.WrappedTargetException
246*cdf0e10cSrcweir      */
247*cdf0e10cSrcweir     protected XDrawPage getDrawPage( int index ) throws com.sun.star.lang.IndexOutOfBoundsException, com.sun.star.lang.WrappedTargetException
248*cdf0e10cSrcweir     {
249*cdf0e10cSrcweir         XDrawPagesSupplier xSuppPages = (XDrawPagesSupplier)UnoRuntime.queryInterface(
250*cdf0e10cSrcweir             XDrawPagesSupplier.class, getDocument() );
251*cdf0e10cSrcweir         XDrawPages xPages = xSuppPages.getDrawPages();
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir         return (XDrawPage)UnoRuntime.queryInterface( XDrawPage.class, xPages.getByIndex( index ) );
254*cdf0e10cSrcweir     }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
257*cdf0e10cSrcweir     /** retrieves the <type scope="com.sun.star.drawing">DrawPage</type> of the document
258*cdf0e10cSrcweir     */
259*cdf0e10cSrcweir     protected XDrawPage getMainDrawPage( ) throws com.sun.star.uno.Exception
260*cdf0e10cSrcweir     {
261*cdf0e10cSrcweir         XDrawPage xReturn;
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir         // in case of a Writer document, this is rather easy: simply ask the XDrawPageSupplier
264*cdf0e10cSrcweir         XDrawPageSupplier xSuppPage = (XDrawPageSupplier)UnoRuntime.queryInterface(
265*cdf0e10cSrcweir             XDrawPageSupplier.class, getDocument() );
266*cdf0e10cSrcweir         if ( null != xSuppPage )
267*cdf0e10cSrcweir             xReturn = xSuppPage.getDrawPage();
268*cdf0e10cSrcweir         else
269*cdf0e10cSrcweir         {   // the model itself is no draw page supplier - okay, it may be a Writer or Calc document
270*cdf0e10cSrcweir             // (or any other multi-page document)
271*cdf0e10cSrcweir             XDrawPagesSupplier xSuppPages = (XDrawPagesSupplier)UnoRuntime.queryInterface(
272*cdf0e10cSrcweir                 XDrawPagesSupplier.class, getDocument() );
273*cdf0e10cSrcweir             XDrawPages xPages = xSuppPages.getDrawPages();
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir             xReturn = (XDrawPage)UnoRuntime.queryInterface( XDrawPage.class, xPages.getByIndex( 0 ) );
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir             // Note that this is no really error-proof code: If the document model does not support the
278*cdf0e10cSrcweir             // XDrawPagesSupplier interface, or if the pages collection returned is empty, this will break.
279*cdf0e10cSrcweir         }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir         return xReturn;
282*cdf0e10cSrcweir     }
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
285*cdf0e10cSrcweir     /** retrieves the root of the hierarchy of form components
286*cdf0e10cSrcweir     */
287*cdf0e10cSrcweir     protected XNameContainer getFormComponentTreeRoot( ) throws com.sun.star.uno.Exception
288*cdf0e10cSrcweir     {
289*cdf0e10cSrcweir         XFormsSupplier xSuppForms = (XFormsSupplier)UnoRuntime.queryInterface(
290*cdf0e10cSrcweir             XFormsSupplier.class, getMainDrawPage( ) );
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir         XNameContainer xFormsCollection = null;
293*cdf0e10cSrcweir         if ( null != xSuppForms )
294*cdf0e10cSrcweir         {
295*cdf0e10cSrcweir             xFormsCollection = xSuppForms.getForms();
296*cdf0e10cSrcweir         }
297*cdf0e10cSrcweir         return xFormsCollection;
298*cdf0e10cSrcweir     }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
301*cdf0e10cSrcweir     /** creates a component at the service factory provided by the document
302*cdf0e10cSrcweir     */
303*cdf0e10cSrcweir     public XInterface createInstance( String serviceSpecifier ) throws com.sun.star.uno.Exception
304*cdf0e10cSrcweir     {
305*cdf0e10cSrcweir         XMultiServiceFactory xORB = (XMultiServiceFactory)UnoRuntime.queryInterface( XMultiServiceFactory.class,
306*cdf0e10cSrcweir             m_documentComponent );
307*cdf0e10cSrcweir         return (XInterface)xORB.createInstance( serviceSpecifier );
308*cdf0e10cSrcweir     }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
311*cdf0e10cSrcweir     /** creates a component at the service factory provided by the document
312*cdf0e10cSrcweir     */
313*cdf0e10cSrcweir     public XInterface createInstanceWithArguments( String serviceSpecifier, Object[] arguments ) throws com.sun.star.uno.Exception
314*cdf0e10cSrcweir     {
315*cdf0e10cSrcweir         XMultiServiceFactory xORB = (XMultiServiceFactory)UnoRuntime.queryInterface( XMultiServiceFactory.class,
316*cdf0e10cSrcweir             m_documentComponent );
317*cdf0e10cSrcweir         return (XInterface) xORB.createInstanceWithArguments( serviceSpecifier, arguments );
318*cdf0e10cSrcweir     }
319*cdf0e10cSrcweir };
320*cdf0e10cSrcweir 
321