ButtonPressHandler.bsh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // this code is bound to the events generated by the buttons in the dialog
  19. // it will close the dialog or find and highlight the text entered in the
  20. // dialog (depending on the button pressed)
  21. import com.sun.star.uno.*;
  22. import com.sun.star.awt.*;
  23. import com.sun.star.lang.*;
  24. import com.sun.star.beans.*;
  25. import com.sun.star.util.*;
  26. import com.sun.star.script.framework.browse.DialogFactory;
  27. // Get the ActionEvent object from the ARGUMENTS list
  28. ActionEvent event = (ActionEvent) ARGUMENTS[0];
  29. // Each argument is of type Any so we must use the AnyConverter class to
  30. // convert it into the interface or primitive type we expect
  31. XButton button = (XButton)AnyConverter.toObject(
  32. new Type(XButton.class), event.Source);
  33. // We can now query for the model of the button and get its properties
  34. XControl control = (XControl)UnoRuntime.queryInterface(XControl.class, button);
  35. XControlModel cmodel = control.getModel();
  36. XPropertySet pset = (XPropertySet)UnoRuntime.queryInterface(
  37. XPropertySet.class, cmodel);
  38. if (pset.getPropertyValue("Label").equals("Exit"))
  39. {
  40. // We can get the XDialog in which this control appears by calling
  41. // getContext() on the XControl interface
  42. XDialog xDialog = (XDialog)UnoRuntime.queryInterface(
  43. XDialog.class, control.getContext());
  44. // Close the dialog
  45. xDialog.endExecute();
  46. }
  47. else
  48. {
  49. // We can get the list of controls for this dialog by calling
  50. // getContext() on the XControl interface of the button
  51. XControlContainer controls = (XControlContainer)UnoRuntime.queryInterface(
  52. XControlContainer.class, control.getContext());
  53. // Now get the text field control from the list
  54. XTextComponent textField = (XTextComponent)
  55. UnoRuntime.queryInterface(
  56. XTextComponent.class, controls.getControl("HighlightTextField"));
  57. String searchKey = textField.getText();
  58. // highlight the text in red
  59. java.awt.Color cRed = new java.awt.Color(255, 0, 0);
  60. int red = cRed.getRGB();
  61. XReplaceable replaceable = (XReplaceable)
  62. UnoRuntime.queryInterface(XReplaceable.class, XSCRIPTCONTEXT.getDocument());
  63. XReplaceDescriptor descriptor =
  64. (XReplaceDescriptor) replaceable.createReplaceDescriptor();
  65. // Gets a XPropertyReplace object for altering the properties
  66. // of the replaced text
  67. XPropertyReplace xPropertyReplace = (XPropertyReplace)
  68. UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
  69. // Sets the replaced text property fontweight value to Bold
  70. PropertyValue wv = new PropertyValue("CharWeight", -1,
  71. new Float(com.sun.star.awt.FontWeight.BOLD),
  72. com.sun.star.beans.PropertyState.DIRECT_VALUE);
  73. // Sets the replaced text property color value to RGB parameter
  74. PropertyValue cv = new PropertyValue("CharColor", -1,
  75. new Integer(red),
  76. com.sun.star.beans.PropertyState.DIRECT_VALUE);
  77. // Apply the properties
  78. PropertyValue[] props = new PropertyValue[] { cv, wv };
  79. try {
  80. xPropertyReplace.setReplaceAttributes(props);
  81. // Only matches whole words and case sensitive
  82. descriptor.setPropertyValue(
  83. "SearchCaseSensitive", new Boolean(true));
  84. descriptor.setPropertyValue("SearchWords", new Boolean(true));
  85. }
  86. catch (com.sun.star.beans.UnknownPropertyException upe) {
  87. System.err.println("Error setting up search properties");
  88. return;
  89. }
  90. catch (com.sun.star.beans.PropertyVetoException pve) {
  91. System.err.println("Error setting up search properties");
  92. return;
  93. }
  94. catch (com.sun.star.lang.WrappedTargetException wte) {
  95. System.err.println("Error setting up search properties");
  96. return;
  97. }
  98. // Replaces all instances of searchKey with new Text properties
  99. // and gets the number of instances of the searchKey
  100. descriptor.setSearchString(searchKey);
  101. descriptor.setReplaceString(searchKey);
  102. replaceable.replaceAll(descriptor);
  103. }
  104. // BeanShell scripts in LibreOffice should always return 0
  105. return 0;