summaryrefslogtreecommitdiffstats
path: root/scripting/examples/javascript/Highlight/ButtonPressHandler.js
blob: e0fbde2400e680f4f71255efaf9fe264e0a67b65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
//this script acts as a handler for the buttons in the Highlight dialog
importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.uno.Type);
importClass(Packages.com.sun.star.uno.AnyConverter);

importClass(Packages.com.sun.star.awt.XButton);
importClass(Packages.com.sun.star.awt.XControl);
importClass(Packages.com.sun.star.awt.ActionEvent);
importClass(Packages.com.sun.star.awt.XControlModel);
importClass(Packages.com.sun.star.awt.XControlContainer);
importClass(Packages.com.sun.star.awt.XDialog);
importClass(Packages.com.sun.star.awt.XTextComponent);

importClass(Packages.com.sun.star.util.XReplaceable);
importClass(Packages.com.sun.star.util.XReplaceDescriptor);
importClass(Packages.com.sun.star.util.XPropertyReplace);

importClass(Packages.com.sun.star.beans.XPropertySet);
importClass(Packages.com.sun.star.beans.PropertyValue);

// Scripting Framework DialogFactory class
importClass(Packages.com.sun.star.script.framework.browse.DialogFactory);

// Get the ActionEvent object from the ARGUMENTS list
event = ARGUMENTS[0];

// Each argument is of type Any so we must use the AnyConverter class to
// convert it into the interface or primitive type we expect
button = AnyConverter.toObject(new Type(XButton), event.Source);

// We can now query for the model of the button and get its properties
control = UnoRuntime.queryInterface(XControl, button);
cmodel = control.getModel();
pset = UnoRuntime.queryInterface(XPropertySet, cmodel);

if (pset.getPropertyValue("Label").equals("Exit"))
{
    // We can get the XDialog in which this control appears by calling
    // getContext() on the XControl interface
    xDialog = UnoRuntime.queryInterface(
        XDialog, control.getContext());

    // Close the dialog
    xDialog.endExecute();
}
else
{
    // We can get the list of controls for this dialog by calling
    // getContext() on the XControl interface of the button
    controls = UnoRuntime.queryInterface(
        XControlContainer, control.getContext());

    // Now get the text field control from the list
    textField =
        UnoRuntime.queryInterface(
            XTextComponent, controls.getControl("HighlightTextField"));

    searchKey = textField.getText();

    // highlight the text in red
    red = java.awt.Color.red.getRGB();

    replaceable =
        UnoRuntime.queryInterface(XReplaceable, XSCRIPTCONTEXT.getDocument());

    descriptor = replaceable.createReplaceDescriptor();

    // Gets a XPropertyReplace object for altering the properties
    // of the replaced text
    xPropertyReplace = UnoRuntime.queryInterface(XPropertyReplace, descriptor);

    // Sets the replaced text property fontweight value to Bold
    wv = new PropertyValue("CharWeight", -1,
        new java.lang.Float(Packages.com.sun.star.awt.FontWeight.BOLD),
            Packages.com.sun.star.beans.PropertyState.DIRECT_VALUE);

    // Sets the replaced text property color value to RGB parameter
    cv = new PropertyValue("CharColor", -1,
        new java.lang.Integer(red),
            Packages.com.sun.star.beans.PropertyState.DIRECT_VALUE);

    // Apply the properties
    props = new Array;
    props[0] = cv;
    props[1] = wv;

    try {
        xPropertyReplace.setReplaceAttributes(props);

        // Only matches whole words and case sensitive
        descriptor.setPropertyValue(
            "SearchCaseSensitive", new java.lang.Boolean(true));
        descriptor.setPropertyValue("SearchWords", new java.lang.Boolean(true));

        // Replaces all instances of searchKey with new Text properties
        // and gets the number of instances of the searchKey
        descriptor.setSearchString(searchKey);
        descriptor.setReplaceString(searchKey);
        replaceable.replaceAll(descriptor);
    }
    catch (e) {
        java.lang.System.err.println("Error setting up search properties"
            + e.getMessage());
    }
}