HighlightText.java 9.2 KB

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