KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.resources.IResource;
21
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.IPackageFragmentRoot;
24 import org.eclipse.jdt.core.JavaModelException;
25
26 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
27 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.DialogPackageExplorerActionGroup;
28
29
30 /**
31  * Operation to include objects of type
32  * <code>IResource</code> or <code>IJavaElement</code>.
33  *
34  * @see org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier#include(List, IJavaProject, IProgressMonitor)
35  * @see org.eclipse.jdt.internal.corext.buildpath.UnincludeOperation
36  */

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

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

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

89     public boolean isValid(List JavaDoc elements, int[] types) throws JavaModelException {
90         if (elements.size() == 0)
91             return false;
92         IJavaProject project= fInformationProvider.getJavaProject();
93         for(int i= 0; i < elements.size(); i++) {
94             Object JavaDoc element= elements.get(i);
95             switch (types[i]) {
96                 case DialogPackageExplorerActionGroup.PACKAGE_FRAGMENT: break; // ok
97
case DialogPackageExplorerActionGroup.FOLDER: if (!isValidFolder((IResource)element, project)) return false; break;
98                 case DialogPackageExplorerActionGroup.EXCLUDED_FOLDER: break; // ok
99
case DialogPackageExplorerActionGroup.EXCLUDED_FILE: break; // ok
100
case DialogPackageExplorerActionGroup.COMPILATION_UNIT: break; // ok
101
default: return false; // all others are not ok
102
}
103             
104         }
105         return true;
106     }
107     
108     /**
109      * Find out whether the folder can be included or not.
110      *
111      * @param resource the resource to be checked
112      * @param project the Java project
113      * @return <code>true</code> if the folder can be included, <code>
114      * false</code> otherwise
115      * @throws JavaModelException
116      */

117     private boolean isValidFolder(IResource resource, IJavaProject project) throws JavaModelException {
118         if (project.isOnClasspath(project) && resource.getProjectRelativePath().segmentCount() == 1) {
119             IPackageFragmentRoot root1= ClasspathModifier.getFragmentRoot(resource, project, null);
120             IPackageFragmentRoot root2= ClasspathModifier.getFragmentRoot(project.getResource(), project, null);
121             if (root1 != null && root1.equals(root2)) {
122                 return true;
123             }
124         }
125         return false;
126     }
127     
128     /**
129      * Get a description for this operation. The description depends on
130      * the provided type parameter, which must be a constant of
131      * <code>DialogPackageExplorerActionGroup</code>. If the type is
132      * <code>DialogPackageExplorerActionGroup.MULTI</code>, then the
133      * description will be very general to describe the situation of
134      * all the different selected objects as good as possible.
135      *
136      * @param type the type of the selected object, must be a constant of
137      * <code>DialogPackageExplorerActionGroup</code>.
138      * @return a string describing the operation
139      */

140     public String JavaDoc getDescription(int type) {
141         if (type == DialogPackageExplorerActionGroup.PACKAGE_FRAGMENT)
142             return NewWizardMessages.PackageExplorerActionGroup_FormText_Include;
143         if (type == DialogPackageExplorerActionGroup.COMPILATION_UNIT)
144             return NewWizardMessages.PackageExplorerActionGroup_FormText_Include;
145         if (type == DialogPackageExplorerActionGroup.FOLDER)
146             return NewWizardMessages.PackageExplorerActionGroup_FormText_Include;
147         if (type == DialogPackageExplorerActionGroup.EXCLUDED_FOLDER)
148             return NewWizardMessages.PackageExplorerActionGroup_FormText_Include;
149         if (type == DialogPackageExplorerActionGroup.EXCLUDED_FILE)
150             return NewWizardMessages.PackageExplorerActionGroup_FormText_Include;
151         return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_Include;
152     }
153 }
154
Popular Tags