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