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// *************************************************************
21importClass(Packages.com.sun.star.uno.UnoRuntime);
22importClass(Packages.com.sun.star.lang.XMultiComponentFactory);
23importClass(Packages.com.sun.star.awt.XDialogProvider);
24importClass(Packages.com.sun.star.awt.XDialog);
25importClass(Packages.com.sun.star.uno.Exception);
26importClass(Packages.com.sun.star.script.provider.XScriptContext);
27
28importClass(java.lang.Thread);
29importClass(java.lang.System);
30
31function tryLoadingLibrary( xmcf, context, name )
32{
33    try
34    {
35        obj = xmcf.createInstanceWithContext(
36               "com.sun.star.script.Application" + name + "LibraryContainer",
37               context.getComponentContext());
38
39        xLibraryContainer = UnoRuntime.queryInterface(XLibraryContainer, obj);
40
41        System.err.println("Got XLibraryContainer");
42
43        serviceObj = context.getComponentContext().getValueByName(
44                    "/singletons/com.sun.star.util.theMacroExpander");
45
46        xme = AnyConverter.toObject(new Type(XMacroExpander), serviceObj);
47
48        bootstrapName = "bootstraprc";
49        if (System.getProperty("os.name").startsWith("Windows"))
50        {
51            bootstrapName = "bootstrap.ini";
52        }
53
54        libURL = xme.expandMacros(
55                "${$OOO_BASE_DIR/program/" + bootstrapName + "::BaseInstallation}" +
56                    "/share/basic/ScriptBindingLibrary/" +
57                    name.toLowerCase() + ".xlb/");
58
59        System.err.println("libURL is: " + libURL);
60
61        xLibraryContainer.createLibraryLink(
62            "ScriptBindingLibrary", libURL, false);
63
64        System.err.println("liblink created");
65
66    }
67    catch (e)
68    {
69        System.err.println("Got an exception loading lib: " + e.getMessage());
70        return false;
71    }
72    return true;
73}
74
75function getDialogProvider()
76{
77    // UNO awt components of the Highlight dialog
78    //get the XMultiServiceFactory
79    xmcf = XSCRIPTCONTEXT.getComponentContext().getServiceManager();
80
81    args = new Array;
82    //get the XDocument from the context
83    args[0] = XSCRIPTCONTEXT.getDocument();
84
85    //try to create the DialogProvider
86    try {
87        obj = xmcf.createInstanceWithArgumentsAndContext(
88            "com.sun.star.awt.DialogProvider", args,
89            XSCRIPTCONTEXT.getComponentContext());
90    }
91    catch (e) {
92        System.err.println("Error getting DialogProvider object");
93        return null;
94    }
95
96    return UnoRuntime.queryInterface(XDialogProvider, obj);
97}
98
99//get the DialogProvider
100xDialogProvider = getDialogProvider();
101
102if (xDialogProvider != null)
103{
104    //try to create the Highlight dialog (found in the ScriptBinding library)
105    try
106    {
107        findDialog = xDialogProvider.createDialog("vnd.sun.star.script:" +
108            "ScriptBindingLibrary.Highlight?location=application");
109        if( findDialog == null )
110        {
111            if (tryLoadingLibrary(xmcf, XSCRIPTCONTEXT, "Dialog") == false ||
112                tryLoadingLibrary(xmcf, XSCRIPTCONTEXT, "Script") == false)
113            {
114                System.err.println("Error loading ScriptBindingLibrary");
115            }
116            else
117            {
118                // try to create the Highlight dialog (found in the
119                // ScriptBindingLibrary)
120                findDialog = xDialogProvider.createDialog("vnd.sun.star.script:" +
121                    "ScriptBindingLibrary.Highlight?location=application");
122            }
123        }
124
125        //launch the dialog
126        if ( findDialog != null )
127        {
128            findDialog.execute();
129        }
130    }
131    catch (e) {
132        System.err.println("Got exception on first creating dialog: " +
133            e.getMessage());
134    }
135}
136