| /******************************************************************************* |
| * Copyright (c) 2000, 2005 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.jdt.internal.ui.browsing; |
| |
| import java.util.Arrays; |
| import java.util.List; |
| |
| import org.eclipse.jface.util.Assert; |
| |
| import org.eclipse.jface.viewers.DecoratingLabelProvider; |
| import org.eclipse.jface.viewers.ISelection; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.jface.viewers.StructuredSelection; |
| import org.eclipse.jface.viewers.TableViewer; |
| |
| import org.eclipse.ui.IActionBars; |
| import org.eclipse.ui.IPageLayout; |
| import org.eclipse.ui.IWorkbenchActionConstants; |
| import org.eclipse.ui.IWorkbenchPart; |
| import org.eclipse.ui.part.IShowInTargetList; |
| |
| import org.eclipse.jdt.core.IClassFile; |
| import org.eclipse.jdt.core.ICompilationUnit; |
| import org.eclipse.jdt.core.IJavaElement; |
| import org.eclipse.jdt.core.IMember; |
| import org.eclipse.jdt.core.IPackageFragment; |
| import org.eclipse.jdt.core.IType; |
| import org.eclipse.jdt.core.JavaModelException; |
| |
| import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; |
| import org.eclipse.jdt.internal.ui.actions.SelectAllAction; |
| import org.eclipse.jdt.internal.ui.filters.NonJavaElementFilter; |
| import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider; |
| import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider; |
| import org.eclipse.jdt.ui.JavaUI; |
| import org.eclipse.jdt.ui.PreferenceConstants; |
| public class TypesView extends JavaBrowsingPart { |
| |
| private SelectAllAction fSelectAllAction; |
| |
| /** |
| * Creates and returns the label provider for this part. |
| * |
| * @return the label provider |
| * @see org.eclipse.jface.viewers.ILabelProvider |
| */ |
| protected JavaUILabelProvider createLabelProvider() { |
| return new AppearanceAwareLabelProvider( |
| AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS, |
| AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS); |
| } |
| |
| /** |
| * Answer the property defined by key. |
| */ |
| public Object getAdapter(Class key) { |
| if (key == IShowInTargetList.class) { |
| return new IShowInTargetList() { |
| public String[] getShowInTargetIds() { |
| return new String[] { JavaUI.ID_PACKAGES, IPageLayout.ID_RES_NAV }; |
| } |
| |
| }; |
| } |
| return super.getAdapter(key); |
| } |
| |
| /** |
| * Adds filters the viewer of this part. |
| */ |
| protected void addFilters() { |
| super.addFilters(); |
| getViewer().addFilter(new NonJavaElementFilter()); |
| } |
| |
| /** |
| * Answers if the given <code>element</code> is a valid |
| * input for this part. |
| * |
| * @param element the object to test |
| * @return <true> if the given element is a valid input |
| */ |
| protected boolean isValidInput(Object element) { |
| return element instanceof IPackageFragment; |
| } |
| |
| /** |
| * Answers if the given <code>element</code> is a valid |
| * element for this part. |
| * |
| * @param element the object to test |
| * @return <true> if the given element is a valid element |
| */ |
| protected boolean isValidElement(Object element) { |
| if (element instanceof ICompilationUnit) |
| return super.isValidElement(((ICompilationUnit)element).getParent()); |
| else if (element instanceof IType) { |
| IType type= (IType)element; |
| return type.getDeclaringType() == null && isValidElement(type.getCompilationUnit()); |
| } |
| return false; |
| } |
| |
| /** |
| * Finds the element which has to be selected in this part. |
| * |
| * @param je the Java element which has the focus |
| */ |
| protected IJavaElement findElementToSelect(IJavaElement je) { |
| if (je == null) |
| return null; |
| |
| switch (je.getElementType()) { |
| case IJavaElement.TYPE: |
| IType type= ((IType)je).getDeclaringType(); |
| if (type == null) |
| type= (IType)je; |
| return getSuitableJavaElement(type); |
| case IJavaElement.COMPILATION_UNIT: |
| return getTypeForCU((ICompilationUnit)je); |
| case IJavaElement.CLASS_FILE: |
| try { |
| return findElementToSelect(((IClassFile)je).getType()); |
| } catch (JavaModelException ex) { |
| return null; |
| } |
| case IJavaElement.IMPORT_CONTAINER: |
| case IJavaElement.IMPORT_DECLARATION: |
| case IJavaElement.PACKAGE_DECLARATION: |
| return findElementToSelect(je.getParent()); |
| default: |
| if (je instanceof IMember) |
| return findElementToSelect(((IMember)je).getDeclaringType()); |
| return null; |
| |
| } |
| } |
| |
| /** |
| * Returns the context ID for the Help system |
| * |
| * @return the string used as ID for the Help context |
| */ |
| protected String getHelpContextId() { |
| return IJavaHelpContextIds.TYPES_VIEW; |
| } |
| |
| protected String getLinkToEditorKey() { |
| return PreferenceConstants.LINK_BROWSING_TYPES_TO_EDITOR; |
| } |
| |
| protected void createActions() { |
| super.createActions(); |
| fSelectAllAction= new SelectAllAction((TableViewer)getViewer()); |
| } |
| |
| protected void fillActionBars(IActionBars actionBars) { |
| super.fillActionBars(actionBars); |
| |
| // Add selectAll action handlers. |
| actionBars.setGlobalActionHandler(IWorkbenchActionConstants.SELECT_ALL, fSelectAllAction); |
| } |
| |
| /** |
| * Handles selection of LogicalPackage in Packages view. |
| * |
| * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection) |
| * @since 2.1 |
| */ |
| public void selectionChanged(IWorkbenchPart part, ISelection selection) { |
| if (!needsToProcessSelectionChanged(part, selection)) |
| return; |
| |
| if (selection instanceof IStructuredSelection) { |
| IStructuredSelection sel= (IStructuredSelection) selection; |
| Object selectedElement= sel.getFirstElement(); |
| if (sel.size() == 1 && (selectedElement instanceof LogicalPackage)) { |
| IPackageFragment[] fragments= ((LogicalPackage)selectedElement).getFragments(); |
| List selectedElements= Arrays.asList(fragments); |
| if (selectedElements.size() > 1) { |
| adjustInput(part, selectedElements); |
| fPreviousSelectedElement= selectedElements; |
| fPreviousSelectionProvider= part; |
| } else if (selectedElements.size() == 1) |
| super.selectionChanged(part, new StructuredSelection(selectedElements.get(0))); |
| else |
| Assert.isLegal(false); |
| return; |
| } |
| } |
| super.selectionChanged(part, selection); |
| } |
| |
| private void adjustInput(IWorkbenchPart part, List selectedElements) { |
| Object currentInput= getViewer().getInput(); |
| if (!selectedElements.equals(currentInput)) |
| setInput(selectedElements); |
| } |
| /* |
| * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#createDecoratingLabelProvider(org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider) |
| */ |
| protected DecoratingLabelProvider createDecoratingLabelProvider(JavaUILabelProvider provider) { |
| DecoratingLabelProvider decoratingLabelProvider= super.createDecoratingLabelProvider(provider); |
| provider.addLabelDecorator(new TopLevelTypeProblemsLabelDecorator(null)); |
| return decoratingLabelProvider; |
| } |
| |
| } |