summaryrefslogtreecommitdiffstats
path: root/scripting/examples/beanshell/Highlight/highlighter.bsh
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /scripting/examples/beanshell/Highlight/highlighter.bsh
parentInitial commit. (diff)
downloadlibreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz
libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'scripting/examples/beanshell/Highlight/highlighter.bsh')
-rw-r--r--scripting/examples/beanshell/Highlight/highlighter.bsh166
1 files changed, 166 insertions, 0 deletions
diff --git a/scripting/examples/beanshell/Highlight/highlighter.bsh b/scripting/examples/beanshell/Highlight/highlighter.bsh
new file mode 100644
index 000000000..a69f76e1a
--- /dev/null
+++ b/scripting/examples/beanshell/Highlight/highlighter.bsh
@@ -0,0 +1,166 @@
+/*
+ * 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 .
+ */
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.util.XReplaceable;
+import com.sun.star.util.XReplaceDescriptor;
+import com.sun.star.util.XPropertyReplace;
+import com.sun.star.beans.PropertyValue;
+import com.sun.star.text.XTextDocument;
+import com.sun.star.script.provider.XScriptContext;
+
+int replaceText(searchKey, color, bold) {
+
+ result = 0;
+
+ try {
+ // Create an XReplaceable object and an XReplaceDescriptor
+ replaceable = (XReplaceable)
+ UnoRuntime.queryInterface(XReplaceable.class, xTextDocument);
+
+ descriptor =
+ (XReplaceDescriptor) replaceable.createReplaceDescriptor();
+
+ // Gets a XPropertyReplace object for altering the properties
+ // of the replaced text
+ xPropertyReplace = (XPropertyReplace)
+ UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
+
+ // Sets the replaced text property fontweight value to Bold or Normal
+ wv = null;
+ if (bold) {
+ wv = new PropertyValue("CharWeight", -1,
+ new Float(com.sun.star.awt.FontWeight.BOLD),
+ com.sun.star.beans.PropertyState.DIRECT_VALUE);
+ }
+ else {
+ wv = new PropertyValue("CharWeight", -1,
+ new Float(com.sun.star.awt.FontWeight.NORMAL),
+ com.sun.star.beans.PropertyState.DIRECT_VALUE);
+ }
+
+ // Sets the replaced text property color value to RGB color parameter
+ cv = new PropertyValue("CharColor", -1, new Integer(color),
+ com.sun.star.beans.PropertyState.DIRECT_VALUE);
+
+ // Apply the properties
+ PropertyValue[] props = { cv, wv };
+ xPropertyReplace.setReplaceAttributes(props);
+
+ // Only matches whole words and case sensitive
+ descriptor.setPropertyValue("SearchCaseSensitive", new Boolean(true));
+ descriptor.setPropertyValue("SearchWords", new 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);
+ result = replaceable.replaceAll(descriptor);
+
+ }
+ catch (Exception e) {
+ }
+
+ return result;
+}
+
+searchKey = "";
+
+// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
+// all BeanShell scripts executed by the Script Framework
+xTextDocument = (XTextDocument)
+ UnoRuntime.queryInterface(XTextDocument.class, XSCRIPTCONTEXT.getDocument());
+
+// Create a JButton and add an ActionListener
+// When clicked the value for the searchKey is read and passed to replaceText
+myListener = new ActionListener() {
+ actionPerformed(ActionEvent e) {
+ searchKey = findTextBox.getText();
+
+ if(searchKey.equalsIgnoreCase("")) {
+ JOptionPane.showMessageDialog(null,
+ "No text entered for search",
+ "No text", JOptionPane.INFORMATION_MESSAGE);
+ }
+ else {
+ // highlight the text in red
+ cRed = new Color(255, 0, 0);
+ red = cRed.getRGB();
+ num = replaceText(searchKey, red, true);
+
+ if(num > 0) {
+ int response = JOptionPane.showConfirmDialog(null,
+ searchKey + " was found " + num +
+ " times\nDo you wish to keep the text highlighted?",
+ "Confirm highlight", JOptionPane.YES_NO_OPTION,
+ JOptionPane.QUESTION_MESSAGE);
+
+ if (response == 1) {
+ cBlack = new Color(255, 255, 255);
+ black = cBlack.getRGB();
+ replaceText(searchKey, black, false);
+ }
+ }
+ else {
+ JOptionPane.showMessageDialog(null,
+ "No matches were found", "Not found",
+ JOptionPane.INFORMATION_MESSAGE);
+ }
+ }
+ }
+};
+
+
+exitListener = new ActionListener() {
+ actionPerformed(ActionEvent e) {
+ frame.dispose();
+ }
+};
+
+
+searchButton = new JButton("Highlight");
+searchButton.addActionListener(myListener);
+
+exitButton = new JButton("Exit");
+exitButton.addActionListener(exitListener);
+
+buttonPanel = new JPanel();
+buttonPanel.setLayout(new FlowLayout());
+buttonPanel.add(searchButton);
+buttonPanel.add(exitButton);
+
+
+// Create a JPanel containing one JTextField for the search text.
+searchPanel = new JPanel();
+searchPanel.setLayout(new FlowLayout());
+findTextBox = new JTextField(20);
+findWhat = new JLabel("Find What: ");
+searchPanel.add(findWhat);
+searchPanel.add(findTextBox);
+
+// Create frame and add a window listener
+frame = new JFrame("Highlight Text");
+frame.setSize(350,130);
+frame.setLocation(430,430);
+frame.setResizable(false);
+// Add the panel and button to the frame
+frame.getContentPane().setLayout(new GridLayout(2,1,10,10));
+frame.getContentPane().add(searchPanel);
+frame.getContentPane().add(buttonPanel);
+
+frame.setVisible(true);
+frame.pack();