Debian Codegraph @

scripting/examples/javascript/Highlight/ButtonPressHandler.js

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