diff options
Diffstat (limited to '')
5 files changed, 1177 insertions, 0 deletions
diff --git a/scripting/java/com/sun/star/script/framework/browse/DialogFactory.java b/scripting/java/com/sun/star/script/framework/browse/DialogFactory.java new file mode 100644 index 000000000..08cfba4c8 --- /dev/null +++ b/scripting/java/com/sun/star/script/framework/browse/DialogFactory.java @@ -0,0 +1,255 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.script.framework.browse; + +import com.sun.star.awt.XButton; +import com.sun.star.awt.XControl; +import com.sun.star.awt.XControlContainer; +import com.sun.star.awt.XControlModel; +import com.sun.star.awt.XDialog; +import com.sun.star.awt.XTextComponent; +import com.sun.star.awt.XToolkit; +import com.sun.star.awt.XWindow; + +import com.sun.star.beans.XPropertySet; + +import com.sun.star.container.XNameContainer; + +import com.sun.star.lang.EventObject; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.lang.XMultiServiceFactory; + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +public class DialogFactory { + + private static DialogFactory factory; + private final XComponentContext xComponentContext; + + // singleton + private DialogFactory(XComponentContext xComponentContext) { + this.xComponentContext = xComponentContext; + } + + public static void createDialogFactory(XComponentContext xComponentContext) { + synchronized (DialogFactory.class) { + if (factory == null) { + factory = new DialogFactory(xComponentContext); + } + } + } + + public static DialogFactory getDialogFactory() throws java.lang.Exception { + if (factory == null) { + throw new java.lang.Exception("DialogFactory not initialized"); + } + return factory; + } + + public String showInputDialog(String title, String prompt) { + final XDialog xDialog; + + try { + xDialog = createInputDialog(title, prompt); + } catch (com.sun.star.uno.Exception e) { + return null; + } + + // add an action listener to the button controls + XControlContainer controls = + UnoRuntime.queryInterface(XControlContainer.class, xDialog); + + XButton okButton = + UnoRuntime.queryInterface(XButton.class, controls.getControl("Ok")); + + okButton.setActionCommand("Ok"); + + XButton cancelButton = + UnoRuntime.queryInterface(XButton.class, controls.getControl("Cancel")); + + cancelButton.setActionCommand("Cancel"); + + final XTextComponent textField = + UnoRuntime.queryInterface(XTextComponent.class, + controls.getControl("NameField")); + + final ResultHolder resultHolder = new ResultHolder(); + + com.sun.star.awt.XActionListener listener = + new com.sun.star.awt.XActionListener() { + public void actionPerformed(com.sun.star.awt.ActionEvent e) { + if (e.ActionCommand.equals("Cancel")) { + resultHolder.setResult(null); + xDialog.endExecute(); + } else { + resultHolder.setResult(textField.getText()); + xDialog.endExecute(); + } + } + + public void disposing(EventObject o) { + // does nothing + } + }; + + okButton.addActionListener(listener); + cancelButton.addActionListener(listener); + + xDialog.execute(); + + return (String)resultHolder.getResult(); + } + + private void setDimensions(Object o, int x, int y, int width, int height) throws + com.sun.star.uno.Exception { + + XPropertySet props = UnoRuntime.queryInterface(XPropertySet.class, o); + + props.setPropertyValue("PositionX", Integer.valueOf(x)); + props.setPropertyValue("PositionY", Integer.valueOf(y)); + props.setPropertyValue("Height", Integer.valueOf(height)); + props.setPropertyValue("Width", Integer.valueOf(width)); + } + + private XDialog createInputDialog(String title, String prompt) throws + com.sun.star.uno.Exception { + + if (title == null || title.length() == 0) { + title = "Scripting Framework"; + } + + if (prompt == null || prompt.length() == 0) { + prompt = "Enter name"; + } + + // get the service manager from the component context + XMultiComponentFactory xMultiComponentFactory = + xComponentContext.getServiceManager(); + + // create the dialog model and set the properties + Object dialogModel = xMultiComponentFactory.createInstanceWithContext( + "com.sun.star.awt.UnoControlDialogModel", + xComponentContext); + + setDimensions(dialogModel, 100, 100, 157, 58); + + XPropertySet props = + UnoRuntime.queryInterface(XPropertySet.class, dialogModel); + + props.setPropertyValue("Title", title); + + // get the service manager from the dialog model + XMultiServiceFactory xMultiServiceFactory = + UnoRuntime.queryInterface(XMultiServiceFactory.class, dialogModel); + + // create the label model and set the properties + Object label = xMultiServiceFactory.createInstance( + "com.sun.star.awt.UnoControlFixedTextModel"); + + setDimensions(label, 15, 5, 134, 12); + + XPropertySet labelProps = + UnoRuntime.queryInterface(XPropertySet.class, label); + + labelProps.setPropertyValue("Name", "PromptLabel"); + labelProps.setPropertyValue("Label", prompt); + + // create the text field + Object edit = xMultiServiceFactory.createInstance( + "com.sun.star.awt.UnoControlEditModel"); + + setDimensions(edit, 15, 18, 134, 12); + + XPropertySet editProps = + UnoRuntime.queryInterface(XPropertySet.class, edit); + + editProps.setPropertyValue("Name", "NameField"); + + // create the OK button + Object okButtonModel = xMultiServiceFactory.createInstance( + "com.sun.star.awt.UnoControlButtonModel"); + + setDimensions(okButtonModel, 40, 39, 38, 15); + + XPropertySet buttonProps = + UnoRuntime.queryInterface(XPropertySet.class, okButtonModel); + + buttonProps.setPropertyValue("Name", "Ok"); + buttonProps.setPropertyValue("Label", "Ok"); + + // create the Cancel button + Object cancelButtonModel = xMultiServiceFactory.createInstance( + "com.sun.star.awt.UnoControlButtonModel"); + + setDimensions(cancelButtonModel, 83, 39, 38, 15); + + buttonProps = + UnoRuntime.queryInterface(XPropertySet.class, cancelButtonModel); + + buttonProps.setPropertyValue("Name", "Cancel"); + buttonProps.setPropertyValue("Label", "Cancel"); + + // insert the control models into the dialog model + XNameContainer xNameCont = + UnoRuntime.queryInterface(XNameContainer.class, dialogModel); + + xNameCont.insertByName("PromptLabel", label); + xNameCont.insertByName("NameField", edit); + xNameCont.insertByName("Ok", okButtonModel); + xNameCont.insertByName("Cancel", cancelButtonModel); + + // create the dialog control and set the model + Object dialog = xMultiComponentFactory.createInstanceWithContext( + "com.sun.star.awt.UnoControlDialog", + xComponentContext); + + XControl xControl = UnoRuntime.queryInterface(XControl.class, dialog); + + XControlModel xControlModel = + UnoRuntime.queryInterface(XControlModel.class, dialogModel); + + xControl.setModel(xControlModel); + + // create a peer + Object toolkit = xMultiComponentFactory.createInstanceWithContext( + "com.sun.star.awt.ExtToolkit", + xComponentContext); + + XToolkit xToolkit = UnoRuntime.queryInterface(XToolkit.class, toolkit); + XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, xControl); + xWindow.setVisible(false); + xControl.createPeer(xToolkit, null); + + return UnoRuntime.queryInterface(XDialog.class, dialog); + } + + private static class ResultHolder { + + private Object result = null; + + public Object getResult() { + return result; + } + + public void setResult(Object result) { + this.result = result; + } + } +}
\ No newline at end of file diff --git a/scripting/java/com/sun/star/script/framework/browse/ParcelBrowseNode.java b/scripting/java/com/sun/star/script/framework/browse/ParcelBrowseNode.java new file mode 100644 index 000000000..cdb8715a5 --- /dev/null +++ b/scripting/java/com/sun/star/script/framework/browse/ParcelBrowseNode.java @@ -0,0 +1,309 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.script.framework.browse; + +import com.sun.star.beans.XIntrospectionAccess; + +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.lib.uno.helper.PropertySet; + +import com.sun.star.script.XInvocation; +import com.sun.star.script.browse.BrowseNodeTypes; +import com.sun.star.script.browse.XBrowseNode; +import com.sun.star.script.framework.container.Parcel; +import com.sun.star.script.framework.container.ParcelContainer; +import com.sun.star.script.framework.container.ScriptEntry; +import com.sun.star.script.framework.container.ScriptMetaData; +import com.sun.star.script.framework.log.LogUtils; +import com.sun.star.script.framework.provider.ScriptProvider; + +import com.sun.star.ucb.XSimpleFileAccess; + +import com.sun.star.uno.Any; +import com.sun.star.uno.AnyConverter; +import com.sun.star.uno.Type; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +import java.util.ArrayList; +import java.util.Collection; + +import javax.swing.JOptionPane; + +public class ParcelBrowseNode extends PropertySet implements + XBrowseNode, XInvocation { + + private final ScriptProvider provider; + private Collection<XBrowseNode> browsenodes; + private final ParcelContainer container; + private Parcel parcel; + // these four are properties, they are accessed via reflection + public boolean deletable = true; + public boolean editable = false; + public boolean creatable = false; + public boolean renamable = true; + + public ParcelBrowseNode(ScriptProvider provider, ParcelContainer container, + String parcelName) throws + com.sun.star.container.NoSuchElementException, + com.sun.star.lang.WrappedTargetException { + + this.provider = provider; + this.container = container; + + this.parcel = (Parcel)this.container.getByName(parcelName); + + registerProperty("Deletable", new Type(boolean.class), (short)0, "deletable"); + registerProperty("Editable", new Type(boolean.class), (short)0, "editable"); + registerProperty("Creatable", new Type(boolean.class), (short)0, "creatable"); + registerProperty("Renamable", new Type(boolean.class), (short)0, "renamable"); + if (provider.hasScriptEditor()) + { + this.creatable = true; + } + + String parcelDirUrl = parcel.getPathToParcel(); + XComponentContext xCtx = provider.getScriptingContext().getComponentContext(); + XMultiComponentFactory xFac = xCtx.getServiceManager(); + + try { + XSimpleFileAccess xSFA = UnoRuntime.queryInterface(XSimpleFileAccess.class, + xFac.createInstanceWithContext( + "com.sun.star.ucb.SimpleFileAccess", + xCtx)); + if ( xSFA != null && ( xSFA.isReadOnly( parcelDirUrl ) || + container.isUnoPkg() ) ) + { + deletable = false; + editable = false; + creatable = false; + renamable = false; + } + } catch (com.sun.star.uno.Exception e) { + // TODO propagate potential errors + LogUtils.DEBUG("Caught exception creating ParcelBrowseNode " + e); + LogUtils.DEBUG(LogUtils.getTrace(e)); + } + + } + + public String getName() { + return parcel.getName(); + } + + public XBrowseNode[] getChildNodes() { + try { + + if (hasChildNodes()) { + String[] names = parcel.getElementNames(); + browsenodes = new ArrayList<XBrowseNode>(names.length); + + for (String name : names) { + browsenodes.add(new ScriptBrowseNode(provider, parcel, name)); + } + } else { + LogUtils.DEBUG("ParcelBrowseNode.getChildeNodes no children "); + return new XBrowseNode[0]; + } + } catch (Exception e) { + LogUtils.DEBUG("Failed to getChildeNodes, exception: " + e); + LogUtils.DEBUG(LogUtils.getTrace(e)); + return new XBrowseNode[0]; + } + + return browsenodes.toArray(new XBrowseNode[browsenodes.size()]); + } + + public boolean hasChildNodes() { + if (container != null && parcel != null && container.hasByName(getName())) { + return parcel.hasElements(); + } + + return false; + } + + public short getType() { + return BrowseNodeTypes.CONTAINER; + } + + @Override + public String toString() { + return getName(); + } + + // implementation of XInvocation interface + public XIntrospectionAccess getIntrospection() { + return null; + } + + public Object invoke(String aFunctionName, Object[] aParams, + short[][] aOutParamIndex, Object[][] aOutParam) throws + com.sun.star.lang.IllegalArgumentException, + com.sun.star.script.CannotConvertException, + com.sun.star.reflection.InvocationTargetException { + + LogUtils.DEBUG("ParcelBrowseNode invoke for " + aFunctionName); + + // Initialise the out parameters - not used but prevents error in + // UNO bridge + aOutParamIndex[0] = new short[0]; + aOutParam[0] = new Object[0]; + + Any result = new Any(new Type(Boolean.class), Boolean.TRUE); + + if (aFunctionName.equals("Creatable")) { + try { + String newName; + + if (aParams == null || aParams.length < 1 + || !AnyConverter.isString(aParams[0])) { + + String prompt = "Enter name for new Script"; + String title = "Create Script"; + + // try to get a DialogFactory instance, if it fails + // just use a Swing JOptionPane to prompt for the name + try { + DialogFactory dialogFactory = DialogFactory.getDialogFactory(); + newName = dialogFactory.showInputDialog(title, prompt); + } catch (Exception e) { + + newName = JOptionPane.showInputDialog(null, prompt, title, + JOptionPane.QUESTION_MESSAGE); + + } + } else { + newName = AnyConverter.toString(aParams[0]); + } + + if (newName == null || newName.length() == 0) { + result = new Any(new Type(Boolean.class), Boolean.FALSE); + } else { + String source = provider.getScriptEditor().getTemplate(); + + String languageName = + newName + "." + provider.getScriptEditor().getExtension(); + + String language = container.getLanguage(); + + ScriptEntry entry = new ScriptEntry(language, languageName); + + Parcel parcel = (Parcel)container.getByName(getName()); + ScriptMetaData data = new ScriptMetaData(parcel, entry, source); + parcel.insertByName(languageName, data); + + ScriptBrowseNode sbn = + new ScriptBrowseNode(provider, parcel, languageName); + + if (browsenodes == null) { + LogUtils.DEBUG("browsenodes null!!"); + browsenodes = new ArrayList<XBrowseNode>(4); + } + + browsenodes.add(sbn); + result = new Any(new Type(XBrowseNode.class), sbn); + } + } catch (Exception e) { + LogUtils.DEBUG("ParcelBrowseNode[create] failed with: " + e); + LogUtils.DEBUG(LogUtils.getTrace(e)); + result = new Any(new Type(Boolean.class), Boolean.FALSE); + } + } else if (aFunctionName.equals("Deletable")) { + try { + if (container.deleteParcel(getName())) { + result = new Any(new Type(Boolean.class), Boolean.TRUE); + } else { + result = new Any(new Type(Boolean.class), Boolean.FALSE); + } + } catch (Exception e) { + result = new Any(new Type(Boolean.class), Boolean.FALSE); + } + } else if (aFunctionName.equals("Renamable")) { + + String newName = null; + + try { + + if (aParams == null || aParams.length < 1 || + !AnyConverter.isString(aParams[0])) { + String prompt = "Enter new name for Library"; + String title = "Rename Library"; + + // try to get a DialogFactory instance, if it fails + // just use a Swing JOptionPane to prompt for the name + try { + DialogFactory dialogFactory = DialogFactory.getDialogFactory(); + newName = dialogFactory.showInputDialog(title, prompt); + } catch (Exception e) { + + newName = JOptionPane.showInputDialog(null, prompt, title, + JOptionPane.QUESTION_MESSAGE); + + } + } else { + newName = AnyConverter.toString(aParams[0]); + } + + container.renameParcel(getName(), newName); + Parcel p = (Parcel)container.getByName(newName); + + if (browsenodes == null) { + getChildNodes(); + } + + ScriptBrowseNode[] childNodes = + browsenodes.toArray(new ScriptBrowseNode[browsenodes.size()]); + + for (int index = 0; index < childNodes.length; index++) { + childNodes[ index ].updateURI(p); + } + + result = new Any(new Type(XBrowseNode.class), this); + } catch (Exception e) { + result = new Any(new Type(Boolean.class), Boolean.FALSE); + } + } + + else { + throw new com.sun.star.lang.IllegalArgumentException( + "Function " + aFunctionName + " not supported."); + } + + return result; + } + + public void setValue(String aPropertyName, Object aValue) throws + com.sun.star.beans.UnknownPropertyException, + com.sun.star.script.CannotConvertException, + com.sun.star.reflection.InvocationTargetException { + } + + public Object getValue(String aPropertyName) throws + com.sun.star.beans.UnknownPropertyException { + return null; + } + + public boolean hasMethod(String aName) { + return false; + } + + public boolean hasProperty(String aName) { + return false; + } +} diff --git a/scripting/java/com/sun/star/script/framework/browse/PkgProviderBrowseNode.java b/scripting/java/com/sun/star/script/framework/browse/PkgProviderBrowseNode.java new file mode 100644 index 000000000..6cdfd2a77 --- /dev/null +++ b/scripting/java/com/sun/star/script/framework/browse/PkgProviderBrowseNode.java @@ -0,0 +1,49 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.script.framework.browse; + +import com.sun.star.script.framework.container.ParcelContainer; +import com.sun.star.script.framework.log.LogUtils; +import com.sun.star.script.framework.provider.ScriptProvider; + +import com.sun.star.uno.XComponentContext; + +public class PkgProviderBrowseNode extends ProviderBrowseNode { + + public PkgProviderBrowseNode(ScriptProvider provider, ParcelContainer container, + XComponentContext xCtx) { + + super(provider, container, xCtx); + + LogUtils.DEBUG("*** PkgProviderBrowseNode ctor container name = " + + container.getName()); + LogUtils.DEBUG("*** PkgProviderBrowseNode ctor container path = " + + container.getParcelContainerDir()); + LogUtils.DEBUG("*** PkgProviderBrowseNode ctor, container has num parcels = " + + container.getElementNames().length); + deletable = false; + editable = false; + creatable = false; + } + + @Override public String getName() { + return (container != null) ? container.getName() : "Unknown"; + } + +} diff --git a/scripting/java/com/sun/star/script/framework/browse/ProviderBrowseNode.java b/scripting/java/com/sun/star/script/framework/browse/ProviderBrowseNode.java new file mode 100644 index 000000000..91c2566be --- /dev/null +++ b/scripting/java/com/sun/star/script/framework/browse/ProviderBrowseNode.java @@ -0,0 +1,264 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.script.framework.browse; + +import com.sun.star.beans.XIntrospectionAccess; + +import com.sun.star.lang.XMultiComponentFactory; + +import com.sun.star.lib.uno.helper.PropertySet; + +import com.sun.star.script.XInvocation; +import com.sun.star.script.browse.BrowseNodeTypes; +import com.sun.star.script.browse.XBrowseNode; +import com.sun.star.script.framework.container.ParcelContainer; +import com.sun.star.script.framework.log.LogUtils; +import com.sun.star.script.framework.provider.ScriptProvider; + +import com.sun.star.ucb.XSimpleFileAccess; + +import com.sun.star.uno.Any; +import com.sun.star.uno.AnyConverter; +import com.sun.star.uno.Type; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +import java.util.ArrayList; +import java.util.Collection; + +import javax.swing.JOptionPane; + +public class ProviderBrowseNode extends PropertySet implements + XBrowseNode, XInvocation { + + private final ScriptProvider provider; + private Collection<XBrowseNode> browsenodes; + private final String name; + protected ParcelContainer container; + private final XComponentContext m_xCtx; + // these are properties, they are accessed by reflection + public boolean deletable = true; + public boolean creatable = true; + public boolean editable = false; + + public ProviderBrowseNode(ScriptProvider provider, ParcelContainer container, + XComponentContext xCtx) { + + LogUtils.DEBUG("*** ProviderBrowseNode ctor"); + this.container = container; + this.name = this.container.getLanguage(); + this.provider = provider; + this.m_xCtx = xCtx; + + registerProperty("Deletable", new Type(boolean.class), (short)0, "deletable"); + registerProperty("Creatable", new Type(boolean.class), (short)0, "creatable"); + registerProperty("Editable", new Type(boolean.class), (short)0, "editable"); + XMultiComponentFactory xFac = m_xCtx.getServiceManager(); + + try { + XSimpleFileAccess xSFA = UnoRuntime.queryInterface(XSimpleFileAccess.class, + xFac.createInstanceWithContext( + "com.sun.star.ucb.SimpleFileAccess", + xCtx)); + if ( container.isUnoPkg() || xSFA.isReadOnly( container.getParcelContainerDir() ) ) + { + deletable = false; + creatable = false; + } + } + // TODO propagate errors + catch (com.sun.star.uno.Exception e) { + LogUtils.DEBUG("Caught exception in creation of ProviderBrowseNode "); + LogUtils.DEBUG(LogUtils.getTrace(e)); + } + } + + public String getName() { + return name; + } + + public XBrowseNode[] getChildNodes() { + LogUtils.DEBUG("***** ProviderBrowseNode.getChildNodes()"); + + if (hasChildNodes()) { + + // needs initialisation? + LogUtils.DEBUG("** ProviderBrowseNode.getChildNodes(), container is " + + container); + + String[] parcels = container.getElementNames(); + browsenodes = new ArrayList<XBrowseNode>(parcels.length); + + for (String parcel : parcels) { + try { + XBrowseNode node = new ParcelBrowseNode(provider, container, parcel); + browsenodes.add(node); + } catch (Exception e) { + LogUtils.DEBUG("*** Failed to create parcel node for " + parcel); + LogUtils.DEBUG(e.toString()); + } + } + + ParcelContainer[] packageContainers = container.getChildContainers(); + + LogUtils.DEBUG("**** For container named " + container.getName() + + " with root path " + container.getParcelContainerDir() + + " has " + packageContainers.length + " child containers "); + + for (ParcelContainer packageContainer : packageContainers) { + + XBrowseNode node = + new PkgProviderBrowseNode(provider, packageContainer, m_xCtx); + + browsenodes.add(node); + } + } else { + LogUtils.DEBUG("*** No container available"); + return new XBrowseNode[0]; + } + + return browsenodes.toArray(new XBrowseNode[browsenodes.size()]); + } + + public boolean hasChildNodes() { + boolean result = true; + + if (container == null || + (!container.hasElements() && container.getChildContainers().length == 0)) { + + result = false; + } + + if (container == null) { + LogUtils.DEBUG("***** ProviderBrowseNode.hasChildNodes(): " + "name=" + name + + ", path=<none>, result=" + result); + } else { + LogUtils.DEBUG("***** ProviderBrowseNode.hasChildNodes(): " + "name=" + name + + ", path=" + container.getParcelContainerDir() + ", result=" + result); + } + + return result; + } + + public short getType() { + return BrowseNodeTypes.CONTAINER; + } + + @Override + public String toString() { + return getName(); + } + + // implementation of XInvocation interface + public XIntrospectionAccess getIntrospection() { + return null; + } + + public Object invoke(String aFunctionName, Object[] aParams, + short[][] aOutParamIndex, Object[][] aOutParam) throws + com.sun.star.lang.IllegalArgumentException, + com.sun.star.script.CannotConvertException, + com.sun.star.reflection.InvocationTargetException { + + // Initialise the out parameters - not used but prevents error in + // UNO bridge + aOutParamIndex[0] = new short[0]; + aOutParam[0] = new Object[0]; + + Any result = new Any(new Type(Boolean.class), Boolean.TRUE); + + if (aFunctionName.equals("Creatable")) { + try { + String name; + + if (aParams == null || aParams.length < 1 || + !AnyConverter.isString(aParams[0])) { + + String prompt = "Enter name for new Parcel"; + String title = "Create Parcel"; + + // try to get a DialogFactory instance, if it fails + // just use a Swing JOptionPane to prompt for the name + try { + DialogFactory dialogFactory = DialogFactory.getDialogFactory(); + name = dialogFactory.showInputDialog(title, prompt); + } catch (Exception e) { + + name = JOptionPane.showInputDialog(null, prompt, title, + JOptionPane.QUESTION_MESSAGE); + + } + } else { + name = AnyConverter.toString(aParams[0]); + } + + if (name == null || name.length() == 0) { + result = new Any(new Type(Boolean.class), Boolean.FALSE); + } else { + + Object newParcel = container.createParcel(name); + LogUtils.DEBUG("Parcel created " + name + " " + newParcel); + + if (newParcel == null) { + result = new Any(new Type(Boolean.class), Boolean.FALSE); + } else { + ParcelBrowseNode parcel = new ParcelBrowseNode(provider, container, name); + LogUtils.DEBUG("created parcel node "); + + if (browsenodes == null) { + browsenodes = new ArrayList<XBrowseNode>(5); + } + + browsenodes.add(parcel); + result = new Any(new Type(XBrowseNode.class), parcel); + } + } + } catch (Exception e) { + LogUtils.DEBUG("ProviderBrowseNode[create] failed with: " + e); + LogUtils.DEBUG(LogUtils.getTrace(e)); + result = new Any(new Type(Boolean.class), Boolean.FALSE); + } + } else { + throw new com.sun.star.lang.IllegalArgumentException( + "Function " + aFunctionName + " not supported."); + } + + return result; + } + + public void setValue(String aPropertyName, Object aValue) throws + com.sun.star.beans.UnknownPropertyException, + com.sun.star.script.CannotConvertException, + com.sun.star.reflection.InvocationTargetException { + } + + public Object getValue(String aPropertyName) throws + com.sun.star.beans.UnknownPropertyException { + + return null; + } + + public boolean hasMethod(String aName) { + return false; + } + + public boolean hasProperty(String aName) { + return false; + } +} diff --git a/scripting/java/com/sun/star/script/framework/browse/ScriptBrowseNode.java b/scripting/java/com/sun/star/script/framework/browse/ScriptBrowseNode.java new file mode 100644 index 000000000..5f91b1a78 --- /dev/null +++ b/scripting/java/com/sun/star/script/framework/browse/ScriptBrowseNode.java @@ -0,0 +1,300 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package com.sun.star.script.framework.browse; + +import com.sun.star.beans.XIntrospectionAccess; + +import com.sun.star.container.ElementExistException; +import com.sun.star.container.NoSuchElementException; + +import com.sun.star.lang.NoSupportException; +import com.sun.star.lang.WrappedTargetException; +import com.sun.star.lang.XMultiComponentFactory; + +import com.sun.star.lib.uno.helper.PropertySet; + +import com.sun.star.reflection.InvocationTargetException; + +import com.sun.star.script.XInvocation; +import com.sun.star.script.browse.BrowseNodeTypes; +import com.sun.star.script.browse.XBrowseNode; +import com.sun.star.script.framework.container.Parcel; +import com.sun.star.script.framework.container.ScriptEntry; +import com.sun.star.script.framework.container.ScriptMetaData; +import com.sun.star.script.framework.log.LogUtils; +import com.sun.star.script.framework.provider.ScriptProvider; +import com.sun.star.script.provider.XScriptContext; + +import com.sun.star.ucb.XSimpleFileAccess; + +import com.sun.star.uno.Any; +import com.sun.star.uno.AnyConverter; +import com.sun.star.uno.Type; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +public class ScriptBrowseNode extends PropertySet implements + XBrowseNode, XInvocation { + + private final ScriptProvider provider; + + private Parcel parent; + private String name; + + // these are properties, accessed by reflection + public String uri; + public String description; + public boolean editable; + public boolean deletable = false; + public boolean renamable = false; + + public ScriptBrowseNode(ScriptProvider provider, Parcel parent, String name) { + + this.provider = provider; + this.name = name; + this.parent = parent; + ScriptMetaData data = null; + XComponentContext xCtx = provider.getScriptingContext().getComponentContext(); + XMultiComponentFactory xFac = xCtx.getServiceManager(); + + try { + data = parent.getByName( name ); + XSimpleFileAccess xSFA = UnoRuntime.queryInterface( + XSimpleFileAccess.class, + xFac.createInstanceWithContext( + "com.sun.star.ucb.SimpleFileAccess", + xCtx)); + + uri = data.getShortFormScriptURL(); + description = data.getDescription(); + if (provider.hasScriptEditor()) { + this.editable = true; + + try { + if (!parent.isUnoPkg() + && !xSFA.isReadOnly(parent.getPathToParcel())) { + + this.deletable = true; + this.renamable = true; + + } + } + // TODO propagate errors + catch (Exception e) { + LogUtils.DEBUG("Caught exception in creation of ScriptBrowseNode"); + LogUtils.DEBUG(LogUtils.getTrace(e)); + } + + } + + } + // TODO fix exception types to be caught here, should we rethrow? + catch (Exception e) { + + LogUtils.DEBUG("** caught exception getting script data for " + name + + " ->" + e.toString()); + + } + + registerProperty("Deletable", new Type(boolean.class), (short)0, "deletable"); + registerProperty("Editable", new Type(boolean.class), (short)0, "editable"); + registerProperty("Renamable", new Type(boolean.class), (short)0, "renamable"); + registerProperty("URI", new Type(String.class), (short)0, "uri"); + registerProperty("Description", new Type(String.class), (short)0, + "description"); + } + + public String getName() { + return name; + } + + public XBrowseNode[] getChildNodes() { + return new XBrowseNode[0]; + } + + public boolean hasChildNodes() { + return false; + } + + public short getType() { + return BrowseNodeTypes.SCRIPT; + } + + @Override + public String toString() { + return getName(); + } + + public void updateURI(Parcel p) { + parent = p; + + try { + ScriptMetaData data = parent.getByName(name); + uri = data.getShortFormScriptURL(); + } + // TODO fix exception types to be caught here, should we rethrow? + catch (Exception e) { + LogUtils.DEBUG("** caught exception getting script data for " + name + + " ->" + e.toString()); + } + } + + // implementation of XInvocation interface + public XIntrospectionAccess getIntrospection() { + return null; + } + + public Object invoke(String aFunctionName, Object[] aParams, + short[][] aOutParamIndex, Object[][] aOutParam) throws + com.sun.star.lang.IllegalArgumentException, + com.sun.star.script.CannotConvertException, + com.sun.star.reflection.InvocationTargetException { + + // Initialise the out parameters - not used but prevents error in + // UNO bridge + aOutParamIndex[0] = new short[0]; + aOutParam[0] = new Object[0]; + + Any result = new Any(new Type(Boolean.class), Boolean.TRUE); + + if (aFunctionName.equals("Editable")) { + if (!editable) { + + NoSupportException nse = + new NoSupportException(aFunctionName + " is not editable "); + + throw new InvocationTargetException( + "Scripting framework error editing script", null, nse); + + } + + XScriptContext ctxt = provider.getScriptingContext(); + ScriptMetaData data = null; + + try { + data = parent.getByName(name); + } catch (NoSuchElementException nse) { + throw new com.sun.star.lang.IllegalArgumentException(nse, + name + " does not exist or can't be found "); + } catch (com.sun.star.lang.WrappedTargetException wte) { + // rethrow + throw new InvocationTargetException( + "Scripting framework editing script ", null, wte.TargetException); + } + + provider.getScriptEditor().edit(ctxt, data); + } else if (aFunctionName.equals("Deletable")) { + if (!deletable) { + + NoSupportException nse = new NoSupportException( + aFunctionName + " is not supported for this node"); + + throw new InvocationTargetException( + "Scripting framework error deleting script", null, nse); + } + + try { + parent.removeByName(name); + result = new Any(new Type(Boolean.class), Boolean.TRUE); + } catch (NoSuchElementException nse) { + throw new com.sun.star.lang.IllegalArgumentException(nse, + name + " does not exist or can't be found "); + } catch (WrappedTargetException wte) { + // rethrow + throw new InvocationTargetException( + "Scripting framework deleting script ", null, wte.TargetException); + } + + } else if (aFunctionName.equals("Renamable")) { + result = new Any(new Type(XBrowseNode.class), new XBrowseNode[0]); + + if (!renamable) { + + NoSupportException nse = new NoSupportException( + aFunctionName + " is not supported for this node"); + + throw new InvocationTargetException( + "Scripting framework error renaming script", null, nse); + } + + try { + String newName = AnyConverter.toString(aParams[0]); + ScriptMetaData oldData = parent.getByName(name); + oldData.loadSource(); + String oldSource = oldData.getSource(); + + LogUtils.DEBUG("Create renamed script"); + + String languageName = + newName + "." + provider.getScriptEditor().getExtension(); + + String language = provider.getName(); + + ScriptEntry entry = new ScriptEntry(language, languageName); + + ScriptMetaData data = + new ScriptMetaData(parent, entry, oldSource); + + parent.insertByName(languageName, data); + + LogUtils.DEBUG("Now remove old script"); + parent.removeByName(name); + + uri = data.getShortFormScriptURL(); + name = languageName; + result = new Any(new Type(XBrowseNode.class), this); + } catch (NoSuchElementException nse) { + throw new com.sun.star.lang.IllegalArgumentException(nse, + name + " does not exist or can't be found "); + } catch (ElementExistException eee) { + // rethrow + throw new InvocationTargetException( + "Scripting framework error renaming script ", null, eee); + } catch (WrappedTargetException wte) { + // rethrow + throw new InvocationTargetException( + "Scripting framework rename script ", null, wte.TargetException); + } + } else { + throw new com.sun.star.lang.IllegalArgumentException( + "Function " + aFunctionName + " not supported."); + } + + return result; + } + + public void setValue(String aPropertyName, Object aValue) throws + com.sun.star.beans.UnknownPropertyException, + com.sun.star.script.CannotConvertException, + com.sun.star.reflection.InvocationTargetException { + } + + public Object getValue(String aPropertyName) throws + com.sun.star.beans.UnknownPropertyException { + + return null; + } + + public boolean hasMethod(String aName) { + return false; + } + + public boolean hasProperty(String aName) { + return false; + } +} |