Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

swt styledtext search/find next

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to SWT. I need to write a search/find next method for styledtext. I am creating a dialog box with a find button and search text. The example I found searches the whole styledtext and highlights the matches at once. How do I change this code to implement find next?

Thanks



import java.util.LinkedList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.LineStyleEvent;
import org.eclipse.swt.custom.LineStyleListener;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.*;

public class FindUtil {
static String selectedText = "initial xxx";
static StyledText keywordText;
static String keyword;
static Button button;

public static String show(Shell shell, final StyledText styledText) {

final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);
dialog.setText("Find Text");
dialog.setSize(250, 150);

GridLayout layout = new GridLayout();
layout.numColumns = 2;
dialog.setLayout(layout);
dialog.addShellListener(new ShellAdapter() {
public void shellClosed(ShellEvent e) {
// don't dispose of the shell, just hide it for later use
e.doit = false;
dialog.setVisible(false);
}
});
Display display = dialog.getDisplay();

dialog.setLayout(new GridLayout(1, false));

// styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;

keywordText = new StyledText(dialog, SWT.SINGLE | SWT.BORDER);
keywordText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// Font font = new Font(shell.getDisplay(), "Courier New", 12, SWT.NORMAL);

button = new Button(dialog, SWT.PUSH);
button.setText("Find");

button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
keyword = keywordText.getText();
styledText.redraw();
}
});

styledText.addLineStyleListener(new LineStyleListener() {
public void lineGetStyle(LineStyleEvent event) {
if(keyword == null || keyword.length() == 0) {
event.styles = new StyleRange[0];
return;
}

String line = event.lineText;
int cursor = -1;

LinkedList list = new LinkedList();
while( (cursor = line.indexOf(keyword, cursor+1)) >= 0) {
list.add(getHighlightStyle(event.lineOffset+cursor, keyword.length(), dialog));
System.out.println("cursor = " + cursor + " " + keyword );
}

event.styles = (StyleRange[]) list.toArray(new StyleRange[list.size()]);
}
});

dialog.open();

while (!dialog.isDisposed() ) {
if (!display.readAndDispatch() ) {
display.sleep();
}
}

return selectedText;
/*
while (!dialog.isDisposed() ) {
if (!dialog.readAndDispatch() ) {
display
}
}
*/
}
private static StyleRange getHighlightStyle(int startOffset, int length, Shell shell) {
StyleRange styleRange = new StyleRange();
styleRange.start = startOffset;
styleRange.length = length;
styleRange.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
return styleRange;
}
}
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic