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