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