KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > browsing > MembersView


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.browsing;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.widgets.Composite;
15
16 import org.eclipse.jface.action.IToolBarManager;
17 import org.eclipse.jface.util.IPropertyChangeListener;
18 import org.eclipse.jface.util.PropertyChangeEvent;
19 import org.eclipse.jface.viewers.DoubleClickEvent;
20 import org.eclipse.jface.viewers.IDoubleClickListener;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredViewer;
23 import org.eclipse.jface.viewers.TreeViewer;
24
25 import org.eclipse.ui.IActionBars;
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.IMemento;
28 import org.eclipse.ui.part.IShowInTargetList;
29
30 import org.eclipse.jdt.core.IClassFile;
31 import org.eclipse.jdt.core.ICompilationUnit;
32 import org.eclipse.jdt.core.IImportContainer;
33 import org.eclipse.jdt.core.IImportDeclaration;
34 import org.eclipse.jdt.core.IJavaElement;
35 import org.eclipse.jdt.core.IMember;
36 import org.eclipse.jdt.core.IType;
37 import org.eclipse.jdt.core.JavaModelException;
38
39 import org.eclipse.jdt.ui.JavaElementLabels;
40 import org.eclipse.jdt.ui.JavaUI;
41 import org.eclipse.jdt.ui.PreferenceConstants;
42 import org.eclipse.jdt.ui.actions.MemberFilterActionGroup;
43
44 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
45 import org.eclipse.jdt.internal.ui.JavaPlugin;
46 import org.eclipse.jdt.internal.ui.actions.CategoryFilterActionGroup;
47 import org.eclipse.jdt.internal.ui.actions.LexicalSortingAction;
48 import org.eclipse.jdt.internal.ui.preferences.MembersOrderPreferenceCache;
49 import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
50 import org.eclipse.jdt.internal.ui.viewsupport.ColoredViewersManager;
51 import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider;
52 import org.eclipse.jdt.internal.ui.viewsupport.ProblemTreeViewer;
53
54 public class MembersView extends JavaBrowsingPart implements IPropertyChangeListener {
55
56     private MemberFilterActionGroup fMemberFilterActionGroup;
57     /**
58      * Category filter action group.
59      * @since 3.2
60      */

61     private CategoryFilterActionGroup fCategoryFilterActionGroup;
62
63
64     public MembersView() {
65         setHasWorkingSetFilter(false);
66         setHasCustomSetFilter(true);
67         JavaPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);
68     }
69
70     /* (non-Javadoc)
71      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#getAdapter(java.lang.Class)
72      */

73     public Object JavaDoc getAdapter(Class JavaDoc key) {
74         if (key == IShowInTargetList.class) {
75             return new IShowInTargetList() {
76                 public String JavaDoc[] getShowInTargetIds() {
77                     return new String JavaDoc[] { JavaUI.ID_PACKAGES };
78                 }
79
80             };
81         }
82         return super.getAdapter(key);
83     }
84
85     /**
86      * Creates and returns the label provider for this part.
87      *
88      * @return the label provider
89      * @see org.eclipse.jface.viewers.ILabelProvider
90      */

91     protected JavaUILabelProvider createLabelProvider() {
92         return new AppearanceAwareLabelProvider(
93                         AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.F_APP_TYPE_SIGNATURE | JavaElementLabels.ALL_CATEGORY,
94                         AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS
95                         );
96     }
97
98     /**
99      * Returns the context ID for the Help system
100      *
101      * @return the string used as ID for the Help context
102      */

103     protected String JavaDoc getHelpContextId() {
104         return IJavaHelpContextIds.MEMBERS_VIEW;
105     }
106
107     protected String JavaDoc getLinkToEditorKey() {
108         return PreferenceConstants.LINK_BROWSING_MEMBERS_TO_EDITOR;
109     }
110
111     /* (non-Javadoc)
112      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#createViewer(org.eclipse.swt.widgets.Composite)
113      */

114     protected StructuredViewer createViewer(Composite parent) {
115         ProblemTreeViewer viewer= new ProblemTreeViewer(parent, SWT.MULTI);
116         ColoredViewersManager.install(viewer);
117         fMemberFilterActionGroup= new MemberFilterActionGroup(viewer, JavaUI.ID_MEMBERS_VIEW);
118         return viewer;
119     }
120
121     protected void fillToolBar(IToolBarManager tbm) {
122         tbm.add(new LexicalSortingAction(getViewer(), JavaUI.ID_MEMBERS_VIEW));
123         fMemberFilterActionGroup.contributeToToolBar(tbm);
124         super.fillToolBar(tbm);
125     }
126
127     /*
128      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#fillActionBars(org.eclipse.ui.IActionBars)
129      * @since 3.2
130      */

131     protected void fillActionBars(IActionBars actionBars) {
132         super.fillActionBars(actionBars);
133         fCategoryFilterActionGroup= new CategoryFilterActionGroup(getViewer(), getViewSite().getId(), getCategoryFilterActionGroupInput());
134         fCategoryFilterActionGroup.contributeToViewMenu(actionBars.getMenuManager());
135     }
136
137     /*
138      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#setInput(java.lang.Object)
139      * @since 3.2
140      */

141     protected void setInput(Object JavaDoc input) {
142         super.setInput(input);
143         if (fCategoryFilterActionGroup != null)
144             fCategoryFilterActionGroup.setInput(getCategoryFilterActionGroupInput());
145     }
146     
147     private IJavaElement[] getCategoryFilterActionGroupInput() {
148         Object JavaDoc input= getInput();
149         if (input instanceof IJavaElement)
150             return new IJavaElement[] { (IJavaElement)input };
151         return new IJavaElement[0];
152     }
153
154     /**
155      * Answers if the given <code>element</code> is a valid
156      * input for this part.
157      *
158      * @param element the object to test
159      * @return <true> if the given element is a valid input
160      */

161     protected boolean isValidInput(Object JavaDoc element) {
162         if (element instanceof IType) {
163             IType type= (IType)element;
164             return type.isBinary() || type.getDeclaringType() == null;
165         }
166         return false;
167     }
168
169     /**
170      * Answers if the given <code>element</code> is a valid
171      * element for this part.
172      *
173      * @param element the object to test
174      * @return <true> if the given element is a valid element
175      */

176     protected boolean isValidElement(Object JavaDoc element) {
177         if (element instanceof IMember)
178             return super.isValidElement(((IMember)element).getDeclaringType());
179         else if (element instanceof IImportDeclaration)
180             return isValidElement(((IJavaElement)element).getParent());
181         else if (element instanceof IImportContainer) {
182             Object JavaDoc input= getViewer().getInput();
183             if (input instanceof IJavaElement) {
184                 ICompilationUnit cu= (ICompilationUnit)((IJavaElement)input).getAncestor(IJavaElement.COMPILATION_UNIT);
185                 if (cu != null) {
186                     ICompilationUnit importContainerCu= (ICompilationUnit)((IJavaElement)element).getAncestor(IJavaElement.COMPILATION_UNIT);
187                     return cu.equals(importContainerCu);
188                 } else {
189                     IClassFile cf= (IClassFile)((IJavaElement)input).getAncestor(IJavaElement.CLASS_FILE);
190                     IClassFile importContainerCf= (IClassFile)((IJavaElement)element).getAncestor(IJavaElement.CLASS_FILE);
191                     return cf != null && cf.equals(importContainerCf);
192                 }
193             }
194         }
195         return false;
196     }
197
198     /**
199      * Finds the element which has to be selected in this part.
200      *
201      * @param je the Java element which has the focus
202      * @return the element to select
203      */

204     protected IJavaElement findElementToSelect(IJavaElement je) {
205         if (je == null)
206             return null;
207
208         switch (je.getElementType()) {
209             case IJavaElement.TYPE:
210                 if (((IType)je).getDeclaringType() == null)
211                     return null;
212                 return je;
213             case IJavaElement.METHOD:
214             case IJavaElement.INITIALIZER:
215             case IJavaElement.FIELD:
216             case IJavaElement.PACKAGE_DECLARATION:
217             case IJavaElement.IMPORT_CONTAINER:
218                 return je;
219             case IJavaElement.IMPORT_DECLARATION:
220                 ICompilationUnit cu= (ICompilationUnit)je.getParent().getParent();
221                 try {
222                     if (cu.getImports()[0].equals(je)) {
223                         Object JavaDoc selectedElement= getSingleElementFromSelection(getViewer().getSelection());
224                         if (selectedElement instanceof IImportContainer)
225                             return (IImportContainer)selectedElement;
226                     }
227                 } catch (JavaModelException ex) {
228                     // return je;
229
}
230                 return je;
231         }
232         return null;
233     }
234
235     /**
236      * Finds the closest Java element which can be used as input for
237      * this part and has the given Java element as child.
238      *
239      * @param je the Java element for which to search the closest input
240      * @return the closest Java element used as input for this part, or <code>null</code>
241      */

242     protected IJavaElement findInputForJavaElement(IJavaElement je) {
243         if (je == null || !je.exists() || (je.getJavaProject() != null && !je.getJavaProject().isOnClasspath(je)))
244             return null;
245
246         switch (je.getElementType()) {
247             case IJavaElement.TYPE:
248                 IType type= ((IType)je).getDeclaringType();
249                 if (type == null)
250                     return je;
251                 else
252                     return findInputForJavaElement(type);
253             case IJavaElement.COMPILATION_UNIT:
254                 return getTypeForCU((ICompilationUnit)je);
255             case IJavaElement.CLASS_FILE:
256                 return findInputForJavaElement(((IClassFile)je).getType());
257             case IJavaElement.IMPORT_DECLARATION:
258                 return findInputForJavaElement(je.getParent());
259             case IJavaElement.PACKAGE_DECLARATION:
260             case IJavaElement.IMPORT_CONTAINER:
261                 IJavaElement parent= je.getParent();
262                 if (parent instanceof ICompilationUnit) {
263                     return getTypeForCU((ICompilationUnit)parent);
264                 }
265                 else if (parent instanceof IClassFile)
266                     return findInputForJavaElement(parent);
267                 return null;
268             default:
269                 if (je instanceof IMember)
270                     return findInputForJavaElement(((IMember)je).getDeclaringType());
271         }
272         return null;
273     }
274
275     /*
276      * Implements method from IViewPart.
277      */

278     public void saveState(IMemento memento) {
279         super.saveState(memento);
280         fMemberFilterActionGroup.saveState(memento);
281     }
282
283     protected void restoreState(IMemento memento) {
284         super.restoreState(memento);
285         fMemberFilterActionGroup.restoreState(memento);
286         getViewer().getControl().setRedraw(false);
287         getViewer().refresh();
288         getViewer().getControl().setRedraw(true);
289     }
290
291     protected void hookViewerListeners() {
292         super.hookViewerListeners();
293         getViewer().addDoubleClickListener(new IDoubleClickListener() {
294             public void doubleClick(DoubleClickEvent event) {
295                 TreeViewer viewer= (TreeViewer)getViewer();
296                 Object JavaDoc element= ((IStructuredSelection)event.getSelection()).getFirstElement();
297                 if (viewer.isExpandable(element))
298                     viewer.setExpandedState(element, !viewer.getExpandedState(element));
299             }
300         });
301     }
302
303     boolean isInputAWorkingCopy() {
304         Object JavaDoc input= getViewer().getInput();
305         if (input instanceof IJavaElement) {
306             ICompilationUnit cu= (ICompilationUnit)((IJavaElement)input).getAncestor(IJavaElement.COMPILATION_UNIT);
307             if (cu != null)
308                 return cu.isWorkingCopy();
309         }
310         return false;
311     }
312
313     protected void restoreSelection() {
314         IEditorPart editor= getViewSite().getPage().getActiveEditor();
315         if (editor != null)
316             setSelectionFromEditor(editor);
317     }
318
319     /* (non-Javadoc)
320      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
321      */

322     public void propertyChange(PropertyChangeEvent event) {
323         if (MembersOrderPreferenceCache.isMemberOrderProperty(event.getProperty())) {
324             getViewer().refresh();
325         }
326     }
327
328     /*
329      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#dispose()
330      */

331     public void dispose() {
332         if (fMemberFilterActionGroup != null) {
333             fMemberFilterActionGroup.dispose();
334             fMemberFilterActionGroup= null;
335         }
336         if (fCategoryFilterActionGroup != null) {
337             fCategoryFilterActionGroup.dispose();
338             fCategoryFilterActionGroup= null;
339         }
340         super.dispose();
341         JavaPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this);
342     }
343 }
344
Popular Tags