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.Arrays;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.core.runtime.SubProgressMonitor;
021:
022: import org.eclipse.core.resources.IFolder;
023: import org.eclipse.core.resources.IResource;
024:
025: import org.eclipse.swt.widgets.Shell;
026:
027: import org.eclipse.jface.operation.IRunnableContext;
028: import org.eclipse.jface.operation.IRunnableWithProgress;
029: import org.eclipse.jface.viewers.IStructuredSelection;
030: import org.eclipse.jface.viewers.StructuredSelection;
031: import org.eclipse.jface.window.Window;
032:
033: import org.eclipse.ui.IWorkbenchSite;
034: import org.eclipse.ui.PlatformUI;
035: import org.eclipse.ui.part.ISetSelectionTarget;
036:
037: import org.eclipse.jdt.core.IClasspathEntry;
038: import org.eclipse.jdt.core.IJavaElement;
039: import org.eclipse.jdt.core.IJavaProject;
040: import org.eclipse.jdt.core.IPackageFragmentRoot;
041: import org.eclipse.jdt.core.JavaModelException;
042:
043: import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
044: import org.eclipse.jdt.internal.corext.buildpath.CPJavaProject;
045: import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
046: import org.eclipse.jdt.internal.corext.util.Messages;
047:
048: import org.eclipse.jdt.internal.ui.JavaPlugin;
049: import org.eclipse.jdt.internal.ui.JavaPluginImages;
050: import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
051: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
052: import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
053: import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IRemoveLinkedFolderQuery;
054:
055: //SelectedElements iff enabled: IPackageFramgentRoot || IJavaProject || ClassPathContainer
056: public class RemoveFromBuildpathAction extends BuildpathModifierAction {
057:
058: private final IRunnableContext fContext;
059:
060: public RemoveFromBuildpathAction(IWorkbenchSite site) {
061: this (site, null, PlatformUI.getWorkbench().getProgressService());
062:
063: setImageDescriptor(JavaPluginImages.DESC_ELCL_REMOVE_FROM_BP);
064: }
065:
066: public RemoveFromBuildpathAction(IRunnableContext context,
067: ISetSelectionTarget selectionTarget) {
068: this (null, selectionTarget, context);
069:
070: setImageDescriptor(JavaPluginImages.DESC_ELCL_REMOVE_AS_SOURCE_FOLDER);
071: setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_REMOVE_AS_SOURCE_FOLDER);
072: }
073:
074: public RemoveFromBuildpathAction(IWorkbenchSite site,
075: ISetSelectionTarget selectionTarget,
076: IRunnableContext context) {
077: super (site, selectionTarget,
078: BuildpathModifierAction.REMOVE_FROM_BP);
079:
080: fContext = context;
081:
082: setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_RemoveFromCP_label);
083: setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_RemoveFromCP_tooltip);
084: }
085:
086: /**
087: * {@inheritDoc}
088: */
089: public String getDetailedDescription() {
090: if (!isEnabled())
091: return null;
092:
093: if (getSelectedElements().size() != 1)
094: return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_FromBuildpath;
095:
096: Object elem = getSelectedElements().get(0);
097:
098: if (elem instanceof IJavaProject) {
099: String name = ClasspathModifier
100: .escapeSpecialChars(((IJavaElement) elem)
101: .getElementName());
102: return Messages
103: .format(
104: NewWizardMessages.PackageExplorerActionGroup_FormText_ProjectFromBuildpath,
105: name);
106: } else if (elem instanceof IPackageFragmentRoot) {
107: String name = ClasspathModifier
108: .escapeSpecialChars(((IJavaElement) elem)
109: .getElementName());
110: return Messages
111: .format(
112: NewWizardMessages.PackageExplorerActionGroup_FormText_fromBuildpath,
113: name);
114: } else if (elem instanceof ClassPathContainer) {
115: return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_FromBuildpath;
116: }
117:
118: return null;
119: }
120:
121: /**
122: * {@inheritDoc}
123: */
124: public void run() {
125: try {
126:
127: final IJavaProject project;
128: Object object = getSelectedElements().get(0);
129: if (object instanceof IJavaProject) {
130: project = (IJavaProject) object;
131: } else if (object instanceof IPackageFragmentRoot) {
132: IPackageFragmentRoot root = (IPackageFragmentRoot) object;
133: project = root.getJavaProject();
134: } else {
135: ClassPathContainer container = (ClassPathContainer) object;
136: project = container.getJavaProject();
137: }
138:
139: final List elementsToRemove = new ArrayList();
140: final List foldersToDelete = new ArrayList();
141: queryToRemoveLinkedFolders(elementsToRemove,
142: foldersToDelete);
143:
144: final IRunnableWithProgress runnable = new IRunnableWithProgress() {
145: public void run(IProgressMonitor monitor)
146: throws InvocationTargetException,
147: InterruptedException {
148: try {
149: monitor
150: .beginTask(
151: NewWizardMessages.ClasspathModifier_Monitor_RemoveFromBuildpath,
152: foldersToDelete.size() + 10);
153:
154: CPJavaProject cpProject = CPJavaProject
155: .createFromExisting(project);
156: CPListElement[] toRemove = new CPListElement[elementsToRemove
157: .size()];
158: int i = 0;
159: for (Iterator iterator = elementsToRemove
160: .iterator(); iterator.hasNext();) {
161: Object element = iterator.next();
162: if (element instanceof IJavaProject) {
163: toRemove[i] = ClasspathModifier
164: .getListElement(
165: ((IJavaProject) element)
166: .getPath(),
167: cpProject
168: .getCPListElements());
169: } else if (element instanceof IPackageFragmentRoot) {
170: toRemove[i] = CPListElement
171: .createFromExisting(
172: ((IPackageFragmentRoot) element)
173: .getRawClasspathEntry(),
174: project);
175: } else {
176: toRemove[i] = CPListElement
177: .createFromExisting(
178: ((ClassPathContainer) element)
179: .getClasspathEntry(),
180: project);
181: }
182: i++;
183: }
184:
185: BuildpathDelta delta = ClasspathModifier
186: .removeFromBuildpath(toRemove,
187: cpProject);
188: ClasspathModifier.commitClassPath(cpProject,
189: new SubProgressMonitor(monitor, 10));
190:
191: deleteFolders(foldersToDelete,
192: new SubProgressMonitor(monitor,
193: foldersToDelete.size()));
194:
195: informListeners(delta);
196:
197: if (delta.getDeletedResources().length == foldersToDelete
198: .size()) {
199: selectAndReveal(new StructuredSelection(
200: project));
201: } else {
202: List result = new ArrayList(
203: Arrays.asList(delta
204: .getDeletedResources()));
205: result.removeAll(foldersToDelete);
206: selectAndReveal(new StructuredSelection(
207: result));
208: }
209: } catch (CoreException e) {
210: throw new InvocationTargetException(e);
211: } finally {
212: monitor.done();
213: }
214: }
215: };
216: fContext.run(false, false, runnable);
217:
218: } catch (CoreException e) {
219: showExceptionDialog(
220: e,
221: NewWizardMessages.RemoveFromBuildpathAction_ErrorTitle);
222: } catch (InvocationTargetException e) {
223: if (e.getCause() instanceof CoreException) {
224: showExceptionDialog(
225: (CoreException) e.getCause(),
226: NewWizardMessages.RemoveFromBuildpathAction_ErrorTitle);
227: } else {
228: JavaPlugin.log(e);
229: }
230: } catch (InterruptedException e) {
231: }
232: }
233:
234: private void deleteFolders(List folders, IProgressMonitor monitor)
235: throws CoreException {
236: try {
237: monitor
238: .beginTask(
239: NewWizardMessages.ClasspathModifier_Monitor_RemoveFromBuildpath,
240: folders.size());
241:
242: for (Iterator iter = folders.iterator(); iter.hasNext();) {
243: IFolder folder = (IFolder) iter.next();
244: folder.delete(true, true, new SubProgressMonitor(
245: monitor, 1));
246: }
247: } finally {
248: monitor.done();
249: }
250: }
251:
252: private void queryToRemoveLinkedFolders(
253: final List elementsToRemove, final List foldersToDelete)
254: throws JavaModelException {
255: final Shell shell = getShell();
256: for (Iterator iter = getSelectedElements().iterator(); iter
257: .hasNext();) {
258: Object element = iter.next();
259: if (element instanceof IPackageFragmentRoot) {
260: IFolder folder = getLinkedSourceFolder((IPackageFragmentRoot) element);
261: if (folder != null) {
262: RemoveLinkedFolderDialog dialog = new RemoveLinkedFolderDialog(
263: shell, folder);
264:
265: final int result = dialog.open() == Window.OK ? dialog
266: .getRemoveStatus()
267: : IRemoveLinkedFolderQuery.REMOVE_CANCEL;
268:
269: if (result != IRemoveLinkedFolderQuery.REMOVE_CANCEL) {
270: if (result == IRemoveLinkedFolderQuery.REMOVE_BUILD_PATH) {
271: elementsToRemove.add(element);
272: } else if (result == IRemoveLinkedFolderQuery.REMOVE_BUILD_PATH_AND_FOLDER) {
273: elementsToRemove.add(element);
274: foldersToDelete.add(folder);
275: }
276: }
277: } else {
278: elementsToRemove.add(element);
279: }
280: } else {
281: elementsToRemove.add(element);
282: }
283: }
284: }
285:
286: private IFolder getLinkedSourceFolder(IPackageFragmentRoot root)
287: throws JavaModelException {
288: if (root.getKind() != IPackageFragmentRoot.K_SOURCE)
289: return null;
290:
291: final IResource resource = root.getCorrespondingResource();
292: if (!(resource instanceof IFolder))
293: return null;
294:
295: final IFolder folder = (IFolder) resource;
296: if (!folder.isLinked())
297: return null;
298:
299: return folder;
300: }
301:
302: protected boolean canHandle(IStructuredSelection elements) {
303: if (elements.size() == 0)
304: return false;
305:
306: try {
307: for (Iterator iter = elements.iterator(); iter.hasNext();) {
308: Object element = iter.next();
309:
310: if (element instanceof IJavaProject) {
311: IJavaProject project = (IJavaProject) element;
312: if (!ClasspathModifier.isSourceFolder(project))
313: return false;
314:
315: } else if (element instanceof IPackageFragmentRoot) {
316: IClasspathEntry entry = ((IPackageFragmentRoot) element)
317: .getRawClasspathEntry();
318: if (entry != null
319: && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
320: return false;
321: }
322: } else if (element instanceof ClassPathContainer) {
323: return true;
324: } else {
325: return false;
326: }
327: }
328: return true;
329: } catch (JavaModelException e) {
330: }
331: return false;
332: }
333: }
|