1 /*************************************************************************
2 *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26 ************************************************************************/
27 package com.sun.star.script.framework.provider;
28 
29 import com.sun.star.document.XScriptInvocationContext;
30 import com.sun.star.frame.XModel;
31 import com.sun.star.frame.XDesktop;
32 import com.sun.star.uno.XComponentContext;
33 import com.sun.star.lang.XMultiComponentFactory;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.beans.PropertyAttribute;
36 import com.sun.star.lib.uno.helper.PropertySet;
37 import com.sun.star.uno.Type;
38 
39 import com.sun.star.script.provider.XScriptContext;
40 
41 import com.sun.star.script.framework.log.LogUtils;
42 
43 
44 /**
45  *  Description of the Class
46  *
47  * @author     Noel Power
48  * @created    August 2, 2002
49  */
50 public class ScriptContext extends PropertySet implements XScriptContext
51 {
52     /**
53      *  Description of the Class
54      *
55      * @author     John Rice
56      * @created    18/09/02
57      */
58 
59     public final static String HM_DOC_REF = "DocumentReference";
60     public final static String HM_DESKTOP = "Desktop";
61     public final static String HM_COMPONENT_CONTEXT = "ComponentContext";
62 
63     private final static String DOC_REF = "SCRIPTING_DOC_REF";
64     private final static String DOC_URI = "SCRIPTING_DOC_URI";
65 
66 
67     public XModel m_xModel = null;
68     public XScriptInvocationContext m_xInvocationContext = null;
69     public String m_sDocURI = null;
70     public XDesktop m_xDeskTop = null;
71     public Integer m_iStorageID = null;
72     public XComponentContext m_xComponentContext = null;
73 
74     public ScriptContext( XComponentContext xmComponentContext,
75         XDesktop xDesktop, XModel xModel, XScriptInvocationContext xInvocContext)
76     {
77         this.m_xDeskTop = xDesktop;
78         this.m_xComponentContext = xmComponentContext;
79         this.m_xModel = xModel;
80         this.m_xInvocationContext = xInvocContext;
81 
82         if ( m_xModel != null )
83         {
84             registerProperty( DOC_URI, new Type(String.class),
85                 (short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), "m_sDocURI");
86         }
87 
88         registerProperty( HM_DOC_REF, new Type(XModel.class),
89             (short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), "m_xModel");
90         registerProperty( HM_DESKTOP, new Type(XDesktop.class),
91             (short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), "m_xDeskTop");
92         registerProperty( HM_COMPONENT_CONTEXT, new Type(XDesktop.class),
93             (short)(PropertyAttribute.MAYBEVOID | PropertyAttribute.TRANSIENT), "m_xComponentContext");
94     }
95 
96     public static XScriptContext createContext( XModel xModel, XScriptInvocationContext xInvocContext,
97         XComponentContext xCtxt, XMultiComponentFactory xMCF)
98     {
99         XScriptContext sc = null;
100 
101         try {
102 
103             Object xInterface = null;
104             XDesktop xDesktop = null;
105 
106             xInterface = xMCF.createInstanceWithContext(
107                 "com.sun.star.frame.Desktop", xCtxt);
108             xDesktop = (XDesktop)
109                 UnoRuntime.queryInterface(XDesktop.class, xInterface);
110             if ( xModel != null )
111             {
112                 sc = new ScriptContext(xCtxt, xDesktop, xModel, xInvocContext);
113             }
114             else
115             {
116                 sc = new EditorScriptContext(xCtxt, xDesktop );
117             }
118 
119         }
120         catch ( Exception e ) {
121             LogUtils.DEBUG( LogUtils.getTrace( e ) );
122         }
123         return sc;
124     }
125 
126     //----------------------------------------------------------------------
127     /**
128         Obtain the document reference on which the script can operate
129 
130         @returns
131 	      XModel interface
132     */
133     public XModel getDocument()
134     {
135         return m_xModel;
136     }
137 
138     public XScriptInvocationContext getInvocationContext()
139     {
140         return m_xInvocationContext;
141     }
142 
143     /**
144         Obtain the desktop reference on which the script can operate
145 
146         @returns
147 	      XDesktop interface
148     */
149     public XDesktop getDesktop()
150     {
151         return m_xDeskTop;
152     }
153 
154     /**
155         Obtain the component context which the script can use to create other uno components
156 
157         @returns
158 	      XComponentContext interface
159     */
160     public XComponentContext getComponentContext()
161     {
162        return m_xComponentContext;
163     }
164 
165 }
166