LibreOffice Module unotest (master) 1
OfficeDocument.java
Go to the documentation of this file.
1/*
2 * This file is part of the LibreOffice project.
3 *
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/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
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 .
17 */
18package org.openoffice.test.tools;
19
20import com.sun.star.beans.PropertyState;
21import com.sun.star.beans.PropertyValue;
22import com.sun.star.document.MacroExecMode;
23import com.sun.star.frame.XComponentLoader;
24import com.sun.star.frame.XController;
25import com.sun.star.frame.XModel;
26import com.sun.star.lang.XComponent;
27import com.sun.star.lang.XMultiServiceFactory;
28import com.sun.star.lang.XServiceInfo;
29import com.sun.star.uno.UnoRuntime;
30import com.sun.star.uno.XInterface;
31import com.sun.star.util.CloseVetoException;
32import com.sun.star.util.XCloseable;
33import java.util.logging.Level;
34import java.util.logging.Logger;
35
36/**************************************************************************/
37
38/**************************************************************************/
41public class OfficeDocument
42{
43 /* ================================================================== */
44 /* ------------------------------------------------------------------ */
45 public OfficeDocument( XMultiServiceFactory orb, XComponent document )
46 {
47 m_orb = orb;
48 m_documentComponent = document;
49 }
50
51 /* ------------------------------------------------------------------ */
52 protected static XComponent implLoadAsComponent( XMultiServiceFactory orb, String documentOrFactoryURL ) throws com.sun.star.uno.Exception
53 {
54 return implLoadAsComponent( orb, documentOrFactoryURL, new PropertyValue[0] );
55 }
56
57 /* ------------------------------------------------------------------ */
58 private static XComponent implLoadAsComponent( XMultiServiceFactory orb, String documentOrFactoryURL, final PropertyValue[] i_args ) throws com.sun.star.uno.Exception
59 {
60 XComponentLoader aLoader = UnoRuntime.queryInterface( XComponentLoader.class,
61 orb.createInstance( "com.sun.star.frame.Desktop" ) );
62
63 XComponent document = UnoRuntime.queryInterface( XComponent.class,
64 aLoader.loadComponentFromURL( documentOrFactoryURL, "_blank", 0, i_args )
65 );
66 return document;
67 }
68
69 /* ------------------------------------------------------------------ */
70 private static OfficeDocument implLoadDocument( XMultiServiceFactory orb, String documentOrFactoryURL, final PropertyValue[] i_args ) throws com.sun.star.uno.Exception
71 {
72 XComponent document = implLoadAsComponent( orb, documentOrFactoryURL, i_args );
73
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 );
78 }
79
80 /* ------------------------------------------------------------------ */
81 public static OfficeDocument blankTextDocument( XMultiServiceFactory orb ) throws com.sun.star.uno.Exception
82 {
83 return blankDocument( orb, DocumentType.WRITER );
84 }
85
86 /* ------------------------------------------------------------------ */
87 public static OfficeDocument blankDocument( XMultiServiceFactory orb, DocumentType eType ) throws com.sun.star.uno.Exception
88 {
89 final PropertyValue[] args = new PropertyValue[] {
90 new PropertyValue( "MacroExecutionMode", -1, MacroExecMode.ALWAYS_EXECUTE, PropertyState.DIRECT_VALUE )
91 };
93 }
94
95 /* ------------------------------------------------------------------ */
96 public boolean close()
97 {
98 try
99 {
100 XCloseable closeDoc = UnoRuntime.queryInterface( XCloseable.class, m_documentComponent );
101 closeDoc.close( true );
102 return true;
103 }
104 catch ( CloseVetoException e )
105 {
106 Logger.getLogger( OfficeDocument.class.getName() ).log( Level.SEVERE, "closing the document was vetoed", e );
107 }
108 return false;
109 }
110
111 /* ================================================================== */
112 /* ------------------------------------------------------------------ */
113 public XComponent getDocument( )
114 {
115 return m_documentComponent;
116 }
117
118 /* ------------------------------------------------------------------ */
124 {
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();
130
131 if ( classify() == DocumentType.CALC )
132 return new SpreadsheetView( m_orb, xController );
133
134 return new OfficeDocumentView( m_orb, xController );
135 }
136
137 /* ------------------------------------------------------------------ */
141 {
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";
151 return "private:factory/simpress";
152 if ( eType == DocumentType.FORMULA )
153 return "private:factory/smath";
154 return "private:factory/swriter";
155 }
156
157 /* ------------------------------------------------------------------ */
161 {
162 XServiceInfo xSI = UnoRuntime.queryInterface( XServiceInfo.class, m_documentComponent );
163
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" ) )
172 else if ( xSI.supportsService( "com.sun.star.formula.FormulaProperties" ) )
173 return DocumentType.FORMULA;
174
175 return DocumentType.UNKNOWN;
176 }
177
178 /* ------------------------------------------------------------------ */
181 public XInterface createInstance( String serviceSpecifier ) throws com.sun.star.uno.Exception
182 {
183 XMultiServiceFactory xORB = UnoRuntime.queryInterface( XMultiServiceFactory.class, m_documentComponent );
184 return (XInterface)xORB.createInstance( serviceSpecifier );
185 }
186
187 /* ------------------------------------------------------------------ */
190 public <T> T createInstance( String i_serviceSpecifier, Class<T> i_interfaceClass ) throws com.sun.star.uno.Exception
191 {
192 return UnoRuntime.queryInterface( i_interfaceClass, createInstance( i_serviceSpecifier ) );
193 }
194
195 private final XMultiServiceFactory m_orb;
196 private XComponent m_documentComponent;
197}
198
a helper "enumeration class" for classifying a document type
static final DocumentType XMLFORM
static final DocumentType FORMULA
static final DocumentType UNKNOWN
static final DocumentType PRESENTATION
static final DocumentType DRAWING
static final DocumentType WRITER
provides a small wrapper around a document view
provides a small wrapper around a document
static XComponent implLoadAsComponent(XMultiServiceFactory orb, String documentOrFactoryURL)
static OfficeDocument blankTextDocument(XMultiServiceFactory orb)
DocumentType classify()
classifies a document
static OfficeDocument implLoadDocument(XMultiServiceFactory orb, String documentOrFactoryURL, final PropertyValue[] i_args)
OfficeDocument(XMultiServiceFactory orb, XComponent document)
static String getDocumentFactoryURL(DocumentType eType)
returns a URL which can be used to create a document of a certain type
static OfficeDocument blankDocument(XMultiServiceFactory orb, DocumentType eType)
OfficeDocumentView getCurrentView()
retrieves the current view of the document
XInterface createInstance(String serviceSpecifier)
creates a component at the service factory provided by the document
static XComponent implLoadAsComponent(XMultiServiceFactory orb, String documentOrFactoryURL, final PropertyValue[] i_args)
DocumentType eType
Reference< XController > xController