Debian Codegraph @

scripting/java/com/sun/star/script/framework/browse/ParcelBrowseNode.java

raw
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.browse;
20
21 import com.sun.star.beans.XIntrospectionAccess;
22
23 import com.sun.star.lang.XMultiComponentFactory;
24 import com.sun.star.lib.uno.helper.PropertySet;
25
26 import com.sun.star.script.XInvocation;
27 import com.sun.star.script.browse.BrowseNodeTypes;
28 import com.sun.star.script.browse.XBrowseNode;
29 import com.sun.star.script.framework.container.Parcel;
30 import com.sun.star.script.framework.container.ParcelContainer;
31 import com.sun.star.script.framework.container.ScriptEntry;
32 import com.sun.star.script.framework.container.ScriptMetaData;
33 import com.sun.star.script.framework.log.LogUtils;
34 import com.sun.star.script.framework.provider.ScriptProvider;
35
36 import com.sun.star.ucb.XSimpleFileAccess;
37
38 import com.sun.star.uno.Any;
39 import com.sun.star.uno.AnyConverter;
40 import com.sun.star.uno.Type;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XComponentContext;
43
44 import java.util.ArrayList;
45 import java.util.Collection;
46
47 import javax.swing.JOptionPane;
48
49 public class ParcelBrowseNode extends PropertySet implements
50 XBrowseNode, XInvocation {
51
52 private final ScriptProvider provider;
53 private Collection<XBrowseNode> browsenodes;
54 private final ParcelContainer container;
55 private Parcel parcel;
56 // these four are properties, they are accessed via reflection
57 public boolean deletable = true;
58 public boolean editable = false;
59 public boolean creatable = false;
60 public boolean renamable = true;
61
62 public ParcelBrowseNode(ScriptProvider provider, ParcelContainer container,
63 String parcelName) throws
64 com.sun.star.container.NoSuchElementException,
65 com.sun.star.lang.WrappedTargetException {
66
67 this.provider = provider;
68 this.container = container;
69
70 this.parcel = (Parcel)this.container.getByName(parcelName);
71
72 registerProperty("Deletable", new Type(boolean.class), (short)0, "deletable");
73 registerProperty("Editable", new Type(boolean.class), (short)0, "editable");
74 registerProperty("Creatable", new Type(boolean.class), (short)0, "creatable");
75 registerProperty("Renamable", new Type(boolean.class), (short)0, "renamable");
76 if (provider.hasScriptEditor())
77 {
78 this.creatable = true;
79 }
80
81 String parcelDirUrl = parcel.getPathToParcel();
82 XComponentContext xCtx = provider.getScriptingContext().getComponentContext();
83 XMultiComponentFactory xFac = xCtx.getServiceManager();
84
85 try {
86 XSimpleFileAccess xSFA = UnoRuntime.queryInterface(XSimpleFileAccess.class,
87 xFac.createInstanceWithContext(
88 "com.sun.star.ucb.SimpleFileAccess",
89 xCtx));
90 if ( xSFA != null && ( xSFA.isReadOnly( parcelDirUrl ) ||
91 container.isUnoPkg() ) )
92 {
93 deletable = false;
94 editable = false;
95 creatable = false;
96 renamable = false;
97 }
98 } catch (com.sun.star.uno.Exception e) {
99 // TODO propagate potential errors
100 LogUtils.DEBUG("Caught exception creating ParcelBrowseNode " + e);
101 LogUtils.DEBUG(LogUtils.getTrace(e));
102 }
103
104 }
105
106 public String getName() {
107 return parcel.getName();
108 }
109
110 public XBrowseNode[] getChildNodes() {
111 try {
112
113 if (hasChildNodes()) {
114 String[] names = parcel.getElementNames();
115 browsenodes = new ArrayList<XBrowseNode>(names.length);
116
117 for (String name : names) {
118 browsenodes.add(new ScriptBrowseNode(provider, parcel, name));
119 }
120 } else {
121 LogUtils.DEBUG("ParcelBrowseNode.getChildeNodes no children ");
122 return new XBrowseNode[0];
123 }
124 } catch (Exception e) {
125 LogUtils.DEBUG("Failed to getChildeNodes, exception: " + e);
126 LogUtils.DEBUG(LogUtils.getTrace(e));
127 return new XBrowseNode[0];
128 }
129
130 return browsenodes.toArray(new XBrowseNode[browsenodes.size()]);
131 }
132
133 public boolean hasChildNodes() {
134 if (container != null && parcel != null && container.hasByName(getName())) {
135 return parcel.hasElements();
136 }
137
138 return false;
139 }
140
141 public short getType() {
142 return BrowseNodeTypes.CONTAINER;
143 }
144
145 @Override
146 public String toString() {
147 return getName();
148 }
149
150 // implementation of XInvocation interface
151 public XIntrospectionAccess getIntrospection() {
152 return null;
153 }
154
155 public Object invoke(String aFunctionName, Object[] aParams,
156 short[][] aOutParamIndex, Object[][] aOutParam) throws
157 com.sun.star.lang.IllegalArgumentException,
158 com.sun.star.script.CannotConvertException,
159 com.sun.star.reflection.InvocationTargetException {
160
161 LogUtils.DEBUG("ParcelBrowseNode invoke for " + aFunctionName);
162
163 // Initialise the out parameters - not used but prevents error in
164 // UNO bridge
165 aOutParamIndex[0] = new short[0];
166 aOutParam[0] = new Object[0];
167
168 Any result = new Any(new Type(Boolean.class), Boolean.TRUE);
169
170 if (aFunctionName.equals("Creatable")) {
171 try {
172 String newName;
173
174 if (aParams == null || aParams.length < 1
175 || !AnyConverter.isString(aParams[0])) {
176
177 String prompt = "Enter name for new Script";
178 String title = "Create Script";
179
180 // try to get a DialogFactory instance, if it fails
181 // just use a Swing JOptionPane to prompt for the name
182 try {
183 DialogFactory dialogFactory = DialogFactory.getDialogFactory();
184 newName = dialogFactory.showInputDialog(title, prompt);
185 } catch (Exception e) {
186
187 newName = JOptionPane.showInputDialog(null, prompt, title,
188 JOptionPane.QUESTION_MESSAGE);
189
190 }
191 } else {
192 newName = AnyConverter.toString(aParams[0]);
193 }
194
195 if (newName == null || newName.length() == 0) {
196 result = new Any(new Type(Boolean.class), Boolean.FALSE);
197 } else {
198 String source = provider.getScriptEditor().getTemplate();
199
200 String languageName =
201 newName + "." + provider.getScriptEditor().getExtension();
202
203 String language = container.getLanguage();
204
205 ScriptEntry entry = new ScriptEntry(language, languageName);
206
207 Parcel parcel = (Parcel)container.getByName(getName());
208 ScriptMetaData data = new ScriptMetaData(parcel, entry, source);
209 parcel.insertByName(languageName, data);
210
211 ScriptBrowseNode sbn =
212 new ScriptBrowseNode(provider, parcel, languageName);
213
214 if (browsenodes == null) {
215 LogUtils.DEBUG("browsenodes null!!");
216 browsenodes = new ArrayList<XBrowseNode>(4);
217 }
218
219 browsenodes.add(sbn);
220 result = new Any(new Type(XBrowseNode.class), sbn);
221 }
222 } catch (Exception e) {
223 LogUtils.DEBUG("ParcelBrowseNode[create] failed with: " + e);
224 LogUtils.DEBUG(LogUtils.getTrace(e));
225 result = new Any(new Type(Boolean.class), Boolean.FALSE);
226 }
227 } else if (aFunctionName.equals("Deletable")) {
228 try {
229 if (container.deleteParcel(getName())) {
230 result = new Any(new Type(Boolean.class), Boolean.TRUE);
231 } else {
232 result = new Any(new Type(Boolean.class), Boolean.FALSE);
233 }
234 } catch (Exception e) {
235 result = new Any(new Type(Boolean.class), Boolean.FALSE);
236 }
237 } else if (aFunctionName.equals("Renamable")) {
238
239 String newName = null;
240
241 try {
242
243 if (aParams == null || aParams.length < 1 ||
244 !AnyConverter.isString(aParams[0])) {
245 String prompt = "Enter new name for Library";
246 String title = "Rename Library";
247
248 // try to get a DialogFactory instance, if it fails
249 // just use a Swing JOptionPane to prompt for the name
250 try {
251 DialogFactory dialogFactory = DialogFactory.getDialogFactory();
252 newName = dialogFactory.showInputDialog(title, prompt);
253 } catch (Exception e) {
254
255 newName = JOptionPane.showInputDialog(null, prompt, title,
256 JOptionPane.QUESTION_MESSAGE);
257
258 }
259 } else {
260 newName = AnyConverter.toString(aParams[0]);
261 }
262
263 container.renameParcel(getName(), newName);
264 Parcel p = (Parcel)container.getByName(newName);
265
266 if (browsenodes == null) {
267 getChildNodes();
268 }
269
270 ScriptBrowseNode[] childNodes =
271 browsenodes.toArray(new ScriptBrowseNode[browsenodes.size()]);
272
273 for (int index = 0; index < childNodes.length; index++) {
274 childNodes[ index ].updateURI(p);
275 }
276
277 result = new Any(new Type(XBrowseNode.class), this);
278 } catch (Exception e) {
279 result = new Any(new Type(Boolean.class), Boolean.FALSE);
280 }
281 }
282
283 else {
284 throw new com.sun.star.lang.IllegalArgumentException(
285 "Function " + aFunctionName + " not supported.");
286 }
287
288 return result;
289 }
290
291 public void setValue(String aPropertyName, Object aValue) throws
292 com.sun.star.beans.UnknownPropertyException,
293 com.sun.star.script.CannotConvertException,
294 com.sun.star.reflection.InvocationTargetException {
295 }
296
297 public Object getValue(String aPropertyName) throws
298 com.sun.star.beans.UnknownPropertyException {
299 return null;
300 }
301
302 public boolean hasMethod(String aName) {
303 return false;
304 }
305
306 public boolean hasProperty(String aName) {
307 return false;
308 }
309 }