*/
protected void executeOperation() throws JavaModelException {
try {
beginTask(Messages.operation_createUnitProgress, 2);
JavaElementDelta delta = newJavaElementDelta();
ICompilationUnit unit = getCompilationUnit();
IPackageFragment pkg = (IPackageFragment) getParentElement();
IContainer folder = (IContainer) pkg.getResource();
worked(1);
IFile compilationUnitFile = folder.getFile(new Path(fName));
if (compilationUnitFile.exists()) {
// update the contents of the existing unit if fForce is true
if (force) {
IBuffer buffer = unit.getBuffer();
if (buffer == null) return;
buffer.setContents(fSource);
unit.save(new NullProgressMonitor(), false);
resultElements = new IJavaElement[] {unit};
if (!Util.isExcluded(unit)
&& unit.getParent().exists()) {
for (int i = 0; i < resultElements.length; i++) {
delta.changed(resultElements[i], IJavaElementDelta.F_CONTENT);
}
addDelta(delta);
}
} else {
throw new JavaModelException(new JavaModelStatus(
IJavaModelStatusConstants.NAME_COLLISION,
Messages.bind(Messages.status_nameCollision, compilationUnitFile.getFullPath().toString())));
}
} else {
try {
String encoding = null;
try {
encoding = folder.getDefaultCharset(); // get folder encoding as file is not accessible
}
catch (CoreException ce) {
// use no encoding
}
InputStream stream = new ByteArrayInputStream(encoding == null ? fSource.getBytes() : fSource.getBytes(encoding));
createFile(folder, unit.getElementName(), stream, force);
resultElements = new IJavaElement[] {unit};
if (!Util.isExcluded(unit)
&& unit.getParent().exists()) {
for (int i = 0; i < resultElements.length; i++) {
delta.added(resultElements[i]);
}
addDelta(delta);
}