KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
14
15 import org.eclipse.core.resources.ResourcesPlugin;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.Composite;
19
20 import org.eclipse.jface.viewers.DoubleClickEvent;
21 import org.eclipse.jface.viewers.IContentProvider;
22 import org.eclipse.jface.viewers.IDoubleClickListener;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.jface.viewers.StructuredViewer;
27 import org.eclipse.jface.viewers.TreeViewer;
28
29 import org.eclipse.ui.IPageLayout;
30 import org.eclipse.ui.IWorkbenchPart;
31 import org.eclipse.ui.part.IShowInTargetList;
32
33 import org.eclipse.jdt.core.IJavaElement;
34 import org.eclipse.jdt.core.IJavaModel;
35 import org.eclipse.jdt.core.IJavaProject;
36 import org.eclipse.jdt.core.IPackageFragmentRoot;
37 import org.eclipse.jdt.core.JavaCore;
38
39 import org.eclipse.jdt.ui.JavaUI;
40 import org.eclipse.jdt.ui.PreferenceConstants;
41 import org.eclipse.jdt.ui.actions.ProjectActionGroup;
42
43 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
44 import org.eclipse.jdt.internal.ui.JavaPlugin;
45 import org.eclipse.jdt.internal.ui.viewsupport.ColoredViewersManager;
46 import org.eclipse.jdt.internal.ui.viewsupport.FilterUpdater;
47 import org.eclipse.jdt.internal.ui.viewsupport.ProblemTreeViewer;
48
49 public class ProjectsView extends JavaBrowsingPart {
50
51     private FilterUpdater fFilterUpdater;
52
53     /* (non-Javadoc)
54      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#createViewer(org.eclipse.swt.widgets.Composite)
55      */

56     protected StructuredViewer createViewer(Composite parent) {
57         ProblemTreeViewer result= new ProblemTreeViewer(parent, SWT.MULTI);
58         ColoredViewersManager.install(result);
59         fFilterUpdater= new FilterUpdater(result);
60         ResourcesPlugin.getWorkspace().addResourceChangeListener(fFilterUpdater);
61         return result;
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#dispose()
66      */

67     public void dispose() {
68         if (fFilterUpdater != null)
69             ResourcesPlugin.getWorkspace().removeResourceChangeListener(fFilterUpdater);
70         super.dispose();
71     }
72
73     /* (non-Javadoc)
74      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#getAdapter(java.lang.Class)
75      */

76     public Object JavaDoc getAdapter(Class JavaDoc key) {
77         if (key == IShowInTargetList.class) {
78             return new IShowInTargetList() {
79                 public String JavaDoc[] getShowInTargetIds() {
80                     return new String JavaDoc[] { JavaUI.ID_PACKAGES, IPageLayout.ID_RES_NAV };
81                 }
82
83             };
84         }
85         return super.getAdapter(key);
86     }
87
88
89     /* (non-Javadoc)
90      * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#createContentProvider()
91      */

92     protected IContentProvider createContentProvider() {
93         return new ProjectAndSourceFolderContentProvider(this);
94     }
95
96     /**
97      * Returns the context ID for the Help system.
98      *
99      * @return the string used as ID for the Help context
100      */

101     protected String JavaDoc getHelpContextId() {
102         return IJavaHelpContextIds.PROJECTS_VIEW;
103     }
104
105     protected String JavaDoc getLinkToEditorKey() {
106         return PreferenceConstants.LINK_BROWSING_PROJECTS_TO_EDITOR;
107     }
108
109
110     /**
111      * Adds additional listeners to this view.
112      */

113     protected void hookViewerListeners() {
114         super.hookViewerListeners();
115         getViewer().addDoubleClickListener(new IDoubleClickListener() {
116             public void doubleClick(DoubleClickEvent event) {
117                 TreeViewer viewer= (TreeViewer)getViewer();
118                 Object JavaDoc element= ((IStructuredSelection)event.getSelection()).getFirstElement();
119                 if (viewer.isExpandable(element))
120                     viewer.setExpandedState(element, !viewer.getExpandedState(element));
121             }
122         });
123     }
124
125     protected void setInitialInput() {
126         IJavaElement root= JavaCore.create(JavaPlugin.getWorkspace().getRoot());
127         getViewer().setInput(root);
128         updateTitle();
129     }
130
131     /**
132      * Answers if the given <code>element</code> is a valid
133      * input for this part.
134      *
135      * @param element the object to test
136      * @return <true> if the given element is a valid input
137      */

138     protected boolean isValidInput(Object JavaDoc element) {
139         return element instanceof IJavaModel;
140     }
141
142     /**
143      * Answers if the given <code>element</code> is a valid
144      * element for this part.
145      *
146      * @param element the object to test
147      * @return <true> if the given element is a valid element
148      */

149     protected boolean isValidElement(Object JavaDoc element) {
150         return element instanceof IJavaProject || element instanceof IPackageFragmentRoot;
151     }
152
153     /**
154      * Finds the element which has to be selected in this part.
155      *
156      * @param je the Java element which has the focus
157      * @return the element to select
158      */

159     protected IJavaElement findElementToSelect(IJavaElement je) {
160         if (je == null)
161             return null;
162
163         switch (je.getElementType()) {
164             case IJavaElement.JAVA_MODEL :
165                 return null;
166             case IJavaElement.JAVA_PROJECT:
167                 return je;
168             case IJavaElement.PACKAGE_FRAGMENT_ROOT:
169                 if (je.getElementName().equals(IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH))
170                     return je.getParent();
171                 else
172                     return je;
173             default :
174                 return findElementToSelect(je.getParent());
175         }
176     }
177
178     /*
179      * @see JavaBrowsingPart#setInput(Object)
180      */

181     protected void setInput(Object JavaDoc input) {
182         // Don't allow to clear input for this view
183
if (input != null)
184             super.setInput(input);
185         else
186             getViewer().setSelection(null);
187     }
188
189     protected void createActions() {
190         super.createActions();
191         fActionGroups.addGroup(new ProjectActionGroup(this));
192     }
193
194     /**
195      * Handles selection of LogicalPackage in Packages view.
196      *
197      * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
198      * @since 2.1
199      */

200     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
201         if (!needsToProcessSelectionChanged(part, selection))
202             return;
203
204         if (selection instanceof IStructuredSelection) {
205             IStructuredSelection sel= (IStructuredSelection)selection;
206             Iterator JavaDoc iter= sel.iterator();
207             while (iter.hasNext()) {
208                 Object JavaDoc selectedElement= iter.next();
209                 if (selectedElement instanceof LogicalPackage) {
210                     selection= new StructuredSelection(((LogicalPackage)selectedElement).getJavaProject());
211                     break;
212                 }
213             }
214         }
215         super.selectionChanged(part, selection);
216     }
217 }
218
Popular Tags