merge the formfield patch from ooo-build
[ooovba.git] / scripting / examples / java / Highlight / HighlightText.java
bloba60985a32a4e0ff3a86f8261954f8ce37acb7c39
1 import com.sun.star.uno.UnoRuntime;
2 import com.sun.star.script.provider.XScriptContext;
3 import com.sun.star.lang.XMultiComponentFactory;
4 import com.sun.star.lang.EventObject;
5 import com.sun.star.uno.Type;
6 import com.sun.star.uno.AnyConverter;
7 import com.sun.star.text.XTextDocument;
8 import com.sun.star.beans.PropertyValue;
9 import com.sun.star.script.XLibraryContainer;
10 import com.sun.star.awt.*;
11 import com.sun.star.util.*;
13 import java.awt.Color;
15 public class HighlightText implements com.sun.star.awt.XActionListener {
17 // UNO awt components of the Highlight dialog
18 XDialog findDialog = null;
19 XTextComponent findTextBox;
21 // The document being searched
22 XTextDocument theDocument;
24 // The text to be searched for
25 private String searchKey = "";
27 public void showForm(XScriptContext context) {
28 System.err.println("Starting showForm");
30 XMultiComponentFactory xmcf =
31 context.getComponentContext().getServiceManager();
33 Object[] args = new Object[1];
34 args[0] = context.getDocument();
36 Object obj;
37 try {
38 obj = xmcf.createInstanceWithArgumentsAndContext(
39 "com.sun.star.awt.DialogProvider", args,
40 context.getComponentContext());
42 catch (com.sun.star.uno.Exception e) {
43 System.err.println("Error getting DialogProvider object");
44 return;
47 XDialogProvider xDialogProvider = (XDialogProvider)
48 UnoRuntime.queryInterface(XDialogProvider.class, obj);
50 System.err.println("Got DialogProvider, now get dialog");
52 try {
53 findDialog = xDialogProvider.createDialog(
54 "vnd.sun.star.script:" +
55 "ScriptBindingLibrary.Highlight?location=application");
57 catch (java.lang.Exception e) {
58 System.err.println("Got exception on first creating dialog: " +
59 e.getMessage());
62 if (findDialog == null) {
63 if (tryLoadingLibrary(xmcf, context, "Dialog") == false ||
64 tryLoadingLibrary(xmcf, context, "Script") == false)
66 System.err.println("Error loading ScriptBindingLibrary");
67 return;
69 try {
70 findDialog = xDialogProvider.createDialog(
71 "vnd.sun.star.script://" +
72 "ScriptBindingLibrary.Highlight?location=application");
74 catch (com.sun.star.lang.IllegalArgumentException iae) {
75 System.err.println("Error loading ScriptBindingLibrary");
76 return;
80 XControlContainer controls = (XControlContainer)
81 UnoRuntime.queryInterface(XControlContainer.class, findDialog);
83 XButton highlightButton = (XButton) UnoRuntime.queryInterface(
84 XButton.class, controls.getControl("HighlightButton"));
85 highlightButton.setActionCommand("Highlight");
87 findTextBox = (XTextComponent) UnoRuntime.queryInterface(
88 XTextComponent.class, controls.getControl("HighlightTextField"));
90 XButton exitButton = (XButton) UnoRuntime.queryInterface(
91 XButton.class, controls.getControl("ExitButton"));
92 exitButton.setActionCommand("Exit");
94 theDocument = (XTextDocument) UnoRuntime.queryInterface(
95 XTextDocument.class, context.getDocument());
97 highlightButton.addActionListener(this);
98 exitButton.addActionListener(this);
100 findDialog.execute();
102 return;
105 public void actionPerformed(ActionEvent e) {
106 if (e.ActionCommand.equals("Exit")) {
107 findDialog.endExecute();
108 return;
110 else if (e.ActionCommand.equals("Highlight")) {
111 searchKey = findTextBox.getText();
113 // highlight the text in red
114 Color cRed = new Color(255, 0, 0);
115 int red = cRed.getRGB();
117 XReplaceable replaceable = (XReplaceable)
118 UnoRuntime.queryInterface(XReplaceable.class, theDocument);
120 XReplaceDescriptor descriptor =
121 (XReplaceDescriptor) replaceable.createReplaceDescriptor();
123 // Gets a XPropertyReplace object for altering the properties
124 // of the replaced text
125 XPropertyReplace xPropertyReplace = (XPropertyReplace)
126 UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
128 // Sets the replaced text property fontweight value to Bold
129 PropertyValue wv = new PropertyValue("CharWeight", -1,
130 new Float(com.sun.star.awt.FontWeight.BOLD),
131 com.sun.star.beans.PropertyState.DIRECT_VALUE);
133 // Sets the replaced text property color value to RGB parameter
134 PropertyValue cv = new PropertyValue("CharColor", -1,
135 new Integer(red),
136 com.sun.star.beans.PropertyState.DIRECT_VALUE);
138 // Apply the properties
139 PropertyValue[] props = new PropertyValue[] { cv, wv };
141 try {
142 xPropertyReplace.setReplaceAttributes(props);
144 // Only matches whole words and case sensitive
145 descriptor.setPropertyValue(
146 "SearchCaseSensitive", new Boolean(true));
147 descriptor.setPropertyValue("SearchWords", new Boolean(true));
149 catch (com.sun.star.beans.UnknownPropertyException upe) {
150 System.err.println("Error setting up search properties");
151 return;
153 catch (com.sun.star.beans.PropertyVetoException pve) {
154 System.err.println("Error setting up search properties");
155 return;
157 catch (com.sun.star.lang.WrappedTargetException wte) {
158 System.err.println("Error setting up search properties");
159 return;
161 catch (com.sun.star.lang.IllegalArgumentException iae) {
162 System.err.println("Error setting up search properties");
163 return;
166 // Replaces all instances of searchKey with new Text properties
167 // and gets the number of instances of the searchKey
168 descriptor.setSearchString(searchKey);
169 descriptor.setReplaceString(searchKey);
170 replaceable.replaceAll(descriptor);
174 public void disposing(EventObject o)
176 // do nothing
179 private boolean tryLoadingLibrary(
180 XMultiComponentFactory xmcf, XScriptContext context, String name)
182 System.err.println("Try to load ScriptBindingLibrary");
184 try {
185 Object obj = xmcf.createInstanceWithContext(
186 "com.sun.star.script.Application" + name + "LibraryContainer",
187 context.getComponentContext());
189 XLibraryContainer xLibraryContainer = (XLibraryContainer)
190 UnoRuntime.queryInterface(XLibraryContainer.class, obj);
192 System.err.println("Got XLibraryContainer");
194 Object serviceObj = context.getComponentContext().getValueByName(
195 "/singletons/com.sun.star.util.theMacroExpander");
197 XMacroExpander xme = (XMacroExpander) AnyConverter.toObject(
198 new Type(XMacroExpander.class), serviceObj);
200 String bootstrapName = "bootstraprc";
201 if (System.getProperty("os.name").startsWith("Windows")) {
202 bootstrapName = "bootstrap.ini";
205 String libURL = xme.expandMacros(
206 "${$BRAND_BASE_DIR/program/" + bootstrapName + "::BaseInstallation}" +
207 "/share/basic/ScriptBindingLibrary/" +
208 name.toLowerCase() + ".xlb/");
210 System.err.println("libURL is: " + libURL);
212 xLibraryContainer.createLibraryLink(
213 "ScriptBindingLibrary", libURL, false);
215 System.err.println("liblink created");
217 } catch (com.sun.star.uno.Exception e) {
218 System.err.println("Got an exception loading lib: " + e.getMessage());
219 return false;
221 return true;