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