ButtonPressHandler.js 4.6 KB

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