001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.wizards.buildpaths.newsourcepage;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.ArrayList;
014: import java.util.Iterator;
015: import java.util.List;
016:
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.core.runtime.NullProgressMonitor;
020: import org.eclipse.core.runtime.SubProgressMonitor;
021:
022: import org.eclipse.jface.operation.IRunnableContext;
023: import org.eclipse.jface.operation.IRunnableWithProgress;
024: import org.eclipse.jface.viewers.IStructuredSelection;
025: import org.eclipse.jface.viewers.StructuredSelection;
026:
027: import org.eclipse.ui.IWorkbenchSite;
028: import org.eclipse.ui.PlatformUI;
029: import org.eclipse.ui.part.ISetSelectionTarget;
030:
031: import org.eclipse.jdt.core.IClasspathEntry;
032: import org.eclipse.jdt.core.IJavaElement;
033: import org.eclipse.jdt.core.IJavaProject;
034: import org.eclipse.jdt.core.IPackageFragmentRoot;
035: import org.eclipse.jdt.core.JavaModelException;
036:
037: import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
038: import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
039:
040: import org.eclipse.jdt.internal.ui.JavaPlugin;
041: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
042: import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
043: import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute;
044:
045: //Warning: This is unused and untested code. Images and descriptions are missing too.
046: //SelectedElements iff enabled: IJavaProject || IPackageFragmentRoot || CPListElementAttribute
047: public class ResetAction extends BuildpathModifierAction {
048:
049: private final IRunnableContext fContext;
050:
051: public ResetAction(IWorkbenchSite site) {
052: this (site, null, PlatformUI.getWorkbench().getProgressService());
053: }
054:
055: public ResetAction(IRunnableContext context,
056: ISetSelectionTarget selectionTarget) {
057: this (null, selectionTarget, context);
058: }
059:
060: public ResetAction(IWorkbenchSite site,
061: ISetSelectionTarget selectionTarget,
062: IRunnableContext context) {
063: super (site, selectionTarget, BuildpathModifierAction.RESET);
064:
065: fContext = context;
066:
067: setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip);
068: setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip);
069: }
070:
071: /**
072: * {@inheritDoc}
073: */
074: public String getDetailedDescription() {
075: if (!isEnabled())
076: return null;
077:
078: Iterator iterator = getSelectedElements().iterator();
079: Object p = iterator.next();
080: while (iterator.hasNext()) {
081: Object q = iterator.next();
082: if ((p instanceof CPListElementAttribute && !(q instanceof CPListElementAttribute))
083: || (q instanceof CPListElementAttribute && !(p instanceof CPListElementAttribute))) {
084: return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_Reset;
085: }
086: p = q;
087: }
088: if (p instanceof CPListElementAttribute) {
089: return NewWizardMessages.PackageExplorerActionGroup_FormText_SetOutputToDefault;
090: } else {
091: return NewWizardMessages.PackageExplorerActionGroup_FormText_ResetFilters;
092: }
093: }
094:
095: /**
096: * {@inheritDoc}
097: */
098: public void run() {
099: final IRunnableWithProgress runnable = new IRunnableWithProgress() {
100: public void run(IProgressMonitor monitor)
101: throws InvocationTargetException,
102: InterruptedException {
103: try {
104: Object firstElement = getSelectedElements().get(0);
105: IJavaProject project = null;
106: if (firstElement instanceof IJavaProject) {
107: project = (IJavaProject) firstElement;
108: } else if (firstElement instanceof IPackageFragmentRoot) {
109: project = ((IPackageFragmentRoot) firstElement)
110: .getJavaProject();
111: } else {
112: project = ((CPListElementAttribute) firstElement)
113: .getParent().getJavaProject();
114: }
115:
116: List result = reset(getSelectedElements(), project,
117: monitor);
118: selectAndReveal(new StructuredSelection(result));
119: } catch (CoreException e) {
120: throw new InvocationTargetException(e);
121: }
122: }
123: };
124: try {
125: fContext.run(false, false, runnable);
126: } catch (InvocationTargetException e) {
127: if (e.getCause() instanceof CoreException) {
128: showExceptionDialog((CoreException) e.getCause(), ""); //$NON-NLS-1$
129: } else {
130: JavaPlugin.log(e);
131: }
132: } catch (InterruptedException e) {
133: }
134: }
135:
136: private List reset(List selection, IJavaProject project,
137: IProgressMonitor monitor) throws JavaModelException {
138: if (monitor == null)
139: monitor = new NullProgressMonitor();
140: try {
141: monitor
142: .beginTask(
143: NewWizardMessages.ClasspathModifier_Monitor_Resetting,
144: selection.size());
145: List entries = ClasspathModifier
146: .getExistingEntries(project);
147: List result = new ArrayList();
148: for (int i = 0; i < selection.size(); i++) {
149: Object element = selection.get(i);
150: if (element instanceof IJavaElement) {
151: IJavaElement javaElement = (IJavaElement) element;
152: IPackageFragmentRoot root;
153: if (element instanceof IJavaProject)
154: root = project.getPackageFragmentRoot(project
155: .getResource());
156: else
157: root = (IPackageFragmentRoot) element;
158: CPListElement entry = ClasspathModifier
159: .getClasspathEntry(entries, root);
160: ClasspathModifier
161: .resetFilters(javaElement, entry, project,
162: new SubProgressMonitor(monitor, 1));
163: result.add(javaElement);
164: } else {
165: CPListElement selElement = ((CPListElementAttribute) element)
166: .getParent();
167: CPListElement entry = ClasspathModifier
168: .getClasspathEntry(entries, selElement);
169: CPListElementAttribute outputFolder = ClasspathModifier
170: .resetOutputFolder(entry, project);
171: result.add(outputFolder);
172: }
173: }
174:
175: ClasspathModifier.commitClassPath(entries, project, null);
176:
177: BuildpathDelta delta = new BuildpathDelta(getToolTipText());
178: delta.setNewEntries((CPListElement[]) entries
179: .toArray(new CPListElement[entries.size()]));
180: informListeners(delta);
181:
182: return result;
183: } finally {
184: monitor.done();
185: }
186: }
187:
188: protected boolean canHandle(IStructuredSelection elements) {
189: try {
190: for (Iterator iterator = elements.iterator(); iterator
191: .hasNext();) {
192: Object element = iterator.next();
193: if (element instanceof IJavaProject) {
194: IJavaProject project = (IJavaProject) element;
195: if (!project.isOnClasspath(project))
196: return false;
197:
198: IClasspathEntry entry = ClasspathModifier
199: .getClasspathEntryFor(project.getPath(),
200: project, IClasspathEntry.CPE_SOURCE);
201: if (entry.getInclusionPatterns().length == 0
202: && entry.getExclusionPatterns().length == 0)
203: return false;
204:
205: return true;
206: } else if (element instanceof IPackageFragmentRoot) {
207: if (ClasspathModifier
208: .filtersSet((IPackageFragmentRoot) element))
209: return true;
210: } else if (element instanceof CPListElementAttribute) {
211: if (!ClasspathModifier
212: .isDefaultOutputFolder((CPListElementAttribute) element))
213: return true;
214: } else {
215: return false;
216: }
217: }
218: } catch (JavaModelException e) {
219: return false;
220: }
221: return false;
222: }
223: }
|