Bug 304083: [build path] Source attachments wrong for automatically added libraries (UI changes)
diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/javadoc/JavaDocLocations.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/javadoc/JavaDocLocations.java
index 65a2750..e02ee76 100644
--- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/javadoc/JavaDocLocations.java
+++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/javadoc/JavaDocLocations.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -304,14 +304,16 @@
 
 		if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
 			IClasspathEntry entry= root.getRawClasspathEntry();
-			if (entry == null) {
-				return null;
-			}
-			if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
-				entry= getRealClasspathEntry(root.getJavaProject(), entry.getPath(), root.getPath());
-				if (entry == null) {
-					return null;
-				}
+			switch (entry.getEntryKind()) {
+				case IClasspathEntry.CPE_LIBRARY:
+				case IClasspathEntry.CPE_VARIABLE:
+					entry= root.getResolvedClasspathEntry();
+					break;
+				case IClasspathEntry.CPE_CONTAINER:
+					entry= getRealClasspathEntry(root.getJavaProject(), entry.getPath(), root.getPath());
+					if (entry == null) {
+						return null;
+					}
 			}
 			return getLibraryJavadocLocation(entry);
 		} else {
diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/util/JavaElementUtil.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/util/JavaElementUtil.java
index 2617109..59c311a 100644
--- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/util/JavaElementUtil.java
+++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/util/JavaElementUtil.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -151,6 +151,8 @@
 	 */
 	public static IJavaProject[] getReferencingProjects(IPackageFragmentRoot root) throws JavaModelException {
 		IClasspathEntry cpe= root.getRawClasspathEntry();
+		if (cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
+			cpe= root.getResolvedClasspathEntry();
 		IJavaProject[] allJavaProjects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
 		List result= new ArrayList(allJavaProjects.length);
 		for (int i= 0; i < allJavaProjects.length; i++) {
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javadocexport/JavadocLinkRef.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javadocexport/JavadocLinkRef.java
index 4f62864..5e00e42 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javadocexport/JavadocLinkRef.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javadocexport/JavadocLinkRef.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -65,7 +65,7 @@
 			String location= url != null ? url.toExternalForm() : null;
 			element.setAttribute(CPListElement.JAVADOC, location);
 			String[] changedAttributes= { CPListElement.JAVADOC };
-			BuildPathSupport.modifyClasspathEntry(null, element.getClasspathEntry(), changedAttributes, fProject, fContainerPath, monitor);
+			BuildPathSupport.modifyClasspathEntry(null, element.getClasspathEntry(), changedAttributes, fProject, fContainerPath, fClasspathEntry.getReferencingEntry() != null, monitor);
 			fClasspathEntry= element.getClasspathEntry();
 		}
 	}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/ClassFileEditor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/ClassFileEditor.java
index 846c8eb..e14a090 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/ClassFileEditor.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/ClassFileEditor.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -215,7 +215,7 @@
 		private void createSourceAttachmentControls(Composite composite, IPackageFragmentRoot root) throws JavaModelException {
 			IClasspathEntry entry;
 			try {
-				entry= root.getRawClasspathEntry();
+				entry= JavaModelUtil.getClasspathEntry(root);
 			} catch (JavaModelException ex) {
 				if (ex.isDoesNotExist())
 					entry= null;
@@ -282,7 +282,7 @@
 					try {
 						IClasspathEntry result= BuildPathDialogAccess.configureSourceAttachment(shell, entry);
 						if (result != null) {
-							applySourceAttachment(shell, result, jproject, containerPath);
+							applySourceAttachment(shell, result, jproject, containerPath, entry.getReferencingEntry() != null);
 							verifyInput(getEditorInput());
 						}
 					} catch (CoreException e) {
@@ -296,9 +296,9 @@
 			};
 		}
 
-		protected void applySourceAttachment(Shell shell, IClasspathEntry newEntry, IJavaProject project, IPath containerPath) {
+		protected void applySourceAttachment(Shell shell, IClasspathEntry newEntry, IJavaProject project, IPath containerPath, boolean isReferencedEntry) {
 			try {
-				IRunnableWithProgress runnable= SourceAttachmentBlock.getRunnable(shell, newEntry, project, containerPath);
+				IRunnableWithProgress runnable= SourceAttachmentBlock.getRunnable(shell, newEntry, project, containerPath, isReferencedEntry);
 				PlatformUI.getWorkbench().getProgressService().run(true, true, runnable);
 
 			} catch (InvocationTargetException e) {
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditorBreadcrumb.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditorBreadcrumb.java
index 65f9504..1fc3ff5 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditorBreadcrumb.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditorBreadcrumb.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2008 IBM Corporation and others.
+ * Copyright (c) 2008, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -66,11 +66,13 @@
 import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.core.JavaModelException;
 
+import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
+
 import org.eclipse.jdt.ui.IWorkingCopyProvider;
 import org.eclipse.jdt.ui.JavaElementComparator;
 import org.eclipse.jdt.ui.JavaElementLabels;
-import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
 import org.eclipse.jdt.ui.ProblemsLabelDecorator.ProblemsLabelChangedEvent;
+import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
 
 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
 import org.eclipse.jdt.internal.ui.JavaPlugin;
@@ -612,7 +614,7 @@
 				IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
 				for (int i= 0; i < roots.length; i++) {
 					IPackageFragmentRoot root= roots[i];
-					IClasspathEntry classpathEntry= root.getRawClasspathEntry();
+					IClasspathEntry classpathEntry= JavaModelUtil.getClasspathEntry(root);
 					int entryKind= classpathEntry.getEntryKind();
 					if (entryKind == IClasspathEntry.CPE_CONTAINER) {
 						// all ClassPathContainers are added later
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavadocConfigurationPropertyPage.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavadocConfigurationPropertyPage.java
index 4a1b6e2..cb0bc64 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavadocConfigurationPropertyPage.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavadocConfigurationPropertyPage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -86,7 +86,7 @@
 			if (elem instanceof IPackageFragmentRoot && ((IPackageFragmentRoot) elem).getKind() == IPackageFragmentRoot.K_BINARY) {
 				IPackageFragmentRoot root= (IPackageFragmentRoot) elem;
 
-				IClasspathEntry entry= root.getRawClasspathEntry();
+				IClasspathEntry entry= JavaModelUtil.getClasspathEntry(root);
 				if (entry == null) {
 					fIsValidElement= false;
 					setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsIncorrectElement_description);
@@ -241,7 +241,7 @@
 						cpElem.setAttribute(CPListElement.JAVADOC, loc);
 						IClasspathEntry newEntry= cpElem.getClasspathEntry();
 						String[] changedAttributes= { CPListElement.JAVADOC };
-						BuildPathSupport.modifyClasspathEntry(shell, newEntry, changedAttributes, project, containerPath, monitor);
+						BuildPathSupport.modifyClasspathEntry(shell, newEntry, changedAttributes, project, containerPath, entry.getReferencingEntry() != null, monitor);
 					} else {
 						JavaUI.setProjectJavadocLocation(project, javadocLocation);
 					}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/NativeLibrariesPropertyPage.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/NativeLibrariesPropertyPage.java
index 7f846b5..0d6632f 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/NativeLibrariesPropertyPage.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/NativeLibrariesPropertyPage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -64,6 +64,7 @@
 	private boolean fIsValidElement;
 	private IClasspathEntry fEntry;
 	private IPath fContainerPath;
+	private String fInitialNativeLibPath;
 
 	/**
 	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
@@ -74,7 +75,7 @@
 			if (elem instanceof IPackageFragmentRoot) {
 				IPackageFragmentRoot root= (IPackageFragmentRoot) elem;
 
-				IClasspathEntry entry= root.getRawClasspathEntry();
+				IClasspathEntry entry= JavaModelUtil.getClasspathEntry(root);
 				if (entry == null) {
 					fIsValidElement= false;
 					setDescription(PreferencesMessages.NativeLibrariesPropertyPage_invalidElementSelection_desription);
@@ -133,15 +134,15 @@
 		if (elem == null)
 			return new Composite(parent, SWT.NONE);
 
-		String nativeLibPath= null;
+		fInitialNativeLibPath= null;
 		IClasspathAttribute[] extraAttributes= fEntry.getExtraAttributes();
 		for (int i= 0; i < extraAttributes.length; i++) {
 			if (extraAttributes[i].getName().equals(JavaRuntime.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY)) {
-				nativeLibPath= extraAttributes[i].getValue();
+				fInitialNativeLibPath= extraAttributes[i].getValue();
 				break;
 			}
 		}
-		fConfigurationBlock= new NativeLibrariesConfigurationBlock(this, getShell(), nativeLibPath, fEntry);
+		fConfigurationBlock= new NativeLibrariesConfigurationBlock(this, getShell(), fInitialNativeLibPath, fEntry);
 		Control control= fConfigurationBlock.createContents(parent);
 
 		Dialog.applyDialogFont(control);
@@ -172,13 +173,14 @@
 	public boolean performOk() {
 		if (fConfigurationBlock != null) {
 			String nativeLibraryPath= fConfigurationBlock.getNativeLibraryPath();
-			if (nativeLibraryPath == null) {
-				return true;//no change
+			if (nativeLibraryPath == null && fInitialNativeLibPath == null
+					|| nativeLibraryPath != null && fInitialNativeLibPath != null && nativeLibraryPath.equals(fInitialNativeLibPath)) {
+				return true; //no change
 			}
 
 			IJavaElement elem= getJavaElement();
 			try {
-				IRunnableWithProgress runnable= getRunnable(getShell(), elem, nativeLibraryPath, fEntry, fContainerPath);
+				IRunnableWithProgress runnable= getRunnable(getShell(), elem, nativeLibraryPath, fEntry, fContainerPath, fEntry.getReferencingEntry() != null);
 				PlatformUI.getWorkbench().getProgressService().run(true, true, runnable);
 			} catch (InvocationTargetException e) {
 				String title= PreferencesMessages.NativeLibrariesPropertyPage_errorAttaching_title;
@@ -193,7 +195,7 @@
 		return true;
 	}
 
-	private static IRunnableWithProgress getRunnable(final Shell shell, final IJavaElement elem, final String nativeLibraryPath, final IClasspathEntry entry, final IPath containerPath) {
+	private static IRunnableWithProgress getRunnable(final Shell shell, final IJavaElement elem, final String nativeLibraryPath, final IClasspathEntry entry, final IPath containerPath, final boolean isReferencedEntry) {
 		return new IRunnableWithProgress() {
 			public void run(IProgressMonitor monitor) throws InvocationTargetException {
 				try {
@@ -203,7 +205,7 @@
 						cpElem.setAttribute(CPListElement.NATIVE_LIB_PATH, nativeLibraryPath);
 						IClasspathEntry newEntry= cpElem.getClasspathEntry();
 						String[] changedAttributes= { CPListElement.NATIVE_LIB_PATH };
-						BuildPathSupport.modifyClasspathEntry(shell, newEntry, changedAttributes, project, containerPath, monitor);
+						BuildPathSupport.modifyClasspathEntry(shell, newEntry, changedAttributes, project, containerPath, isReferencedEntry,  monitor);
 					}
 				} catch (CoreException e) {
 					throw new InvocationTargetException(e);
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/SourceAttachmentPropertyPage.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/SourceAttachmentPropertyPage.java
index ca5b765..f98a0e8 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/SourceAttachmentPropertyPage.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/SourceAttachmentPropertyPage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -19,7 +19,6 @@
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 
-import org.eclipse.core.runtime.Assert;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IPath;
@@ -96,30 +95,24 @@
 
 			IPath containerPath= null;
 			IJavaProject jproject= fRoot.getJavaProject();
-			IClasspathEntry entry= fRoot.getRawClasspathEntry();
-			if (entry == null) {
-				// use a dummy entry to use for initialization
-				entry= JavaCore.newLibraryEntry(fRoot.getPath(), null, null);
-			} else {
-				if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
-					containerPath= entry.getPath();
-					ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
-					IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
-					if (initializer == null || container == null) {
-						return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_invalid_container, BasicElementLabels.getPathLabel(containerPath, false)));
-					}
-					String containerName= container.getDescription();
-
-					IStatus status= initializer.getSourceAttachmentStatus(containerPath, jproject);
-					if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
-						return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_not_supported, containerName));
-					}
-					if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
-						return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_read_only, containerName));
-					}
-					entry= JavaModelUtil.findEntryInContainer(container, fRoot.getPath());
-					Assert.isNotNull(entry);
+			IClasspathEntry entry= JavaModelUtil.getClasspathEntry(fRoot);
+			if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
+				containerPath= entry.getPath();
+				ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
+				IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
+				if (initializer == null || container == null) {
+					return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_invalid_container, BasicElementLabels.getPathLabel(containerPath, false)));
 				}
+				String containerName= container.getDescription();
+
+				IStatus status= initializer.getSourceAttachmentStatus(containerPath, jproject);
+				if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
+					return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_not_supported, containerName));
+				}
+				if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
+					return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_read_only, containerName));
+				}
+				entry= JavaModelUtil.findEntryInContainer(container, fRoot.getPath());
 			}
 			fContainerPath= containerPath;
 			fEntry= entry;
@@ -161,7 +154,7 @@
 					return true; // no change
 				}
 
-				IRunnableWithProgress runnable= SourceAttachmentBlock.getRunnable(getShell(), entry, fRoot.getJavaProject(), fContainerPath);
+				IRunnableWithProgress runnable= SourceAttachmentBlock.getRunnable(getShell(), entry, fRoot.getJavaProject(), fContainerPath, fEntry.getReferencingEntry() != null);
 				PlatformUI.getWorkbench().getProgressService().run(true, true, runnable);
 			} catch (InvocationTargetException e) {
 				String title= PreferencesMessages.SourceAttachmentPropertyPage_error_title;
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/BuildPathSupport.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/BuildPathSupport.java
index 5adeeab..5d16aac 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/BuildPathSupport.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/BuildPathSupport.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -192,31 +192,20 @@
 	 * @param changedAttributes The attributes that have changed. See {@link CPListElement} for constants values.
 	 * @param jproject Project where the entry belongs to
 	 * @param containerPath The path of the entry's parent container or <code>null</code> if the entry is not in a container
+	 * @param isReferencedEntry <code>true</code> iff the entry has a {@link IClasspathEntry#getReferencingEntry() referencing entry}
 	 * @param monitor The progress monitor to use
 	 * @throws CoreException if the update failed
 	 */
-	public static void modifyClasspathEntry(Shell shell, IClasspathEntry newEntry, String[] changedAttributes, IJavaProject jproject, IPath containerPath, IProgressMonitor monitor) throws CoreException {
+	public static void modifyClasspathEntry(Shell shell, IClasspathEntry newEntry, String[] changedAttributes, IJavaProject jproject, IPath containerPath, boolean isReferencedEntry, IProgressMonitor monitor) throws CoreException {
 		if (containerPath != null) {
 			updateContainerClasspath(jproject, containerPath, newEntry, changedAttributes, monitor);
+		} else if (isReferencedEntry) {
+			updateReferencedClasspathEntry(jproject, newEntry, changedAttributes, monitor);
 		} else {
 			updateProjectClasspath(shell, jproject, newEntry, changedAttributes, monitor);
 		}
 	}
 
-
-	/**
-	 * Apply a modified classpath entry to the classpath. The classpath entry can also be from a classpath container.
-	 * @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
-	 * @param newEntry The modified entry. The entry's kind or path must be unchanged.
-	 * @param jproject Project where the entry belongs to
-	 * @param containerPath The path of the entry's parent container or <code>null</code> if the entry is not in a container
-	 * @param monitor The progress monitor to use
-	 * @throws CoreException if the update failed
-	 */
-	public static void modifyClasspathEntry(Shell shell, IClasspathEntry newEntry, IJavaProject jproject, IPath containerPath, IProgressMonitor monitor) throws CoreException {
-		modifyClasspathEntry(shell, newEntry, null, jproject, containerPath, monitor);
-	}
-
 	private static void updateContainerClasspath(IJavaProject jproject, IPath containerPath, IClasspathEntry newEntry, String[] changedAttributes, IProgressMonitor monitor) throws CoreException {
 		IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
 		if (container == null) {
@@ -310,6 +299,39 @@
 	}
 
 	/**
+	 * Apply a modified referenced classpath entry to the classpath.
+	 * @param newReferencedEntry the modified entry. The entry's kind or path must be unchanged.
+	 * @param changedAttributes the attributes that have changed. See {@link CPListElement} for constants values.
+	 * @param jproject project where the entry belongs to
+	 * @param monitor the progress monitor to use
+	 * @throws CoreException if the update failed
+	 */
+	private static void updateReferencedClasspathEntry(IJavaProject jproject, IClasspathEntry newReferencedEntry, String[] changedAttributes, IProgressMonitor monitor) throws CoreException {
+		IClasspathEntry[] oldReferencedClasspath= jproject.getReferencedClasspathEntries();
+		int nEntries= oldReferencedClasspath.length;
+		ArrayList newReferencedEntries= new ArrayList(nEntries + 1);
+		int entryKind= newReferencedEntry.getEntryKind();
+		IPath jarPath= newReferencedEntry.getPath();
+		boolean found= false;
+		for (int i= 0; i < nEntries; i++) {
+			IClasspathEntry curr= oldReferencedClasspath[i];
+			if (curr.getEntryKind() == entryKind && curr.getPath().equals(jarPath)) {
+				// add modified entry
+				newReferencedEntries.add(getUpdatedEntry(curr, newReferencedEntry, changedAttributes, jproject));
+				found= true;
+			} else {
+				newReferencedEntries.add(curr);
+			}
+		}
+		if (!found) {
+			newReferencedEntries.add(newReferencedEntry);
+		}
+		IClasspathEntry[] newReferencedClasspath= (IClasspathEntry[]) newReferencedEntries.toArray(new IClasspathEntry[newReferencedEntries.size()]);
+		
+		jproject.setRawClasspath(jproject.getRawClasspath(), newReferencedClasspath, jproject.getOutputLocation(), monitor);
+	}
+	
+	/**
 	 * Sets the default compiler compliance options iff <code>modifiedClassPathEntries</code>
 	 * contains a classpath container entry that is modified or new and that points to an execution
 	 * environment. Does nothing if the EE or the options could not be resolved.
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/LibrariesWorkbookPage.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/LibrariesWorkbookPage.java
index 3614f18..f4767bc 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/LibrariesWorkbookPage.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/LibrariesWorkbookPage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -532,7 +532,7 @@
 		try {
 			IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
 				public void run(IProgressMonitor monitor) throws CoreException {
-					BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, jproject, containerPath, monitor);
+					BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, jproject, containerPath, false, monitor);
 				}
 			};
 			PlatformUI.getWorkbench().getProgressService().run(true, true, new WorkbenchRunnableAdapter(runnable));
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/SourceAttachmentBlock.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/SourceAttachmentBlock.java
index d600a93..d439cc0 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/SourceAttachmentBlock.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/SourceAttachmentBlock.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -578,14 +578,15 @@
 	 * @param newEntry the new entry
 	 * @param jproject the Java project
 	 * @param containerPath the path of the parent container or <code>null</code> if the element is not in a container
+	 * @param isReferencedEntry <code>true</code> iff the entry has a {@link IClasspathEntry#getReferencingEntry() referencing entry}
 	 * @return return the runnable
 	 */
-	public static IRunnableWithProgress getRunnable(final Shell shell, final IClasspathEntry newEntry, final IJavaProject jproject, final IPath containerPath) {
+	public static IRunnableWithProgress getRunnable(final Shell shell, final IClasspathEntry newEntry, final IJavaProject jproject, final IPath containerPath, final boolean isReferencedEntry) {
 		return new IRunnableWithProgress() {
 			public void run(IProgressMonitor monitor) throws InvocationTargetException {
 				try {
 					String[] changedAttributes= { CPListElement.SOURCEATTACHMENT };
-					BuildPathSupport.modifyClasspathEntry(shell, newEntry, changedAttributes, jproject, containerPath, monitor);
+					BuildPathSupport.modifyClasspathEntry(shell, newEntry, changedAttributes, jproject, containerPath, isReferencedEntry, monitor);
 				} catch (CoreException e) {
 					throw new InvocationTargetException(e);
 				}