From de01048e5121356658eed00b3c215ad79cf278b3 Mon Sep 17 00:00:00 2001 From: Bogdan Gheorghe Date: Mon, 29 Mar 2004 04:32:01 +0000 Subject: [PATCH] Added background search from CSearchPage --- .../utils/org/eclipse/cdt/utils/TimeOut.java | 3 +- core/org.eclipse.cdt.ui/ChangeLog | 12 ++ .../cdt/internal/ui/search/CSearchPage.java | 11 +- .../cdt/internal/ui/search/CSearchQuery.java | 152 ++++++++++++++++++ .../cdt/internal/ui/search/CSearchResult.java | 140 ++++++++++++++++ .../ui/search/CSearchResultCollector.java | 45 ++++-- 6 files changed, 349 insertions(+), 14 deletions(-) create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchQuery.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResult.java diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/TimeOut.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/TimeOut.java index 2320ed80e65..7bf03ea6f17 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/TimeOut.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/TimeOut.java @@ -85,9 +85,8 @@ public class TimeOut implements Runnable { public void reset() { - System.out.println("TimeOut reset"); enabled=false; - thread = new Thread(this, "Time Out Thread"); + thread = new Thread(this, "Time Out Thread"); //$NON-NLS-1$ thread.setDaemon(true); thread.setPriority(threadPriority); thread.start(); diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog index c394c8fea7c..4c004414a1e 100644 --- a/core/org.eclipse.cdt.ui/ChangeLog +++ b/core/org.eclipse.cdt.ui/ChangeLog @@ -1,3 +1,15 @@ +2004-03-28 Bogdan Gheorghe + Modified CSearchPage to use new CSearchQuery (which uses the new background + jobs API) + + Modified CSearchResultCollector to use CSearchQuery if present (ie. if invoked + from the CSearchPage) + + * src/org/eclipse/cdt/internal/ui/search/CSearchResultCollector.java + * src/org/eclipse/cdt/internal/ui/search/CSearchPage.java + * src/org/eclipse/cdt/internal/ui/search/CSearchResult.java + * src/org/eclipse/cdt/internal/ui/search/CSearchQuery.java + 2004-03-28 Alain Magloire Provide a global hook for the action "Show Selected element" diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchPage.java index a5d2e44e7f1..1727b87bd48 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchPage.java @@ -43,6 +43,7 @@ import org.eclipse.search.internal.ui.util.RowLayouter; import org.eclipse.search.ui.ISearchPage; import org.eclipse.search.ui.ISearchPageContainer; import org.eclipse.search.ui.ISearchResultViewEntry; +import org.eclipse.search.ui.NewSearchUI; import org.eclipse.search.ui.SearchUI; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; @@ -126,7 +127,8 @@ public class CSearchPage extends DialogPage implements ISearchPage, ICSearchCons } else { searching = data.searchFor; } - +//TODO: Remove +/* CSearchOperation op = new CSearchOperation(workspace, data.pattern, data.isCaseSensitive, searching, data.limitTo, scope, scopeDescription, collector); @@ -139,6 +141,13 @@ public class CSearchPage extends DialogPage implements ISearchPage, ICSearchCons } catch (InterruptedException ex) { return false; } +*/ + + CSearchQuery job = new CSearchQuery(workspace, data.pattern, data.isCaseSensitive, searching, data.limitTo, scope, scopeDescription, collector); + NewSearchUI.activateSearchResultView(); + CSearchResult result = new CSearchResult(job); + NewSearchUI.runSearchInBackground(job, result); + return true; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchQuery.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchQuery.java new file mode 100644 index 00000000000..3b68d2d9135 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchQuery.java @@ -0,0 +1,152 @@ +/* + * Created on Mar 26, 2004 + * + * To change the template for this generated file go to + * Window - Preferences - Java - Code Generation - Code and Comments + */ +package org.eclipse.cdt.internal.ui.search; + +import java.util.Iterator; +import java.util.List; + +import org.eclipse.cdt.core.search.ICSearchConstants; +import org.eclipse.cdt.core.search.ICSearchPattern; +import org.eclipse.cdt.core.search.ICSearchScope; +import org.eclipse.cdt.core.search.OrPattern; +import org.eclipse.cdt.core.search.SearchEngine; +import org.eclipse.cdt.internal.ui.CPluginImages; +import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.search.ui.ISearchQuery; +import org.eclipse.search.ui.ISearchResult; +/** + * @author bog + * + * To change the template for this generated type comment go to + * Window - Preferences - Java - Code Generation - Code and Comments + */ +public class CSearchQuery implements ISearchQuery, ICSearchConstants { + + private CSearchResultCollector _collector; + private IWorkspace _workspace; + private ICSearchScope _scope; + private String _stringPattern; + private String _scopeDescription; + private boolean _caseSensitive; + private LimitTo _limitTo; + private List _searchFor; + + public CSearchQuery(IWorkspace workspace, String pattern, boolean caseSensitive, List searchFor, LimitTo limitTo, ICSearchScope scope, String scopeDescription, CSearchResultCollector collector) { + this( workspace, limitTo, scope, scopeDescription, collector ); + _stringPattern = pattern; + _caseSensitive = caseSensitive; + _searchFor = searchFor; + } + + public CSearchQuery(IWorkspace workspace, LimitTo limitTo, ICSearchScope scope, String scopeDescription, CSearchResultCollector collector ){ + _workspace = workspace; + _limitTo = limitTo; + _scope = scope; + _scopeDescription = scopeDescription; + _collector = collector; + _collector.setOperation( this ); + } + /* (non-Javadoc) + * @see org.eclipse.search.ui.ISearchQuery#run(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.search.ui.ISearchResult) + */ + public IStatus run(IProgressMonitor monitor, ISearchResult result) { + _collector.setProgressMonitor( monitor ); + + SearchEngine engine = new SearchEngine( CUIPlugin.getSharedWorkingCopies() ); + + ICSearchPattern pattern = null; + if( _searchFor.size() > 1 ){ + OrPattern orPattern = new OrPattern(); + for (Iterator iter = _searchFor.iterator(); iter.hasNext();) { + SearchFor element = (SearchFor)iter.next(); + orPattern.addPattern( SearchEngine.createSearchPattern( _stringPattern, element, _limitTo, _caseSensitive ) ); + } + + pattern = orPattern; + + } else { + Iterator iter = _searchFor.iterator(); + pattern = SearchEngine.createSearchPattern( _stringPattern, (SearchFor)iter.next(), _limitTo, _caseSensitive ); + } + + try { + engine.search( _workspace, pattern, _scope, _collector, false ); + } catch (InterruptedException e) { + } + + return new Status(IStatus.OK, CUIPlugin.getPluginId(),0,"",null); //$NON-NLS-1$ + } + + /* (non-Javadoc) + * @see org.eclipse.search.ui.ISearchQuery#getName() + */ + public String getName() { + // TODO Auto-generated method stub + return "CDT Search Job"; //$NON-NLS-1$ + } + + /** + * @return + */ + public String getSingularLabel() { + String desc = null; + + //if( _elementPattern != null ){ + // desc = _elementPattern.getElementName(); + //} else { + desc = _stringPattern; + //} + + String [] args = new String [] { desc, _scopeDescription }; + + if( _limitTo == DECLARATIONS ){ + return CSearchMessages.getFormattedString( "CSearchOperation.singularDeclarationsPostfix", args ); //$NON_NLS-1$ //$NON-NLS-1$ + } else if( _limitTo == REFERENCES ){ + return CSearchMessages.getFormattedString( "CSearchOperation.singularReferencesPostfix", args ); //$NON_NLS-1$ //$NON-NLS-1$ + } else { + return CSearchMessages.getFormattedString( "CSearchOperation.singularOccurrencesPostfix", args ); //$NON_NLS-1$ //$NON-NLS-1$ + } + } + + /** + * @return + */ + public String getPluralLabelPattern() { + String desc = null; + + // if( _elementPattern != null ){ + // desc = _elementPattern.getElementName(); + // } else { + desc = _stringPattern; + // } + + String [] args = new String [] { desc, "{0}", _scopeDescription }; //$NON-NLS-1$ + if( _limitTo == DECLARATIONS ){ + return CSearchMessages.getFormattedString( "CSearchOperation.pluralDeclarationsPostfix", args ); //$NON_NLS-1$ //$NON-NLS-1$ + } else if ( _limitTo == REFERENCES ){ + return CSearchMessages.getFormattedString( "CSearchOperation.pluralReferencesPostfix", args ); //$NON_NLS-1$ //$NON-NLS-1$ + } else { + return CSearchMessages.getFormattedString( "CSearchOperation.pluralOccurrencesPostfix", args ); //$NON_NLS-1$ //$NON-NLS-1$ + } + } + + /** + * @return + */ + public ImageDescriptor getImageDescriptor() { + if( _limitTo == ICSearchConstants.DECLARATIONS ){ + return CPluginImages.DESC_OBJS_SEARCH_DECL; + } else { + return CPluginImages.DESC_OBJS_SEARCH_REF; + } + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResult.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResult.java new file mode 100644 index 00000000000..87e6d404a2d --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResult.java @@ -0,0 +1,140 @@ +/* + * Created on Mar 26, 2004 + * + * To change the template for this generated file go to + * Window - Preferences - Java - Code Generation - Code and Comments + */ +package org.eclipse.cdt.internal.ui.search; + +import java.text.MessageFormat; +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.IParent; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.search.ui.ISearchQuery; +import org.eclipse.search.ui.text.AbstractTextSearchResult; +import org.eclipse.search.ui.text.Match; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IFileEditorInput; +/** + * @author bog + * + * To change the template for this generated type comment go to + * Window - Preferences - Java - Code Generation - Code and Comments + */ +public class CSearchResult extends AbstractTextSearchResult { + CSearchQuery cQuery; + + public CSearchResult(CSearchQuery query){ + cQuery = query; + } + + /* (non-Javadoc) + * @see org.eclipse.search.ui.text.AbstractTextSearchResult#findContainedMatches(org.eclipse.core.resources.IFile) + */ + public Match[] findContainedMatches(IFile file) { + ICElement cElement= create(file); + Set matches= new HashSet(); + collectMatches(matches, cElement); + return (Match[]) matches.toArray(new Match[matches.size()]); + } + + private ICElement create(IFile file){ + IProject project = file.getProject(); + ICProject cProject = CCorePlugin.getDefault().getCoreModel().create(project); + return cProject; + } + + private void collectMatches(Set matches, ICElement element) { + Match[] m= getMatches(element); + if (m.length != 0) { + for (int i= 0; i < m.length; i++) { + matches.add(m[i]); + } + } + if (element instanceof IParent) { + IParent parent= (IParent) element; + + ICElement[] children= parent.getChildren(); + for (int i= 0; i < children.length; i++) { + collectMatches(matches, children[i]); + } + + } + } + /* (non-Javadoc) + * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getFile(java.lang.Object) + */ + public IFile getFile(Object element) { + if (element instanceof ICElement) { + ICElement cElement= (ICElement) element; + element= cElement.getUnderlyingResource(); + } + if (element instanceof IFile) + return (IFile)element; + return null; + } + /* (non-Javadoc) + * @see org.eclipse.search.ui.text.AbstractTextSearchResult#isShownInEditor(org.eclipse.search.ui.text.Match, org.eclipse.ui.IEditorPart) + */ + public boolean isShownInEditor(Match match, IEditorPart editor) { + // TODO Auto-generated method stub + return false; + } + /* (non-Javadoc) + * @see org.eclipse.search.ui.text.AbstractTextSearchResult#findContainedMatches(org.eclipse.ui.IEditorPart) + */ + public Match[] findContainedMatches(IEditorPart editor) { + IEditorInput editorInput= editor.getEditorInput(); + if (editorInput instanceof IFileEditorInput) { + IFileEditorInput fileEditorInput= (IFileEditorInput) editorInput; + return findContainedMatches(fileEditorInput.getFile()); + } /*else if (editorInput instanceof IClassFileEditorInput) { + IClassFileEditorInput classFileEditorInput= (IClassFileEditorInput) editorInput; + Set matches= new HashSet(); + collectMatches(matches, classFileEditorInput.getClassFile()); + return (Match[]) matches.toArray(new Match[matches.size()]); + }*/ + return null; + } + /* (non-Javadoc) + * @see org.eclipse.search.ui.ISearchResult#getText() + */ + public String getText() { + int matchCount= getMatchCount(); + String format= null; + if (matchCount == 1) + format= cQuery.getSingularLabel(); + else + format= cQuery.getPluralLabelPattern(); + return MessageFormat.format(format, new Object[] { new Integer(matchCount) }); + } + /* (non-Javadoc) + * @see org.eclipse.search.ui.ISearchResult#getTooltip() + */ + public String getTooltip() { + // TODO Auto-generated method stub + return getText(); + } + /* (non-Javadoc) + * @see org.eclipse.search.ui.ISearchResult#getImageDescriptor() + */ + public ImageDescriptor getImageDescriptor() { + return cQuery.getImageDescriptor(); + } + /* (non-Javadoc) + * @see org.eclipse.search.ui.ISearchResult#getQuery() + */ + public ISearchQuery getQuery() { + // TODO Auto-generated method stub + return cQuery; + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResultCollector.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResultCollector.java index afa3be82e1f..c29edbe80a3 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResultCollector.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResultCollector.java @@ -72,17 +72,32 @@ public class CSearchResultCollector extends BasicSearchResultCollector{ _computer = new GroupByKeyComputer(); if( _view != null ){ - _view.searchStarted( - null,//new ActionGroupFactory(), - _operation.getSingularLabel(), - _operation.getPluralLabelPattern(), - _operation.getImageDescriptor(), - CSearchPage.EXTENSION_POINT_ID, - labelProvider, - new GotoMarkerAction(), - _computer, - _operation - ); + if (_operation != null){ + _view.searchStarted( + null,//new ActionGroupFactory(), + _operation.getSingularLabel(), + _operation.getPluralLabelPattern(), + _operation.getImageDescriptor(), + CSearchPage.EXTENSION_POINT_ID, + labelProvider, + new GotoMarkerAction(), + _computer, + _operation + ); + } + else if (_query != null){ + _view.searchStarted( + null,//new ActionGroupFactory(), + _query.getSingularLabel(), + _query.getPluralLabelPattern(), + _query.getImageDescriptor(), + CSearchPage.EXTENSION_POINT_ID, + labelProvider, + new GotoMarkerAction(), + _computer, + null + ); + } } if( getProgressMonitor() != null && !getProgressMonitor().isCanceled() ){ @@ -200,4 +215,12 @@ public class CSearchResultCollector extends BasicSearchResultCollector{ private ISearchResultView _view; private IGroupByKeyComputer _computer; private int _matchCount; + private CSearchQuery _query; + + /** + * @param query + */ + public void setOperation(CSearchQuery query) { + _query = query; + } } \ No newline at end of file