3c1285c51c86967282961ca2fc8661a039baa060
[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 java.io.ByteArrayInputStream;
14 import java.io.UnsupportedEncodingException;
15
16 import net.sourceforge.phpdt.core.IBuffer;
17 import net.sourceforge.phpdt.core.ICompilationUnit;
18 import net.sourceforge.phpdt.core.IJavaElement;
19 import net.sourceforge.phpdt.core.IJavaModelStatus;
20 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.internal.core.util.Util;
23
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.IWorkspace;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.jobs.ISchedulingRule;
29
30
31 /**
32  * Commits the contents of a working copy compilation
33  * unit to its original element and resource, bringing
34  * the Java Model up-to-date with the current contents of the working
35  * copy.
36  *
37  * <p>It is possible that the contents of the
38  * original resource have changed since the working copy was created,
39  * in which case there is an update conflict. This operation allows
40  * for two settings to resolve conflict set by the <code>fForce</code> flag:<ul>
41  * <li>force flag is <code>false</code> - in this case an <code>JavaModelException</code>
42  *      is thrown</li>
43  * <li>force flag is <code>true</code> - in this case the contents of
44  *      the working copy are applied to the underlying resource even though
45  *      the working copy was created before a subsequent change in the
46  *      resource</li>
47  * </ul>
48  *
49  * <p>The default conflict resolution setting is the force flag is <code>false</code>
50  *
51  * A JavaModelOperation exception is thrown either if the commit could not
52  * be performed or if the new content of the compilation unit violates some Java Model
53  * constraint (e.g. if the new package declaration doesn't match the name of the folder
54  * containing the compilation unit).
55  */
56 public class CommitWorkingCopyOperation extends JavaModelOperation {
57         /**
58          * Constructs an operation to commit the contents of a working copy
59          * to its original compilation unit.
60          */
61         public CommitWorkingCopyOperation(ICompilationUnit element, boolean force) {
62                 super(new IJavaElement[] {element}, force);
63         }
64         /**
65          * @exception JavaModelException if setting the source
66          *      of the original compilation unit fails
67          */
68         protected void executeOperation() throws JavaModelException {
69                 try {
70                         beginTask(Util.bind("workingCopy.commit"), 2); //$NON-NLS-1$
71                         CompilationUnit workingCopy = getCompilationUnit();
72                         IFile resource = (IFile)workingCopy.getResource();
73                         ICompilationUnit primary = workingCopy.getPrimary();
74                         boolean isPrimary = workingCopy.isPrimary();
75
76                         JavaElementDeltaBuilder deltaBuilder = null;
77 //                      PackageFragmentRoot root = (PackageFragmentRoot)workingCopy.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
78                         boolean isIncluded = !Util.isExcluded(workingCopy);
79 //                      if (isPrimary || (root.isOnClasspath() && isIncluded && resource.isAccessible() && Util.isValidCompilationUnitName(workingCopy.getElementName()))) {
80                         if (isPrimary || (isIncluded && resource.isAccessible() && Util.isValidCompilationUnitName(workingCopy.getElementName()))) {
81                                         
82                                 // force opening so that the delta builder can get the old info
83                                 if (!isPrimary && !primary.isOpen()) {
84                                         primary.open(null);
85                                 }
86
87                                 // creates the delta builder (this remembers the content of the cu) if:
88                                 // - it is not excluded
89                                 // - and it is not a primary or it is a non-consistent primary
90                                 if (isIncluded && (!isPrimary || !workingCopy.isConsistent())) {
91                                         deltaBuilder = new JavaElementDeltaBuilder(primary);
92                                 }
93                         
94                                 // save the cu
95                                 IBuffer primaryBuffer = primary.getBuffer();
96                                 if (!isPrimary) {
97                                         if (primaryBuffer == null) return;
98                                         char[] primaryContents = primaryBuffer.getCharacters();
99                                         boolean hasSaved = false;
100                                         try {
101                                                 IBuffer workingCopyBuffer = workingCopy.getBuffer();
102                                                 if (workingCopyBuffer == null) return;
103                                                 primaryBuffer.setContents(workingCopyBuffer.getCharacters());
104                                                 primaryBuffer.save(this.progressMonitor, this.force);
105                                                 primary.makeConsistent(this);
106                                                 hasSaved = true;
107                                         } finally {
108                                                 if (!hasSaved){
109                                                         // restore original buffer contents since something went wrong
110                                                         primaryBuffer.setContents(primaryContents);
111                                                 }
112                                         }
113                                 } else {
114                                         // for a primary working copy no need to set the content of the buffer again
115                                         primaryBuffer.save(this.progressMonitor, this.force);
116                                         primary.makeConsistent(this);
117                                 }
118                         } else {
119                                 // working copy on cu outside classpath OR resource doesn't exist yet
120                                 String encoding = null;
121                                 try {
122                                         encoding = resource.getCharset();
123                                 }
124                                 catch (CoreException ce) {
125                                         // use no encoding
126                                 }
127                                 String contents = workingCopy.getSource();
128                                 if (contents == null) return;
129                                 try {
130                                         byte[] bytes = encoding == null 
131                                                 ? contents.getBytes() 
132                                                 : contents.getBytes(encoding);
133                                         ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
134                                         if (resource.exists()) {
135                                                 resource.setContents(
136                                                         stream, 
137                                                         this.force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY, 
138                                                         null);
139                                         } else {
140                                                 resource.create(
141                                                         stream,
142                                                         this.force,
143                                                         this.progressMonitor);
144                                         }
145                                 } catch (CoreException e) {
146                                         throw new JavaModelException(e);
147                                 } catch (UnsupportedEncodingException e) {
148                                         throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
149                                 }
150                                 
151                         }
152
153                         setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); 
154                         
155                         // make sure working copy is in sync
156                         workingCopy.updateTimeStamp((CompilationUnit)primary);
157                         workingCopy.makeConsistent(this);
158                         worked(1);
159                 
160                         // build the deltas
161                         if (deltaBuilder != null) {
162                                 deltaBuilder.buildDeltas();
163                         
164                                 // add the deltas to the list of deltas created during this operation
165                                 if (deltaBuilder.delta != null) {
166                                         addDelta(deltaBuilder.delta);
167                                 }
168                         }
169                         worked(1);
170                 } finally {     
171                         done();
172                 }
173         }
174         
175         /**
176          * Returns the compilation unit this operation is working on.
177          */
178         protected CompilationUnit getCompilationUnit() {
179                 return (CompilationUnit)getElementToProcess();
180         }
181         protected ISchedulingRule getSchedulingRule() {
182                 IResource resource = getElementToProcess().getResource();
183                 IWorkspace workspace = resource.getWorkspace();
184                 if (resource.exists()) {
185                         return workspace.getRuleFactory().modifyRule(resource);
186                 } else {
187                         return workspace.getRuleFactory().createRule(resource);
188                 }
189         }
190         /**
191          * Possible failures: <ul>
192          *      <li>INVALID_ELEMENT_TYPES - the compilation unit supplied to this
193          *              operation is not a working copy
194          *  <li>ELEMENT_NOT_PRESENT - the compilation unit the working copy is
195          *              based on no longer exists.
196          *  <li>UPDATE_CONFLICT - the original compilation unit has changed since
197          *              the working copy was created and the operation specifies no force
198          *  <li>READ_ONLY - the original compilation unit is in read-only mode
199          *  </ul>
200          */
201         public IJavaModelStatus verify() {
202                 ICompilationUnit cu = getCompilationUnit();
203                 if (!cu.isWorkingCopy()) {
204                         return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, cu);
205                 }
206                 ICompilationUnit original= (ICompilationUnit)cu.getOriginalElement();
207                 IResource resource = original.getResource();
208                 if (!cu.isBasedOn(resource) && !force) {
209                         return new JavaModelStatus(IJavaModelStatusConstants.UPDATE_CONFLICT);
210                 }
211                 // no read-only check, since some repository adapters can change the flag on save
212                 // operation.   
213                 return JavaModelStatus.VERIFIED_OK;
214         }
215 }