blob: 1eedd1c67f3c0120f6cb7d098170fc780ba5a700 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2003, 2004 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jst.j2ee.internal.common;
import java.util.ArrayList;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.ClasspathEntry;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.wst.common.frameworks.internal.FlexibleJavaProjectPreferenceUtil;
/**
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class UpdateProjectClasspath {
public UpdateProjectClasspath(String sourceFolder, String componentName, IProject jProject){
addSrcFolderToProject(sourceFolder, componentName, jProject);
}
private IClasspathEntry[] getClasspathEntries(String sourceFolder, String componentName, IProject jProject) {
ArrayList list = new ArrayList();
list.add(JavaCore.newSourceEntry(jProject.getFullPath().append(sourceFolder)));
IClasspathEntry[] classpath = new IClasspathEntry[list.size()];
//adjust the output path to be bin/ComponentName
boolean isProjectMultiComponents = FlexibleJavaProjectPreferenceUtil.getMultipleModulesPerProjectProp();
IPath newOutputPath = null;
for (int i = 0; i < classpath.length; i++) {
classpath[i] = (IClasspathEntry) list.get(i);
if(isProjectMultiComponents)
newOutputPath = Path.fromOSString(Path.SEPARATOR +jProject.getName() + "/bin/" + componentName + Path.SEPARATOR);
else
newOutputPath = Path.fromOSString(Path.SEPARATOR +jProject.getName() + "/bin/");
((ClasspathEntry)classpath[i]).specificOutputLocation = newOutputPath;
}
return classpath;
}
private void addSrcFolderToProject(String sourceFolder,String componentName, IProject jProject) {
IJavaProject javaProject = JavaCore.create( jProject );
try {
IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
IClasspathEntry[] newEntries = getClasspathEntries(sourceFolder, componentName, jProject);
int oldSize = oldEntries.length;
int newSize = newEntries.length;
IClasspathEntry[] classpathEnties = new IClasspathEntry[oldSize + newSize];
int k = 0;
for (int i = 0; i < oldEntries.length; i++) {
classpathEnties[i] = oldEntries[i];
k++;
}
for( int j=0; j< newEntries.length; j++){
classpathEnties[k] = newEntries[j];
k++;
}
javaProject.setRawClasspath(classpathEnties, null);
}
catch (JavaModelException e) {
Logger.getLogger().logError(e);
}
}
}