Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / DocumentHelper.java
blobabfda37c450e3c9de3f81042cd68739fa51ca3ee
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 /**************************************************************************/
21 import com.sun.star.uno.*;
22 import com.sun.star.lang.*;
23 import com.sun.star.drawing.*;
24 import com.sun.star.frame.*;
25 import com.sun.star.form.*;
26 import com.sun.star.beans.*;
27 import com.sun.star.container.*;
29 /**************************************************************************/
30 /** provides a small wrapper around a document
32 public class DocumentHelper
34 /// the remote office context
35 private XComponentContext m_remoteContext;
36 /// the remote service manager
37 private XMultiServiceFactory m_orb;
38 protected XComponent m_documentComponent;
40 /* ------------------------------------------------------------------ */
41 public XComponent getDocument( )
43 return m_documentComponent;
46 /* ------------------------------------------------------------------ */
47 public XComponentContext getContext( )
49 return m_remoteContext;
52 /* ------------------------------------------------------------------ */
53 public XMultiServiceFactory getOrb( )
55 return m_orb;
58 /* ------------------------------------------------------------------ */
59 public DocumentHelper( XComponentContext xContext, XComponent document )
61 m_remoteContext = xContext;
62 m_orb = UnoRuntime.queryInterface(
63 XMultiServiceFactory.class, m_remoteContext.getServiceManager());
64 m_documentComponent = document;
67 /* ------------------------------------------------------------------ */
68 protected static XComponent implCreateBlankDocument( XComponentContext xCtx, String factoryURL ) throws com.sun.star.uno.Exception
70 XComponentLoader aLoader = UnoRuntime.queryInterface(
71 XComponentLoader.class,
72 xCtx.getServiceManager().createInstanceWithContext(
73 "com.sun.star.frame.Desktop", xCtx ));
75 return UNO.queryComponent(
76 aLoader.loadComponentFromURL( factoryURL, "_blank", 0, new PropertyValue[ 0 ] )
82 /* ------------------------------------------------------------------ */
83 public static DocumentHelper blankDocument( XComponentContext xCtx, DocumentType eType ) throws com.sun.star.uno.Exception
85 XComponent document = implCreateBlankDocument( xCtx, getDocumentFactoryURL( eType ) );
86 if ( eType == DocumentType.CALC )
87 return new SpreadsheetDocument( xCtx, document );
89 return new DocumentHelper( xCtx, document );
92 /* ------------------------------------------------------------------ */
93 /** retrieves the current view of the document
94 @return
95 the view component, queried for the interface described by aInterfaceClass
97 public DocumentViewHelper getCurrentView( )
99 // get the model interface for the document
100 XModel xDocModel = UnoRuntime.queryInterface(XModel.class, m_documentComponent );
101 // get the current controller for the document - as a controller is tied to a view,
102 // this gives us the currently active view for the document.
103 XController xController = xDocModel.getCurrentController();
105 if ( classify() == DocumentType.CALC )
106 return new SpreadsheetView( m_orb, this, xController );
108 return new DocumentViewHelper( m_orb, this, xController );
111 /* ------------------------------------------------------------------ */
112 /** creates a new form which is a child of the given form components container
114 @param xParentContainer
115 The parent container for the new form
116 @param sInitialName
117 The initial name of the form. May be null, in this case the default (which
118 is an implementation detail) applies.
120 private XIndexContainer createSubForm( XIndexContainer xParentContainer, String sInitialName )
121 throws com.sun.star.uno.Exception
123 // create a new form
124 Object xNewForm = m_orb.createInstance( "com.sun.star.form.component.DataForm" );
126 // insert
127 xParentContainer.insertByIndex( xParentContainer.getCount(), xNewForm );
129 // set the name if necessary
130 if ( null != sInitialName )
132 XPropertySet xFormProps = UNO.queryPropertySet( xNewForm );
133 xFormProps.setPropertyValue( "Name", sInitialName );
136 // outta here
137 return UnoRuntime.queryInterface( XIndexContainer.class, xNewForm );
140 /* ------------------------------------------------------------------ */
141 /** creates a new form which is a child of the given form components container
143 @param aParentContainer
144 The parent container for the new form
145 @param sInitialName
146 The initial name of the form. May be null, in this case the default (which
147 is an implementation detail) applies.
149 public XIndexContainer createSubForm( Object aParentContainer, String sInitialName )
150 throws com.sun.star.uno.Exception
152 XIndexContainer xParentContainer = UnoRuntime.queryInterface(
153 XIndexContainer.class, aParentContainer );
154 return createSubForm( xParentContainer, sInitialName );
157 /* ------------------------------------------------------------------ */
158 /** creates a form which is a sibling of the given form
159 @param aForm
160 A sinbling of the to be created form.
162 @param sInitialName
163 The initial name of the form. May be null, in this case the default (which
164 is an implementation detail) applies.
166 public XIndexContainer createSiblingForm( Object aForm, String sInitialName ) throws com.sun.star.uno.Exception
168 // get the parent
169 XChild xAsChild = UnoRuntime.queryInterface( XChild.class, aForm );
170 XIndexContainer xContainer = UnoRuntime.queryInterface(
171 XIndexContainer.class, xAsChild.getParent() );
172 // append a new form to this parent container
173 return createSubForm( xContainer, sInitialName );
176 /* ------------------------------------------------------------------ */
177 /** retrieves the document model which a given form component belongs to
179 public static DocumentHelper getDocumentForComponent( Object aFormComponent, XComponentContext xCtx )
181 XChild xChild = UnoRuntime.queryInterface( XChild.class, aFormComponent );
182 XModel xModel = null;
183 while ( ( null != xChild ) && ( null == xModel ) )
185 XInterface xParent = (XInterface)xChild.getParent();
186 xModel = UnoRuntime.queryInterface( XModel.class, xParent );
187 xChild = UnoRuntime.queryInterface( XChild.class, xParent );
190 return new DocumentHelper( xCtx, xModel );
193 /* ------------------------------------------------------------------ */
194 /** returns a URL which can be used to create a document of a certain type
196 private static String getDocumentFactoryURL( DocumentType eType )
198 if ( eType == DocumentType.WRITER )
199 return "private:factory/swriter";
200 if ( eType == DocumentType.CALC )
201 return "private:factory/scalc";
202 if ( eType == DocumentType.DRAWING )
203 return "private:factory/sdraw";
204 return "private:factory/swriter";
207 /* ------------------------------------------------------------------ */
208 /** classifies a document
210 public DocumentType classify( )
212 XServiceInfo xSI = UnoRuntime.queryInterface(
213 XServiceInfo.class, m_documentComponent );
215 if ( xSI.supportsService( "com.sun.star.text.TextDocument" ) )
216 return DocumentType.WRITER;
217 else if ( xSI.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
218 return DocumentType.CALC;
219 else if ( xSI.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
220 return DocumentType.DRAWING;
222 return DocumentType.UNKNOWN;
224 /* ------------------------------------------------------------------ */
225 /** retrieves a com.sun.star.drawing.DrawPage of the document, denoted by index
226 * @param index
227 * the index of the draw page<br>
228 * @throws
229 * com.sun.star.lang.IndexOutOfBoundsException
230 * com.sun.star.lang.WrappedTargetException
232 protected XDrawPage getDrawPage( int index ) throws com.sun.star.lang.IndexOutOfBoundsException, com.sun.star.lang.WrappedTargetException
234 XDrawPagesSupplier xSuppPages = UnoRuntime.queryInterface(
235 XDrawPagesSupplier.class, getDocument() );
236 XDrawPages xPages = xSuppPages.getDrawPages();
238 return UnoRuntime.queryInterface( XDrawPage.class, xPages.getByIndex( index ) );
241 /* ------------------------------------------------------------------ */
242 /** retrieves the <type scope="com.sun.star.drawing">DrawPage</type> of the document
244 protected XDrawPage getMainDrawPage( ) throws com.sun.star.uno.Exception
246 XDrawPage xReturn;
248 // in case of a Writer document, this is rather easy: simply ask the XDrawPageSupplier
249 XDrawPageSupplier xSuppPage = UnoRuntime.queryInterface(
250 XDrawPageSupplier.class, getDocument() );
251 if ( null != xSuppPage )
252 xReturn = xSuppPage.getDrawPage();
253 else
254 { // the model itself is no draw page supplier - okay, it may be a Writer or Calc document
255 // (or any other multi-page document)
256 XDrawPagesSupplier xSuppPages = UnoRuntime.queryInterface(
257 XDrawPagesSupplier.class, getDocument() );
258 XDrawPages xPages = xSuppPages.getDrawPages();
260 xReturn = UnoRuntime.queryInterface( XDrawPage.class, xPages.getByIndex( 0 ) );
262 // Note that this is no really error-proof code: If the document model does not support the
263 // XDrawPagesSupplier interface, or if the pages collection returned is empty, this will break.
266 return xReturn;
269 /* ------------------------------------------------------------------ */
270 /** retrieves the root of the hierarchy of form components
272 protected XNameContainer getFormComponentTreeRoot( ) throws com.sun.star.uno.Exception
274 XFormsSupplier xSuppForms = UnoRuntime.queryInterface(
275 XFormsSupplier.class, getMainDrawPage( ) );
277 XNameContainer xFormsCollection = null;
278 if ( null != xSuppForms )
280 xFormsCollection = xSuppForms.getForms();
282 return xFormsCollection;
285 /* ------------------------------------------------------------------ */
286 /** creates a component at the service factory provided by the document
288 public XInterface createInstance( String serviceSpecifier ) throws com.sun.star.uno.Exception
290 XMultiServiceFactory xORB = UnoRuntime.queryInterface( XMultiServiceFactory.class,
291 m_documentComponent );
292 return (XInterface)xORB.createInstance( serviceSpecifier );
295 /* ------------------------------------------------------------------ */
296 /** creates a component at the service factory provided by the document
298 public XInterface createInstanceWithArguments( String serviceSpecifier, Object[] arguments ) throws com.sun.star.uno.Exception
300 XMultiServiceFactory xORB = UnoRuntime.queryInterface( XMultiServiceFactory.class,
301 m_documentComponent );
302 return (XInterface) xORB.createInstanceWithArguments( serviceSpecifier, arguments );
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */