| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 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/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 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 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.libreoffice.example.java_scripts; |
| 20 |
|
| 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.*; |
| 32 |
|
| 33 |
import java.awt.Color; |
| 34 |
|
| 35 |
public class HighlightText implements com.sun.star.awt.XActionListener { |
| 36 |
|
| 37 |
// UNO awt components of the Highlight dialog |
| 38 |
XDialog findDialog = null; |
| 39 |
XTextComponent findTextBox; |
| 40 |
|
| 41 |
// The document being searched |
| 42 |
XTextDocument theDocument; |
| 43 |
|
| 44 |
// The text to be searched for |
| 45 |
private String searchKey = ""; |
| 46 |
|
| 47 |
public void showForm(XScriptContext context) { |
| 48 |
System.err.println("Starting showForm"); |
| 49 |
|
| 50 |
XMultiComponentFactory xmcf = |
| 51 |
context.getComponentContext().getServiceManager(); |
| 52 |
|
| 53 |
Object[] args = new Object[1]; |
| 54 |
args[0] = context.getDocument(); |
| 55 |
|
| 56 |
Object obj; |
| 57 |
|
| 58 |
try { |
| 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"); |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
XDialogProvider xDialogProvider = (XDialogProvider) |
| 68 |
UnoRuntime.queryInterface(XDialogProvider.class, obj); |
| 69 |
|
| 70 |
System.err.println("Got DialogProvider, now get dialog"); |
| 71 |
|
| 72 |
try { |
| 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: " + |
| 78 |
e.getMessage()); |
| 79 |
} |
| 80 |
|
| 81 |
if (findDialog == null) { |
| 82 |
if (!tryLoadingLibrary(xmcf, context, "Dialog") || |
| 83 |
!tryLoadingLibrary(xmcf, context, "Script")) { |
| 84 |
System.err.println("Error loading ScriptBindingLibrary"); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
try { |
| 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"); |
| 94 |
return; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
XControlContainer controls = (XControlContainer) |
| 99 |
UnoRuntime.queryInterface(XControlContainer.class, findDialog); |
| 100 |
|
| 101 |
XButton highlightButton = (XButton) UnoRuntime.queryInterface( |
| 102 |
XButton.class, controls.getControl("HighlightButton")); |
| 103 |
highlightButton.setActionCommand("Highlight"); |
| 104 |
|
| 105 |
findTextBox = (XTextComponent) UnoRuntime.queryInterface( |
| 106 |
XTextComponent.class, controls.getControl("HighlightTextField")); |
| 107 |
|
| 108 |
XButton exitButton = (XButton) UnoRuntime.queryInterface( |
| 109 |
XButton.class, controls.getControl("ExitButton")); |
| 110 |
exitButton.setActionCommand("Exit"); |
| 111 |
|
| 112 |
theDocument = (XTextDocument) UnoRuntime.queryInterface( |
| 113 |
XTextDocument.class, context.getDocument()); |
| 114 |
|
| 115 |
highlightButton.addActionListener(this); |
| 116 |
exitButton.addActionListener(this); |
| 117 |
|
| 118 |
findDialog.execute(); |
| 119 |
} |
| 120 |
|
| 121 |
public void actionPerformed(ActionEvent e) { |
| 122 |
if (e.ActionCommand.equals("Exit")) { |
| 123 |
findDialog.endExecute(); |
| 124 |
return; |
| 125 |
} else if (e.ActionCommand.equals("Highlight")) { |
| 126 |
searchKey = findTextBox.getText(); |
| 127 |
|
| 128 |
// highlight the text in red |
| 129 |
Color cRed = new Color(255, 0, 0); |
| 130 |
int red = cRed.getRGB(); |
| 131 |
|
| 132 |
XReplaceable replaceable = (XReplaceable) |
| 133 |
UnoRuntime.queryInterface(XReplaceable.class, theDocument); |
| 134 |
|
| 135 |
XReplaceDescriptor descriptor = |
| 136 |
(XReplaceDescriptor) replaceable.createReplaceDescriptor(); |
| 137 |
|
| 138 |
// Gets a XPropertyReplace object for altering the properties |
| 139 |
// of the replaced text |
| 140 |
XPropertyReplace xPropertyReplace = (XPropertyReplace) |
| 141 |
UnoRuntime.queryInterface(XPropertyReplace.class, descriptor); |
| 142 |
|
| 143 |
// Sets the replaced text property fontweight value to Bold |
| 144 |
PropertyValue wv = new PropertyValue("CharWeight", -1, |
| 145 |
Float.valueOf(com.sun.star.awt.FontWeight.BOLD), |
| 146 |
com.sun.star.beans.PropertyState.DIRECT_VALUE); |
| 147 |
|
| 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); |
| 152 |
|
| 153 |
// Apply the properties |
| 154 |
PropertyValue[] props = new PropertyValue[] { cv, wv }; |
| 155 |
|
| 156 |
try { |
| 157 |
xPropertyReplace.setReplaceAttributes(props); |
| 158 |
|
| 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"); |
| 165 |
return; |
| 166 |
} catch (com.sun.star.beans.PropertyVetoException pve) { |
| 167 |
System.err.println("Error setting up search properties"); |
| 168 |
return; |
| 169 |
} catch (com.sun.star.lang.WrappedTargetException wte) { |
| 170 |
System.err.println("Error setting up search properties"); |
| 171 |
return; |
| 172 |
} catch (com.sun.star.lang.IllegalArgumentException iae) { |
| 173 |
System.err.println("Error setting up search properties"); |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 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); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
public void disposing(EventObject o) { |
| 186 |
// do nothing |
| 187 |
} |
| 188 |
|
| 189 |
private boolean tryLoadingLibrary( |
| 190 |
XMultiComponentFactory xmcf, XScriptContext context, String name) { |
| 191 |
System.err.println("Try to load ScriptBindingLibrary"); |
| 192 |
|
| 193 |
try { |
| 194 |
Object obj = xmcf.createInstanceWithContext( |
| 195 |
"com.sun.star.script.Application" + name + "LibraryContainer", |
| 196 |
context.getComponentContext()); |
| 197 |
|
| 198 |
XLibraryContainer xLibraryContainer = (XLibraryContainer) |
| 199 |
UnoRuntime.queryInterface(XLibraryContainer.class, obj); |
| 200 |
|
| 201 |
System.err.println("Got XLibraryContainer"); |
| 202 |
|
| 203 |
Object serviceObj = context.getComponentContext().getValueByName( |
| 204 |
"/singletons/com.sun.star.util.theMacroExpander"); |
| 205 |
|
| 206 |
XMacroExpander xme = (XMacroExpander) AnyConverter.toObject( |
| 207 |
new Type(XMacroExpander.class), serviceObj); |
| 208 |
|
| 209 |
String bootstrapName = "bootstraprc"; |
| 210 |
|
| 211 |
if (System.getProperty("os.name").startsWith("Windows")) { |
| 212 |
bootstrapName = "bootstrap.ini"; |
| 213 |
} |
| 214 |
|
| 215 |
String libURL = xme.expandMacros( |
| 216 |
"$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/basic/ScriptBindingLibrary/" + |
| 217 |
name.toLowerCase() + ".xlb/"); |
| 218 |
|
| 219 |
System.err.println("libURL is: " + libURL); |
| 220 |
|
| 221 |
xLibraryContainer.createLibraryLink( |
| 222 |
"ScriptBindingLibrary", libURL, false); |
| 223 |
|
| 224 |
System.err.println("liblink created"); |
| 225 |
|
| 226 |
} catch (com.sun.star.uno.Exception e) { |
| 227 |
System.err.println("Got an exception loading lib: " + e.getMessage()); |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
return true; |
| 232 |
} |
| 233 |
} |