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.List;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IProgressMonitor;
018: import org.eclipse.core.runtime.NullProgressMonitor;
019: import org.eclipse.core.runtime.SubProgressMonitor;
020:
021: import org.eclipse.jface.operation.IRunnableContext;
022: import org.eclipse.jface.operation.IRunnableWithProgress;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024:
025: import org.eclipse.ui.IWorkbenchSite;
026: import org.eclipse.ui.part.ISetSelectionTarget;
027:
028: import org.eclipse.jdt.core.IClasspathEntry;
029: import org.eclipse.jdt.core.IJavaElement;
030: import org.eclipse.jdt.core.IJavaProject;
031: import org.eclipse.jdt.core.IPackageFragmentRoot;
032: import org.eclipse.jdt.core.JavaModelException;
033:
034: import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
035: import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
036:
037: import org.eclipse.jdt.internal.ui.JavaPlugin;
038: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
039: import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
040: import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute;
041:
042: public class ResetAllOutputFoldersAction extends
043: BuildpathModifierAction {
044:
045: private final IRunnableContext fContext;
046: private final IJavaProject fJavaProject;
047:
048: public ResetAllOutputFoldersAction(IRunnableContext context,
049: IJavaProject project, ISetSelectionTarget selectionTarget) {
050: this (null, selectionTarget, context, project);
051: }
052:
053: public ResetAllOutputFoldersAction(IWorkbenchSite site,
054: ISetSelectionTarget selectionTarget,
055: IRunnableContext context, IJavaProject javaProject) {
056: super (site, selectionTarget,
057: BuildpathModifierAction.RESET_ALL_OUTPUT_FOLDERS);
058:
059: fContext = context;
060: fJavaProject = javaProject;
061:
062: setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip);
063: setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip);
064: }
065:
066: /**
067: * {@inheritDoc}
068: */
069: public String getDetailedDescription() {
070: return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_ResetAllOutputFolders;
071: }
072:
073: /**
074: * {@inheritDoc}
075: */
076: public void run() {
077: final IRunnableWithProgress runnable = new IRunnableWithProgress() {
078: public void run(IProgressMonitor monitor)
079: throws InvocationTargetException,
080: InterruptedException {
081: try {
082: resetOutputFolders(fJavaProject, monitor);
083: } catch (CoreException e) {
084: throw new InvocationTargetException(e);
085: }
086: }
087: };
088: try {
089: fContext.run(false, false, runnable);
090: } catch (InvocationTargetException e) {
091: if (e.getCause() instanceof CoreException) {
092: showExceptionDialog(
093: (CoreException) e.getCause(),
094: NewWizardMessages.RemoveFromBuildpathAction_ErrorTitle);
095: } else {
096: JavaPlugin.log(e);
097: }
098: } catch (InterruptedException e) {
099: }
100: }
101:
102: private List resetOutputFolders(IJavaProject project,
103: IProgressMonitor monitor) throws JavaModelException {
104: if (monitor == null)
105: monitor = new NullProgressMonitor();
106: try {
107: IPackageFragmentRoot[] roots = project
108: .getPackageFragmentRoots();
109: monitor
110: .beginTask(
111: NewWizardMessages.ClasspathModifier_Monitor_ResetOutputFolder,
112: roots.length + 10);
113: List entries = new ArrayList();
114: for (int i = 0; i < roots.length; i++) {
115: monitor.worked(1);
116: if (roots[i].isArchive())
117: continue;
118: IClasspathEntry entry = roots[i].getRawClasspathEntry();
119: CPListElement element = CPListElement
120: .createFromExisting(entry, project);
121: CPListElementAttribute outputFolder = new CPListElementAttribute(
122: element, CPListElement.OUTPUT, element
123: .getAttribute(CPListElement.OUTPUT),
124: true);
125: entries.add(outputFolder);
126: }
127: return reset(entries, project, new SubProgressMonitor(
128: monitor, 10));
129: } finally {
130: monitor.done();
131: }
132: }
133:
134: private List reset(List selection, IJavaProject project,
135: IProgressMonitor monitor) throws JavaModelException {
136: if (monitor == null)
137: monitor = new NullProgressMonitor();
138: try {
139: monitor
140: .beginTask(
141: NewWizardMessages.ClasspathModifier_Monitor_Resetting,
142: selection.size());
143: List entries = ClasspathModifier
144: .getExistingEntries(project);
145: List result = new ArrayList();
146: for (int i = 0; i < selection.size(); i++) {
147: Object element = selection.get(i);
148: if (element instanceof IJavaElement) {
149: IJavaElement javaElement = (IJavaElement) element;
150: IPackageFragmentRoot root;
151: if (element instanceof IJavaProject)
152: root = project.getPackageFragmentRoot(project
153: .getResource());
154: else
155: root = (IPackageFragmentRoot) element;
156: CPListElement entry = ClasspathModifier
157: .getClasspathEntry(entries, root);
158: ClasspathModifier
159: .resetFilters(javaElement, entry, project,
160: new SubProgressMonitor(monitor, 1));
161: result.add(javaElement);
162: } else {
163: CPListElement selElement = ((CPListElementAttribute) element)
164: .getParent();
165: CPListElement entry = ClasspathModifier
166: .getClasspathEntry(entries, selElement);
167: CPListElementAttribute outputFolder = ClasspathModifier
168: .resetOutputFolder(entry, project);
169: result.add(outputFolder);
170: }
171: }
172:
173: ClasspathModifier.commitClassPath(entries, project, null);
174:
175: BuildpathDelta delta = new BuildpathDelta(getToolTipText());
176: delta.setNewEntries((CPListElement[]) entries
177: .toArray(new CPListElement[entries.size()]));
178: informListeners(delta);
179:
180: return result;
181: } finally {
182: monitor.done();
183: }
184: }
185:
186: protected boolean canHandle(IStructuredSelection elements) {
187: return true;
188: }
189: }
|