1 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 61 private CategoryFilterActionGroup fCategoryFilterActionGroup; 62 63 64 public MembersView() { 65 setHasWorkingSetFilter(false); 66 setHasCustomSetFilter(true); 67 JavaPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this); 68 } 69 70 73 public Object getAdapter(Class key) { 74 if (key == IShowInTargetList.class) { 75 return new IShowInTargetList() { 76 public String [] getShowInTargetIds() { 77 return new String [] { JavaUI.ID_PACKAGES }; 78 } 79 80 }; 81 } 82 return super.getAdapter(key); 83 } 84 85 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 103 protected String getHelpContextId() { 104 return IJavaHelpContextIds.MEMBERS_VIEW; 105 } 106 107 protected String getLinkToEditorKey() { 108 return PreferenceConstants.LINK_BROWSING_MEMBERS_TO_EDITOR; 109 } 110 111 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 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 141 protected void setInput(Object input) { 142 super.setInput(input); 143 if (fCategoryFilterActionGroup != null) 144 fCategoryFilterActionGroup.setInput(getCategoryFilterActionGroupInput()); 145 } 146 147 private IJavaElement[] getCategoryFilterActionGroupInput() { 148 Object input= getInput(); 149 if (input instanceof IJavaElement) 150 return new IJavaElement[] { (IJavaElement)input }; 151 return new IJavaElement[0]; 152 } 153 154 161 protected boolean isValidInput(Object 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 176 protected boolean isValidElement(Object 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 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 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 selectedElement= getSingleElementFromSelection(getViewer().getSelection()); 224 if (selectedElement instanceof IImportContainer) 225 return (IImportContainer)selectedElement; 226 } 227 } catch (JavaModelException ex) { 228 } 230 return je; 231 } 232 return null; 233 } 234 235 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 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 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 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 322 public void propertyChange(PropertyChangeEvent event) { 323 if (MembersOrderPreferenceCache.isMemberOrderProperty(event.getProperty())) { 324 getViewer().refresh(); 325 } 326 } 327 328 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 |