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