001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 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.dependencies;
011:
012: import org.eclipse.jdt.core.IJavaElement;
013: import org.eclipse.jdt.core.IType;
014: import org.eclipse.jdt.core.JavaModelException;
015: import org.eclipse.jdt.ui.JavaElementLabelProvider;
016: import org.eclipse.jdt.ui.actions.FindReferencesInWorkingSetAction;
017: import org.eclipse.jdt.ui.actions.JavaSearchActionGroup;
018: import org.eclipse.jface.action.Action;
019: import org.eclipse.jface.action.IMenuManager;
020: import org.eclipse.jface.action.Separator;
021: import org.eclipse.jface.viewers.ILabelProvider;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.jface.viewers.ViewerComparator;
024: import org.eclipse.pde.core.plugin.IPluginExtension;
025: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
026: import org.eclipse.pde.core.plugin.IPluginObject;
027: import org.eclipse.pde.internal.ui.PDEPlugin;
028: import org.eclipse.pde.internal.ui.PDEUIMessages;
029: import org.eclipse.pde.internal.ui.search.AbstractSearchResultPage;
030: import org.eclipse.pde.internal.ui.search.ManifestEditorOpener;
031: import org.eclipse.pde.internal.ui.search.PluginSearchActionGroup;
032: import org.eclipse.search.ui.text.Match;
033: import org.eclipse.swt.graphics.Image;
034: import org.eclipse.ui.IWorkingSet;
035: import org.eclipse.ui.IWorkingSetManager;
036: import org.eclipse.ui.PartInitException;
037: import org.eclipse.ui.PlatformUI;
038: import org.eclipse.ui.actions.ActionContext;
039:
040: public class DependencyExtentSearchResultPage extends
041: AbstractSearchResultPage {
042:
043: class Comparator extends ViewerComparator {
044: /* (non-Javadoc)
045: * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
046: */
047: public int category(Object element) {
048: try {
049: if (element instanceof IType) {
050: if (((IType) element).isClass())
051: return 1;
052: return 0;
053: }
054: } catch (JavaModelException e) {
055: }
056: return 2;
057: }
058: }
059:
060: class LabelProvider extends JavaElementLabelProvider {
061: /* (non-Javadoc)
062: * @see org.eclipse.jdt.ui.JavaElementLabelProvider#getImage(java.lang.Object)
063: */
064: public Image getImage(Object element) {
065: if (element instanceof IPluginObject)
066: return PDEPlugin.getDefault().getLabelProvider()
067: .getImage(element);
068: return super .getImage(element);
069: }
070:
071: /* (non-Javadoc)
072: * @see org.eclipse.jdt.ui.JavaElementLabelProvider#getText(java.lang.Object)
073: */
074: public String getText(Object element) {
075: if (element instanceof IPluginExtensionPoint)
076: return ((IPluginExtensionPoint) element).getFullId();
077:
078: if (element instanceof IPluginExtension)
079: return ((IPluginExtension) element).getPoint();
080:
081: if (element instanceof IJavaElement) {
082: IJavaElement javaElement = (IJavaElement) element;
083: String text = super .getText(javaElement)
084: + " - " //$NON-NLS-1$
085: + javaElement.getAncestor(
086: IJavaElement.PACKAGE_FRAGMENT)
087: .getElementName();
088: if (!(javaElement instanceof IType)) {
089: IJavaElement ancestor = javaElement
090: .getAncestor(IJavaElement.TYPE);
091: if (ancestor == null)
092: ancestor = javaElement
093: .getAncestor(IJavaElement.CLASS_FILE);
094: if (ancestor == null)
095: ancestor = javaElement
096: .getAncestor(IJavaElement.COMPILATION_UNIT);
097: if (ancestor != null)
098: text += "." + ancestor.getElementName(); //$NON-NLS-1$
099: }
100: return text;
101: }
102: return super .getText(element);
103: }
104: }
105:
106: public DependencyExtentSearchResultPage() {
107: PDEPlugin.getDefault().getLabelProvider().connect(this );
108: }
109:
110: /* (non-Javadoc)
111: * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#dispose()
112: */
113: public void dispose() {
114: PDEPlugin.getDefault().getLabelProvider().disconnect(this );
115: super .dispose();
116: }
117:
118: /* (non-Javadoc)
119: * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#fillContextMenu(org.eclipse.jface.action.IMenuManager)
120: */
121: protected void fillContextMenu(IMenuManager mgr) {
122: super .fillContextMenu(mgr);
123: mgr.add(new Separator());
124: JavaSearchActionGroup group = new JavaSearchActionGroup(this );
125: group.setContext(new ActionContext(getViewer().getSelection()));
126: group.fillContextMenu(mgr);
127: addJavaSearchGroup(mgr);
128: PluginSearchActionGroup actionGroup = new PluginSearchActionGroup();
129: actionGroup.setContext(new ActionContext(getViewer()
130: .getSelection()));
131: actionGroup.fillContextMenu(mgr);
132: }
133:
134: private void addJavaSearchGroup(IMenuManager mgr) {
135: IStructuredSelection ssel = (IStructuredSelection) getViewer()
136: .getSelection();
137: if (ssel.size() == 1) {
138: final Object object = ssel.getFirstElement();
139: if (object instanceof IType) {
140: mgr.add(new Separator());
141: mgr
142: .add(new Action(
143: PDEUIMessages.DependencyExtentSearchResultPage_referencesInPlugin) {
144: public void run() {
145: DependencyExtentQuery query = (DependencyExtentQuery) getInput()
146: .getQuery();
147: IWorkingSetManager manager = PlatformUI
148: .getWorkbench()
149: .getWorkingSetManager();
150: IWorkingSet set = manager
151: .createWorkingSet(
152: "temp", query.getDirectRoots()); //$NON-NLS-1$
153: new FindReferencesInWorkingSetAction(
154: getViewPart().getSite(),
155: new IWorkingSet[] { set })
156: .run((IType) object);
157: manager.removeWorkingSet(set);
158: }
159: });
160: }
161: }
162: }
163:
164: /* (non-Javadoc)
165: * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#showMatch(org.eclipse.search.ui.text.Match, int, int, boolean)
166: */
167: protected void showMatch(Match match, int currentOffset,
168: int currentLength, boolean activate)
169: throws PartInitException {
170: if (match.getElement() instanceof IPluginObject) {
171: ManifestEditorOpener.open(match, activate);
172: } else {
173: try {
174: JavaEditorOpener.open(match, currentOffset,
175: currentLength, activate);
176: } catch (PartInitException e) {
177: } catch (JavaModelException e) {
178: }
179: }
180: }
181:
182: /* (non-Javadoc)
183: * @see org.eclipse.pde.internal.ui.search.AbstractSearchResultPage#createLabelProvider()
184: */
185: protected ILabelProvider createLabelProvider() {
186: return new LabelProvider();
187: }
188:
189: /* (non-Javadoc)
190: * @see org.eclipse.pde.internal.ui.search.AbstractSearchResultPage#createViewerSorter()
191: */
192: protected ViewerComparator createViewerComparator() {
193: return new Comparator();
194: }
195:
196: }
|