| 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 |
// this script serves as an example of how to launch a Basic Dialog |
| 19 |
// from a script |
| 20 |
import com.sun.star.uno.UnoRuntime; |
| 21 |
import com.sun.star.script.provider.XScriptContext; |
| 22 |
import com.sun.star.lang.XMultiComponentFactory; |
| 23 |
import com.sun.star.lang.EventObject; |
| 24 |
import com.sun.star.uno.Type; |
| 25 |
import com.sun.star.uno.AnyConverter; |
| 26 |
import com.sun.star.text.XTextDocument; |
| 27 |
import com.sun.star.beans.PropertyValue; |
| 28 |
import com.sun.star.script.XLibraryContainer; |
| 29 |
import com.sun.star.awt.*; |
| 30 |
import com.sun.star.util.*; |
| 31 |
|
| 32 |
boolean tryLoadingLibrary( xmcf, context, name ) |
| 33 |
{ |
| 34 |
try |
| 35 |
{ |
| 36 |
obj = xmcf.createInstanceWithContext( |
| 37 |
"com.sun.star.script.Application" + name + "LibraryContainer", |
| 38 |
context.getComponentContext()); |
| 39 |
|
| 40 |
xLibraryContainer = (XLibraryContainer) |
| 41 |
UnoRuntime.queryInterface(XLibraryContainer.class, obj); |
| 42 |
|
| 43 |
System.err.println("Got XLibraryContainer"); |
| 44 |
|
| 45 |
serviceObj = context.getComponentContext().getValueByName( |
| 46 |
"/singletons/com.sun.star.util.theMacroExpander"); |
| 47 |
|
| 48 |
xme = (XMacroExpander) AnyConverter.toObject( |
| 49 |
new Type(XMacroExpander.class), serviceObj); |
| 50 |
|
| 51 |
bootstrapName = "bootstraprc"; |
| 52 |
if (System.getProperty("os.name").startsWith("Windows")) |
| 53 |
{ |
| 54 |
bootstrapName = "bootstrap.ini"; |
| 55 |
} |
| 56 |
|
| 57 |
libURL = xme.expandMacros( |
| 58 |
"$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/basic/ScriptBindingLibrary/" + |
| 59 |
name.toLowerCase() + ".xlb/"); |
| 60 |
|
| 61 |
System.err.println("libURL is: " + libURL); |
| 62 |
|
| 63 |
xLibraryContainer.createLibraryLink( |
| 64 |
"ScriptBindingLibrary", libURL, false); |
| 65 |
|
| 66 |
System.err.println("liblink created"); |
| 67 |
|
| 68 |
} |
| 69 |
catch (com.sun.star.uno.Exception e) |
| 70 |
{ |
| 71 |
System.err.println("Got an exception loading lib: " + e.getMessage()); |
| 72 |
return false; |
| 73 |
} |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
// get the XMultiComponentFactory from the XSCRIPTCONTEXT |
| 78 |
XMultiComponentFactory xmcf = |
| 79 |
XSCRIPTCONTEXT.getComponentContext().getServiceManager(); |
| 80 |
|
| 81 |
Object[] args = new Object[1]; |
| 82 |
args[0] = XSCRIPTCONTEXT.getDocument(); |
| 83 |
|
| 84 |
Object obj; |
| 85 |
try { |
| 86 |
// try to create an instance of the DialogProvider |
| 87 |
obj = xmcf.createInstanceWithArgumentsAndContext( |
| 88 |
"com.sun.star.awt.DialogProvider", args, |
| 89 |
XSCRIPTCONTEXT.getComponentContext()); |
| 90 |
/* |
| 91 |
obj = xmcf.createInstanceWithContext( |
| 92 |
"com.sun.star.awt.DialogProvider", |
| 93 |
XSCRIPTCONTEXT.getComponentContext()); |
| 94 |
*/ |
| 95 |
} |
| 96 |
catch (com.sun.star.uno.Exception e) { |
| 97 |
System.err.println("Error getting DialogProvider object"); |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
// get the XDialogProvider interface from the object created above |
| 102 |
XDialogProvider xDialogProvider = (XDialogProvider) |
| 103 |
UnoRuntime.queryInterface(XDialogProvider.class, obj); |
| 104 |
|
| 105 |
System.err.println("Got DialogProvider, now get dialog"); |
| 106 |
|
| 107 |
try { |
| 108 |
// try to create the Highlight dialog (found in the ScriptBindingLibrary) |
| 109 |
findDialog = xDialogProvider.createDialog("vnd.sun.star.script:" + |
| 110 |
"ScriptBindingLibrary.Highlight?location=application"); |
| 111 |
if( findDialog == null ) |
| 112 |
{ |
| 113 |
if (tryLoadingLibrary(xmcf, XSCRIPTCONTEXT, "Dialog") == false || |
| 114 |
tryLoadingLibrary(xmcf, XSCRIPTCONTEXT, "Script") == false) |
| 115 |
{ |
| 116 |
System.err.println("Error loading ScriptBindingLibrary"); |
| 117 |
return 0; |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
// try to create the Highlight dialog (found in the ScriptBindingLibrary) |
| 122 |
findDialog = xDialogProvider.createDialog("vnd.sun.star.script:" + |
| 123 |
"ScriptBindingLibrary.Highlight?location=application"); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
catch (java.lang.Exception e) { |
| 128 |
System.err.println("Got exception on first creating dialog: " + |
| 129 |
e.getMessage()); |
| 130 |
} |
| 131 |
|
| 132 |
// execute the dialog in a new thread (so that this script can finish) |
| 133 |
Thread t = new Thread() { |
| 134 |
public void run() { |
| 135 |
findDialog.execute(); |
| 136 |
} |
| 137 |
}; |
| 138 |
t.start(); |
| 139 |
|
| 140 |
return 0; |