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