1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import net.sourceforge.phpdt.core.IBuffer;
14 import net.sourceforge.phpdt.core.ICompilationUnit;
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IJavaModelStatus;
17 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
18 import net.sourceforge.phpdt.core.JavaModelException;
20 import org.eclipse.core.resources.IResource;
24 * Commits the contents of a working copy compilation
25 * unit to its original element and resource, bringing
26 * the Java Model up-to-date with the current contents of the working
29 * <p>It is possible that the contents of the
30 * original resource have changed since the working copy was created,
31 * in which case there is an update conflict. This operation allows
32 * for two settings to resolve conflict set by the <code>fForce</code> flag:<ul>
33 * <li>force flag is <code>false</code> - in this case an <code>JavaModelException</code>
35 * <li>force flag is <code>true</code> - in this case the contents of
36 * the working copy are applied to the underlying resource even though
37 * the working copy was created before a subsequent change in the
41 * <p>The default conflict resolution setting is the force flag is <code>false</code>
43 * A JavaModelOperation exception is thrown either if the commit could not
44 * be performed or if the new content of the compilation unit violates some Java Model
45 * constraint (e.g. if the new package declaration doesn't match the name of the folder
46 * containing the compilation unit).
48 public class CommitWorkingCopyOperation extends JavaModelOperation {
50 * Constructs an operation to commit the contents of a working copy
51 * to its original compilation unit.
53 public CommitWorkingCopyOperation(ICompilationUnit element, boolean force) {
54 super(new IJavaElement[] {element}, force);
57 * @exception JavaModelException if setting the source
58 * of the original compilation unit fails
60 protected void executeOperation() throws JavaModelException {
62 beginTask(Util.bind("workingCopy.commit"), 2); //$NON-NLS-1$
63 WorkingCopy copy = (WorkingCopy)getCompilationUnit();
64 ICompilationUnit original = (ICompilationUnit) copy.getOriginalElement();
67 // creates the delta builder (this remembers the content of the cu)
68 if (!original.isOpen()) {
69 // force opening so that the delta builder can get the old info
72 JavaElementDeltaBuilder deltaBuilder;
73 if (Util.isExcluded(original)) {
76 deltaBuilder = new JavaElementDeltaBuilder(original);
80 IBuffer originalBuffer = original.getBuffer();
81 if (originalBuffer == null) return;
82 char[] originalContents = originalBuffer.getCharacters();
83 boolean hasSaved = false;
85 IBuffer copyBuffer = copy.getBuffer();
86 if (copyBuffer == null) return;
87 originalBuffer.setContents(copyBuffer.getCharacters());
88 original.save(fMonitor, fForce);
89 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
93 // restore original buffer contents since something went wrong
94 originalBuffer.setContents(originalContents);
97 // make sure working copy is in sync
98 copy.updateTimeStamp((CompilationUnit)original);
99 copy.makeConsistent(this);
102 if (deltaBuilder != null) {
104 deltaBuilder.buildDeltas();
106 // add the deltas to the list of deltas created during this operation
107 if (deltaBuilder.delta != null) {
108 addDelta(deltaBuilder.delta);
117 * Returns the compilation unit this operation is working on.
119 protected ICompilationUnit getCompilationUnit() {
120 return (ICompilationUnit)getElementToProcess();
123 * Possible failures: <ul>
124 * <li>INVALID_ELEMENT_TYPES - the compilation unit supplied to this
125 * operation is not a working copy
126 * <li>ELEMENT_NOT_PRESENT - the compilation unit the working copy is
127 * based on no longer exists.
128 * <li>UPDATE_CONFLICT - the original compilation unit has changed since
129 * the working copy was created and the operation specifies no force
130 * <li>READ_ONLY - the original compilation unit is in read-only mode
133 public IJavaModelStatus verify() {
134 ICompilationUnit cu = getCompilationUnit();
135 if (!cu.isWorkingCopy()) {
136 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, cu);
138 ICompilationUnit original= (ICompilationUnit)cu.getOriginalElement();
139 IResource resource = original.getResource();
140 if (!cu.isBasedOn(resource) && !fForce) {
141 return new JavaModelStatus(IJavaModelStatusConstants.UPDATE_CONFLICT);
143 // no read-only check, since some repository adapters can change the flag on save
145 return JavaModelStatus.VERIFIED_OK;