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