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