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