X-Git-Url: http://git.phpeclipse.com
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/CommitWorkingCopyOperation.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/CommitWorkingCopyOperation.java
index 743484c..b03a1c5 100644
--- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/CommitWorkingCopyOperation.java
+++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/CommitWorkingCopyOperation.java
@@ -10,138 +10,197 @@
*******************************************************************************/
package net.sourceforge.phpdt.internal.core;
+import java.io.ByteArrayInputStream;
+import java.io.UnsupportedEncodingException;
+
import net.sourceforge.phpdt.core.IBuffer;
import net.sourceforge.phpdt.core.ICompilationUnit;
import net.sourceforge.phpdt.core.IJavaElement;
import net.sourceforge.phpdt.core.IJavaModelStatus;
import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
import net.sourceforge.phpdt.core.JavaModelException;
+import net.sourceforge.phpdt.internal.core.util.Util;
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
-
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.jobs.ISchedulingRule;
/**
- * Commits the contents of a working copy compilation
- * unit to its original element and resource, bringing
- * the Java Model up-to-date with the current contents of the working
- * copy.
- *
- *
It is possible that the contents of the
- * original resource have changed since the working copy was created,
- * in which case there is an update conflict. This operation allows
- * for two settings to resolve conflict set by the fForce
flag:
- * - force flag is
false
- in this case an JavaModelException
- * is thrown
- * - force flag is
true
- in this case the contents of
- * the working copy are applied to the underlying resource even though
- * the working copy was created before a subsequent change in the
- * resource
+ * Commits the contents of a working copy compilation unit to its original element and resource, bringing the Java Model up-to-date
+ * with the current contents of the working copy.
+ *
+ *
+ * It is possible that the contents of the original resource have changed since the working copy was created, in which case there is
+ * an update conflict. This operation allows for two settings to resolve conflict set by the fForce
flag:
+ *
+ * - force flag is
false
- in this case an JavaModelException
is thrown
+ * - force flag is
true
- in this case the contents of the working copy are applied to the underlying resource
+ * even though the working copy was created before a subsequent change in the resource
*
- *
- * The default conflict resolution setting is the force flag is false
- *
- * A JavaModelOperation exception is thrown either if the commit could not
- * be performed or if the new content of the compilation unit violates some Java Model
- * constraint (e.g. if the new package declaration doesn't match the name of the folder
- * containing the compilation unit).
+ *
+ *
+ * The default conflict resolution setting is the force flag is false
+ *
+ * A JavaModelOperation exception is thrown either if the commit could not be performed or if the new content of the compilation
+ * unit violates some Java Model constraint (e.g. if the new package declaration doesn't match the name of the folder containing the
+ * compilation unit).
*/
public class CommitWorkingCopyOperation extends JavaModelOperation {
- /**
- * Constructs an operation to commit the contents of a working copy
- * to its original compilation unit.
- */
- public CommitWorkingCopyOperation(ICompilationUnit element, boolean force) {
- super(new IJavaElement[] {element}, force);
- }
- /**
- * @exception JavaModelException if setting the source
- * of the original compilation unit fails
- */
- protected void executeOperation() throws JavaModelException {
- try {
- beginTask(Util.bind("workingCopy.commit"), 2); //$NON-NLS-1$
- WorkingCopy copy = (WorkingCopy)getCompilationUnit();
- ICompilationUnit original = (ICompilationUnit) copy.getOriginalElement();
-
-
- // creates the delta builder (this remembers the content of the cu)
- if (!original.isOpen()) {
- // force opening so that the delta builder can get the old info
- original.open(null);
- }
- JavaElementDeltaBuilder deltaBuilder;
- if (Util.isExcluded(original)) {
- deltaBuilder = null;
- } else {
- deltaBuilder = new JavaElementDeltaBuilder(original);
- }
-
- // save the cu
- IBuffer originalBuffer = original.getBuffer();
- if (originalBuffer == null) return;
- char[] originalContents = originalBuffer.getCharacters();
- boolean hasSaved = false;
- try {
- IBuffer copyBuffer = copy.getBuffer();
- if (copyBuffer == null) return;
- originalBuffer.setContents(copyBuffer.getCharacters());
- original.save(fMonitor, fForce);
- this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
- hasSaved = true;
- } finally {
- if (!hasSaved){
- // restore original buffer contents since something went wrong
- originalBuffer.setContents(originalContents);
- }
- }
- // make sure working copy is in sync
- copy.updateTimeStamp((CompilationUnit)original);
- copy.makeConsistent(this);
- worked(1);
-
- if (deltaBuilder != null) {
- // build the deltas
- deltaBuilder.buildDeltas();
-
- // add the deltas to the list of deltas created during this operation
- if (deltaBuilder.delta != null) {
- addDelta(deltaBuilder.delta);
- }
- }
- worked(1);
- } finally {
- done();
- }
- }
- /**
- * Returns the compilation unit this operation is working on.
- */
- protected ICompilationUnit getCompilationUnit() {
- return (ICompilationUnit)getElementToProcess();
- }
- /**
- * Possible failures:
- * - INVALID_ELEMENT_TYPES - the compilation unit supplied to this
- * operation is not a working copy
- *
- ELEMENT_NOT_PRESENT - the compilation unit the working copy is
- * based on no longer exists.
- *
- UPDATE_CONFLICT - the original compilation unit has changed since
- * the working copy was created and the operation specifies no force
- *
- READ_ONLY - the original compilation unit is in read-only mode
- *
- */
- public IJavaModelStatus verify() {
- ICompilationUnit cu = getCompilationUnit();
- if (!cu.isWorkingCopy()) {
- return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, cu);
- }
- ICompilationUnit original= (ICompilationUnit)cu.getOriginalElement();
- IResource resource = original.getResource();
- if (!cu.isBasedOn(resource) && !fForce) {
- return new JavaModelStatus(IJavaModelStatusConstants.UPDATE_CONFLICT);
- }
- // no read-only check, since some repository adapters can change the flag on save
- // operation.
- return JavaModelStatus.VERIFIED_OK;
- }
-}
+ /**
+ * Constructs an operation to commit the contents of a working copy to its original compilation unit.
+ */
+ public CommitWorkingCopyOperation(ICompilationUnit element, boolean force) {
+ super(new IJavaElement[] { element }, force);
+ }
+
+ /**
+ * @exception JavaModelException
+ * if setting the source of the original compilation unit fails
+ */
+ protected void executeOperation() throws JavaModelException {
+ try {
+ beginTask(Util.bind("workingCopy.commit"), 2); //$NON-NLS-1$
+ CompilationUnit workingCopy = getCompilationUnit();
+ IFile resource = (IFile) workingCopy.getResource();
+ ICompilationUnit primary = workingCopy.getPrimary();
+ boolean isPrimary = workingCopy.isPrimary();
+
+ JavaElementDeltaBuilder deltaBuilder = null;
+ // PackageFragmentRoot root = (PackageFragmentRoot)workingCopy.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
+ boolean isIncluded = !Util.isExcluded(workingCopy);
+ // if (isPrimary || (root.isOnClasspath() && isIncluded && resource.isAccessible() &&
+ // ProjectPrefUtil.isValidCompilationUnitName(workingCopy.getElementName()))) {
+ if (isPrimary || (isIncluded && resource.isAccessible() && Util.isValidCompilationUnitName(workingCopy.getElementName()))) {
+
+ // force opening so that the delta builder can get the old info
+ if (!isPrimary && !primary.isOpen()) {
+ primary.open(null);
+ }
+
+ // creates the delta builder (this remembers the content of the cu) if:
+ // - it is not excluded
+ // - and it is not a primary or it is a non-consistent primary
+ if (isIncluded && (!isPrimary || !workingCopy.isConsistent())) {
+ deltaBuilder = new JavaElementDeltaBuilder(primary);
+ }
+
+ // save the cu
+ IBuffer primaryBuffer = primary.getBuffer();
+ if (!isPrimary) {
+ if (primaryBuffer == null)
+ return;
+ char[] primaryContents = primaryBuffer.getCharacters();
+ boolean hasSaved = false;
+ try {
+ IBuffer workingCopyBuffer = workingCopy.getBuffer();
+ if (workingCopyBuffer == null)
+ return;
+ primaryBuffer.setContents(workingCopyBuffer.getCharacters());
+ primaryBuffer.save(this.progressMonitor, this.force);
+ primary.makeConsistent(this);
+ hasSaved = true;
+ } finally {
+ if (!hasSaved) {
+ // restore original buffer contents since something went wrong
+ primaryBuffer.setContents(primaryContents);
+ }
+ }
+ } else {
+ // for a primary working copy no need to set the content of the buffer again
+ primaryBuffer.save(this.progressMonitor, this.force);
+ primary.makeConsistent(this);
+ }
+ } else {
+ // working copy on cu outside classpath OR resource doesn't exist yet
+ String encoding = null;
+ try {
+ encoding = resource.getCharset();
+ } catch (CoreException ce) {
+ // use no encoding
+ }
+ String contents = workingCopy.getSource();
+ if (contents == null)
+ return;
+ try {
+ byte[] bytes = encoding == null ? contents.getBytes() : contents.getBytes(encoding);
+ ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
+ if (resource.exists()) {
+ resource.setContents(stream, this.force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY, null);
+ } else {
+ resource.create(stream, this.force, this.progressMonitor);
+ }
+ } catch (CoreException e) {
+ throw new JavaModelException(e);
+ } catch (UnsupportedEncodingException e) {
+ throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
+ }
+
+ }
+
+ setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
+
+ // make sure working copy is in sync
+ workingCopy.updateTimeStamp((CompilationUnit) primary);
+ workingCopy.makeConsistent(this);
+ worked(1);
+
+ // build the deltas
+ if (deltaBuilder != null) {
+ deltaBuilder.buildDeltas();
+
+ // add the deltas to the list of deltas created during this operation
+ if (deltaBuilder.delta != null) {
+ addDelta(deltaBuilder.delta);
+ }
+ }
+ worked(1);
+ } finally {
+ done();
+ }
+ }
+
+ /**
+ * Returns the compilation unit this operation is working on.
+ */
+ protected CompilationUnit getCompilationUnit() {
+ return (CompilationUnit) getElementToProcess();
+ }
+
+ protected ISchedulingRule getSchedulingRule() {
+ IResource resource = getElementToProcess().getResource();
+ IWorkspace workspace = resource.getWorkspace();
+ if (resource.exists()) {
+ return workspace.getRuleFactory().modifyRule(resource);
+ } else {
+ return workspace.getRuleFactory().createRule(resource);
+ }
+ }
+
+ /**
+ * Possible failures:
+ *
+ * - INVALID_ELEMENT_TYPES - the compilation unit supplied to this operation is not a working copy
+ *
- ELEMENT_NOT_PRESENT - the compilation unit the working copy is based on no longer exists.
+ *
- UPDATE_CONFLICT - the original compilation unit has changed since the working copy was created and the operation specifies
+ * no force
+ *
- READ_ONLY - the original compilation unit is in read-only mode
+ *
+ */
+ public IJavaModelStatus verify() {
+ CompilationUnit cu = getCompilationUnit();
+ if (!cu.isWorkingCopy()) {
+ return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, cu);
+ }
+ if (cu.hasResourceChanged() && !this.force) {
+ // axelcl deleted start - force it to VERIFIED_OK, need to be fixed
+ // return new JavaModelStatus(IJavaModelStatusConstants.UPDATE_CONFLICT);
+ // axelcl end
+ }
+
+ // no read-only check, since some repository adapters can change the flag on save
+ // operation.
+ return JavaModelStatus.VERIFIED_OK;
+ }
+}
\ No newline at end of file