SWT浏览器关注的是next和previous高亮文本

时间:2023-01-12 13:58:14

I am developing a small application with SWT Browser widget. I am highlighting a search text word with

我正在开发一个带有SWT浏览器小部件的小应用程序。我用高亮显示一个搜索文本单词

<a id="xyz" href=''><mark>test</mark></a>

in a HTML document. and replace all the search words in HTML Text in this way so we get all the search words highlighted.

在一个HTML文档。用这种方法替换HTML文本中的所有搜索词,这样我们就可以突出显示所有搜索词。

    htmltext.replaceAll("(?i)"+Pattern.quote(searchword), "\\<a id='xyz' href=''> <mark>$0\\</mark></a>

I want to implement functionality that if I click on next button, next highlighted word should get focus and if I click on previous button previous highlighted text should get focus. how can I accomplish Next and Previous Hit using Javascript in Eclipse RCP application.

我想实现的功能是,如果我点击next按钮,下一个突出显示的单词就会得到关注,如果我点击上一个按钮,先前突出显示的文本就会得到关注。如何在Eclipse RCP应用程序中使用Javascript完成下一个和上一个命中。

1 个解决方案

#1


1  

This is best solved by combining JavaScript with Java code. It depends what kind of HTML content are you going to handle, if it's stateful (e.g. cannot reload), dynamic with lot of JS code, or plain static. In most cases, the best solution would involve most of logic to be written in JS and just minimal code in Java to bind JS actions to SWT GUI.

最好通过将JavaScript和Java代码结合使用来解决这个问题。这取决于您要处理什么样的HTML内容,如果它是有状态的(例如不能重载)、动态的、带有大量JS代码的,或者纯静态的。在大多数情况下,最好的解决方案是用JS编写大部分的逻辑,而Java中的最小代码将JS操作绑定到SWT GUI。

There's several things you need to implement:

你需要实现以下几点:

  1. keyword searching
  2. 关键字搜索
  3. toggling highlighting
  4. 切换高亮显示
  5. toggling highlight from one word to another
  6. 从一个词到另一个词的突出强调。

1. Search: you realise that you won't be able to search for words that span through many HTML elements, like W<span>o</span>rd? If that's ok then you can just search and replace from Java as you do now. I'd go for individually tagging each word match with id: <span id="match1"> and remembering how many matches in total were found.

1。搜索:您意识到您无法搜索跨越许多HTML元素的单词,比如Word?如果可以,那么您可以像现在这样从Java中搜索和替换。我将使用id: 对每个单词进行单独标记,并记住总共找到了多少个匹配。

You could likely do such search on JS side as well by adding a function that iterates through DOM and searches for specific text and wraps it with another DOM object.

通过添加一个在DOM中迭代并搜索特定文本并使用另一个DOM对象包装它的函数,您也可以在JS端进行这样的搜索。

2. Toggling highlighting: It's best done in JavaScript. Append to your HTML a JS code fragment that toggles DOM element style. Something like: `

2。切换高亮显示:最好使用JavaScript。在HTML后面附加一个用于切换DOM元素样式的JS代码片段。比如:“

function highlight(id) {
  document.getElementById(id).className = 'highlighted'
}

You'll be able to call this JS from SWT by invoking swtBrowser.execute("highlight('match1')") Further you should implement function that takes off highlighting.

通过调用swtbrowsr .execute(“highlight('match1')”),您将能够从SWT调用这个JS。

3. Toggling highlighting between elements: This can be done both on Java side and on JS side. I would probably go with JS and add two more functions: highlightNext() and highlightPrev() that would just call highlight() function with proper ids. Then in Java you could make SWT buttons that call JS functions through SWTBrowser.execute().

3所示。在元素之间切换突出显示:这可以在Java端和JS端完成。我可能会使用JS,并添加两个功能:highlightNext()和highlightPrev(),它只调用具有适当id的高亮显示()函数。然后在Java中,可以创建SWT按钮,通过swtbrowsr .execute()调用JS函数。

#1


1  

This is best solved by combining JavaScript with Java code. It depends what kind of HTML content are you going to handle, if it's stateful (e.g. cannot reload), dynamic with lot of JS code, or plain static. In most cases, the best solution would involve most of logic to be written in JS and just minimal code in Java to bind JS actions to SWT GUI.

最好通过将JavaScript和Java代码结合使用来解决这个问题。这取决于您要处理什么样的HTML内容,如果它是有状态的(例如不能重载)、动态的、带有大量JS代码的,或者纯静态的。在大多数情况下,最好的解决方案是用JS编写大部分的逻辑,而Java中的最小代码将JS操作绑定到SWT GUI。

There's several things you need to implement:

你需要实现以下几点:

  1. keyword searching
  2. 关键字搜索
  3. toggling highlighting
  4. 切换高亮显示
  5. toggling highlight from one word to another
  6. 从一个词到另一个词的突出强调。

1. Search: you realise that you won't be able to search for words that span through many HTML elements, like W<span>o</span>rd? If that's ok then you can just search and replace from Java as you do now. I'd go for individually tagging each word match with id: <span id="match1"> and remembering how many matches in total were found.

1。搜索:您意识到您无法搜索跨越许多HTML元素的单词,比如Word?如果可以,那么您可以像现在这样从Java中搜索和替换。我将使用id: 对每个单词进行单独标记,并记住总共找到了多少个匹配。

You could likely do such search on JS side as well by adding a function that iterates through DOM and searches for specific text and wraps it with another DOM object.

通过添加一个在DOM中迭代并搜索特定文本并使用另一个DOM对象包装它的函数,您也可以在JS端进行这样的搜索。

2. Toggling highlighting: It's best done in JavaScript. Append to your HTML a JS code fragment that toggles DOM element style. Something like: `

2。切换高亮显示:最好使用JavaScript。在HTML后面附加一个用于切换DOM元素样式的JS代码片段。比如:“

function highlight(id) {
  document.getElementById(id).className = 'highlighted'
}

You'll be able to call this JS from SWT by invoking swtBrowser.execute("highlight('match1')") Further you should implement function that takes off highlighting.

通过调用swtbrowsr .execute(“highlight('match1')”),您将能够从SWT调用这个JS。

3. Toggling highlighting between elements: This can be done both on Java side and on JS side. I would probably go with JS and add two more functions: highlightNext() and highlightPrev() that would just call highlight() function with proper ids. Then in Java you could make SWT buttons that call JS functions through SWTBrowser.execute().

3所示。在元素之间切换突出显示:这可以在Java端和JS端完成。我可能会使用JS,并添加两个功能:highlightNext()和highlightPrev(),它只调用具有适当id的高亮显示()函数。然后在Java中,可以创建SWT按钮,通过swtbrowsr .execute()调用JS函数。