Samples > 文字列検索ダイアログ |
|
文字列検索ダイアログ文字列検索用のダイアログのサンプルプログラムです。このプラグラムのFindDialogクラスをコピーアンドペーストするだけで、汎用的に使えると思います。使用方法は以下のように、親となるshellウィンドウとITextViewerをインターフェースにもつテキストウィジェットを検索対象として与え、openメソッドでダイアログが表示されます。 Shell shell = ...; TextViewer text = ...; ... FindDialog dialog = new FindDialog(shell, text); dialog.open(); サンプルプログラムの例では、検索対象となるテキストウィジェットとしてTextViewerを使っていますが、SourceViewerもITextViewerインターフェースを持っているので、そちらを使っても結構です。また検索機能の実装は、PatternクラスやMatcherクラスを使って正規表現ベースで検索を行っています。 スクリーンショットソースコード (FindTextTest.java)import java.util.regex.Matcher; import java.util.regex.Pattern; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.TextViewer; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class FindTextTest extends ApplicationWindow { private TextViewer text; public FindTextTest() { super(null); } public static void main(String[] args) { ApplicationWindow w = new FindTextTest(); w.setBlockOnOpen(true); w.open(); Display.getCurrent().dispose(); } protected Control createContents(Composite parent) { getShell().setText("FindTextTest"); Composite container = new Composite(parent, SWT.NONE); container.setLayout(new GridLayout(1, false)); Button openFindDialog = new Button(container, SWT.PUSH); openFindDialog.setText("検索"); openFindDialog.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { FindDialog dialog = new FindDialog(getShell(), text); dialog.open(); } }); text = new TextViewer(container, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL); text.getTextWidget().setWordWrap(true); text.getTextWidget().setLayoutData(new GridData(GridData.FILL_BOTH)); Document doc = new Document(); text.setDocument(doc); getShell().setSize(300, 300); return container; } } class FindDialog extends Dialog { ITextViewer viewer; int findOffset = 0; private Text text; public FindDialog(Shell parent, ITextViewer viewer) { super(parent, SWT.DIALOG_TRIM | SWT.MODELESS); this.viewer = viewer; findOffset = viewer.getTextWidget().getCaretOffset(); } public void open() { Shell shell = new Shell(getParent(), getStyle()); shell.setText("検索"); shell.setLayout(new GridLayout(2, false)); new Label(shell, SWT.NONE).setText("検索:"); text = new Text(shell, SWT.BORDER | SWT.SINGLE); Button findNext = new Button(shell, SWT.PUSH); findNext.setText("前方検索"); findNext.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { doFind(text.getText(), true); } }); Button findPrev = new Button(shell, SWT.PUSH); findPrev.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { doFind(text.getText(), false); } }); findPrev.setText("後方検索"); shell.pack(); shell.open(); Display display = getParent().getDisplay(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void doFind(String str, boolean forwardSearch) { IDocument doc = viewer.getDocument(); Pattern pattern = Pattern.compile(str); Matcher matcher = pattern.matcher(doc.get()); boolean found; int length; if (forwardSearch) { //前方検索 found = matcher.find(findOffset); if (found) { int startOffset = matcher.start(); length = matcher.group().length(); viewer.setSelectedRange(startOffset, length); findOffset = startOffset + length; } } else { //後方検索 found = matcher.find(0); int index = -1; length = -1; while (found && matcher.start() < findOffset) { index = matcher.start(); length = matcher.group().length(); found = matcher.find(index + 1); } if (index != -1) { viewer.setSelectedRange(index, length); findOffset = index; matcher.find(index); } } } } 最新更新日: 2004年8月28日 |
- PR -
マイクロソフト お得な見積! まとめての購入ならオトクな方法で。ライセンスだから管理も簡単。 シマンテック 割引価格! オンライン販売だから低価格。いつでも簡単、見積・購入。 |
||||
|
|||||
Copyright(C) 2003,2004 Jasmin Project. All Right Reserved. |
SEO | [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送 | ||