1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DocumentHelper.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 /**************************************************************************/
32 import com
.sun
.star
.uno
.*;
33 import com
.sun
.star
.lang
.*;
34 import com
.sun
.star
.util
.*;
35 import com
.sun
.star
.awt
.*;
36 import com
.sun
.star
.drawing
.*;
37 import com
.sun
.star
.frame
.*;
38 import com
.sun
.star
.form
.*;
39 import com
.sun
.star
.beans
.*;
40 import com
.sun
.star
.container
.*;
41 import com
.sun
.star
.container
.*;
43 /**************************************************************************/
44 /** provides a small wrapper around a document
46 public class DocumentHelper
48 /// the remote office context
49 protected XComponentContext m_remoteContext
;
50 /// the remote service manager
51 protected XMultiServiceFactory m_orb
;
52 protected XComponent m_documentComponent
;
54 /* ------------------------------------------------------------------ */
55 public XComponent
getDocument( )
57 return m_documentComponent
;
60 /* ------------------------------------------------------------------ */
61 public XComponentContext
getContext( )
63 return m_remoteContext
;
66 /* ------------------------------------------------------------------ */
67 public XMultiServiceFactory
getOrb( )
72 /* ------------------------------------------------------------------ */
73 public DocumentHelper( XComponentContext xContext
, XComponent document
)
75 m_remoteContext
= xContext
;
76 m_orb
= (XMultiServiceFactory
)UnoRuntime
.queryInterface(
77 XMultiServiceFactory
.class, m_remoteContext
.getServiceManager());
78 m_documentComponent
= document
;
81 /* ------------------------------------------------------------------ */
82 protected static XComponent
implCreateBlankDocument( XComponentContext xCtx
, String factoryURL
) throws com
.sun
.star
.uno
.Exception
84 XComponentLoader aLoader
= (XComponentLoader
)UnoRuntime
.queryInterface(
85 XComponentLoader
.class,
86 xCtx
.getServiceManager().createInstanceWithContext(
87 "com.sun.star.frame.Desktop", xCtx
));
89 return UNO
.queryComponent(
90 aLoader
.loadComponentFromURL( factoryURL
, "_blank", 0, new PropertyValue
[ 0 ] )
94 /* ------------------------------------------------------------------ */
95 public static DocumentHelper
blankTextDocument( XComponentContext xCtx
) throws com
.sun
.star
.uno
.Exception
97 return blankDocument( xCtx
, DocumentType
.WRITER
);
100 /* ------------------------------------------------------------------ */
101 public static DocumentHelper
blankDocument( XComponentContext xCtx
, DocumentType eType
) throws com
.sun
.star
.uno
.Exception
103 XComponent document
= implCreateBlankDocument( xCtx
, getDocumentFactoryURL( eType
) );
104 if ( eType
== DocumentType
.CALC
)
105 return new SpreadsheetDocument( xCtx
, document
);
107 return new DocumentHelper( xCtx
, document
);
110 /* ------------------------------------------------------------------ */
111 /** retrieves the current view of the document
113 the view component, queried for the interface described by aInterfaceClass
115 public DocumentViewHelper
getCurrentView( )
117 // get the model interface for the document
118 XModel xDocModel
= (XModel
)UnoRuntime
.queryInterface(XModel
.class, m_documentComponent
);
119 // get the current controller for the document - as a controller is tied to a view,
120 // this gives us the currently active view for the document.
121 XController xController
= xDocModel
.getCurrentController();
123 if ( classify() == DocumentType
.CALC
)
124 return new SpreadsheetView( m_orb
, this, xController
);
126 return new DocumentViewHelper( m_orb
, this, xController
);
129 /* ------------------------------------------------------------------ */
130 /** creates a new form which is a child of the given form components container
132 @param xParentContainer
133 The parent container for the new form
135 The initial name of the form. May be null, in this case the default (which
136 is an implementation detail) applies.
138 protected XIndexContainer
createSubForm( XIndexContainer xParentContainer
, String sInitialName
)
139 throws com
.sun
.star
.uno
.Exception
142 Object xNewForm
= m_orb
.createInstance( "com.sun.star.form.component.DataForm" );
145 xParentContainer
.insertByIndex( xParentContainer
.getCount(), xNewForm
);
147 // set the name if necessary
148 if ( null != sInitialName
)
150 XPropertySet xFormProps
= UNO
.queryPropertySet( xNewForm
);
151 xFormProps
.setPropertyValue( "Name", sInitialName
);
155 return (XIndexContainer
)UnoRuntime
.queryInterface( XIndexContainer
.class, xNewForm
);
158 /* ------------------------------------------------------------------ */
159 /** creates a new form which is a child of the given form components container
161 @param aParentContainer
162 The parent container for the new form
164 The initial name of the form. May be null, in this case the default (which
165 is an implementation detail) applies.
167 public XIndexContainer
createSubForm( Object aParentContainer
, String sInitialName
)
168 throws com
.sun
.star
.uno
.Exception
170 XIndexContainer xParentContainer
= (XIndexContainer
)UnoRuntime
.queryInterface(
171 XIndexContainer
.class, aParentContainer
);
172 return createSubForm( xParentContainer
, sInitialName
);
175 /* ------------------------------------------------------------------ */
176 /** creates a form which is a sibling of the given form
178 A sinbling of the to be created form.
181 The initial name of the form. May be null, in this case the default (which
182 is an implementation detail) applies.
184 public XIndexContainer
createSiblingForm( Object aForm
, String sInitialName
) throws com
.sun
.star
.uno
.Exception
187 XChild xAsChild
= (XChild
)UnoRuntime
.queryInterface( XChild
.class, aForm
);
188 XIndexContainer xContainer
= (XIndexContainer
)UnoRuntime
.queryInterface(
189 XIndexContainer
.class, xAsChild
.getParent() );;
190 // append a new form to this parent container
191 return createSubForm( xContainer
, sInitialName
);
194 /* ------------------------------------------------------------------ */
195 /** retrieves the document model which a given form component belongs to
197 static public DocumentHelper
getDocumentForComponent( Object aFormComponent
, XComponentContext xCtx
)
199 XChild xChild
= (XChild
)UnoRuntime
.queryInterface( XChild
.class, aFormComponent
);
200 XModel xModel
= null;
201 while ( ( null != xChild
) && ( null == xModel
) )
203 XInterface xParent
= (XInterface
)xChild
.getParent();
204 xModel
= (XModel
)UnoRuntime
.queryInterface( XModel
.class, xParent
);
205 xChild
= (XChild
)UnoRuntime
.queryInterface( XChild
.class, xParent
);
208 return new DocumentHelper( xCtx
, xModel
);
211 /* ------------------------------------------------------------------ */
212 /** returns a URL which can be used to create a document of a certain type
214 public static String
getDocumentFactoryURL( DocumentType eType
)
216 if ( eType
== DocumentType
.WRITER
)
217 return "private:factory/swriter";
218 if ( eType
== DocumentType
.CALC
)
219 return "private:factory/scalc";
220 if ( eType
== DocumentType
.DRAWING
)
221 return "private:factory/sdraw";
222 return "private:factory/swriter";
225 /* ------------------------------------------------------------------ */
226 /** classifies a document
228 public DocumentType
classify( )
230 XServiceInfo xSI
= (XServiceInfo
)UnoRuntime
.queryInterface(
231 XServiceInfo
.class, m_documentComponent
);
233 if ( xSI
.supportsService( "com.sun.star.text.TextDocument" ) )
234 return DocumentType
.WRITER
;
235 else if ( xSI
.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
236 return DocumentType
.CALC
;
237 else if ( xSI
.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
238 return DocumentType
.DRAWING
;
240 return DocumentType
.UNKNOWN
;
242 /* ------------------------------------------------------------------ */
243 /** retrieves a com.sun.star.drawing.DrawPage of the document, denoted by index
245 * the index of the draw page<br/>
247 * com.sun.star.lang.IndexOutOfBoundsException
248 * com.sun.star.lang.WrappedTargetException
250 protected XDrawPage
getDrawPage( int index
) throws com
.sun
.star
.lang
.IndexOutOfBoundsException
, com
.sun
.star
.lang
.WrappedTargetException
252 XDrawPagesSupplier xSuppPages
= (XDrawPagesSupplier
)UnoRuntime
.queryInterface(
253 XDrawPagesSupplier
.class, getDocument() );
254 XDrawPages xPages
= xSuppPages
.getDrawPages();
256 return (XDrawPage
)UnoRuntime
.queryInterface( XDrawPage
.class, xPages
.getByIndex( index
) );
259 /* ------------------------------------------------------------------ */
260 /** retrieves the <type scope="com.sun.star.drawing">DrawPage</type> of the document
262 protected XDrawPage
getMainDrawPage( ) throws com
.sun
.star
.uno
.Exception
266 // in case of a Writer document, this is rather easy: simply ask the XDrawPageSupplier
267 XDrawPageSupplier xSuppPage
= (XDrawPageSupplier
)UnoRuntime
.queryInterface(
268 XDrawPageSupplier
.class, getDocument() );
269 if ( null != xSuppPage
)
270 xReturn
= xSuppPage
.getDrawPage();
272 { // the model itself is no draw page supplier - okay, it may be a Writer or Calc document
273 // (or any other multi-page document)
274 XDrawPagesSupplier xSuppPages
= (XDrawPagesSupplier
)UnoRuntime
.queryInterface(
275 XDrawPagesSupplier
.class, getDocument() );
276 XDrawPages xPages
= xSuppPages
.getDrawPages();
278 xReturn
= (XDrawPage
)UnoRuntime
.queryInterface( XDrawPage
.class, xPages
.getByIndex( 0 ) );
280 // Note that this is no really error-proof code: If the document model does not support the
281 // XDrawPagesSupplier interface, or if the pages collection returned is empty, this will break.
287 /* ------------------------------------------------------------------ */
288 /** retrieves the root of the hierarchy of form components
290 protected XNameContainer
getFormComponentTreeRoot( ) throws com
.sun
.star
.uno
.Exception
292 XFormsSupplier xSuppForms
= (XFormsSupplier
)UnoRuntime
.queryInterface(
293 XFormsSupplier
.class, getMainDrawPage( ) );
295 XNameContainer xFormsCollection
= null;
296 if ( null != xSuppForms
)
298 xFormsCollection
= xSuppForms
.getForms();
300 return xFormsCollection
;
303 /* ------------------------------------------------------------------ */
304 /** creates a component at the service factory provided by the document
306 public XInterface
createInstance( String serviceSpecifier
) throws com
.sun
.star
.uno
.Exception
308 XMultiServiceFactory xORB
= (XMultiServiceFactory
)UnoRuntime
.queryInterface( XMultiServiceFactory
.class,
309 m_documentComponent
);
310 return (XInterface
)xORB
.createInstance( serviceSpecifier
);
313 /* ------------------------------------------------------------------ */
314 /** creates a component at the service factory provided by the document
316 public XInterface
createInstanceWithArguments( String serviceSpecifier
, Object
[] arguments
) throws com
.sun
.star
.uno
.Exception
318 XMultiServiceFactory xORB
= (XMultiServiceFactory
)UnoRuntime
.queryInterface( XMultiServiceFactory
.class,
319 m_documentComponent
);
320 return (XInterface
) xORB
.createInstanceWithArguments( serviceSpecifier
, arguments
);