2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package org
.libreoffice
.example
.java_scripts
;
21 import com
.sun
.star
.uno
.UnoRuntime
;
22 import com
.sun
.star
.script
.provider
.XScriptContext
;
23 import com
.sun
.star
.lang
.XMultiComponentFactory
;
24 import com
.sun
.star
.lang
.EventObject
;
25 import com
.sun
.star
.uno
.Type
;
26 import com
.sun
.star
.uno
.AnyConverter
;
27 import com
.sun
.star
.text
.XTextDocument
;
28 import com
.sun
.star
.beans
.PropertyValue
;
29 import com
.sun
.star
.script
.XLibraryContainer
;
30 import com
.sun
.star
.awt
.*;
31 import com
.sun
.star
.util
.*;
33 import java
.awt
.Color
;
35 public class HighlightText
implements com
.sun
.star
.awt
.XActionListener
{
37 // UNO awt components of the Highlight dialog
38 XDialog findDialog
= null;
39 XTextComponent findTextBox
;
41 // The document being searched
42 XTextDocument theDocument
;
44 // The text to be searched for
45 private String searchKey
= "";
47 public void showForm(XScriptContext context
) {
48 System
.err
.println("Starting showForm");
50 XMultiComponentFactory xmcf
=
51 context
.getComponentContext().getServiceManager();
53 Object
[] args
= new Object
[1];
54 args
[0] = context
.getDocument();
59 obj
= xmcf
.createInstanceWithArgumentsAndContext(
60 "com.sun.star.awt.DialogProvider", args
,
61 context
.getComponentContext());
62 } catch (com
.sun
.star
.uno
.Exception e
) {
63 System
.err
.println("Error getting DialogProvider object");
67 XDialogProvider xDialogProvider
= (XDialogProvider
)
68 UnoRuntime
.queryInterface(XDialogProvider
.class, obj
);
70 System
.err
.println("Got DialogProvider, now get dialog");
73 findDialog
= xDialogProvider
.createDialog(
74 "vnd.sun.star.script:" +
75 "ScriptBindingLibrary.Highlight?location=application");
76 } catch (java
.lang
.Exception e
) {
77 System
.err
.println("Got exception on first creating dialog: " +
81 if (findDialog
== null) {
82 if (!tryLoadingLibrary(xmcf
, context
, "Dialog") ||
83 !tryLoadingLibrary(xmcf
, context
, "Script")) {
84 System
.err
.println("Error loading ScriptBindingLibrary");
89 findDialog
= xDialogProvider
.createDialog(
90 "vnd.sun.star.script://" +
91 "ScriptBindingLibrary.Highlight?location=application");
92 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
93 System
.err
.println("Error loading ScriptBindingLibrary");
98 XControlContainer controls
= (XControlContainer
)
99 UnoRuntime
.queryInterface(XControlContainer
.class, findDialog
);
101 XButton highlightButton
= (XButton
) UnoRuntime
.queryInterface(
102 XButton
.class, controls
.getControl("HighlightButton"));
103 highlightButton
.setActionCommand("Highlight");
105 findTextBox
= (XTextComponent
) UnoRuntime
.queryInterface(
106 XTextComponent
.class, controls
.getControl("HighlightTextField"));
108 XButton exitButton
= (XButton
) UnoRuntime
.queryInterface(
109 XButton
.class, controls
.getControl("ExitButton"));
110 exitButton
.setActionCommand("Exit");
112 theDocument
= (XTextDocument
) UnoRuntime
.queryInterface(
113 XTextDocument
.class, context
.getDocument());
115 highlightButton
.addActionListener(this);
116 exitButton
.addActionListener(this);
118 findDialog
.execute();
121 public void actionPerformed(ActionEvent e
) {
122 if (e
.ActionCommand
.equals("Exit")) {
123 findDialog
.endExecute();
125 } else if (e
.ActionCommand
.equals("Highlight")) {
126 searchKey
= findTextBox
.getText();
128 // highlight the text in red
129 Color cRed
= new Color(255, 0, 0);
130 int red
= cRed
.getRGB();
132 XReplaceable replaceable
= (XReplaceable
)
133 UnoRuntime
.queryInterface(XReplaceable
.class, theDocument
);
135 XReplaceDescriptor descriptor
=
136 (XReplaceDescriptor
) replaceable
.createReplaceDescriptor();
138 // Gets a XPropertyReplace object for altering the properties
139 // of the replaced text
140 XPropertyReplace xPropertyReplace
= (XPropertyReplace
)
141 UnoRuntime
.queryInterface(XPropertyReplace
.class, descriptor
);
143 // Sets the replaced text property fontweight value to Bold
144 PropertyValue wv
= new PropertyValue("CharWeight", -1,
145 new Float(com
.sun
.star
.awt
.FontWeight
.BOLD
),
146 com
.sun
.star
.beans
.PropertyState
.DIRECT_VALUE
);
148 // Sets the replaced text property color value to RGB parameter
149 PropertyValue cv
= new PropertyValue("CharColor", -1,
150 Integer
.valueOf(red
),
151 com
.sun
.star
.beans
.PropertyState
.DIRECT_VALUE
);
153 // Apply the properties
154 PropertyValue
[] props
= new PropertyValue
[] { cv
, wv
};
157 xPropertyReplace
.setReplaceAttributes(props
);
159 // Only matches whole words and case sensitive
160 descriptor
.setPropertyValue(
161 "SearchCaseSensitive", Boolean
.TRUE
);
162 descriptor
.setPropertyValue("SearchWords", Boolean
.TRUE
);
163 } catch (com
.sun
.star
.beans
.UnknownPropertyException upe
) {
164 System
.err
.println("Error setting up search properties");
166 } catch (com
.sun
.star
.beans
.PropertyVetoException pve
) {
167 System
.err
.println("Error setting up search properties");
169 } catch (com
.sun
.star
.lang
.WrappedTargetException wte
) {
170 System
.err
.println("Error setting up search properties");
172 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
173 System
.err
.println("Error setting up search properties");
177 // Replaces all instances of searchKey with new Text properties
178 // and gets the number of instances of the searchKey
179 descriptor
.setSearchString(searchKey
);
180 descriptor
.setReplaceString(searchKey
);
181 replaceable
.replaceAll(descriptor
);
185 public void disposing(EventObject o
) {
189 private boolean tryLoadingLibrary(
190 XMultiComponentFactory xmcf
, XScriptContext context
, String name
) {
191 System
.err
.println("Try to load ScriptBindingLibrary");
194 Object obj
= xmcf
.createInstanceWithContext(
195 "com.sun.star.script.Application" + name
+ "LibraryContainer",
196 context
.getComponentContext());
198 XLibraryContainer xLibraryContainer
= (XLibraryContainer
)
199 UnoRuntime
.queryInterface(XLibraryContainer
.class, obj
);
201 System
.err
.println("Got XLibraryContainer");
203 Object serviceObj
= context
.getComponentContext().getValueByName(
204 "/singletons/com.sun.star.util.theMacroExpander");
206 XMacroExpander xme
= (XMacroExpander
) AnyConverter
.toObject(
207 new Type(XMacroExpander
.class), serviceObj
);
209 String bootstrapName
= "bootstraprc";
211 if (System
.getProperty("os.name").startsWith("Windows")) {
212 bootstrapName
= "bootstrap.ini";
215 String libURL
= xme
.expandMacros(
216 "$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/basic/ScriptBindingLibrary/" +
217 name
.toLowerCase() + ".xlb/");
219 System
.err
.println("libURL is: " + libURL
);
221 xLibraryContainer
.createLibraryLink(
222 "ScriptBindingLibrary", libURL
, false);
224 System
.err
.println("liblink created");
226 } catch (com
.sun
.star
.uno
.Exception e
) {
227 System
.err
.println("Got an exception loading lib: " + e
.getMessage());