KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.NullProgressMonitor;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22
23 import org.eclipse.jface.operation.IRunnableContext;
24 import org.eclipse.jface.operation.IRunnableWithProgress;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27
28 import org.eclipse.ui.IWorkbenchSite;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.part.ISetSelectionTarget;
31
32 import org.eclipse.jdt.core.IClasspathEntry;
33 import org.eclipse.jdt.core.IJavaElement;
34 import org.eclipse.jdt.core.IJavaProject;
35 import org.eclipse.jdt.core.IPackageFragmentRoot;
36 import org.eclipse.jdt.core.JavaModelException;
37
38 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
39 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
40
41 import org.eclipse.jdt.internal.ui.JavaPlugin;
42 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
43 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
44 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute;
45
46 //Warning: This is unused and untested code. Images and descriptions are missing too.
47
//SelectedElements iff enabled: IJavaProject || IPackageFragmentRoot || CPListElementAttribute
48
public class ResetAction extends BuildpathModifierAction {
49
50     private final IRunnableContext fContext;
51
52     public ResetAction(IWorkbenchSite site) {
53         this(site, null, PlatformUI.getWorkbench().getProgressService());
54     }
55     
56     public ResetAction(IRunnableContext context, ISetSelectionTarget selectionTarget) {
57         this(null, selectionTarget, context);
58     }
59
60     public ResetAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) {
61         super(site, selectionTarget, BuildpathModifierAction.RESET);
62         
63         fContext= context;
64         
65         setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip);
66         setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip);
67     }
68     
69     /**
70      * {@inheritDoc}
71      */

72     public String JavaDoc getDetailedDescription() {
73         if (!isEnabled())
74             return null;
75         
76         Iterator JavaDoc iterator= getSelectedElements().iterator();
77         Object JavaDoc p= iterator.next();
78         while (iterator.hasNext()) {
79             Object JavaDoc q= iterator.next();
80             if (
81                     (p instanceof CPListElementAttribute && !(q instanceof CPListElementAttribute)) ||
82                     (q instanceof CPListElementAttribute && !(p instanceof CPListElementAttribute))
83             ) {
84                 return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_Reset;
85             }
86             p= q;
87         }
88         if (p instanceof CPListElementAttribute) {
89             return NewWizardMessages.PackageExplorerActionGroup_FormText_SetOutputToDefault;
90         } else {
91             return NewWizardMessages.PackageExplorerActionGroup_FormText_ResetFilters;
92         }
93     }
94
95     /**
96      * {@inheritDoc}
97      */

98     public void run() {
99         final IRunnableWithProgress runnable= new IRunnableWithProgress() {
100             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
101                 try {
102                     Object JavaDoc firstElement= getSelectedElements().get(0);
103                     IJavaProject project= null;
104                     if (firstElement instanceof IJavaProject) {
105                         project= (IJavaProject)firstElement;
106                     } else if (firstElement instanceof IPackageFragmentRoot) {
107                         project= ((IPackageFragmentRoot)firstElement).getJavaProject();
108                     } else {
109                         project= ((CPListElementAttribute)firstElement).getParent().getJavaProject();
110                     }
111                     
112                     List JavaDoc result= reset(getSelectedElements(), project, monitor);
113                     selectAndReveal(new StructuredSelection(result));
114                 } catch (CoreException e) {
115                     throw new InvocationTargetException JavaDoc(e);
116                 }
117             }
118         };
119         try {
120             fContext.run(false, false, runnable);
121         } catch (InvocationTargetException JavaDoc e) {
122             if (e.getCause() instanceof CoreException) {
123                 showExceptionDialog((CoreException)e.getCause(), ""); //$NON-NLS-1$
124
} else {
125                 JavaPlugin.log(e);
126             }
127         } catch (InterruptedException JavaDoc e) {
128         }
129     }
130     
131     private List JavaDoc reset(List JavaDoc selection, IJavaProject project, IProgressMonitor monitor) throws JavaModelException {
132         if (monitor == null)
133             monitor= new NullProgressMonitor();
134         try {
135             monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_Resetting, selection.size());
136             List JavaDoc entries= ClasspathModifier.getExistingEntries(project);
137             List JavaDoc result= new ArrayList JavaDoc();
138             for (int i= 0; i < selection.size(); i++) {
139                 Object JavaDoc element= selection.get(i);
140                 if (element instanceof IJavaElement) {
141                     IJavaElement javaElement= (IJavaElement) element;
142                     IPackageFragmentRoot root;
143                     if (element instanceof IJavaProject)
144                         root= project.getPackageFragmentRoot(project.getResource());
145                     else
146                         root= (IPackageFragmentRoot) element;
147                     CPListElement entry= ClasspathModifier.getClasspathEntry(entries, root);
148                     ClasspathModifier.resetFilters(javaElement, entry, project, new SubProgressMonitor(monitor, 1));
149                     result.add(javaElement);
150                 } else {
151                     CPListElement selElement= ((CPListElementAttribute) element).getParent();
152                     CPListElement entry= ClasspathModifier.getClasspathEntry(entries, selElement);
153                     CPListElementAttribute outputFolder= ClasspathModifier.resetOutputFolder(entry, project);
154                     result.add(outputFolder);
155                 }
156             }
157         
158             ClasspathModifier.commitClassPath(entries, project, null);
159         
160             BuildpathDelta delta= new BuildpathDelta(getToolTipText());
161             delta.setNewEntries((CPListElement[])entries.toArray(new CPListElement[entries.size()]));
162             informListeners(delta);
163             
164             return result;
165         } finally {
166             monitor.done();
167         }
168     }
169     
170     protected boolean canHandle(IStructuredSelection elements) {
171         try {
172             for (Iterator JavaDoc iterator= elements.iterator(); iterator.hasNext();) {
173                 Object JavaDoc element= iterator.next();
174                 if (element instanceof IJavaProject) {
175                     IJavaProject project= (IJavaProject)element;
176                     if (!project.isOnClasspath(project))
177                         return false;
178                     
179                     IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(project.getPath(), project, IClasspathEntry.CPE_SOURCE);
180                     if (entry.getInclusionPatterns().length == 0 && entry.getExclusionPatterns().length == 0)
181                         return false;
182                     
183                     return true;
184                 } else if (element instanceof IPackageFragmentRoot) {
185                     if (ClasspathModifier.filtersSet((IPackageFragmentRoot)element))
186                         return true;
187                 } else if (element instanceof CPListElementAttribute) {
188                     if (!ClasspathModifier.isDefaultOutputFolder((CPListElementAttribute)element))
189                         return true;
190                 } else {
191                     return false;
192                 }
193             }
194         } catch (JavaModelException e) {
195             return false;
196         }
197         return false;
198     }
199 }
Popular Tags