| 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 |
*/ |
| 18 |
|
| 19 |
package com.sun.star.script.framework.provider; |
| 20 |
|
| 21 |
import com.sun.star.beans.PropertyAttribute; |
| 22 |
|
| 23 |
import com.sun.star.document.XScriptInvocationContext; |
| 24 |
|
| 25 |
import com.sun.star.frame.XDesktop; |
| 26 |
import com.sun.star.frame.XModel; |
| 27 |
|
| 28 |
import com.sun.star.lang.XMultiComponentFactory; |
| 29 |
|
| 30 |
import com.sun.star.lib.uno.helper.PropertySet; |
| 31 |
|
| 32 |
import com.sun.star.script.framework.log.LogUtils; |
| 33 |
import com.sun.star.script.provider.XScriptContext; |
| 34 |
|
| 35 |
import com.sun.star.uno.Type; |
| 36 |
import com.sun.star.uno.UnoRuntime; |
| 37 |
import com.sun.star.uno.XComponentContext; |
| 38 |
|
| 39 |
public class ScriptContext extends PropertySet implements XScriptContext { |
| 40 |
|
| 41 |
private static final String HM_DOC_REF = "DocumentReference"; |
| 42 |
private static final String HM_DESKTOP = "Desktop"; |
| 43 |
private static final String HM_COMPONENT_CONTEXT = "ComponentContext"; |
| 44 |
|
| 45 |
private static final String DOC_URI = "SCRIPTING_DOC_URI"; |
| 46 |
|
| 47 |
public final XModel m_xModel; |
| 48 |
private final XScriptInvocationContext m_xInvocationContext; |
| 49 |
|
| 50 |
private final XDesktop m_xDeskTop; |
| 51 |
|
| 52 |
private final XComponentContext m_xComponentContext; |
| 53 |
|
| 54 |
// property, accessed via reflection |
| 55 |
public String m_sDocURI = null; |
| 56 |
|
| 57 |
private ScriptContext(XComponentContext xmComponentContext, XDesktop xDesktop, |
| 58 |
XModel xModel, XScriptInvocationContext xInvocContext) { |
| 59 |
|
| 60 |
this.m_xDeskTop = xDesktop; |
| 61 |
this.m_xComponentContext = xmComponentContext; |
| 62 |
this.m_xModel = xModel; |
| 63 |
this.m_xInvocationContext = xInvocContext; |
| 64 |
|
| 65 |
if (m_xModel != null) { |
| 66 |
|
| 67 |
registerProperty(DOC_URI, new Type(String.class), |
| 68 |
(short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), |
| 69 |
"m_sDocURI"); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
registerProperty(HM_DOC_REF, new Type(XModel.class), |
| 74 |
(short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), |
| 75 |
"m_xModel"); |
| 76 |
|
| 77 |
registerProperty(HM_DESKTOP, new Type(XDesktop.class), |
| 78 |
(short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), |
| 79 |
"m_xDeskTop"); |
| 80 |
|
| 81 |
registerProperty(HM_COMPONENT_CONTEXT, new Type(XDesktop.class), |
| 82 |
(short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), |
| 83 |
"m_xComponentContext"); |
| 84 |
} |
| 85 |
|
| 86 |
public static XScriptContext createContext(XModel xModel, |
| 87 |
XScriptInvocationContext xInvocContext, |
| 88 |
XComponentContext xCtxt, XMultiComponentFactory xMCF) { |
| 89 |
|
| 90 |
XScriptContext sc = null; |
| 91 |
|
| 92 |
try { |
| 93 |
|
| 94 |
Object xInterface = |
| 95 |
xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xCtxt); |
| 96 |
|
| 97 |
XDesktop xDesktop = UnoRuntime.queryInterface(XDesktop.class, xInterface); |
| 98 |
|
| 99 |
if (xModel != null) { |
| 100 |
sc = new ScriptContext(xCtxt, xDesktop, xModel, xInvocContext); |
| 101 |
} else { |
| 102 |
sc = new EditorScriptContext(xCtxt, xDesktop); |
| 103 |
} |
| 104 |
|
| 105 |
} catch (Exception e) { |
| 106 |
LogUtils.DEBUG(LogUtils.getTrace(e)); |
| 107 |
} |
| 108 |
|
| 109 |
return sc; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
/** |
| 114 |
Obtain the document reference on which the script can operate |
| 115 |
|
| 116 |
@return XModel interface |
| 117 |
*/ |
| 118 |
public XModel getDocument() { |
| 119 |
return m_xModel; |
| 120 |
} |
| 121 |
|
| 122 |
public XScriptInvocationContext getInvocationContext() { |
| 123 |
return m_xInvocationContext; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
Obtain the desktop reference on which the script can operate |
| 128 |
|
| 129 |
@return XDesktop interface |
| 130 |
*/ |
| 131 |
public XDesktop getDesktop() { |
| 132 |
return m_xDeskTop; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
Obtain the component context which the script can use to create other uno components |
| 137 |
|
| 138 |
@return XComponentContext interface |
| 139 |
*/ |
| 140 |
public XComponentContext getComponentContext() { |
| 141 |
return m_xComponentContext; |
| 142 |
} |
| 143 |
} |