001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.browsing;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.core.resources.ResourcesPlugin;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.widgets.Composite;
018:
019: import org.eclipse.jface.viewers.DoubleClickEvent;
020: import org.eclipse.jface.viewers.IContentProvider;
021: import org.eclipse.jface.viewers.IDoubleClickListener;
022: import org.eclipse.jface.viewers.ISelection;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.jface.viewers.StructuredSelection;
025: import org.eclipse.jface.viewers.StructuredViewer;
026: import org.eclipse.jface.viewers.TreeViewer;
027:
028: import org.eclipse.ui.IPageLayout;
029: import org.eclipse.ui.IWorkbenchPart;
030: import org.eclipse.ui.part.IShowInTargetList;
031:
032: import org.eclipse.jdt.core.IJavaElement;
033: import org.eclipse.jdt.core.IJavaModel;
034: import org.eclipse.jdt.core.IJavaProject;
035: import org.eclipse.jdt.core.IPackageFragmentRoot;
036: import org.eclipse.jdt.core.JavaCore;
037:
038: import org.eclipse.jdt.ui.JavaUI;
039: import org.eclipse.jdt.ui.PreferenceConstants;
040: import org.eclipse.jdt.ui.actions.ProjectActionGroup;
041:
042: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
043: import org.eclipse.jdt.internal.ui.JavaPlugin;
044: import org.eclipse.jdt.internal.ui.viewsupport.ColoredViewersManager;
045: import org.eclipse.jdt.internal.ui.viewsupport.FilterUpdater;
046: import org.eclipse.jdt.internal.ui.viewsupport.ProblemTreeViewer;
047:
048: public class ProjectsView extends JavaBrowsingPart {
049:
050: private FilterUpdater fFilterUpdater;
051:
052: /* (non-Javadoc)
053: * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#createViewer(org.eclipse.swt.widgets.Composite)
054: */
055: protected StructuredViewer createViewer(Composite parent) {
056: ProblemTreeViewer result = new ProblemTreeViewer(parent,
057: SWT.MULTI);
058: ColoredViewersManager.install(result);
059: fFilterUpdater = new FilterUpdater(result);
060: ResourcesPlugin.getWorkspace().addResourceChangeListener(
061: fFilterUpdater);
062: return result;
063: }
064:
065: /* (non-Javadoc)
066: * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#dispose()
067: */
068: public void dispose() {
069: if (fFilterUpdater != null)
070: ResourcesPlugin.getWorkspace()
071: .removeResourceChangeListener(fFilterUpdater);
072: super .dispose();
073: }
074:
075: /* (non-Javadoc)
076: * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#getAdapter(java.lang.Class)
077: */
078: public Object getAdapter(Class key) {
079: if (key == IShowInTargetList.class) {
080: return new IShowInTargetList() {
081: public String[] getShowInTargetIds() {
082: return new String[] { JavaUI.ID_PACKAGES,
083: IPageLayout.ID_RES_NAV };
084: }
085:
086: };
087: }
088: return super .getAdapter(key);
089: }
090:
091: /* (non-Javadoc)
092: * @see org.eclipse.jdt.internal.ui.browsing.JavaBrowsingPart#createContentProvider()
093: */
094: protected IContentProvider createContentProvider() {
095: return new ProjectAndSourceFolderContentProvider(this );
096: }
097:
098: /**
099: * Returns the context ID for the Help system.
100: *
101: * @return the string used as ID for the Help context
102: */
103: protected String getHelpContextId() {
104: return IJavaHelpContextIds.PROJECTS_VIEW;
105: }
106:
107: protected String getLinkToEditorKey() {
108: return PreferenceConstants.LINK_BROWSING_PROJECTS_TO_EDITOR;
109: }
110:
111: /**
112: * Adds additional listeners to this view.
113: */
114: protected void hookViewerListeners() {
115: super .hookViewerListeners();
116: getViewer().addDoubleClickListener(new IDoubleClickListener() {
117: public void doubleClick(DoubleClickEvent event) {
118: TreeViewer viewer = (TreeViewer) getViewer();
119: Object element = ((IStructuredSelection) event
120: .getSelection()).getFirstElement();
121: if (viewer.isExpandable(element))
122: viewer.setExpandedState(element, !viewer
123: .getExpandedState(element));
124: }
125: });
126: }
127:
128: protected void setInitialInput() {
129: IJavaElement root = JavaCore.create(JavaPlugin.getWorkspace()
130: .getRoot());
131: getViewer().setInput(root);
132: updateTitle();
133: }
134:
135: /**
136: * Answers if the given <code>element</code> is a valid
137: * input for this part.
138: *
139: * @param element the object to test
140: * @return <true> if the given element is a valid input
141: */
142: protected boolean isValidInput(Object element) {
143: return element instanceof IJavaModel;
144: }
145:
146: /**
147: * Answers if the given <code>element</code> is a valid
148: * element for this part.
149: *
150: * @param element the object to test
151: * @return <true> if the given element is a valid element
152: */
153: protected boolean isValidElement(Object element) {
154: return element instanceof IJavaProject
155: || element instanceof IPackageFragmentRoot;
156: }
157:
158: /**
159: * Finds the element which has to be selected in this part.
160: *
161: * @param je the Java element which has the focus
162: * @return the element to select
163: */
164: protected IJavaElement findElementToSelect(IJavaElement je) {
165: if (je == null)
166: return null;
167:
168: switch (je.getElementType()) {
169: case IJavaElement.JAVA_MODEL:
170: return null;
171: case IJavaElement.JAVA_PROJECT:
172: return je;
173: case IJavaElement.PACKAGE_FRAGMENT_ROOT:
174: if (je.getElementName().equals(
175: IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH))
176: return je.getParent();
177: else
178: return je;
179: default:
180: return findElementToSelect(je.getParent());
181: }
182: }
183:
184: /*
185: * @see JavaBrowsingPart#setInput(Object)
186: */
187: protected void setInput(Object input) {
188: // Don't allow to clear input for this view
189: if (input != null)
190: super .setInput(input);
191: else
192: getViewer().setSelection(null);
193: }
194:
195: protected void createActions() {
196: super .createActions();
197: fActionGroups.addGroup(new ProjectActionGroup(this ));
198: }
199:
200: /**
201: * Handles selection of LogicalPackage in Packages view.
202: *
203: * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
204: * @since 2.1
205: */
206: public void selectionChanged(IWorkbenchPart part,
207: ISelection selection) {
208: if (!needsToProcessSelectionChanged(part, selection))
209: return;
210:
211: if (selection instanceof IStructuredSelection) {
212: IStructuredSelection sel = (IStructuredSelection) selection;
213: Iterator iter = sel.iterator();
214: while (iter.hasNext()) {
215: Object selectedElement = iter.next();
216: if (selectedElement instanceof LogicalPackage) {
217: selection = new StructuredSelection(
218: ((LogicalPackage) selectedElement)
219: .getJavaProject());
220: break;
221: }
222: }
223: }
224: super.selectionChanged(part, selection);
225: }
226: }
|