1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.script.framework.provider;
24 
25 import com.sun.star.document.XScriptInvocationContext;
26 import com.sun.star.frame.XModel;
27 import com.sun.star.frame.XDesktop;
28 import com.sun.star.uno.XComponentContext;
29 import com.sun.star.lang.XMultiComponentFactory;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.beans.PropertyAttribute;
32 import com.sun.star.lib.uno.helper.PropertySet;
33 import com.sun.star.uno.Type;
34 
35 import com.sun.star.script.provider.XScriptContext;
36 
37 import com.sun.star.script.framework.log.LogUtils;
38 
39 
40 /**
41  *  Description of the Class
42  *
43  * @author     Noel Power
44  * @created    August 2, 2002
45  */
46 public class ScriptContext extends PropertySet implements XScriptContext
47 {
48     /**
49      *  Description of the Class
50      *
51      * @author     John Rice
52      * @created    18/09/02
53      */
54 
55     public final static String HM_DOC_REF = "DocumentReference";
56     public final static String HM_DESKTOP = "Desktop";
57     public final static String HM_COMPONENT_CONTEXT = "ComponentContext";
58 
59     private final static String DOC_REF = "SCRIPTING_DOC_REF";
60     private final static String DOC_URI = "SCRIPTING_DOC_URI";
61 
62 
63     public XModel m_xModel = null;
64     public XScriptInvocationContext m_xInvocationContext = null;
65     public String m_sDocURI = null;
66     public XDesktop m_xDeskTop = null;
67     public Integer m_iStorageID = null;
68     public XComponentContext m_xComponentContext = null;
69 
ScriptContext( XComponentContext xmComponentContext, XDesktop xDesktop, XModel xModel, XScriptInvocationContext xInvocContext)70     public ScriptContext( XComponentContext xmComponentContext,
71         XDesktop xDesktop, XModel xModel, XScriptInvocationContext xInvocContext)
72     {
73         this.m_xDeskTop = xDesktop;
74         this.m_xComponentContext = xmComponentContext;
75         this.m_xModel = xModel;
76         this.m_xInvocationContext = xInvocContext;
77 
78         if ( m_xModel != null )
79         {
80             registerProperty( DOC_URI, new Type(String.class),
81                 (short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), "m_sDocURI");
82         }
83 
84         registerProperty( HM_DOC_REF, new Type(XModel.class),
85             (short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), "m_xModel");
86         registerProperty( HM_DESKTOP, new Type(XDesktop.class),
87             (short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), "m_xDeskTop");
88         registerProperty( HM_COMPONENT_CONTEXT, new Type(XDesktop.class),
89             (short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), "m_xComponentContext");
90     }
91 
createContext( XModel xModel, XScriptInvocationContext xInvocContext, XComponentContext xCtxt, XMultiComponentFactory xMCF)92     public static XScriptContext createContext( XModel xModel, XScriptInvocationContext xInvocContext,
93         XComponentContext xCtxt, XMultiComponentFactory xMCF)
94     {
95         XScriptContext sc = null;
96 
97         try {
98 
99             Object xInterface = null;
100             XDesktop xDesktop = null;
101 
102             xInterface = xMCF.createInstanceWithContext(
103                 "com.sun.star.frame.Desktop", xCtxt);
104             xDesktop = (XDesktop)
105                 UnoRuntime.queryInterface(XDesktop.class, xInterface);
106             if ( xModel != null )
107             {
108                 sc = new ScriptContext(xCtxt, xDesktop, xModel, xInvocContext);
109             }
110             else
111             {
112                 sc = new EditorScriptContext(xCtxt, xDesktop );
113             }
114 
115         }
116         catch ( Exception e ) {
117             LogUtils.DEBUG( LogUtils.getTrace( e ) );
118         }
119         return sc;
120     }
121 
122     //----------------------------------------------------------------------
123     /**
124         Obtain the document reference on which the script can operate
125 
126         @returns
127 	      XModel interface
128     */
getDocument()129     public XModel getDocument()
130     {
131         return m_xModel;
132     }
133 
getInvocationContext()134     public XScriptInvocationContext getInvocationContext()
135     {
136         return m_xInvocationContext;
137     }
138 
139     /**
140         Obtain the desktop reference on which the script can operate
141 
142         @returns
143 	      XDesktop interface
144     */
getDesktop()145     public XDesktop getDesktop()
146     {
147         return m_xDeskTop;
148     }
149 
150     /**
151         Obtain the component context which the script can use to create other uno components
152 
153         @returns
154 	      XComponentContext interface
155     */
getComponentContext()156     public XComponentContext getComponentContext()
157     {
158        return m_xComponentContext;
159     }
160 
161 }
162