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