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