2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package org
.openoffice
.test
.tools
;
20 import com
.sun
.star
.beans
.PropertyState
;
21 import com
.sun
.star
.beans
.PropertyValue
;
22 import com
.sun
.star
.document
.MacroExecMode
;
23 import com
.sun
.star
.frame
.XComponentLoader
;
24 import com
.sun
.star
.frame
.XController
;
25 import com
.sun
.star
.frame
.XModel
;
26 import com
.sun
.star
.lang
.XComponent
;
27 import com
.sun
.star
.lang
.XMultiServiceFactory
;
28 import com
.sun
.star
.lang
.XServiceInfo
;
29 import com
.sun
.star
.uno
.UnoRuntime
;
30 import com
.sun
.star
.uno
.XInterface
;
31 import com
.sun
.star
.util
.CloseVetoException
;
32 import com
.sun
.star
.util
.XCloseable
;
33 import java
.util
.logging
.Level
;
34 import java
.util
.logging
.Logger
;
36 /**************************************************************************/
38 /**************************************************************************/
39 /** provides a small wrapper around a document
41 public class OfficeDocument
43 /* ================================================================== */
44 /* ------------------------------------------------------------------ */
45 public OfficeDocument( XMultiServiceFactory orb
, XComponent document
)
48 m_documentComponent
= document
;
51 /* ------------------------------------------------------------------ */
52 protected static XComponent
implLoadAsComponent( XMultiServiceFactory orb
, String documentOrFactoryURL
) throws com
.sun
.star
.uno
.Exception
54 return implLoadAsComponent( orb
, documentOrFactoryURL
, new PropertyValue
[0] );
57 /* ------------------------------------------------------------------ */
58 private static XComponent
implLoadAsComponent( XMultiServiceFactory orb
, String documentOrFactoryURL
, final PropertyValue
[] i_args
) throws com
.sun
.star
.uno
.Exception
60 XComponentLoader aLoader
= UnoRuntime
.queryInterface( XComponentLoader
.class,
61 orb
.createInstance( "com.sun.star.frame.Desktop" ) );
63 XComponent document
= UnoRuntime
.queryInterface( XComponent
.class,
64 aLoader
.loadComponentFromURL( documentOrFactoryURL
, "_blank", 0, i_args
)
69 /* ------------------------------------------------------------------ */
70 private static OfficeDocument
implLoadDocument( XMultiServiceFactory orb
, String documentOrFactoryURL
, final PropertyValue
[] i_args
) throws com
.sun
.star
.uno
.Exception
72 XComponent document
= implLoadAsComponent( orb
, documentOrFactoryURL
, i_args
);
74 XServiceInfo xSI
= UnoRuntime
.queryInterface( XServiceInfo
.class, document
);
75 if ( xSI
.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
76 return new SpreadsheetDocument( orb
, document
);
77 return new OfficeDocument( orb
, document
);
80 /* ------------------------------------------------------------------ */
81 public static OfficeDocument
blankTextDocument( XMultiServiceFactory orb
) throws com
.sun
.star
.uno
.Exception
83 return blankDocument( orb
, DocumentType
.WRITER
);
86 /* ------------------------------------------------------------------ */
87 public static OfficeDocument
blankDocument( XMultiServiceFactory orb
, DocumentType eType
) throws com
.sun
.star
.uno
.Exception
89 final PropertyValue
[] args
= new PropertyValue
[] {
90 new PropertyValue( "MacroExecutionMode", -1, MacroExecMode
.ALWAYS_EXECUTE
, PropertyState
.DIRECT_VALUE
)
92 return implLoadDocument( orb
, getDocumentFactoryURL( eType
), args
);
95 /* ------------------------------------------------------------------ */
96 public boolean close()
100 XCloseable closeDoc
= UnoRuntime
.queryInterface( XCloseable
.class, m_documentComponent
);
101 closeDoc
.close( true );
104 catch ( CloseVetoException e
)
106 Logger
.getLogger( OfficeDocument
.class.getName() ).log( Level
.SEVERE
, "closing the document was vetoed", e
);
111 /* ================================================================== */
112 /* ------------------------------------------------------------------ */
113 public XComponent
getDocument( )
115 return m_documentComponent
;
118 /* ------------------------------------------------------------------ */
119 /** retrieves the current view of the document
121 the view component, queried for the interface described by aInterfaceClass
123 public OfficeDocumentView
getCurrentView( )
125 // get the model interface for the document
126 XModel xDocModel
= UnoRuntime
.queryInterface( XModel
.class, m_documentComponent
);
127 // get the current controller for the document - as a controller is tied to a view,
128 // this gives us the currently active view for the document.
129 XController xController
= xDocModel
.getCurrentController();
131 if ( classify() == DocumentType
.CALC
)
132 return new SpreadsheetView( m_orb
, xController
);
134 return new OfficeDocumentView( m_orb
, xController
);
137 /* ------------------------------------------------------------------ */
138 /** returns a URL which can be used to create a document of a certain type
140 private static String
getDocumentFactoryURL( DocumentType eType
)
142 if ( eType
== DocumentType
.WRITER
)
143 return "private:factory/swriter";
144 if ( eType
== DocumentType
.CALC
)
145 return "private:factory/scalc";
146 if ( eType
== DocumentType
.DRAWING
)
147 return "private:factory/sdraw";
148 if ( eType
== DocumentType
.XMLFORM
)
149 return "private:factory/swriter?slot=21053";
150 if ( eType
== DocumentType
.PRESENTATION
)
151 return "private:factory/simpress";
152 if ( eType
== DocumentType
.FORMULA
)
153 return "private:factory/smath";
154 return "private:factory/swriter";
157 /* ------------------------------------------------------------------ */
158 /** classifies a document
160 private DocumentType
classify( )
162 XServiceInfo xSI
= UnoRuntime
.queryInterface( XServiceInfo
.class, m_documentComponent
);
164 if ( xSI
.supportsService( "com.sun.star.text.TextDocument" ) )
165 return DocumentType
.WRITER
;
166 else if ( xSI
.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
167 return DocumentType
.CALC
;
168 else if ( xSI
.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
169 return DocumentType
.DRAWING
;
170 else if ( xSI
.supportsService( "com.sun.star.presentation.PresentationDocument" ) )
171 return DocumentType
.PRESENTATION
;
172 else if ( xSI
.supportsService( "com.sun.star.formula.FormulaProperties" ) )
173 return DocumentType
.FORMULA
;
175 return DocumentType
.UNKNOWN
;
178 /* ------------------------------------------------------------------ */
179 /** creates a component at the service factory provided by the document
181 public XInterface
createInstance( String serviceSpecifier
) throws com
.sun
.star
.uno
.Exception
183 XMultiServiceFactory xORB
= UnoRuntime
.queryInterface( XMultiServiceFactory
.class, m_documentComponent
);
184 return (XInterface
)xORB
.createInstance( serviceSpecifier
);
187 /* ------------------------------------------------------------------ */
188 /** creates a component at the service factory provided by the document, queried for a given interface type
190 public <T
> T
createInstance( String i_serviceSpecifier
, Class
<T
> i_interfaceClass
) throws com
.sun
.star
.uno
.Exception
192 return UnoRuntime
.queryInterface( i_interfaceClass
, createInstance( i_serviceSpecifier
) );
195 private final XMultiServiceFactory m_orb
;
196 private XComponent m_documentComponent
;