/** * Creates a compilation unit. * * @exception ModelException if unable to create the compilation unit. */ protected void executeOperation() throws ModelException { try { // beginTask(Messages.operation_createUnitProgress, 2); ModelElementDelta delta = newModelElementDelta(); ISourceModule unit = getSourceModule(); IScriptFolder pkg = (IScriptFolder) 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 IModelElement[] {unit}; if (!Util.isExcluded(unit) && unit.getParent().exists()) { for (int i = 0; i < resultElements.length; i++) { delta.changed(resultElements[i], IModelElementDelta.F_CONTENT); } addDelta(delta); } } else { throw new ModelException( new ModelStatus( IModelStatusConstants.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 IModelElement[] {unit}; if (!Util.isExcluded(unit) && unit.getParent().exists()) { for (int i = 0; i < resultElements.length; i++) { delta.added(resultElements[i]); } addDelta(delta); } } catch (IOException e) { throw new ModelException(e, IModelStatusConstants.IO_EXCEPTION); } } worked(1); } finally { done(); } }