Files
file-online-preview/server/libreoffice/share/Scripts/java/Highlight/HighlightText.java

234 lines
9.2 KiB
Java
Raw Normal View History

2021-06-23 10:26:22 +08:00
/*
* 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 .
*/
package org.libreoffice.example.java_scripts;
2019-04-15 10:23:03 +08:00
import com.sun.star.uno.UnoRuntime;
import com.sun.star.script.provider.XScriptContext;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.EventObject;
import com.sun.star.uno.Type;
import com.sun.star.uno.AnyConverter;
import com.sun.star.text.XTextDocument;
import com.sun.star.beans.PropertyValue;
import com.sun.star.script.XLibraryContainer;
import com.sun.star.awt.*;
import com.sun.star.util.*;
import java.awt.Color;
public class HighlightText implements com.sun.star.awt.XActionListener {
// UNO awt components of the Highlight dialog
XDialog findDialog = null;
XTextComponent findTextBox;
// The document being searched
XTextDocument theDocument;
// The text to be searched for
private String searchKey = "";
public void showForm(XScriptContext context) {
System.err.println("Starting showForm");
XMultiComponentFactory xmcf =
context.getComponentContext().getServiceManager();
Object[] args = new Object[1];
args[0] = context.getDocument();
Object obj;
2021-06-23 10:26:22 +08:00
2019-04-15 10:23:03 +08:00
try {
obj = xmcf.createInstanceWithArgumentsAndContext(
2021-06-23 10:26:22 +08:00
"com.sun.star.awt.DialogProvider", args,
context.getComponentContext());
} catch (com.sun.star.uno.Exception e) {
2019-04-15 10:23:03 +08:00
System.err.println("Error getting DialogProvider object");
return;
}
XDialogProvider xDialogProvider = (XDialogProvider)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XDialogProvider.class, obj);
2019-04-15 10:23:03 +08:00
System.err.println("Got DialogProvider, now get dialog");
try {
findDialog = xDialogProvider.createDialog(
2021-06-23 10:26:22 +08:00
"vnd.sun.star.script:" +
"ScriptBindingLibrary.Highlight?location=application");
} catch (java.lang.Exception e) {
2019-04-15 10:23:03 +08:00
System.err.println("Got exception on first creating dialog: " +
2021-06-23 10:26:22 +08:00
e.getMessage());
2019-04-15 10:23:03 +08:00
}
if (findDialog == null) {
2021-06-23 10:26:22 +08:00
if (!tryLoadingLibrary(xmcf, context, "Dialog") ||
!tryLoadingLibrary(xmcf, context, "Script")) {
2019-04-15 10:23:03 +08:00
System.err.println("Error loading ScriptBindingLibrary");
return;
}
2021-06-23 10:26:22 +08:00
2019-04-15 10:23:03 +08:00
try {
findDialog = xDialogProvider.createDialog(
2021-06-23 10:26:22 +08:00
"vnd.sun.star.script://" +
"ScriptBindingLibrary.Highlight?location=application");
} catch (com.sun.star.lang.IllegalArgumentException iae) {
2019-04-15 10:23:03 +08:00
System.err.println("Error loading ScriptBindingLibrary");
return;
}
}
XControlContainer controls = (XControlContainer)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XControlContainer.class, findDialog);
2019-04-15 10:23:03 +08:00
XButton highlightButton = (XButton) UnoRuntime.queryInterface(
2021-06-23 10:26:22 +08:00
XButton.class, controls.getControl("HighlightButton"));
2019-04-15 10:23:03 +08:00
highlightButton.setActionCommand("Highlight");
findTextBox = (XTextComponent) UnoRuntime.queryInterface(
2021-06-23 10:26:22 +08:00
XTextComponent.class, controls.getControl("HighlightTextField"));
2019-04-15 10:23:03 +08:00
XButton exitButton = (XButton) UnoRuntime.queryInterface(
2021-06-23 10:26:22 +08:00
XButton.class, controls.getControl("ExitButton"));
2019-04-15 10:23:03 +08:00
exitButton.setActionCommand("Exit");
theDocument = (XTextDocument) UnoRuntime.queryInterface(
2021-06-23 10:26:22 +08:00
XTextDocument.class, context.getDocument());
2019-04-15 10:23:03 +08:00
highlightButton.addActionListener(this);
exitButton.addActionListener(this);
findDialog.execute();
}
public void actionPerformed(ActionEvent e) {
if (e.ActionCommand.equals("Exit")) {
findDialog.endExecute();
return;
2021-06-23 10:26:22 +08:00
} else if (e.ActionCommand.equals("Highlight")) {
2019-04-15 10:23:03 +08:00
searchKey = findTextBox.getText();
// highlight the text in red
Color cRed = new Color(255, 0, 0);
int red = cRed.getRGB();
2021-06-23 10:26:22 +08:00
2019-04-15 10:23:03 +08:00
XReplaceable replaceable = (XReplaceable)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XReplaceable.class, theDocument);
2019-04-15 10:23:03 +08:00
XReplaceDescriptor descriptor =
(XReplaceDescriptor) replaceable.createReplaceDescriptor();
// Gets a XPropertyReplace object for altering the properties
// of the replaced text
XPropertyReplace xPropertyReplace = (XPropertyReplace)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
2019-04-15 10:23:03 +08:00
// Sets the replaced text property fontweight value to Bold
PropertyValue wv = new PropertyValue("CharWeight", -1,
2021-06-23 10:26:22 +08:00
new Float(com.sun.star.awt.FontWeight.BOLD),
com.sun.star.beans.PropertyState.DIRECT_VALUE);
2019-04-15 10:23:03 +08:00
// Sets the replaced text property color value to RGB parameter
PropertyValue cv = new PropertyValue("CharColor", -1,
2021-06-23 10:26:22 +08:00
Integer.valueOf(red),
com.sun.star.beans.PropertyState.DIRECT_VALUE);
2019-04-15 10:23:03 +08:00
// Apply the properties
2021-06-23 10:26:22 +08:00
PropertyValue[] props = new PropertyValue[] { cv, wv };
2019-04-15 10:23:03 +08:00
try {
xPropertyReplace.setReplaceAttributes(props);
// Only matches whole words and case sensitive
descriptor.setPropertyValue(
2021-06-23 10:26:22 +08:00
"SearchCaseSensitive", Boolean.TRUE);
descriptor.setPropertyValue("SearchWords", Boolean.TRUE);
} catch (com.sun.star.beans.UnknownPropertyException upe) {
2019-04-15 10:23:03 +08:00
System.err.println("Error setting up search properties");
return;
2021-06-23 10:26:22 +08:00
} catch (com.sun.star.beans.PropertyVetoException pve) {
2019-04-15 10:23:03 +08:00
System.err.println("Error setting up search properties");
return;
2021-06-23 10:26:22 +08:00
} catch (com.sun.star.lang.WrappedTargetException wte) {
2019-04-15 10:23:03 +08:00
System.err.println("Error setting up search properties");
return;
2021-06-23 10:26:22 +08:00
} catch (com.sun.star.lang.IllegalArgumentException iae) {
2019-04-15 10:23:03 +08:00
System.err.println("Error setting up search properties");
return;
}
// Replaces all instances of searchKey with new Text properties
2021-06-23 10:26:22 +08:00
// and gets the number of instances of the searchKey
descriptor.setSearchString(searchKey);
descriptor.setReplaceString(searchKey);
2019-04-15 10:23:03 +08:00
replaceable.replaceAll(descriptor);
}
}
2021-06-23 10:26:22 +08:00
public void disposing(EventObject o) {
2019-04-15 10:23:03 +08:00
// do nothing
}
private boolean tryLoadingLibrary(
2021-06-23 10:26:22 +08:00
XMultiComponentFactory xmcf, XScriptContext context, String name) {
2019-04-15 10:23:03 +08:00
System.err.println("Try to load ScriptBindingLibrary");
try {
Object obj = xmcf.createInstanceWithContext(
2021-06-23 10:26:22 +08:00
"com.sun.star.script.Application" + name + "LibraryContainer",
context.getComponentContext());
2019-04-15 10:23:03 +08:00
XLibraryContainer xLibraryContainer = (XLibraryContainer)
2021-06-23 10:26:22 +08:00
UnoRuntime.queryInterface(XLibraryContainer.class, obj);
2019-04-15 10:23:03 +08:00
System.err.println("Got XLibraryContainer");
Object serviceObj = context.getComponentContext().getValueByName(
2021-06-23 10:26:22 +08:00
"/singletons/com.sun.star.util.theMacroExpander");
2019-04-15 10:23:03 +08:00
XMacroExpander xme = (XMacroExpander) AnyConverter.toObject(
2021-06-23 10:26:22 +08:00
new Type(XMacroExpander.class), serviceObj);
2019-04-15 10:23:03 +08:00
String bootstrapName = "bootstraprc";
2021-06-23 10:26:22 +08:00
2019-04-15 10:23:03 +08:00
if (System.getProperty("os.name").startsWith("Windows")) {
bootstrapName = "bootstrap.ini";
}
String libURL = xme.expandMacros(
2021-06-23 10:26:22 +08:00
"$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/basic/ScriptBindingLibrary/" +
name.toLowerCase() + ".xlb/");
2019-04-15 10:23:03 +08:00
System.err.println("libURL is: " + libURL);
xLibraryContainer.createLibraryLink(
"ScriptBindingLibrary", libURL, false);
System.err.println("liblink created");
} catch (com.sun.star.uno.Exception e) {
System.err.println("Got an exception loading lib: " + e.getMessage());
return false;
}
2021-06-23 10:26:22 +08:00
2019-04-15 10:23:03 +08:00
return true;
}
}