KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > AddProjectAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.internal.debug.ui.actions;
12
13
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.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.IWorkspaceRoot;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.MultiStatus;
24 import org.eclipse.jdt.core.IClasspathContainer;
25 import org.eclipse.jdt.core.IClasspathEntry;
26 import org.eclipse.jdt.core.IJavaModel;
27 import org.eclipse.jdt.core.IJavaProject;
28 import org.eclipse.jdt.core.JavaCore;
29 import org.eclipse.jdt.core.JavaModelException;
30 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
31 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
32 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer;
33 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
34 import org.eclipse.jdt.launching.JavaRuntime;
35 import org.eclipse.jdt.ui.JavaElementLabelProvider;
36 import org.eclipse.jface.viewers.ILabelProvider;
37 import org.eclipse.jface.viewers.IStructuredContentProvider;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39 import org.eclipse.jface.viewers.Viewer;
40 import org.eclipse.jface.window.Window;
41
42 /**
43  * Adds a project to the runtime class path.
44  */

45 public class AddProjectAction extends RuntimeClasspathAction {
46     
47     class ContentProvider implements IStructuredContentProvider {
48         
49         private List JavaDoc fProjects;
50         
51         public ContentProvider(List JavaDoc projects) {
52             fProjects = projects;
53         }
54         
55         /**
56          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
57          */

58         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
59             return fProjects.toArray();
60         }
61
62         /**
63          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
64          */

65         public void dispose() {
66         }
67
68         /**
69          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
70          */

71         public void inputChanged(
72             Viewer viewer,
73             Object JavaDoc oldInput,
74             Object JavaDoc newInput) {
75         }
76
77     }
78
79     public AddProjectAction(IClasspathViewer viewer) {
80         super(ActionMessages.AddProjectAction_Add_Project_1, viewer);
81     }
82
83     /**
84      * Prompts for a project to add.
85      *
86      * @see IAction#run()
87      */

88     public void run() {
89         List JavaDoc projects = getPossibleAdditions();
90         
91         ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
92         IStructuredContentProvider content = new ContentProvider(projects);
93         ProjectSelectionDialog dialog= new ProjectSelectionDialog(getShell(),projects, content, labelProvider, ActionMessages.AddProjectAction_Choose__project_s__to_add__3);
94         dialog.setTitle(ActionMessages.AddProjectAction_Project_Selection_2);
95         MultiStatus status = new MultiStatus(JDIDebugUIPlugin.getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, ActionMessages.AddProjectAction_One_or_more_exceptions_occurred_while_adding_projects__1, null);
96                 
97         if (dialog.open() == Window.OK) {
98             Object JavaDoc[] selections = dialog.getResult();
99             
100             List JavaDoc additions = new ArrayList JavaDoc(selections.length);
101             try {
102                 for (int i = 0; i < selections.length; i++) {
103                     IJavaProject jp = (IJavaProject)selections[i];
104                     if (dialog.isAddRequiredProjects()) {
105                         collectRequiredProjects(jp, additions);
106                     } else {
107                         additions.add(jp);
108                     }
109                 }
110             } catch (JavaModelException e) {
111                 status.add(e.getStatus());
112             }
113             
114             List JavaDoc runtimeEntries = new ArrayList JavaDoc(additions.size());
115             Iterator JavaDoc iter = additions.iterator();
116             while (iter.hasNext()) {
117                 IJavaProject jp = (IJavaProject)iter.next();
118                 runtimeEntries.add(JavaRuntime.newProjectRuntimeClasspathEntry(jp));
119                 if (dialog.isAddExportedEntries()) {
120                     try {
121                         collectExportedEntries(jp, runtimeEntries);
122                     } catch (CoreException e) {
123                         status.add(e.getStatus());
124                     }
125                 }
126             }
127             IRuntimeClasspathEntry[] entries = (IRuntimeClasspathEntry[])runtimeEntries.toArray(new IRuntimeClasspathEntry[runtimeEntries.size()]);
128             getViewer().addEntries(entries);
129         }
130         
131         content.dispose();
132         labelProvider.dispose();
133         
134         if (!status.isOK()) {
135             JDIDebugUIPlugin.statusDialog(status);
136         }
137     }
138
139     /**
140      * @see SelectionListenerAction#updateSelection(IStructuredSelection)
141      */

142     protected boolean updateSelection(IStructuredSelection selection) {
143         return getViewer().updateSelection(getActionType(), selection) && !getPossibleAdditions().isEmpty();
144     }
145     
146     protected int getActionType() {
147         return ADD;
148     }
149     
150     /**
151      * Returns the possible projects that can be added
152      */

153     protected List JavaDoc getPossibleAdditions() {
154         IJavaProject[] projects;
155         IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
156         try {
157             projects= JavaCore.create(root).getJavaProjects();
158         } catch (JavaModelException e) {
159             JDIDebugUIPlugin.log(e);
160             projects= new IJavaProject[0];
161         }
162         List JavaDoc remaining = new ArrayList JavaDoc();
163         for (int i = 0; i < projects.length; i++) {
164             remaining.add(projects[i]);
165         }
166         List JavaDoc alreadySelected = new ArrayList JavaDoc();
167         IRuntimeClasspathEntry[] entries = getViewer().getEntries();
168         for (int i = 0; i < entries.length; i++) {
169             if (entries[i].getType() == IRuntimeClasspathEntry.PROJECT) {
170                 IResource res = root.findMember(entries[i].getPath());
171                 IJavaProject jp = (IJavaProject)JavaCore.create(res);
172                 alreadySelected.add(jp);
173             }
174         }
175         remaining.removeAll(alreadySelected);
176         return remaining;
177     }
178     
179     /**
180      * Adds all projects required by <code>proj</code> to the list
181      * <code>res</code>
182      *
183      * @param proj the project for which to compute required
184      * projects
185      * @param res the list to add all required projects too
186      */

187     protected void collectRequiredProjects(IJavaProject proj, List JavaDoc res) throws JavaModelException {
188         if (!res.contains(proj)) {
189             res.add(proj);
190             
191             IJavaModel model= proj.getJavaModel();
192             
193             IClasspathEntry[] entries= proj.getRawClasspath();
194             for (int i= 0; i < entries.length; i++) {
195                 IClasspathEntry curr= entries[i];
196                 if (curr.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
197                     IJavaProject ref= model.getJavaProject(curr.getPath().segment(0));
198                     if (ref.exists()) {
199                         collectRequiredProjects(ref, res);
200                     }
201                 }
202             }
203         }
204     }
205     
206     /**
207      * Adds all exported entries defined by <code>proj</code> to the list
208      * <code>runtimeEntries</code>.
209      *
210      * @param proj
211      * @param runtimeEntries
212      * @throws JavaModelException
213      */

214     protected void collectExportedEntries(IJavaProject proj, List JavaDoc runtimeEntries) throws CoreException {
215         IClasspathEntry[] entries = proj.getRawClasspath();
216         for (int i = 0; i < entries.length; i++) {
217             IClasspathEntry entry = entries[i];
218             if (entry.isExported()) {
219                 IRuntimeClasspathEntry rte = null;
220                 switch (entry.getEntryKind()) {
221                     case IClasspathEntry.CPE_CONTAINER:
222                         IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), proj);
223                         int kind = 0;
224                         switch (container.getKind()) {
225                             case IClasspathContainer.K_APPLICATION:
226                                 kind = IRuntimeClasspathEntry.USER_CLASSES;
227                                 break;
228                             case IClasspathContainer.K_SYSTEM:
229                                 kind = IRuntimeClasspathEntry.BOOTSTRAP_CLASSES;
230                                 break;
231                             case IClasspathContainer.K_DEFAULT_SYSTEM:
232                                 kind = IRuntimeClasspathEntry.STANDARD_CLASSES;
233                                 break;
234                         }
235                         rte = JavaRuntime.newRuntimeContainerClasspathEntry(entry.getPath(), kind, proj);
236                         break;
237                     case IClasspathEntry.CPE_LIBRARY:
238                         rte = JavaRuntime.newArchiveRuntimeClasspathEntry(entry.getPath());
239                         rte.setSourceAttachmentPath(entry.getSourceAttachmentPath());
240                         rte.setSourceAttachmentRootPath(entry.getSourceAttachmentRootPath());
241                         break;
242                     case IClasspathEntry.CPE_PROJECT:
243                         String JavaDoc name = entry.getPath().segment(0);
244                         IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
245                         if (p.exists()) {
246                             IJavaProject jp = JavaCore.create(p);
247                             if (jp.exists()) {
248                                 rte = JavaRuntime.newProjectRuntimeClasspathEntry(jp);
249                             }
250                         }
251                         break;
252                     case IClasspathEntry.CPE_VARIABLE:
253                         rte = JavaRuntime.newVariableRuntimeClasspathEntry(entry.getPath());
254                         break;
255                     default:
256                         break;
257                 }
258                 if (rte != null) {
259                     if (!runtimeEntries.contains(rte)) {
260                         runtimeEntries.add(rte);
261                     }
262                 }
263             }
264         }
265     }
266 }
267
Popular Tags