Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

swt styledtext search/find next  RSS feed

 
Manon Baratt
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;
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!