KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > newsourcepage > ResetAllOutputFoldersAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.wizards.buildpaths.newsourcepage;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.NullProgressMonitor;
20 import org.eclipse.core.runtime.SubProgressMonitor;
21
22 import org.eclipse.jface.operation.IRunnableContext;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25
26 import org.eclipse.ui.IWorkbenchSite;
27 import org.eclipse.ui.part.ISetSelectionTarget;
28
29 import org.eclipse.jdt.core.IClasspathEntry;
30 import org.eclipse.jdt.core.IJavaElement;
31 import org.eclipse.jdt.core.IJavaProject;
32 import org.eclipse.jdt.core.IPackageFragmentRoot;
33 import org.eclipse.jdt.core.JavaModelException;
34
35 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
36 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
37
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
40 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
41 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute;
42
43 public class ResetAllOutputFoldersAction extends BuildpathModifierAction {
44     
45     private final IRunnableContext fContext;
46     private final IJavaProject fJavaProject;
47
48     
49     public ResetAllOutputFoldersAction(IRunnableContext context, IJavaProject project, ISetSelectionTarget selectionTarget) {
50         this(null, selectionTarget, context, project);
51     }
52
53     public ResetAllOutputFoldersAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context, IJavaProject javaProject) {
54         super(site, selectionTarget, BuildpathModifierAction.RESET_ALL_OUTPUT_FOLDERS);
55         
56         fContext= context;
57         fJavaProject= javaProject;
58         
59         setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip);
60         setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip);
61     }
62     
63     /**
64      * {@inheritDoc}
65      */

66     public String JavaDoc getDetailedDescription() {
67         return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_ResetAllOutputFolders;
68     }
69
70     /**
71      * {@inheritDoc}
72      */

73     public void run() {
74         final IRunnableWithProgress runnable= new IRunnableWithProgress() {
75             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
76                 try {
77                     resetOutputFolders(fJavaProject, monitor);
78                 } catch (CoreException e) {
79                     throw new InvocationTargetException JavaDoc(e);
80                 }
81             }
82         };
83         try {
84             fContext.run(false, false, runnable);
85         } catch (InvocationTargetException JavaDoc e) {
86             if (e.getCause() instanceof CoreException) {
87                 showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.RemoveFromBuildpathAction_ErrorTitle);
88             } else {
89                 JavaPlugin.log(e);
90             }
91         } catch (InterruptedException JavaDoc e) {
92         }
93     }
94     
95     private List JavaDoc resetOutputFolders(IJavaProject project, IProgressMonitor monitor) throws JavaModelException {
96         if (monitor == null)
97             monitor= new NullProgressMonitor();
98         try {
99             IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
100             monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_ResetOutputFolder, roots.length + 10);
101             List JavaDoc entries= new ArrayList JavaDoc();
102             for (int i= 0; i < roots.length; i++) {
103                 monitor.worked(1);
104                 if (roots[i].isArchive())
105                     continue;
106                 IClasspathEntry entry= roots[i].getRawClasspathEntry();
107                 CPListElement element= CPListElement.createFromExisting(entry, project);
108                 CPListElementAttribute outputFolder= new CPListElementAttribute(element, CPListElement.OUTPUT, element.getAttribute(CPListElement.OUTPUT), true);
109                 entries.add(outputFolder);
110             }
111             return reset(entries, project, new SubProgressMonitor(monitor, 10));
112         } finally {
113             monitor.done();
114         }
115     }
116     
117     private List JavaDoc reset(List JavaDoc selection, IJavaProject project, IProgressMonitor monitor) throws JavaModelException {
118         if (monitor == null)
119             monitor= new NullProgressMonitor();
120         try {
121             monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_Resetting, selection.size());
122             List JavaDoc entries= ClasspathModifier.getExistingEntries(project);
123             List JavaDoc result= new ArrayList JavaDoc();
124             for (int i= 0; i < selection.size(); i++) {
125                 Object JavaDoc element= selection.get(i);
126                 if (element instanceof IJavaElement) {
127                     IJavaElement javaElement= (IJavaElement) element;
128                     IPackageFragmentRoot root;
129                     if (element instanceof IJavaProject)
130                         root= project.getPackageFragmentRoot(project.getResource());
131                     else
132                         root= (IPackageFragmentRoot) element;
133                     CPListElement entry= ClasspathModifier.getClasspathEntry(entries, root);
134                     ClasspathModifier.resetFilters(javaElement, entry, project, new SubProgressMonitor(monitor, 1));
135                     result.add(javaElement);
136                 } else {
137                     CPListElement selElement= ((CPListElementAttribute) element).getParent();
138                     CPListElement entry= ClasspathModifier.getClasspathEntry(entries, selElement);
139                     CPListElementAttribute outputFolder= ClasspathModifier.resetOutputFolder(entry, project);
140                     result.add(outputFolder);
141                 }
142             }
143         
144             ClasspathModifier.commitClassPath(entries, project, null);
145             
146             BuildpathDelta delta= new BuildpathDelta(getToolTipText());
147             delta.setNewEntries((CPListElement[])entries.toArray(new CPListElement[entries.size()]));
148             informListeners(delta);
149             
150             return result;
151         } finally {
152             monitor.done();
153         }
154     }
155     
156     protected boolean canHandle(IStructuredSelection elements) {
157         return true;
158     }
159 }
160
Popular Tags