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 
24 package com.sun.star.script.framework.browse;
25 
26 import com.sun.star.lib.uno.helper.PropertySet;
27 import com.sun.star.uno.Any;
28 import com.sun.star.uno.AnyConverter;
29 import com.sun.star.uno.Type;
30 import com.sun.star.uno.XComponentContext;
31 import com.sun.star.uno.UnoRuntime;
32 
33 import com.sun.star.lang.XMultiComponentFactory;
34 
35 import com.sun.star.ucb.XSimpleFileAccess;
36 
37 import com.sun.star.beans.XIntrospectionAccess;
38 import com.sun.star.script.XInvocation;
39 
40 import com.sun.star.script.browse.XBrowseNode;
41 import com.sun.star.script.browse.BrowseNodeTypes;
42 
43 import com.sun.star.script.framework.provider.ScriptProvider;
44 import com.sun.star.script.framework.log.*;
45 import com.sun.star.script.framework.container.*;
46 import com.sun.star.script.framework.browse.DialogFactory;
47 
48 import java.util.*;
49 import javax.swing.JOptionPane;
50 
51 public class ProviderBrowseNode extends PropertySet
52     implements XBrowseNode, XInvocation
53 {
54     protected ScriptProvider provider;
55     protected Collection browsenodes;
56     protected String name;
57     protected ParcelContainer container;
58     protected Parcel parcel;
59     protected XComponentContext m_xCtx;
60 
61     public boolean deletable = true;
62     public boolean creatable = true;
63     public boolean editable = false;
64 
ProviderBrowseNode( ScriptProvider provider, ParcelContainer container, XComponentContext xCtx )65     public ProviderBrowseNode( ScriptProvider provider, ParcelContainer container, XComponentContext xCtx ) {
66         LogUtils.DEBUG("*** ProviderBrowseNode ctor");
67         this.container = container;
68         this.name = this.container.getLanguage();
69         this.provider = provider;
70         this.m_xCtx = xCtx;
71 
72         registerProperty("Deletable", new Type(boolean.class),
73             (short)0, "deletable");
74         registerProperty("Creatable", new Type(boolean.class),
75             (short)0, "creatable");
76         registerProperty("Editable", new Type(boolean.class),
77             (short)0, "editable");
78         XSimpleFileAccess xSFA = null;
79         XMultiComponentFactory xFac = m_xCtx.getServiceManager();
80         try
81         {
82             xSFA = ( XSimpleFileAccess)
83                 UnoRuntime.queryInterface( XSimpleFileAccess.class,
84                     xFac.createInstanceWithContext(
85                         "com.sun.star.ucb.SimpleFileAccess",
86                         xCtx ) );
87             if (  container.isUnoPkg() || xSFA.isReadOnly( container.getParcelContainerDir() ) )
88             {
89                 deletable = false;
90                 creatable = false;
91             }
92         }
93         // TODO propage errors
94         catch( com.sun.star.uno.Exception e )
95         {
96                 LogUtils.DEBUG("Caught exception in creation of ProviderBrowseNode ");
97                 LogUtils.DEBUG( LogUtils.getTrace(e));
98 
99         }
100     }
101 
getName()102     public String getName() {
103         return name;
104     }
105 
getChildNodes()106     public XBrowseNode[] getChildNodes() {
107         LogUtils.DEBUG("***** ProviderBrowseNode.getChildNodes()");
108         if ( hasChildNodes() )
109         {
110             // needs initialisation?
111             LogUtils.DEBUG("** ProviderBrowseNode.getChildNodes(), container is " + container );
112             String[] parcels = container.getElementNames();
113             browsenodes = new ArrayList( parcels.length );
114             for ( int index = 0; index < parcels.length; index++ )
115             {
116                 try
117                 {
118                     XBrowseNode node  = new ParcelBrowseNode( provider, container, parcels[ index ] );
119                     browsenodes.add( node );
120                 }
121                 catch ( Exception e )
122                 {
123                     LogUtils.DEBUG("*** Failed to create parcel node for " + parcels[ index ] );
124                     LogUtils.DEBUG( e.toString() );
125                 }
126             }
127             ParcelContainer[] packageContainers = container.getChildContainers();
128             LogUtils.DEBUG("**** For container named " + container.getName() + " with root path " + container.getParcelContainerDir() + " has " + packageContainers.length + " child containers " );
129 
130             for ( int i = 0; i < packageContainers.length; i++ )
131             {
132                 XBrowseNode node = new PkgProviderBrowseNode( provider, packageContainers[ i ], m_xCtx );
133                 browsenodes.add( node );
134             }
135         }
136         else
137         {
138             LogUtils.DEBUG("*** No container available");
139             return new XBrowseNode[0];
140         }
141         return ( XBrowseNode[] )browsenodes.toArray( new XBrowseNode[0] );
142     }
143 
hasChildNodes()144     public boolean hasChildNodes() {
145         boolean result = true;
146 
147         if ( container == null ||
148              ( !container.hasElements() &&
149                container.getChildContainers().length == 0 ) )
150         {
151             result = false;
152         }
153 
154         LogUtils.DEBUG("***** ProviderBrowseNode.hasChildNodes(): " +
155             "name=" + name +
156             ", path=" + container.getParcelContainerDir() +
157             ", result=" + result );
158 
159         return result;
160     }
161 
getType()162     public short getType() {
163         return BrowseNodeTypes.CONTAINER;
164     }
165 
toString()166     public String toString()
167     {
168         return getName();
169     }
170 
171     // implementation of XInvocation interface
getIntrospection()172     public XIntrospectionAccess getIntrospection() {
173         return null;
174     }
175 
invoke(String aFunctionName, Object[] aParams, short[][] aOutParamIndex, Object[][] aOutParam)176     public Object invoke(String aFunctionName, Object[] aParams,
177                          short[][] aOutParamIndex, Object[][] aOutParam)
178         throws com.sun.star.lang.IllegalArgumentException,
179                com.sun.star.script.CannotConvertException,
180                com.sun.star.reflection.InvocationTargetException
181     {
182         // Initialise the out paramters - not used but prevents error in
183         // UNO bridge
184         aOutParamIndex[0] = new short[0];
185         aOutParam[0] = new Object[0];
186 
187         Any result = new Any(new Type(Boolean.class), Boolean.TRUE);
188 
189         if (aFunctionName.equals("Creatable"))
190         {
191             try
192             {
193                 String name;
194 
195                 if (aParams == null || aParams.length < 1 ||
196                     AnyConverter.isString(aParams[0]) == false)
197                 {
198                     String prompt = "Enter name for new Parcel";
199                     String title = "Create Parcel";
200 
201                     // try to get a DialogFactory instance, if it fails
202                     // just use a Swing JOptionPane to prompt for the name
203                     try
204                     {
205                         DialogFactory dialogFactory =
206                             DialogFactory.getDialogFactory();
207 
208                         name = dialogFactory.showInputDialog(title, prompt);
209                     }
210                     catch (Exception e)
211                     {
212                         name = JOptionPane.showInputDialog(null, prompt, title,
213                             JOptionPane.QUESTION_MESSAGE);
214                     }
215                 }
216                 else {
217                     name = (String) AnyConverter.toString(aParams[0]);
218                 }
219 
220                 if (name == null || name.equals(""))
221                 {
222                     result =  new Any(new Type(Boolean.class), Boolean.FALSE);
223                 }
224                 else
225                 {
226 
227                     Object newParcel  = container.createParcel( name );
228                     LogUtils.DEBUG("Parcel created " + name + " " + newParcel );
229                     if ( newParcel == null )
230                     {
231                         result =  new Any(new Type(Boolean.class), Boolean.FALSE);
232                     }
233                     else
234                     {
235                         ParcelBrowseNode parcel = new ParcelBrowseNode( provider, container, name );
236                         LogUtils.DEBUG("created parcel node ");
237                         if ( browsenodes == null )
238                         {
239                             browsenodes = new ArrayList( 5 );
240                         }
241 
242                         browsenodes.add(parcel);
243 
244 
245                         result = new Any(new Type(XBrowseNode.class), parcel);
246                     }
247                 }
248             }
249             catch (Exception e)
250             {
251 		LogUtils.DEBUG("ProviderBrowseNode[create] failed with: " + e );
252                 LogUtils.DEBUG( LogUtils.getTrace( e ) );
253                 result = new Any(new Type(Boolean.class), Boolean.FALSE);
254 
255                 // throw new com.sun.star.reflection.InvocationTargetException(
256                 //     "Error creating script: " + e.getMessage());
257             }
258         }
259         else {
260             throw new com.sun.star.lang.IllegalArgumentException(
261                 "Function " + aFunctionName + " not supported.");
262         }
263 
264         return result;
265     }
266 
setValue(String aPropertyName, Object aValue)267     public void setValue(String aPropertyName, Object aValue)
268         throws com.sun.star.beans.UnknownPropertyException,
269                com.sun.star.script.CannotConvertException,
270                com.sun.star.reflection.InvocationTargetException
271     {
272     }
273 
getValue(String aPropertyName)274     public Object getValue(String aPropertyName)
275         throws com.sun.star.beans.UnknownPropertyException
276     {
277         return null;
278     }
279 
hasMethod(String aName)280     public boolean hasMethod(String aName) {
281         return false;
282     }
283 
hasProperty(String aName)284     public boolean hasProperty(String aName) {
285         return false;
286     }
287 }
288