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