KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > BuildPathSupport


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.ui.wizards.buildpaths;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20
21 import org.eclipse.core.resources.ResourcesPlugin;
22
23 import org.eclipse.swt.widgets.Shell;
24
25 import org.eclipse.jface.dialogs.MessageDialog;
26
27 import org.eclipse.jdt.core.ClasspathContainerInitializer;
28 import org.eclipse.jdt.core.IClasspathAttribute;
29 import org.eclipse.jdt.core.IClasspathContainer;
30 import org.eclipse.jdt.core.IClasspathEntry;
31 import org.eclipse.jdt.core.IJavaModel;
32 import org.eclipse.jdt.core.IJavaProject;
33 import org.eclipse.jdt.core.JavaCore;
34 import org.eclipse.jdt.core.JavaModelException;
35
36 import org.eclipse.jdt.internal.corext.util.Messages;
37
38 import org.eclipse.jdt.ui.JavaUI;
39
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
42
43 /**
44  *
45  */

46 public class BuildPathSupport {
47     
48     public static final String JavaDoc JRE_PREF_PAGE_ID= "org.eclipse.jdt.debug.ui.preferences.VMPreferencePage"; //$NON-NLS-1$
49

50     
51     private BuildPathSupport() {
52         super();
53     }
54     
55     /**
56      * Returns a deprecation message for a classpath variable name.
57      *
58      * @param variableName classpath variable name
59      * @return the deprecation message, or <code>null</code> iff
60      * <code>variableName</code> is not a classpath variable or the
61      * variable is not deprecated
62      */

63     public static String JavaDoc getDeprecationMessage(String JavaDoc variableName) {
64         String JavaDoc deprecationMessage= JavaCore.getClasspathVariableDeprecationMessage(variableName);
65         if (deprecationMessage == null )
66             return null;
67         else
68             return Messages.format(NewWizardMessages.BuildPathSupport_deprecated,
69                     new Object JavaDoc[] {variableName, deprecationMessage});
70     }
71
72     /**
73      * Finds a source attachment for a new archive in the existing classpaths.
74      * @param elem The new classpath entry
75      * @return A path to be taken for the source attachment or <code>null</code>
76      */

77     public static IPath guessSourceAttachment(CPListElement elem) {
78         if (elem.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
79             return null;
80         }
81         IJavaProject currProject= elem.getJavaProject(); // can be null
82
try {
83             // try if the jar itself contains the source
84
IJavaModel jmodel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
85             IJavaProject[] jprojects= jmodel.getJavaProjects();
86             for (int i= 0; i < jprojects.length; i++) {
87                 IJavaProject curr= jprojects[i];
88                 if (!curr.equals(currProject)) {
89                     IClasspathEntry[] entries= curr.getRawClasspath();
90                     for (int k= 0; k < entries.length; k++) {
91                         IClasspathEntry entry= entries[k];
92                         if (entry.getEntryKind() == elem.getEntryKind()
93                             && entry.getPath().equals(elem.getPath())) {
94                             IPath attachPath= entry.getSourceAttachmentPath();
95                             if (attachPath != null && !attachPath.isEmpty()) {
96                                 return attachPath;
97                             }
98                         }
99                     }
100                 }
101             }
102         } catch (JavaModelException e) {
103             JavaPlugin.log(e.getStatus());
104         }
105         return null;
106     }
107     
108     /**
109      * Finds a javadoc location for a new archive in the existing classpaths.
110      * @param elem The new classpath entry
111      * @return A javadoc location found in a similar classpath entry or <code>null</code>.
112      */

113     public static String JavaDoc guessJavadocLocation(CPListElement elem) {
114         if (elem.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
115             return null;
116         }
117         IJavaProject currProject= elem.getJavaProject(); // can be null
118
try {
119             // try if the jar itself contains the source
120
IJavaModel jmodel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
121             IJavaProject[] jprojects= jmodel.getJavaProjects();
122             for (int i= 0; i < jprojects.length; i++) {
123                 IJavaProject curr= jprojects[i];
124                 if (!curr.equals(currProject)) {
125                     IClasspathEntry[] entries= curr.getRawClasspath();
126                     for (int k= 0; k < entries.length; k++) {
127                         IClasspathEntry entry= entries[k];
128                         if (entry.getEntryKind() == elem.getEntryKind() && entry.getPath().equals(elem.getPath())) {
129                             IClasspathAttribute[] attributes= entry.getExtraAttributes();
130                             for (int n= 0; n < attributes.length; n++) {
131                                 IClasspathAttribute attrib= attributes[n];
132                                 if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attrib.getName())) {
133                                     return attrib.getValue();
134                                 }
135                             }
136                         }
137                     }
138                 }
139             }
140         } catch (JavaModelException e) {
141             JavaPlugin.log(e.getStatus());
142         }
143         return null;
144     }
145     
146     private static class UpdatedClasspathContainer implements IClasspathContainer {
147
148         private IClasspathEntry[] fNewEntries;
149         private IClasspathContainer fOriginal;
150
151         public UpdatedClasspathContainer(IClasspathContainer original, IClasspathEntry[] newEntries) {
152             fNewEntries= newEntries;
153             fOriginal= original;
154         }
155
156         public IClasspathEntry[] getClasspathEntries() {
157             return fNewEntries;
158         }
159
160         public String JavaDoc getDescription() {
161             return fOriginal.getDescription();
162         }
163
164         public int getKind() {
165             return fOriginal.getKind();
166         }
167
168         public IPath getPath() {
169             return fOriginal.getPath();
170         }
171     }
172
173     /**
174      * Apply a modified classpath entry to the classpath. The classpath entry can also be from a classpath container.
175      * @param shell If not null and the entry could not be found on the projects classpath, a dialog will ask to put the entry on the classpath
176      * @param newEntry The modified entry. The entry's kind or path must be unchanged.
177      * @param changedAttributes The attibutes that have changed. See {@link CPListElement} for constants values.
178      * @param jproject Project where the entry belongs to
179      * @param containerPath The path of the entry's parent container or <code>null</code> if the entry is not in a container
180      * @param monitor The progress monitor to use
181      * @throws CoreException
182      */

183     public static void modifyClasspathEntry(Shell shell, IClasspathEntry newEntry, String JavaDoc[] changedAttributes, IJavaProject jproject, IPath containerPath, IProgressMonitor monitor) throws CoreException {
184         if (containerPath != null) {
185             updateContainerClasspath(jproject, containerPath, newEntry, changedAttributes, monitor);
186         } else {
187             updateProjectClasspath(shell, jproject, newEntry, changedAttributes, monitor);
188         }
189     }
190     
191     
192     /**
193      * Apply a modified classpath entry to the classpath. The classpath entry can also be from a classpath container.
194      * @param shell If not null and the entry could not be found on the projects classpath, a dialog will ask to put the entry on the classpath
195      * @param newEntry The modified entry. The entry's kind or path must be unchanged.
196      * @param jproject Project where the entry belongs to
197      * @param containerPath The path of the entry's parent container or <code>null</code> if the entry is not in a container
198      * @param monitor The progress monitor to use
199      * @throws CoreException
200      */

201     public static void modifyClasspathEntry(Shell shell, IClasspathEntry newEntry, IJavaProject jproject, IPath containerPath, IProgressMonitor monitor) throws CoreException {
202         modifyClasspathEntry(shell, newEntry, null, jproject, containerPath, monitor);
203     }
204
205     private static void updateContainerClasspath(IJavaProject jproject, IPath containerPath, IClasspathEntry newEntry, String JavaDoc[] changedAttributes, IProgressMonitor monitor) throws CoreException {
206         IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
207         if (container == null) {
208             throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, "Container " + containerPath + " cannot be resolved", null)); //$NON-NLS-1$//$NON-NLS-2$
209
}
210         IClasspathEntry[] entries= container.getClasspathEntries();
211         IClasspathEntry[] newEntries= new IClasspathEntry[entries.length];
212         for (int i= 0; i < entries.length; i++) {
213             IClasspathEntry curr= entries[i];
214             if (curr.getEntryKind() == newEntry.getEntryKind() && curr.getPath().equals(newEntry.getPath())) {
215                 newEntries[i]= getUpdatedEntry(curr, newEntry, changedAttributes, jproject);
216             } else {
217                 newEntries[i]= curr;
218             }
219         }
220         requestContainerUpdate(jproject, container, newEntries);
221         monitor.worked(1);
222     }
223
224     private static IClasspathEntry getUpdatedEntry(IClasspathEntry currEntry, IClasspathEntry updatedEntry, String JavaDoc[] updatedAttributes, IJavaProject jproject) {
225         if (updatedAttributes == null) {
226             return updatedEntry; // used updated entry 'as is'
227
}
228         CPListElement currElem= CPListElement.createFromExisting(currEntry, jproject);
229         CPListElement newElem= CPListElement.createFromExisting(updatedEntry, jproject);
230         for (int i= 0; i < updatedAttributes.length; i++) {
231             String JavaDoc attrib= updatedAttributes[i];
232             currElem.setAttribute(attrib, newElem.getAttribute(attrib));
233         }
234         return currElem.getClasspathEntry();
235     }
236
237     /**
238      * Request a container update.
239      * @param jproject The project of the container
240      * @param container The container to requesta change to
241      * @param newEntries The updated entries
242      * @throws CoreException
243      */

244     public static void requestContainerUpdate(IJavaProject jproject, IClasspathContainer container, IClasspathEntry[] newEntries) throws CoreException {
245         IPath containerPath= container.getPath();
246         IClasspathContainer updatedContainer= new UpdatedClasspathContainer(container, newEntries);
247         ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
248         if (initializer != null) {
249             initializer.requestClasspathContainerUpdate(containerPath, jproject, updatedContainer);
250         }
251     }
252
253     private static void updateProjectClasspath(Shell shell, IJavaProject jproject, IClasspathEntry newEntry, String JavaDoc[] changedAttributes, IProgressMonitor monitor) throws JavaModelException {
254         IClasspathEntry[] oldClasspath= jproject.getRawClasspath();
255         int nEntries= oldClasspath.length;
256         ArrayList JavaDoc newEntries= new ArrayList JavaDoc(nEntries + 1);
257         int entryKind= newEntry.getEntryKind();
258         IPath jarPath= newEntry.getPath();
259         boolean found= false;
260         for (int i= 0; i < nEntries; i++) {
261             IClasspathEntry curr= oldClasspath[i];
262             if (curr.getEntryKind() == entryKind && curr.getPath().equals(jarPath)) {
263                 // add modified entry
264
newEntries.add(getUpdatedEntry(curr, newEntry, changedAttributes, jproject));
265                 found= true;
266             } else {
267                 newEntries.add(curr);
268             }
269         }
270         if (!found) {
271             if (!putJarOnClasspathDialog(shell)) {
272                 return;
273             }
274             // add new
275
newEntries.add(newEntry);
276         }
277         IClasspathEntry[] newClasspath= (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);
278         jproject.setRawClasspath(newClasspath, monitor);
279     }
280     
281     private static boolean putJarOnClasspathDialog(final Shell shell) {
282         if (shell == null) {
283             return false;
284         }
285         
286         final boolean[] result= new boolean[1];
287         shell.getDisplay().syncExec(new Runnable JavaDoc() {
288             public void run() {
289                 String JavaDoc title= NewWizardMessages.BuildPathSupport_putoncpdialog_title;
290                 String JavaDoc message= NewWizardMessages.BuildPathSupport_putoncpdialog_message;
291                 result[0]= MessageDialog.openQuestion(shell, title, message);
292             }
293         });
294         return result[0];
295     }
296 }
297
Popular Tags