KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Arrays JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.swt.widgets.Composite;
19
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.jface.viewers.StructuredViewer;
24 import org.eclipse.jface.viewers.TableViewer;
25
26 import org.eclipse.ui.IActionBars;
27 import org.eclipse.ui.IPageLayout;
28 import org.eclipse.ui.IWorkbenchPart;
29 import org.eclipse.ui.actions.ActionFactory;
30 import org.eclipse.ui.part.IShowInTargetList;
31
32 import org.eclipse.jdt.core.IClassFile;
33 import org.eclipse.jdt.core.ICompilationUnit;
34 import org.eclipse.jdt.core.IJavaElement;
35 import org.eclipse.jdt.core.IMember;
36 import org.eclipse.jdt.core.IPackageFragment;
37 import org.eclipse.jdt.core.IType;
38
39 import org.eclipse.jdt.ui.JavaElementLabels;
40 import org.eclipse.jdt.ui.JavaUI;
41 import org.eclipse.jdt.ui.PreferenceConstants;
42
43 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
44 import org.eclipse.jdt.internal.ui.actions.SelectAllAction;
45 import org.eclipse.jdt.internal.ui.filters.NonJavaElementFilter;
46 import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
47 import org.eclipse.jdt.internal.ui.viewsupport.ColoredViewersManager;
48 import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
49 import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider;
50
51 public class TypesView extends JavaBrowsingPart {
52
53     private SelectAllAction fSelectAllAction;
54
55     /**
56      * Creates and returns the label provider for this part.
57      *
58      * @return the label provider
59      * @see org.eclipse.jface.viewers.ILabelProvider
60      */

61     protected JavaUILabelProvider createLabelProvider() {
62         return new AppearanceAwareLabelProvider(
63                         AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.T_CATEGORY,
64                         AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS);
65     }
66
67     protected StructuredViewer createViewer(Composite parent) {
68         StructuredViewer viewer= super.createViewer(parent);
69         ColoredViewersManager.install(viewer);
70         return viewer;
71     }
72     
73     
74     /* (non-Javadoc)
75      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#getAdapter(java.lang.Class)
76      */

77     public Object JavaDoc getAdapter(Class JavaDoc key) {
78         if (key == IShowInTargetList.class) {
79             return new IShowInTargetList() {
80                 public String JavaDoc[] getShowInTargetIds() {
81                     return new String JavaDoc[] { JavaUI.ID_PACKAGES, IPageLayout.ID_RES_NAV };
82                 }
83
84             };
85         }
86         return super.getAdapter(key);
87     }
88
89     /**
90      * Adds filters the viewer of this part.
91      */

92     protected void addFilters() {
93         super.addFilters();
94         getViewer().addFilter(new NonJavaElementFilter());
95     }
96
97     /**
98      * Answers if the given <code>element</code> is a valid
99      * input for this part.
100      *
101      * @param element the object to test
102      * @return <true> if the given element is a valid input
103      */

104     protected boolean isValidInput(Object JavaDoc element) {
105         return element instanceof IPackageFragment;
106     }
107
108     /**
109      * Answers if the given <code>element</code> is a valid
110      * element for this part.
111      *
112      * @param element the object to test
113      * @return <true> if the given element is a valid element
114      */

115     protected boolean isValidElement(Object JavaDoc element) {
116         if (element instanceof ICompilationUnit)
117             return super.isValidElement(((ICompilationUnit)element).getParent());
118         else if (element instanceof IType) {
119             IType type= (IType)element;
120             return type.getDeclaringType() == null && isValidElement(type.getCompilationUnit());
121         }
122         return false;
123     }
124
125     /**
126      * Finds the element which has to be selected in this part.
127      *
128      * @param je the Java element which has the focus
129      * @return the element to select
130      */

131     protected IJavaElement findElementToSelect(IJavaElement je) {
132         if (je == null)
133             return null;
134
135         switch (je.getElementType()) {
136             case IJavaElement.TYPE:
137                 IType type= ((IType)je).getDeclaringType();
138                 if (type == null)
139                     type= (IType)je;
140                 return type;
141             case IJavaElement.COMPILATION_UNIT:
142                 return getTypeForCU((ICompilationUnit)je);
143             case IJavaElement.CLASS_FILE:
144                 return findElementToSelect(((IClassFile)je).getType());
145             case IJavaElement.IMPORT_CONTAINER:
146             case IJavaElement.IMPORT_DECLARATION:
147             case IJavaElement.PACKAGE_DECLARATION:
148                 return findElementToSelect(je.getParent());
149             default:
150                 if (je instanceof IMember)
151                     return findElementToSelect(((IMember)je).getDeclaringType());
152                 return null;
153
154         }
155     }
156
157     /**
158      * Returns the context ID for the Help system
159      *
160      * @return the string used as ID for the Help context
161      */

162     protected String JavaDoc getHelpContextId() {
163         return IJavaHelpContextIds.TYPES_VIEW;
164     }
165
166     protected String JavaDoc getLinkToEditorKey() {
167         return PreferenceConstants.LINK_BROWSING_TYPES_TO_EDITOR;
168     }
169
170     protected void createActions() {
171         super.createActions();
172         fSelectAllAction= new SelectAllAction((TableViewer)getViewer());
173     }
174
175     protected void fillActionBars(IActionBars actionBars) {
176         super.fillActionBars(actionBars);
177
178         // Add selectAll action handlers.
179
actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), fSelectAllAction);
180     }
181
182     /**
183      * Handles selection of LogicalPackage in Packages view.
184      *
185      * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
186      * @since 2.1
187      */

188     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
189         if (!needsToProcessSelectionChanged(part, selection))
190             return;
191
192         if (selection instanceof IStructuredSelection) {
193             IStructuredSelection sel= (IStructuredSelection) selection;
194             Object JavaDoc selectedElement= sel.getFirstElement();
195             if (sel.size() == 1 && (selectedElement instanceof LogicalPackage)) {
196                 IPackageFragment[] fragments= ((LogicalPackage)selectedElement).getFragments();
197                 List JavaDoc selectedElements= Arrays.asList(fragments);
198                 if (selectedElements.size() > 1) {
199                     adjustInput(part, selectedElements);
200                     fPreviousSelectedElement= selectedElements;
201                     fPreviousSelectionProvider= part;
202                 } else if (selectedElements.size() == 1)
203                     super.selectionChanged(part, new StructuredSelection(selectedElements.get(0)));
204                 else
205                     Assert.isLegal(false);
206                 return;
207             }
208         }
209         super.selectionChanged(part, selection);
210     }
211
212     private void adjustInput(IWorkbenchPart part, List JavaDoc selectedElements) {
213         Object JavaDoc currentInput= getViewer().getInput();
214         if (!selectedElements.equals(currentInput))
215             setInput(selectedElements);
216     }
217     /*
218      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#createDecoratingLabelProvider(org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider)
219      */

220     protected DecoratingJavaLabelProvider createDecoratingLabelProvider(JavaUILabelProvider provider) {
221         DecoratingJavaLabelProvider decoratingLabelProvider= super.createDecoratingLabelProvider(provider);
222         provider.addLabelDecorator(new TopLevelTypeProblemsLabelDecorator(null));
223         return decoratingLabelProvider;
224     }
225
226 }
227
Popular Tags