KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > UserLibraryClasspathContainerInitializer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.jdt.core.ClasspathContainerInitializer;
16 import org.eclipse.jdt.core.IClasspathContainer;
17 import org.eclipse.jdt.core.IJavaProject;
18 import org.eclipse.jdt.core.JavaCore;
19 import org.eclipse.jdt.internal.core.util.Util;
20
21 /**
22  *
23  */

24 public class UserLibraryClasspathContainerInitializer extends ClasspathContainerInitializer {
25
26     /* (non-Javadoc)
27      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#canUpdateClasspathContainer(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
28      */

29     public boolean canUpdateClasspathContainer(IPath containerPath, IJavaProject project) {
30         return isUserLibraryContainer(containerPath);
31     }
32
33     /* (non-Javadoc)
34      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getComparisonID(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
35      */

36     public Object JavaDoc getComparisonID(IPath containerPath, IJavaProject project) {
37         return containerPath;
38     }
39     
40     /**
41      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#getDescription(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject)
42      */

43     public String JavaDoc getDescription(IPath containerPath, IJavaProject project) {
44         if (isUserLibraryContainer(containerPath)) {
45             return containerPath.segment(1);
46         }
47         return super.getDescription(containerPath, project);
48     }
49
50     public void initialize(IPath containerPath, IJavaProject project) throws CoreException {
51         if (isUserLibraryContainer(containerPath)) {
52             String JavaDoc userLibName = containerPath.segment(1);
53             UserLibrary userLibrary = JavaModelManager.getUserLibraryManager().getUserLibrary(userLibName);
54             if (userLibrary != null) {
55                 UserLibraryClasspathContainer container = new UserLibraryClasspathContainer(userLibName);
56                 JavaCore.setClasspathContainer(containerPath, new IJavaProject[] { project }, new IClasspathContainer[] { container }, null);
57             } else if (JavaModelManager.CP_RESOLVE_VERBOSE) {
58                 verbose_no_user_library_found(project, userLibName);
59             }
60         } else if (JavaModelManager.CP_RESOLVE_VERBOSE) {
61             verbose_not_a_user_library(project, containerPath);
62         }
63     }
64     
65     private boolean isUserLibraryContainer(IPath path) {
66         return path != null && path.segmentCount() == 2 && JavaCore.USER_LIBRARY_CONTAINER_ID.equals(path.segment(0));
67     }
68     
69     /**
70      * @see org.eclipse.jdt.core.ClasspathContainerInitializer#requestClasspathContainerUpdate(org.eclipse.core.runtime.IPath, org.eclipse.jdt.core.IJavaProject, org.eclipse.jdt.core.IClasspathContainer)
71      */

72     public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project, IClasspathContainer containerSuggestion) throws CoreException {
73         if (isUserLibraryContainer(containerPath)) {
74             String JavaDoc name = containerPath.segment(1);
75             if (containerSuggestion != null) {
76                 JavaModelManager.getUserLibraryManager().setUserLibrary(name, containerSuggestion.getClasspathEntries(), containerSuggestion.getKind() == IClasspathContainer.K_SYSTEM);
77             } else {
78                 JavaModelManager.getUserLibraryManager().removeUserLibrary(name);
79             }
80             // update of affected projects was done as a consequence of setUserLibrary() or removeUserLibrary()
81
}
82     }
83
84     private void verbose_no_user_library_found(IJavaProject project, String JavaDoc userLibraryName) {
85         Util.verbose(
86             "UserLibrary INIT - FAILED (no user library found)\n" + //$NON-NLS-1$
87
" project: " + project.getElementName() + '\n' + //$NON-NLS-1$
88
" userLibraryName: " + userLibraryName); //$NON-NLS-1$
89
}
90     
91     private void verbose_not_a_user_library(IJavaProject project, IPath containerPath) {
92         Util.verbose(
93             "UserLibrary INIT - FAILED (not a user library)\n" + //$NON-NLS-1$
94
" project: " + project.getElementName() + '\n' + //$NON-NLS-1$
95
" container path: " + containerPath); //$NON-NLS-1$
96
}
97 }
98
Popular Tags