KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19
20 import org.eclipse.jdt.core.IClasspathEntry;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.JavaModelException;
23
24 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
25 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.DialogPackageExplorerActionGroup;
26
27 /**
28  * Operation to reset either the inclusion/exclusion filters on a source folder or
29  * the output folder of a source folder (depends on the selected element provided by
30  * the <code>IClasspathInformationProvider</code>).
31  *
32  * @see org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier#reset(List, IJavaProject, IProgressMonitor)
33  */

34 public class ResetOperation extends ClasspathModifierOperation {
35     
36     /**
37      * Constructor
38      *
39      * @param listener a <code>IClasspathModifierListener</code> that is notified about
40      * changes on classpath entries or <code>null</code> if no such notification is
41      * necessary.
42      * @param informationProvider a provider to offer information to the operation
43      *
44      * @see IClasspathInformationProvider
45      * @see ClasspathModifier
46      */

47     public ResetOperation(IClasspathModifierListener listener, IClasspathInformationProvider informationProvider) {
48         super(listener, informationProvider, NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Reset_tooltip, IClasspathInformationProvider.RESET);
49     }
50     
51     /**
52      * Method which runs the actions with a progress monitor.<br>
53      *
54      * This operation does not require any queries from the provider.
55      *
56      * @param monitor a progress monitor, can be <code>null</code>
57      */

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

86     public boolean isValid(List JavaDoc elements, int[] types) throws JavaModelException {
87         if (elements.size() == 0)
88             return false;
89         IJavaProject project= fInformationProvider.getJavaProject();
90         boolean hasResetableFragmentRoot= false;
91         boolean hasOutputFolder= false;
92         boolean hasResetableProject= false;
93         /*
94          * This computation is special compared to the other ones in operations:
95          * As soon as there is at least one element which allows resetting, reseting
96          * is allowed (note: NOT ALL have to allow this!).
97          *
98          * Of course, resetting is still not allowed if there is at least one element
99          * which does not support resetting at all!
100          */

101         for(int i= 0; i < elements.size(); i++) {
102             switch(types[i]) {
103                 case DialogPackageExplorerActionGroup.JAVA_PROJECT: hasResetableProject= isValidProject(project); break; // as standalone selection, this is not ok --> check at end
104
case DialogPackageExplorerActionGroup.PACKAGE_FRAGMENT_ROOT: break; // as standalone selection, this is not ok --> check at end
105
case DialogPackageExplorerActionGroup.MODIFIED_FRAGMENT_ROOT: hasResetableFragmentRoot= true; break; // is ok
106
case DialogPackageExplorerActionGroup.OUTPUT: hasOutputFolder= true; break; // is ok
107
case DialogPackageExplorerActionGroup.DEFAULT_OUTPUT: break; // as standalone selection, this is not ok --> check at end
108
default: return false; // all others are not ok
109
}
110             
111         }
112         return hasResetableFragmentRoot || hasOutputFolder || hasResetableProject;
113     }
114     
115     /**
116      * Find out whether the filters on the project can be reset or not.
117      *
118      * @param project the Java project
119      * @return <code>true</code> if this operation can be executed on the project,
120      * <code>false</code> otherwise
121      * @throws JavaModelException
122      */

123     private boolean isValidProject(IJavaProject project) throws JavaModelException {
124         if (project.isOnClasspath(project)) {
125             IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(project.getPath(), project, IClasspathEntry.CPE_SOURCE);
126             if (entry.getInclusionPatterns().length != 0 || entry.getExclusionPatterns().length != 0)
127                 return true;
128         }
129         return false;
130     }
131     
132     /**
133      * Get a description for this operation. The description depends on
134      * the provided type parameter, which must be a constant of
135      * <code>DialogPackageExplorerActionGroup</code>. If the type is
136      * <code>DialogPackageExplorerActionGroup.MULTI</code>, then the
137      * description will be very general to describe the situation of
138      * all the different selected objects as good as possible.
139      *
140      * @param type the type of the selected object, must be a constant of
141      * <code>DialogPackageExplorerActionGroup</code>.
142      * @return a string describing the operation
143      */

144     public String JavaDoc getDescription(int type) {
145         if (type == DialogPackageExplorerActionGroup.OUTPUT ||
146                 type == (DialogPackageExplorerActionGroup.OUTPUT | DialogPackageExplorerActionGroup.MULTI))
147             return NewWizardMessages.PackageExplorerActionGroup_FormText_SetOutputToDefault;
148         if (type == DialogPackageExplorerActionGroup.MODIFIED_FRAGMENT_ROOT ||
149                 type == (DialogPackageExplorerActionGroup.MODIFIED_FRAGMENT_ROOT | DialogPackageExplorerActionGroup.MULTI))
150             return NewWizardMessages.PackageExplorerActionGroup_FormText_ResetFilters;
151         return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_Reset;
152     }
153 }
154
Popular Tags