RCP定制自己的搜索插件-实例 博客分类: rcp/swfEclipse eclipse搜索插件javaide
程序员文章站
2024-03-17 12:51:40
...
本文介绍如何扩展/定制 Eclipse 的搜索插件 org.eclipse.search(后面附实例)
资源查找是 Eclipse 的一个重要功能。可是,当我们做RCP开发时,Eclipse 搜索功能不一定适合个性化的需求,这是我们就需要扩展 Eclipse 的搜索插件 org.eclipse.search。
这里以Spring IDE Bean Search为例,介绍应扩展之处和其中的关键点。搜索页的类名为BeanSearchPage, 搜索结果页的类名为BeanSearchResultPage
1. 菜单加入显示搜索页(这是入口)
方法一:在org.eclipse.search.menu/dialogGroup中加一个action,以在java\file search 的旁边加一 Tab , 显示你的搜索页 MyBeanSearchPage。
方法二:菜单加入高级查找和替换。如图
2. 扩展org.eclipse.search.searchPages,定制自己的搜索页 MyBeanSearchPage
public class MyBeanSearchPage extends DialogPage implements ISearchPage { private Text txtDirectory; private ISearchPageContainer fContainer; private Text txtPath; public MyBeanSearchPage() { } @Override public void createControl(Composite parent) { initializeDialogUnits(parent); Composite composite = new Composite(parent, SWT.NONE); composite.setFont(parent.getFont()); GridLayout gridLayout = new GridLayout(3, false); composite.setLayout(gridLayout); Label lblNewLabel = new Label(composite, SWT.NONE); lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); lblNewLabel.setText("搜索关键字"); txtDirectory = new Text(composite, SWT.BORDER); txtDirectory.setText("directory"); txtDirectory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); setControl(composite); new Label(composite, SWT.NONE); Label lblNewLabel_1 = new Label(composite, SWT.NONE); lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); lblNewLabel_1.setText("目录路径"); txtPath = new Text(composite, SWT.BORDER); txtPath.setText("E:\\yucheng\\bsp\\trunk\\runtime-BankITIDE.product\\HelloBSP\\HelloWorld\\HelloCommu\\Hello\\input\\pcs"); txtPath.setEditable(false); txtPath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); Button btnNewButton = new Button(composite, SWT.NONE); btnNewButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { DirectoryDialog directoryDialog = new DirectoryDialog( getShell()); directoryDialog.setText("选择目录路径"); String directoryPath = directoryDialog.open(); txtPath.setText(directoryPath); } }); btnNewButton.setText("选择目录路径"); } @Override public boolean performAction() { NewSearchUI.activateSearchResultView(); MyBeanSearchQuery myBeanSearchQuery = new MyBeanSearchQuery(); myBeanSearchQuery.setKeyword(txtDirectory.getText()); myBeanSearchQuery.setDirectoryPath(txtPath.getText()); NewSearchUI.runQueryInBackground(myBeanSearchQuery); return true; } @Override public void setContainer(ISearchPageContainer container) { fContainer = container; } }
3. 扩展org.eclipse.search.searchResultViewPages,定制自己的搜索结果页BeanSearchResultPage
public class MySearchResultPage extends AbstractTextSearchViewPage { private MyFileTreeContentProvider fContentProvider; @Override protected void elementsChanged(Object[] objects) { if (fContentProvider != null) { fContentProvider.elementsChanged(objects); } } @Override protected void clear() { if (fContentProvider != null) fContentProvider.clear(); } @Override protected void configureTreeViewer(TreeViewer viewer) { viewer.setUseHashlookup(true); MyFileLabelProvider myFileLabelProvider = new MyFileLabelProvider(); viewer.setLabelProvider(myFileLabelProvider); viewer.setContentProvider(new MyFileTreeContentProvider(viewer)); fContentProvider = (MyFileTreeContentProvider) viewer .getContentProvider(); this.setElementLimit(50); } @Override protected void configureTableViewer(TableViewer viewer) { // TODO Auto-generated method stub } }
2和3的扩展点
4. 此外,还用到的相关接口,详见附件的插件工程
ISearchQuery.java,搜索的入口
Match.java,结果集
NewSearchUI.java,UI启动类
5. 运行
搜索输入界面
搜索结果显示界面
6. 附件是一个简单的示例。