001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.search;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.runtime.IPath;
018: import org.eclipse.core.runtime.Path;
019: import org.eclipse.jface.resource.ImageDescriptor;
020: import org.eclipse.jface.text.IDocument;
021: import org.eclipse.pde.core.plugin.IPluginObject;
022: import org.eclipse.pde.internal.ui.PDEPluginImages;
023: import org.eclipse.pde.internal.ui.PDEUIMessages;
024: import org.eclipse.search.ui.ISearchQuery;
025: import org.eclipse.search.ui.text.AbstractTextSearchResult;
026: import org.eclipse.search.ui.text.IEditorMatchAdapter;
027: import org.eclipse.search.ui.text.IFileMatchAdapter;
028: import org.eclipse.search.ui.text.ISearchEditorAccess;
029: import org.eclipse.search.ui.text.Match;
030: import org.eclipse.ui.IEditorPart;
031: import org.eclipse.ui.texteditor.ITextEditor;
032:
033: public class SearchResult extends AbstractTextSearchResult implements
034: IEditorMatchAdapter {
035: protected ISearchQuery fQuery;
036:
037: public SearchResult(ISearchQuery query) {
038: fQuery = query;
039: }
040:
041: /* (non-Javadoc)
042: * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getEditorMatchAdapter()
043: */
044: public IEditorMatchAdapter getEditorMatchAdapter() {
045: return this ;
046: }
047:
048: /* (non-Javadoc)
049: * @see org.eclipse.search.ui.ISearchResult#getLabel()
050: */
051: public String getLabel() {
052: int numMatches = getMatchCount();
053: return fQuery.getLabel()
054: + " - " + numMatches + " " + (numMatches == 1 ? PDEUIMessages.SearchResult_match : PDEUIMessages.SearchResult_matches); //$NON-NLS-1$ //$NON-NLS-2$
055: }
056:
057: /* (non-Javadoc)
058: * @see org.eclipse.search.ui.ISearchResult#getTooltip()
059: */
060: public String getTooltip() {
061: return null;
062: }
063:
064: /* (non-Javadoc)
065: * @see org.eclipse.search.ui.ISearchResult#getImageDescriptor()
066: */
067: public ImageDescriptor getImageDescriptor() {
068: return PDEPluginImages.DESC_PSEARCH_OBJ;
069: }
070:
071: /* (non-Javadoc)
072: * @see org.eclipse.search.ui.ISearchResult#getQuery()
073: */
074: public ISearchQuery getQuery() {
075: return fQuery;
076: }
077:
078: /* (non-Javadoc)
079: * @see org.eclipse.search.ui.text.IEditorMatchAdapter#isShownInEditor(org.eclipse.search.ui.text.Match, org.eclipse.ui.IEditorPart)
080: */
081: public boolean isShownInEditor(Match match, IEditorPart editor) {
082: Object element = match.getElement();
083: if (element instanceof IPluginObject)
084: return isMatchContained(editor, (IPluginObject) element);
085: return false;
086: }
087:
088: /* (non-Javadoc)
089: * @see org.eclipse.search.ui.text.IEditorMatchAdapter#computeContainedMatches(org.eclipse.search.ui.text.AbstractTextSearchResult, org.eclipse.ui.IEditorPart)
090: */
091: public Match[] computeContainedMatches(
092: AbstractTextSearchResult result, IEditorPart editor) {
093: ArrayList list = new ArrayList();
094: Object[] objects = result.getElements();
095: for (int i = 0; i < objects.length; i++) {
096: if (objects[i] instanceof IPluginObject) {
097: IPluginObject object = (IPluginObject) objects[i];
098: if (isMatchContained(editor, object)) {
099: Match[] matches = getMatches(object);
100: for (int j = 0; j < matches.length; j++) {
101: IDocument document = getDocument(editor,
102: matches[j]);
103: if (document != null)
104: list.add(ManifestEditorOpener
105: .findExactMatch(document,
106: matches[j], editor));
107: }
108: }
109: }
110: }
111: return (Match[]) list.toArray(new Match[list.size()]);
112: }
113:
114: /* (non-Javadoc)
115: * @see org.eclipse.search.ui.text.AbstractTextSearchResult#getFileMatchAdapter()
116: */
117: public IFileMatchAdapter getFileMatchAdapter() {
118: return null;
119: }
120:
121: protected boolean isMatchContained(IEditorPart editor,
122: IPluginObject object) {
123: IFile resource = (IFile) editor.getEditorInput().getAdapter(
124: IFile.class);
125: if (resource != null) {
126: IResource objectResource = object.getModel()
127: .getUnderlyingResource();
128: if (objectResource != null)
129: return resource.getProject().equals(
130: objectResource.getProject());
131: }
132: File file = (File) editor.getEditorInput().getAdapter(
133: File.class);
134: if (file != null) {
135: IPath path = new Path(object.getModel()
136: .getInstallLocation());
137: IPath filePath = null;
138: if ("MANIFEST.MF".equals(file.getName())) //$NON-NLS-1$
139: filePath = new Path(file.getParentFile().getParent());
140: else if (file.getName().endsWith("jar")) { //$NON-NLS-1$
141: filePath = new Path(file.getPath());
142: } else {
143: filePath = new Path(file.getParent());
144: }
145: return path.equals(filePath);
146: }
147: return false;
148: }
149:
150: protected IDocument getDocument(IEditorPart editor, Match match) {
151: IDocument document = null;
152: if (editor instanceof ISearchEditorAccess) {
153: document = ((ISearchEditorAccess) editor)
154: .getDocument(match);
155: } else if (editor instanceof ITextEditor) {
156: document = ((ITextEditor) editor).getDocumentProvider()
157: .getDocument(editor.getEditorInput());
158: }
159: return document;
160: }
161:
162: }
|