1/************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21// this script serves as an example of how to launch a Basic Dialog 22// from a script 23import com.sun.star.uno.UnoRuntime; 24import com.sun.star.script.provider.XScriptContext; 25import com.sun.star.lang.XMultiComponentFactory; 26import com.sun.star.lang.EventObject; 27import com.sun.star.uno.Type; 28import com.sun.star.uno.AnyConverter; 29import com.sun.star.text.XTextDocument; 30import com.sun.star.beans.PropertyValue; 31import com.sun.star.script.XLibraryContainer; 32import com.sun.star.awt.*; 33import com.sun.star.util.*; 34 35boolean tryLoadingLibrary( xmcf, context, name ) 36{ 37 try 38 { 39 obj = xmcf.createInstanceWithContext( 40 "com.sun.star.script.Application" + name + "LibraryContainer", 41 context.getComponentContext()); 42 43 xLibraryContainer = (XLibraryContainer) 44 UnoRuntime.queryInterface(XLibraryContainer.class, obj); 45 46 System.err.println("Got XLibraryContainer"); 47 48 serviceObj = context.getComponentContext().getValueByName( 49 "/singletons/com.sun.star.util.theMacroExpander"); 50 51 xme = (XMacroExpander) AnyConverter.toObject( 52 new Type(XMacroExpander.class), serviceObj); 53 54 bootstrapName = "bootstraprc"; 55 if (System.getProperty("os.name").startsWith("Windows")) 56 { 57 bootstrapName = "bootstrap.ini"; 58 } 59 60 libURL = xme.expandMacros( 61 "${$OOO_BASE_DIR/program/" + bootstrapName + "::BaseInstallation}" + 62 "/share/basic/ScriptBindingLibrary/" + 63 name.toLowerCase() + ".xlb/"); 64 65 System.err.println("libURL is: " + libURL); 66 67 xLibraryContainer.createLibraryLink( 68 "ScriptBindingLibrary", libURL, false); 69 70 System.err.println("liblink created"); 71 72 } 73 catch (com.sun.star.uno.Exception e) 74 { 75 System.err.println("Got an exception loading lib: " + e.getMessage()); 76 return false; 77 } 78 return true; 79} 80 81// get the XMultiComponentFactory from the XSCRIPTCONTEXT 82XMultiComponentFactory xmcf = 83 XSCRIPTCONTEXT.getComponentContext().getServiceManager(); 84 85Object[] args = new Object[1]; 86args[0] = XSCRIPTCONTEXT.getDocument(); 87 88Object obj; 89try { 90 // try to create an instance of the DialogProvider 91 obj = xmcf.createInstanceWithArgumentsAndContext( 92 "com.sun.star.awt.DialogProvider", args, 93 XSCRIPTCONTEXT.getComponentContext()); 94 /* 95 obj = xmcf.createInstanceWithContext( 96 "com.sun.star.awt.DialogProvider", 97 XSCRIPTCONTEXT.getComponentContext()); 98 */ 99} 100catch (com.sun.star.uno.Exception e) { 101 System.err.println("Error getting DialogProvider object"); 102 return 0; 103} 104 105// get the XDialogProvider interface from the object created above 106XDialogProvider xDialogProvider = (XDialogProvider) 107 UnoRuntime.queryInterface(XDialogProvider.class, obj); 108 109System.err.println("Got DialogProvider, now get dialog"); 110 111try { 112 // try to create the Highlight dialog (found in the ScriptBindingLibrary) 113 findDialog = xDialogProvider.createDialog("vnd.sun.star.script:" + 114 "ScriptBindingLibrary.Highlight?location=application"); 115 if( findDialog == null ) 116 { 117 if (tryLoadingLibrary(xmcf, XSCRIPTCONTEXT, "Dialog") == false || 118 tryLoadingLibrary(xmcf, XSCRIPTCONTEXT, "Script") == false) 119 { 120 System.err.println("Error loading ScriptBindingLibrary"); 121 return 0; 122 } 123 else 124 { 125 // try to create the Highlight dialog (found in the ScriptBindingLibrary) 126 findDialog = xDialogProvider.createDialog("vnd.sun.star.script:" + 127 "ScriptBindingLibrary.Highlight?location=application"); 128 } 129 } 130} 131catch (java.lang.Exception e) { 132 System.err.println("Got exception on first creating dialog: " + 133 e.getMessage()); 134} 135 136// execute the dialog in a new thread (so that this script can finish) 137Thread t = new Thread() { 138 public void run() { 139 findDialog.execute(); 140 } 141}; 142t.start(); 143 144return 0; 145