KickJava   Java API By Example, From Geeks To Geeks.

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


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.Assert;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22
23 import org.eclipse.core.resources.IFolder;
24 import org.eclipse.core.resources.IResource;
25
26 import org.eclipse.swt.widgets.Shell;
27
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.operation.IRunnableContext;
30 import org.eclipse.jface.operation.IRunnableWithProgress;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.jface.window.Window;
34
35 import org.eclipse.ui.IWorkbenchSite;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.part.ISetSelectionTarget;
38
39 import org.eclipse.jdt.core.IClasspathEntry;
40 import org.eclipse.jdt.core.IJavaProject;
41 import org.eclipse.jdt.core.IPackageFragmentRoot;
42 import org.eclipse.jdt.core.JavaCore;
43 import org.eclipse.jdt.core.JavaModelException;
44
45 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
46 import org.eclipse.jdt.internal.corext.buildpath.CPJavaProject;
47 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
48 import org.eclipse.jdt.internal.corext.util.Messages;
49
50 import org.eclipse.jdt.internal.ui.JavaPlugin;
51 import org.eclipse.jdt.internal.ui.JavaPluginImages;
52 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
53 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
54 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute;
55 import org.eclipse.jdt.internal.ui.wizards.buildpaths.OutputLocationDialog;
56
57 //SelectedElements iff enabled: (IPackageFragmentRoot || IJavaProject || CPListElementAttribute) && size == 1
58
public class EditOutputFolderAction extends BuildpathModifierAction {
59
60     private final IRunnableContext fContext;
61     private boolean fShowOutputFolders;
62
63     public EditOutputFolderAction(final IWorkbenchSite site) {
64         this(site, null, PlatformUI.getWorkbench().getProgressService());
65         
66         fShowOutputFolders= true;
67     }
68     
69     public EditOutputFolderAction(IRunnableContext context, ISetSelectionTarget selectionTarget) {
70         this(null, selectionTarget, context);
71     }
72
73     private EditOutputFolderAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) {
74         super(site, selectionTarget, BuildpathModifierAction.EDIT_OUTPUT);
75         
76         fContext= context;
77         fShowOutputFolders= false;
78         
79         setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_EditOutput_label);
80         setImageDescriptor(JavaPluginImages.DESC_ELCL_CONFIGURE_OUTPUT_FOLDER);
81         setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_EditOutput_tooltip);
82         setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CONFIGURE_OUTPUT_FOLDER);
83     }
84     
85     /**
86      * {@inheritDoc}
87      */

88     public String JavaDoc getDetailedDescription() {
89         return NewWizardMessages.PackageExplorerActionGroup_FormText_EditOutputFolder;
90     }
91     
92
93     public void showOutputFolders(boolean showOutputFolders) {
94         fShowOutputFolders= showOutputFolders;
95     }
96
97     /**
98      * {@inheritDoc}
99      */

100     public void run() {
101         try {
102
103             final Shell shell= getShell();
104             
105             final IJavaProject javaProject;
106             CPListElement cpElement= null;
107             Object JavaDoc firstElement= getSelectedElements().get(0);
108             if (firstElement instanceof IJavaProject) {
109                 javaProject= (IJavaProject)firstElement;
110                 
111                 final IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(javaProject.getPath(), javaProject, IClasspathEntry.CPE_SOURCE);
112                 cpElement= CPListElement.createFromExisting(entry, javaProject);
113             } else if (firstElement instanceof IPackageFragmentRoot) {
114                 IPackageFragmentRoot root= (IPackageFragmentRoot)firstElement;
115                 
116                 javaProject= root.getJavaProject();
117                                 
118                 final IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(root.getPath(), javaProject, IClasspathEntry.CPE_SOURCE);
119                 cpElement= CPListElement.createFromExisting(entry, javaProject);
120             } else if (firstElement instanceof CPListElementAttribute) {
121                 CPListElementAttribute attribute= (CPListElementAttribute)firstElement;
122
123                 cpElement= attribute.getParent();
124                 javaProject= cpElement.getJavaProject();
125             } else {
126                 return;
127             }
128
129             final List JavaDoc classpathEntries= ClasspathModifier.getExistingEntries(javaProject);
130             final CPListElement element= ClasspathModifier.getClasspathEntry(classpathEntries, cpElement);
131
132             final OutputLocationDialog dialog= new OutputLocationDialog(shell, element, classpathEntries, javaProject.getOutputLocation(), false);
133             if (dialog.open() != Window.OK)
134                 return;
135             
136             final CPJavaProject cpProject= CPJavaProject.createFromExisting(javaProject);
137             final BuildpathDelta delta= ClasspathModifier.setOutputLocation(cpProject.getCPElement(element), dialog.getOutputLocation(), false, cpProject);
138             
139             IFolder oldOutputFolder= getOldOutputFolder(delta);
140             final IFolder folderToDelete;
141             if (oldOutputFolder != null) {
142                 String JavaDoc message= Messages.format(NewWizardMessages.EditOutputFolderAction_DeleteOldOutputFolderQuestion, oldOutputFolder.getLocation().toString());
143                 if (MessageDialog.openQuestion(getShell(), NewWizardMessages.OutputLocationDialog_title, message)) {
144                     folderToDelete= oldOutputFolder;
145                 } else {
146                     folderToDelete= null;
147                 }
148             } else {
149                 folderToDelete= null;
150             }
151
152             try {
153                 final IRunnableWithProgress runnable= new IRunnableWithProgress() {
154                     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
155                         try {
156                             monitor.beginTask(NewWizardMessages.EditOutputFolderAction_ProgressMonitorDescription, 50 + (folderToDelete == null?0:10));
157                             
158                             ClasspathModifier.commitClassPath(cpProject, new SubProgressMonitor(monitor, 50));
159                             if (folderToDelete != null)
160                                 folderToDelete.delete(true, new SubProgressMonitor(monitor, 10));
161                             
162                             informListeners(delta);
163                             selectAndReveal(new StructuredSelection(JavaCore.create(element.getResource())));
164                         } catch (CoreException e) {
165                             throw new InvocationTargetException JavaDoc(e);
166                         } finally {
167                             monitor.done();
168                         }
169                     }
170                 };
171                 fContext.run(false, false, runnable);
172             } catch (final InvocationTargetException JavaDoc e) {
173                 JavaPlugin.log(e);
174             } catch (final InterruptedException JavaDoc e) {
175             }
176             
177         } catch (final CoreException e) {
178             showExceptionDialog(e, NewWizardMessages.EditOutputFolderAction_ErrorDescription);
179         }
180     }
181
182     private IFolder getOldOutputFolder(final BuildpathDelta delta) {
183         IResource[] deletedResources= delta.getDeletedResources();
184         List JavaDoc existingFolders= new ArrayList JavaDoc();
185         for (int i= 0; i < deletedResources.length; i++) {
186             if (deletedResources[i] instanceof IFolder && deletedResources[i].exists()) {
187                 existingFolders.add(deletedResources[i]);
188             }
189         }
190         if (existingFolders.size() > 0) {
191             if (existingFolders.size() > 1) {
192                 String JavaDoc message= "Found more then one existing folders:"; //$NON-NLS-1$
193
for (Iterator JavaDoc iterator= existingFolders.iterator(); iterator.hasNext();) {
194                     IFolder folder= (IFolder)iterator.next();
195                     message+= "\n" + folder.toString(); //$NON-NLS-1$
196
}
197                 Assert.isTrue(false, message);
198             }
199             return (IFolder)existingFolders.get(0);
200         }
201         return null;
202     }
203
204     protected boolean canHandle(final IStructuredSelection elements) {
205         if (!fShowOutputFolders)
206             return false;
207         
208         if (elements.size() != 1)
209             return false;
210
211         final Object JavaDoc element= elements.getFirstElement();
212         try {
213             if (element instanceof IPackageFragmentRoot) {
214                 final IPackageFragmentRoot root= (IPackageFragmentRoot)element;
215                 if (root.getKind() != IPackageFragmentRoot.K_SOURCE)
216                     return false;
217                     
218                 IJavaProject javaProject= root.getJavaProject();
219                 if (javaProject == null)
220                     return false;
221                 
222                 final IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(root.getPath(), javaProject, IClasspathEntry.CPE_SOURCE);
223                 if (entry == null)
224                     return false;
225                 
226                 return true;
227             } else if (element instanceof IJavaProject) {
228                 IJavaProject project= (IJavaProject)element;
229                 if (!(ClasspathModifier.isSourceFolder(project)))
230                     return false;
231                 
232                 return true;
233             } else if (element instanceof CPListElementAttribute) {
234                 CPListElementAttribute attribute= (CPListElementAttribute)element;
235                 if (attribute.getKey() != CPListElement.OUTPUT)
236                     return false;
237                 
238                 return true;
239             }
240
241         } catch (final JavaModelException e) {
242             return false;
243         }
244         return false;
245     }
246 }
Popular Tags