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