KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > search > dependencies > DependencyExtentSearchResultPage


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.search.dependencies;
12
13 import org.eclipse.jdt.core.IJavaElement;
14 import org.eclipse.jdt.core.IType;
15 import org.eclipse.jdt.core.JavaModelException;
16 import org.eclipse.jdt.ui.JavaElementLabelProvider;
17 import org.eclipse.jdt.ui.actions.FindReferencesInWorkingSetAction;
18 import org.eclipse.jdt.ui.actions.JavaSearchActionGroup;
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.action.IMenuManager;
21 import org.eclipse.jface.action.Separator;
22 import org.eclipse.jface.viewers.ILabelProvider;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.ViewerComparator;
25 import org.eclipse.pde.core.plugin.IPluginExtension;
26 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
27 import org.eclipse.pde.core.plugin.IPluginObject;
28 import org.eclipse.pde.internal.ui.PDEPlugin;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.internal.ui.search.AbstractSearchResultPage;
31 import org.eclipse.pde.internal.ui.search.ManifestEditorOpener;
32 import org.eclipse.pde.internal.ui.search.PluginSearchActionGroup;
33 import org.eclipse.search.ui.text.Match;
34 import org.eclipse.swt.graphics.Image;
35 import org.eclipse.ui.IWorkingSet;
36 import org.eclipse.ui.IWorkingSetManager;
37 import org.eclipse.ui.PartInitException;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.actions.ActionContext;
40
41
42 public class DependencyExtentSearchResultPage extends
43         AbstractSearchResultPage {
44     
45
46     class Comparator extends ViewerComparator {
47         /* (non-Javadoc)
48          * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
49          */

50         public int category(Object JavaDoc element) {
51             try {
52                 if (element instanceof IType) {
53                     if (((IType)element).isClass())
54                         return 1;
55                     return 0;
56                 }
57             } catch (JavaModelException e) {
58             }
59             return 2;
60         }
61     }
62     
63     class LabelProvider extends JavaElementLabelProvider {
64         /* (non-Javadoc)
65          * @see org.eclipse.jdt.ui.JavaElementLabelProvider#getImage(java.lang.Object)
66          */

67         public Image getImage(Object JavaDoc element) {
68             if (element instanceof IPluginObject)
69                 return PDEPlugin.getDefault().getLabelProvider().getImage(element);
70             return super.getImage(element);
71         }
72         
73         /* (non-Javadoc)
74          * @see org.eclipse.jdt.ui.JavaElementLabelProvider#getText(java.lang.Object)
75          */

76         public String JavaDoc getText(Object JavaDoc element) {
77             if (element instanceof IPluginExtensionPoint)
78                 return ((IPluginExtensionPoint) element).getFullId();
79             
80             if (element instanceof IPluginExtension)
81                 return ((IPluginExtension)element).getPoint();
82             
83             if (element instanceof IJavaElement) {
84                 IJavaElement javaElement = (IJavaElement) element;
85                 String JavaDoc text =
86                     super.getText(javaElement)
87                         + " - " //$NON-NLS-1$
88
+ javaElement
89                             .getAncestor(IJavaElement.PACKAGE_FRAGMENT)
90                             .getElementName();
91                 if (!(javaElement instanceof IType)) {
92                     IJavaElement ancestor = javaElement.getAncestor(IJavaElement.TYPE);
93                     if (ancestor == null)
94                         ancestor = javaElement.getAncestor(IJavaElement.CLASS_FILE);
95                     if (ancestor == null)
96                         ancestor = javaElement.getAncestor(IJavaElement.COMPILATION_UNIT);
97                     if (ancestor != null)
98                         text += "." + ancestor.getElementName(); //$NON-NLS-1$
99
}
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().getSelection()));
130         actionGroup.fillContextMenu(mgr);
131     }
132     
133     private void addJavaSearchGroup(IMenuManager mgr) {
134         IStructuredSelection ssel = (IStructuredSelection)getViewer().getSelection();
135         if (ssel.size() == 1) {
136             final Object JavaDoc object = ssel.getFirstElement();
137             if (object instanceof IType) {
138                 mgr.add(new Separator());
139                 mgr.add(new Action(PDEUIMessages.DependencyExtentSearchResultPage_referencesInPlugin) {
140                     public void run() {
141                         DependencyExtentQuery query = (DependencyExtentQuery)getInput().getQuery();
142                         IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
143                         IWorkingSet set = manager.createWorkingSet("temp", query.getDirectRoots()); //$NON-NLS-1$
144
new FindReferencesInWorkingSetAction(getViewPart().getSite(), new IWorkingSet[] {set}).run((IType)object);
145                         manager.removeWorkingSet(set);
146                     }
147                 });
148             }
149         }
150     }
151
152     /* (non-Javadoc)
153      * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#showMatch(org.eclipse.search.ui.text.Match, int, int, boolean)
154      */

155     protected void showMatch(Match match, int currentOffset, int currentLength,
156             boolean activate) throws PartInitException {
157         if (match.getElement() instanceof IPluginObject) {
158             ManifestEditorOpener.open(match, activate);
159         } else {
160             try {
161                 JavaEditorOpener.open(match, currentOffset, currentLength, activate);
162             } catch (PartInitException e) {
163             } catch (JavaModelException e) {
164             }
165         }
166     }
167
168     /* (non-Javadoc)
169      * @see org.eclipse.pde.internal.ui.search.AbstractSearchResultPage#createLabelProvider()
170      */

171     protected ILabelProvider createLabelProvider() {
172         return new LabelProvider();
173     }
174
175     /* (non-Javadoc)
176      * @see org.eclipse.pde.internal.ui.search.AbstractSearchResultPage#createViewerSorter()
177      */

178     protected ViewerComparator createViewerComparator() {
179         return new Comparator();
180     }
181
182 }
183
Popular Tags