KickJava   Java API By Example, From Geeks To Geeks.

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


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.IJavaElement;
23 import org.eclipse.jdt.core.IJavaProject;
24 import org.eclipse.jdt.core.JavaModelException;
25
26 import org.eclipse.jdt.internal.corext.util.Messages;
27
28 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
29 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.DialogPackageExplorerActionGroup;
30 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.OutputFolderQuery;
31
32
33 /**
34  * Operation to add objects (of type <code>IFolder</code> or <code>
35  * IJavaElement</code> as source folder to the classpath.
36  *
37  * @see org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier#addToClasspath(List, IJavaProject, OutputFolderQuery, IProgressMonitor)
38  * @see org.eclipse.jdt.internal.corext.buildpath.RemoveFromClasspathOperation
39  */

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

53     public AddSelectedSourceFolderOperation(IClasspathModifierListener listener, IClasspathInformationProvider informationProvider) {
54         super(listener, informationProvider, NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddSelSFToCP_tooltip, IClasspathInformationProvider.ADD_SEL_SF_TO_BP);
55     }
56     
57     /**
58      * Method which runs the actions with a progress monitor.<br>
59      *
60      * This operation requires the following query from the provider:
61      * <li>IOutputFolderQuery</li>
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             List JavaDoc elements= getSelectedElements();
70             IJavaProject project= fInformationProvider.getJavaProject();
71             OutputFolderQuery query= fInformationProvider.getOutputFolderQuery();
72             result= addToClasspath(elements, project, query, monitor);
73         } catch (CoreException e) {
74             fException= e;
75             result= null;
76         }
77        super.handleResult(result, monitor);
78     }
79     
80     /**
81      * Find out whether this operation can be executed on
82      * the provided list of elements.
83      *
84      * @param elements a list of elements
85      * @param types an array of types for each element, that is,
86      * the type at position 'i' belongs to the selected element
87      * at position 'i'
88      *
89      * @return <code>true</code> if the operation can be
90      * executed on the provided list of elements, <code>
91      * false</code> otherwise.
92      * @throws JavaModelException
93      */

94     public boolean isValid(List JavaDoc elements, int[] types) throws JavaModelException {
95         if (elements.size() == 0)
96             return false;
97         for (int i= 0; i < elements.size(); i++) {
98             Object JavaDoc object= elements.get(i);
99             switch (types[i]) {
100                 case DialogPackageExplorerActionGroup.JAVA_PROJECT: if (isSourceFolder((IJavaProject) object)) return false; break;
101                 case DialogPackageExplorerActionGroup.PACKAGE_FRAGMENT: break; // is ok
102
case DialogPackageExplorerActionGroup.INCLUDED_FOLDER: break; // is ok
103
case DialogPackageExplorerActionGroup.FOLDER: break; // is ok
104
case DialogPackageExplorerActionGroup.EXCLUDED_FOLDER: break; // is ok
105
default: return false; // all others are not ok
106
}
107             
108         }
109         return true;
110     }
111         
112     /**
113      * Get a description for this operation. The description depends on
114      * the provided type parameter, which must be a constant of
115      * <code>DialogPackageExplorerActionGroup</code>. If the type is
116      * <code>DialogPackageExplorerActionGroup.MULTI</code>, then the
117      * description will be very general to describe the situation of
118      * all the different selected objects as good as possible.
119      *
120      * @param type the type of the selected object, must be a constant of
121      * <code>DialogPackageExplorerActionGroup</code>.
122      * @return a string describing the operation
123      */

124     public String JavaDoc getDescription(int type) {
125         Object JavaDoc obj= getSelectedElements().get(0);
126         if (obj instanceof IJavaElement) {
127             String JavaDoc name= escapeSpecialChars(((IJavaElement) obj).getElementName());
128             if (type == DialogPackageExplorerActionGroup.JAVA_PROJECT)
129                 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_ProjectToBuildpath, name);
130             if (type == DialogPackageExplorerActionGroup.PACKAGE_FRAGMENT)
131                 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_PackageToBuildpath, name);
132             if (type == DialogPackageExplorerActionGroup.MODIFIED_FRAGMENT_ROOT)
133                 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_PackageToBuildpath, name);
134         } else if (obj instanceof IResource) {
135             String JavaDoc name= escapeSpecialChars(((IResource) obj).getName());
136             if (type == DialogPackageExplorerActionGroup.FOLDER)
137                 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_FolderToBuildpath, name);
138             if (type == DialogPackageExplorerActionGroup.EXCLUDED_FOLDER)
139                 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_FolderToBuildpath, name);
140         }
141          return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_toBuildpath;
142     }
143 }
144
Popular Tags