KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > buildpath > RemoveFromClasspathOperation


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
12 package org.eclipse.jdt.internal.corext.buildpath;
13
14 import java.lang.reflect.InvocationTargetException 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
21 import org.eclipse.jdt.core.IClasspathEntry;
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.IJavaProject;
24 import org.eclipse.jdt.core.IPackageFragmentRoot;
25 import org.eclipse.jdt.core.JavaModelException;
26
27 import org.eclipse.jdt.internal.corext.util.Messages;
28
29 import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
30 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
31 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.DialogPackageExplorerActionGroup;
32 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IRemoveLinkedFolderQuery;
33
34 /**
35  * Operation to remove source folders (of type <code>
36  * IPackageFragmentRoot</code> from the classpath.
37  *
38  * @see org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier#removeFromClasspath(IRemoveLinkedFolderQuery, List, IJavaProject, IProgressMonitor)
39  * @see org.eclipse.jdt.internal.corext.buildpath.AddSelectedSourceFolderOperation
40  */

41 public class RemoveFromClasspathOperation extends ClasspathModifierOperation {
42     
43     /**
44      * Constructor
45      *
46      * @param listener a <code>IClasspathModifierListener</code> that is notified about
47      * changes on classpath entries or <code>null</code> if no such notification is
48      * necessary.
49      * @param informationProvider a provider to offer information to the operation
50      *
51      * @see IClasspathInformationProvider
52      * @see ClasspathModifier
53      */

54     public RemoveFromClasspathOperation(IClasspathModifierListener listener, IClasspathInformationProvider informationProvider) {
55         super(listener, informationProvider, NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_RemoveFromCP_tooltip, IClasspathInformationProvider.REMOVE_FROM_BP);
56     }
57     
58     /**
59      * Method which runs the actions with a progress monitor.<br>
60      *
61      * This operation does not require any queries from the provider.
62      *
63      * @param monitor a progress monitor, can be <code>null</code>
64      */

65     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
66         List JavaDoc result= null;
67         fException= null;
68         try {
69             result= removeFromClasspath(fInformationProvider.getRemoveLinkedFolderQuery(), getSelectedElements(), fInformationProvider.getJavaProject(), monitor);
70         } catch (CoreException e) {
71             fException= e;
72             result= null;
73         }
74         super.handleResult(result, monitor);
75     }
76     
77     /**
78      * Find out whether this operation can be executed on
79      * the provided list of elements.
80      *
81      * @param elements a list of elements
82      * @param types an array of types for each element, that is,
83      * the type at position 'i' belongs to the selected element
84      * at position 'i'
85      *
86      * @return <code>true</code> if the operation can be
87      * executed on the provided list of elements, <code>
88      * false</code> otherwise.
89      * @throws JavaModelException
90      */

91     public boolean isValid(List JavaDoc elements, int[] types) throws JavaModelException {
92         if (elements.size() == 0)
93             return false;
94         IJavaProject project= fInformationProvider.getJavaProject();
95         Iterator JavaDoc iterator= elements.iterator();
96         while (iterator.hasNext()) {
97             Object JavaDoc element= iterator.next();
98             if (!(element instanceof IPackageFragmentRoot || element instanceof IJavaProject || element instanceof ClassPathContainer))
99                 return false;
100             if (element instanceof IJavaProject) {
101                 if (!isSourceFolder(project))
102                     return false;
103             } else if (element instanceof IPackageFragmentRoot) {
104                 IClasspathEntry entry= ((IPackageFragmentRoot) element).getRawClasspathEntry();
105                 if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
106                     return false;
107                 }
108             }
109         }
110         return true;
111     }
112
113
114     
115     /**
116      * Get a description for this operation. The description depends on
117      * the provided type parameter, which must be a constant of
118      * <code>DialogPackageExplorerActionGroup</code>. If the type is
119      * <code>DialogPackageExplorerActionGroup.MULTI</code>, then the
120      * description will be very general to describe the situation of
121      * all the different selected objects as good as possible.
122      *
123      * @param type the type of the selected object, must be a constant of
124      * <code>DialogPackageExplorerActionGroup</code>.
125      * @return a string describing the operation
126      */

127     public String JavaDoc getDescription(int type) {
128         IJavaElement elem= (IJavaElement)getSelectedElements().get(0);
129         String JavaDoc name= escapeSpecialChars(elem.getElementName());
130         if (type == DialogPackageExplorerActionGroup.JAVA_PROJECT)
131             return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_ProjectFromBuildpath, name);
132         if (type == DialogPackageExplorerActionGroup.PACKAGE_FRAGMENT_ROOT || type == DialogPackageExplorerActionGroup.MODIFIED_FRAGMENT_ROOT)
133             return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_fromBuildpath, name);
134         return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_FromBuildpath;
135     }
136 }
137
Popular Tags