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