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 *************************************************************/ 21 22 import com.sun.star.uno.UnoRuntime; 23 import com.sun.star.script.provider.XScriptContext; 24 import com.sun.star.lang.XMultiComponentFactory; 25 import com.sun.star.lang.EventObject; 26 import com.sun.star.uno.Type; 27 import com.sun.star.uno.AnyConverter; 28 import com.sun.star.text.XTextDocument; 29 import com.sun.star.beans.PropertyValue; 30 import com.sun.star.script.XLibraryContainer; 31 import com.sun.star.awt.*; 32 import com.sun.star.util.*; 33 34 import java.awt.Color; 35 36 public class HighlightText implements com.sun.star.awt.XActionListener { 37 38 // UNO awt components of the Highlight dialog 39 XDialog findDialog = null; 40 XTextComponent findTextBox; 41 42 // The document being searched 43 XTextDocument theDocument; 44 45 // The text to be searched for 46 private String searchKey = ""; 47 showForm(XScriptContext context)48 public void showForm(XScriptContext context) { 49 System.err.println("Starting showForm"); 50 51 XMultiComponentFactory xmcf = 52 context.getComponentContext().getServiceManager(); 53 54 Object[] args = new Object[1]; 55 args[0] = context.getDocument(); 56 57 Object obj; 58 try { 59 obj = xmcf.createInstanceWithArgumentsAndContext( 60 "com.sun.star.awt.DialogProvider", args, 61 context.getComponentContext()); 62 } 63 catch (com.sun.star.uno.Exception e) { 64 System.err.println("Error getting DialogProvider object"); 65 return; 66 } 67 68 XDialogProvider xDialogProvider = (XDialogProvider) 69 UnoRuntime.queryInterface(XDialogProvider.class, obj); 70 71 System.err.println("Got DialogProvider, now get dialog"); 72 73 try { 74 findDialog = xDialogProvider.createDialog( 75 "vnd.sun.star.script:" + 76 "ScriptBindingLibrary.Highlight?location=application"); 77 } 78 catch (java.lang.Exception e) { 79 System.err.println("Got exception on first creating dialog: " + 80 e.getMessage()); 81 } 82 83 if (findDialog == null) { 84 if (tryLoadingLibrary(xmcf, context, "Dialog") == false || 85 tryLoadingLibrary(xmcf, context, "Script") == false) 86 { 87 System.err.println("Error loading ScriptBindingLibrary"); 88 return; 89 } 90 try { 91 findDialog = xDialogProvider.createDialog( 92 "vnd.sun.star.script://" + 93 "ScriptBindingLibrary.Highlight?location=application"); 94 } 95 catch (com.sun.star.lang.IllegalArgumentException iae) { 96 System.err.println("Error loading ScriptBindingLibrary"); 97 return; 98 } 99 } 100 101 XControlContainer controls = (XControlContainer) 102 UnoRuntime.queryInterface(XControlContainer.class, findDialog); 103 104 XButton highlightButton = (XButton) UnoRuntime.queryInterface( 105 XButton.class, controls.getControl("HighlightButton")); 106 highlightButton.setActionCommand("Highlight"); 107 108 findTextBox = (XTextComponent) UnoRuntime.queryInterface( 109 XTextComponent.class, controls.getControl("HighlightTextField")); 110 111 XButton exitButton = (XButton) UnoRuntime.queryInterface( 112 XButton.class, controls.getControl("ExitButton")); 113 exitButton.setActionCommand("Exit"); 114 115 theDocument = (XTextDocument) UnoRuntime.queryInterface( 116 XTextDocument.class, context.getDocument()); 117 118 highlightButton.addActionListener(this); 119 exitButton.addActionListener(this); 120 121 findDialog.execute(); 122 123 return; 124 } 125 actionPerformed(ActionEvent e)126 public void actionPerformed(ActionEvent e) { 127 if (e.ActionCommand.equals("Exit")) { 128 findDialog.endExecute(); 129 return; 130 } 131 else if (e.ActionCommand.equals("Highlight")) { 132 searchKey = findTextBox.getText(); 133 134 // highlight the text in red 135 Color cRed = new Color(255, 0, 0); 136 int red = cRed.getRGB(); 137 138 XReplaceable replaceable = (XReplaceable) 139 UnoRuntime.queryInterface(XReplaceable.class, theDocument); 140 141 XReplaceDescriptor descriptor = 142 (XReplaceDescriptor) replaceable.createReplaceDescriptor(); 143 144 // Gets a XPropertyReplace object for altering the properties 145 // of the replaced text 146 XPropertyReplace xPropertyReplace = (XPropertyReplace) 147 UnoRuntime.queryInterface(XPropertyReplace.class, descriptor); 148 149 // Sets the replaced text property fontweight value to Bold 150 PropertyValue wv = new PropertyValue("CharWeight", -1, 151 new Float(com.sun.star.awt.FontWeight.BOLD), 152 com.sun.star.beans.PropertyState.DIRECT_VALUE); 153 154 // Sets the replaced text property color value to RGB parameter 155 PropertyValue cv = new PropertyValue("CharColor", -1, 156 new Integer(red), 157 com.sun.star.beans.PropertyState.DIRECT_VALUE); 158 159 // Apply the properties 160 PropertyValue[] props = new PropertyValue[] { cv, wv }; 161 162 try { 163 xPropertyReplace.setReplaceAttributes(props); 164 165 // Only matches whole words and case sensitive 166 descriptor.setPropertyValue( 167 "SearchCaseSensitive", new Boolean(true)); 168 descriptor.setPropertyValue("SearchWords", new Boolean(true)); 169 } 170 catch (com.sun.star.beans.UnknownPropertyException upe) { 171 System.err.println("Error setting up search properties"); 172 return; 173 } 174 catch (com.sun.star.beans.PropertyVetoException pve) { 175 System.err.println("Error setting up search properties"); 176 return; 177 } 178 catch (com.sun.star.lang.WrappedTargetException wte) { 179 System.err.println("Error setting up search properties"); 180 return; 181 } 182 catch (com.sun.star.lang.IllegalArgumentException iae) { 183 System.err.println("Error setting up search properties"); 184 return; 185 } 186 187 // Replaces all instances of searchKey with new Text properties 188 // and gets the number of instances of the searchKey 189 descriptor.setSearchString(searchKey); 190 descriptor.setReplaceString(searchKey); 191 replaceable.replaceAll(descriptor); 192 } 193 } 194 disposing(EventObject o)195 public void disposing(EventObject o) 196 { 197 // do nothing 198 } 199 tryLoadingLibrary( XMultiComponentFactory xmcf, XScriptContext context, String name)200 private boolean tryLoadingLibrary( 201 XMultiComponentFactory xmcf, XScriptContext context, String name) 202 { 203 System.err.println("Try to load ScriptBindingLibrary"); 204 205 try { 206 Object obj = xmcf.createInstanceWithContext( 207 "com.sun.star.script.Application" + name + "LibraryContainer", 208 context.getComponentContext()); 209 210 XLibraryContainer xLibraryContainer = (XLibraryContainer) 211 UnoRuntime.queryInterface(XLibraryContainer.class, obj); 212 213 System.err.println("Got XLibraryContainer"); 214 215 Object serviceObj = context.getComponentContext().getValueByName( 216 "/singletons/com.sun.star.util.theMacroExpander"); 217 218 XMacroExpander xme = (XMacroExpander) AnyConverter.toObject( 219 new Type(XMacroExpander.class), serviceObj); 220 221 String bootstrapName = "bootstraprc"; 222 if (System.getProperty("os.name").startsWith("Windows")) { 223 bootstrapName = "bootstrap.ini"; 224 } 225 226 String libURL = xme.expandMacros( 227 "${$OOO_BASE_DIR/program/" + bootstrapName + "::BaseInstallation}" + 228 "/share/basic/ScriptBindingLibrary/" + 229 name.toLowerCase() + ".xlb/"); 230 231 System.err.println("libURL is: " + libURL); 232 233 xLibraryContainer.createLibraryLink( 234 "ScriptBindingLibrary", libURL, false); 235 236 System.err.println("liblink created"); 237 238 } catch (com.sun.star.uno.Exception e) { 239 System.err.println("Got an exception loading lib: " + e.getMessage()); 240 return false; 241 } 242 return true; 243 } 244 } 245