KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > RemoveFromClasspathAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.actions;
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.resources.IResource;
19 import org.eclipse.core.resources.IWorkspaceRunnable;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23
24 import org.eclipse.jface.viewers.IStructuredSelection;
25
26 import org.eclipse.ui.IWorkbenchSite;
27 import org.eclipse.ui.PlatformUI;
28
29 import org.eclipse.jdt.core.IClasspathEntry;
30 import org.eclipse.jdt.core.IPackageFragmentRoot;
31 import org.eclipse.jdt.core.JavaModelException;
32
33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
34
35 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
36 import org.eclipse.jdt.internal.ui.JavaPlugin;
37 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
39 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
40
41 /**
42  * Action to remove package fragment roots from the classpath of its parent
43  * project. Currently, the action is applicable to selections containing
44  * non-external archives (JAR or zip).
45  *
46  * <p>
47  * This class may be instantiated; it is not intended to be subclassed.
48  * </p>
49  *
50  * @since 2.1
51  */

52 public class RemoveFromClasspathAction extends SelectionDispatchAction {
53
54     /**
55      * Creates a new <code>RemoveFromClasspathAction</code>. The action requires
56      * that the selection provided by the site's selection provider is of type
57      * <code> org.eclipse.jface.viewers.IStructuredSelection</code>.
58      *
59      * @param site the site providing context information for this action
60      */

61     public RemoveFromClasspathAction(IWorkbenchSite site) {
62         super(site);
63         setText(ActionMessages.RemoveFromClasspathAction_Remove);
64         setToolTipText(ActionMessages.RemoveFromClasspathAction_tooltip);
65         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.REMOVE_FROM_CLASSPATH_ACTION);
66     }
67     
68     /* (non-Javadoc)
69      * Method declared in SelectionDispatchAction
70      */

71     public void selectionChanged(IStructuredSelection selection) {
72         setEnabled(checkEnabled(selection));
73     }
74     
75     private static boolean checkEnabled(IStructuredSelection selection) {
76         if (selection.isEmpty())
77             return false;
78         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
79             if (! canRemove(iter.next()))
80                 return false;
81         }
82         return true;
83     }
84     
85     /* (non-Javadoc)
86      * Method declared in SelectionDispatchAction
87      */

88     public void run(final IStructuredSelection selection) {
89         try {
90             PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, new WorkbenchRunnableAdapter(new IWorkspaceRunnable() {
91                 public void run(IProgressMonitor pm) throws CoreException {
92                     try{
93                         IPackageFragmentRoot[] roots= getRootsToRemove(selection);
94                         pm.beginTask(ActionMessages.RemoveFromClasspathAction_Removing, roots.length);
95                         for (int i= 0; i < roots.length; i++) {
96                             int jCoreFlags= IPackageFragmentRoot.NO_RESOURCE_MODIFICATION | IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH;
97                             roots[i].delete(IResource.NONE, jCoreFlags, new SubProgressMonitor(pm, 1));
98                         }
99                     } finally {
100                         pm.done();
101                     }
102                 }
103         }));
104         } catch (InvocationTargetException JavaDoc e) {
105             ExceptionHandler.handle(e, getShell(),
106                     ActionMessages.RemoveFromClasspathAction_exception_dialog_title,
107                     ActionMessages.RemoveFromClasspathAction_Problems_occurred);
108         } catch (InterruptedException JavaDoc e) {
109             // canceled
110
}
111     }
112     
113     private static IPackageFragmentRoot[] getRootsToRemove(IStructuredSelection selection){
114         List JavaDoc result= new ArrayList JavaDoc(selection.size());
115         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
116             Object JavaDoc element= iter.next();
117             if (canRemove(element))
118                 result.add(element);
119         }
120         return (IPackageFragmentRoot[]) result.toArray(new IPackageFragmentRoot[result.size()]);
121     }
122
123     private static boolean canRemove(Object JavaDoc element){
124         if (! (element instanceof IPackageFragmentRoot))
125             return false;
126         IPackageFragmentRoot root= (IPackageFragmentRoot)element;
127         try {
128             IClasspathEntry cpe= root.getRawClasspathEntry();
129             if (cpe == null || cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER)
130                 return false; // don't want to remove the container if only a child is selected
131
return true;
132         } catch (JavaModelException e) {
133             if (JavaModelUtil.isExceptionToBeLogged(e))
134                 JavaPlugin.log(e);
135         }
136         return false;
137     }
138 }
139
140
Popular Tags