743484ce641ef2e20a2ded230e4820121aaacf06
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / CommitWorkingCopyOperation.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
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;
19
20 import org.eclipse.core.resources.IResource;
21
22
23 /**
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
27  * copy.
28  *
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>
34  *      is thrown</li>
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
38  *      resource</li>
39  * </ul>
40  *
41  * <p>The default conflict resolution setting is the force flag is <code>false</code>
42  *
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).
47  */
48 public class CommitWorkingCopyOperation extends JavaModelOperation {
49         /**
50          * Constructs an operation to commit the contents of a working copy
51          * to its original compilation unit.
52          */
53         public CommitWorkingCopyOperation(ICompilationUnit element, boolean force) {
54                 super(new IJavaElement[] {element}, force);
55         }
56         /**
57          * @exception JavaModelException if setting the source
58          *      of the original compilation unit fails
59          */
60         protected void executeOperation() throws JavaModelException {
61                 try {
62                         beginTask(Util.bind("workingCopy.commit"), 2); //$NON-NLS-1$
63                         WorkingCopy copy = (WorkingCopy)getCompilationUnit();
64                         ICompilationUnit original = (ICompilationUnit) copy.getOriginalElement();
65                 
66                         
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
70                                 original.open(null);
71                         }
72                         JavaElementDeltaBuilder deltaBuilder;
73                         if (Util.isExcluded(original)) {
74                                 deltaBuilder = null;
75                         } else {
76                                 deltaBuilder = new JavaElementDeltaBuilder(original);
77                         }
78                 
79                         // save the cu
80                         IBuffer originalBuffer = original.getBuffer();
81                         if (originalBuffer == null) return;
82                         char[] originalContents = originalBuffer.getCharacters();
83                         boolean hasSaved = false;
84                         try {
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); 
90                                 hasSaved = true;
91                         } finally {
92                                 if (!hasSaved){
93                                         // restore original buffer contents since something went wrong
94                                         originalBuffer.setContents(originalContents);
95                                 }
96                         }
97                         // make sure working copy is in sync
98                         copy.updateTimeStamp((CompilationUnit)original);
99                         copy.makeConsistent(this);
100                         worked(1);
101                 
102                         if (deltaBuilder != null) {
103                                 // build the deltas
104                                 deltaBuilder.buildDeltas();
105                         
106                                 // add the deltas to the list of deltas created during this operation
107                                 if (deltaBuilder.delta != null) {
108                                         addDelta(deltaBuilder.delta);
109                                 }
110                         }
111                         worked(1);
112                 } finally {     
113                         done();
114                 }
115         }
116         /**
117          * Returns the compilation unit this operation is working on.
118          */
119         protected ICompilationUnit getCompilationUnit() {
120                 return (ICompilationUnit)getElementToProcess();
121         }
122         /**
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
131          *  </ul>
132          */
133         public IJavaModelStatus verify() {
134                 ICompilationUnit cu = getCompilationUnit();
135                 if (!cu.isWorkingCopy()) {
136                         return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, cu);
137                 }
138                 ICompilationUnit original= (ICompilationUnit)cu.getOriginalElement();
139                 IResource resource = original.getResource();
140                 if (!cu.isBasedOn(resource) && !fForce) {
141                         return new JavaModelStatus(IJavaModelStatusConstants.UPDATE_CONFLICT);
142                 }
143                 // no read-only check, since some repository adapters can change the flag on save
144                 // operation.   
145                 return JavaModelStatus.VERIFIED_OK;
146         }
147 }