KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IProgressMonitor;
20
21 import org.eclipse.core.resources.IResource;
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.part.ISetSelectionTarget;
29
30 import org.eclipse.jdt.core.ElementChangedEvent;
31 import org.eclipse.jdt.core.IClasspathEntry;
32 import org.eclipse.jdt.core.IElementChangedListener;
33 import org.eclipse.jdt.core.IJavaProject;
34 import org.eclipse.jdt.core.JavaCore;
35 import org.eclipse.jdt.core.JavaModelException;
36
37 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
38 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
39
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.JavaPluginImages;
42 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
43 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
44
45 //TODO: Use global history
46
public class ResetAllAction extends BuildpathModifierAction {
47     
48     private final HintTextGroup fProvider;
49     private final IRunnableContext fContext;
50     private IJavaProject fJavaProject;
51     private List JavaDoc fEntries;
52     private IPath fOutputLocation;
53
54     public ResetAllAction(HintTextGroup provider, IRunnableContext context, ISetSelectionTarget selectionTarget) {
55         super(null, selectionTarget, BuildpathModifierAction.RESET_ALL);
56         
57         fProvider= provider;
58         fContext= context;
59         
60         setImageDescriptor(JavaPluginImages.DESC_ELCL_CLEAR);
61         setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CLEAR);
62         setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_label);
63         setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip);
64         setEnabled(false);
65     }
66     
67     /**
68      * {@inheritDoc}
69      */

70     public String JavaDoc getDetailedDescription() {
71         return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_ResetAll;
72     }
73
74     public void setBreakPoint(IJavaProject javaProject) {
75         fJavaProject= javaProject;
76         if (fJavaProject.exists()) {
77             try {
78                 fEntries= ClasspathModifier.getExistingEntries(fJavaProject);
79                 fOutputLocation= fJavaProject.getOutputLocation();
80             } catch (JavaModelException e) {
81                 JavaPlugin.log(e);
82                 return;
83             }
84             setEnabled(true);
85         } else {
86             JavaCore.addElementChangedListener(new IElementChangedListener() {
87         
88                 public void elementChanged(ElementChangedEvent event) {
89                     if (fJavaProject.exists()) {
90                         try {
91                             fEntries= ClasspathModifier.getExistingEntries(fJavaProject);
92                             fOutputLocation= fJavaProject.getOutputLocation();
93                         } catch (JavaModelException e) {
94                             JavaPlugin.log(e);
95                             return;
96                         } finally {
97                             JavaCore.removeElementChangedListener(this);
98                         }
99                         setEnabled(true);
100                     }
101                 }
102                 
103             }, ElementChangedEvent.POST_CHANGE);
104         }
105     }
106     
107     /**
108      * {@inheritDoc}
109      */

110     public void run() {
111
112         try {
113             final IRunnableWithProgress runnable= new IRunnableWithProgress() {
114                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
115
116                     monitor.beginTask("", 3); //$NON-NLS-1$
117
try {
118                         if (!hasChange(fJavaProject))
119                             return;
120                         
121                         BuildpathDelta delta= new BuildpathDelta(getToolTipText());
122                         
123                         ClasspathModifier.commitClassPath(fEntries, fJavaProject, monitor);
124                         delta.setNewEntries((CPListElement[])fEntries.toArray(new CPListElement[fEntries.size()]));
125                         
126                         fJavaProject.setOutputLocation(fOutputLocation, monitor);
127                         delta.setDefaultOutputLocation(fOutputLocation);
128                         
129                         for (Iterator JavaDoc iterator= fProvider.getCreatedResources().iterator(); iterator.hasNext();) {
130                             IResource resource= (IResource)iterator.next();
131                             resource.delete(false, null);
132                             delta.addDeletedResource(resource);
133                         }
134                         
135                         fProvider.resetCreatedResources();
136                         
137                         informListeners(delta);
138                         
139                         selectAndReveal(new StructuredSelection(fJavaProject));
140                     } catch (JavaModelException e) {
141                         showExceptionDialog(e, NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip);
142                     } catch (CoreException e) {
143                         showExceptionDialog(e, NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip);
144                     } finally {
145                         monitor.done();
146                     }
147                 }
148             };
149             fContext.run(false, false, runnable);
150         } catch (InvocationTargetException JavaDoc e) {
151             if (e.getCause() instanceof CoreException) {
152                 showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip);
153             } else {
154                 JavaPlugin.log(e);
155             }
156         } catch (InterruptedException JavaDoc e) {
157         }
158     }
159     
160
161     /**
162      * {@inheritDoc}
163      */

164     protected boolean canHandle(IStructuredSelection elements) {
165         if (fJavaProject == null)
166             return false;
167         
168         return true;
169     }
170
171     
172     //TODO: Remove, action should be disabled if not hasChange
173
private boolean hasChange(IJavaProject project) throws JavaModelException {
174         if (!project.getOutputLocation().equals(fOutputLocation))
175             return true;
176       
177         IClasspathEntry[] currentEntries= project.getRawClasspath();
178         if (currentEntries.length != fEntries.size())
179             return true;
180         
181         int i= 0;
182         for (Iterator JavaDoc iterator= fEntries.iterator(); iterator.hasNext();) {
183             CPListElement oldEntrie= (CPListElement)iterator.next();
184             if (!oldEntrie.getClasspathEntry().equals(currentEntries[i]))
185                 return true;
186             i++;
187         }
188         return false;
189     }
190 }
191
Popular Tags