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 java.io.ByteArrayInputStream;
14 import java.io.UnsupportedEncodingException;
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;
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;
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.
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:
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>
49 * The default conflict resolution setting is the force flag is
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).
57 public class CommitWorkingCopyOperation extends JavaModelOperation {
59 * Constructs an operation to commit the contents of a working copy to its
60 * original compilation unit.
62 public CommitWorkingCopyOperation(ICompilationUnit element, boolean force) {
63 super(new IJavaElement[] { element }, force);
67 * @exception JavaModelException
68 * if setting the source of the original compilation unit
71 protected void executeOperation() throws JavaModelException {
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();
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())))
88 || (isIncluded && resource.isAccessible() && Util
89 .isValidCompilationUnitName(workingCopy
90 .getElementName()))) {
92 // force opening so that the delta builder can get the old info
93 if (!isPrimary && !primary.isOpen()) {
97 // creates the delta builder (this remembers the content of the
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);
106 IBuffer primaryBuffer = primary.getBuffer();
108 if (primaryBuffer == null)
110 char[] primaryContents = primaryBuffer.getCharacters();
111 boolean hasSaved = false;
113 IBuffer workingCopyBuffer = workingCopy.getBuffer();
114 if (workingCopyBuffer == null)
116 primaryBuffer.setContents(workingCopyBuffer
118 primaryBuffer.save(this.progressMonitor, this.force);
119 primary.makeConsistent(this);
123 // restore original buffer contents since something
125 primaryBuffer.setContents(primaryContents);
129 // for a primary working copy no need to set the content of
131 primaryBuffer.save(this.progressMonitor, this.force);
132 primary.makeConsistent(this);
135 // working copy on cu outside classpath OR resource doesn't
137 String encoding = null;
139 encoding = resource.getCharset();
140 } catch (CoreException ce) {
143 String contents = workingCopy.getSource();
144 if (contents == null)
147 byte[] bytes = encoding == null ? contents.getBytes()
148 : contents.getBytes(encoding);
149 ByteArrayInputStream stream = new ByteArrayInputStream(
151 if (resource.exists()) {
152 resource.setContents(stream,
153 this.force ? IResource.FORCE
154 | IResource.KEEP_HISTORY
155 : IResource.KEEP_HISTORY, null);
157 resource.create(stream, this.force,
158 this.progressMonitor);
160 } catch (CoreException e) {
161 throw new JavaModelException(e);
162 } catch (UnsupportedEncodingException e) {
163 throw new JavaModelException(e,
164 IJavaModelStatusConstants.IO_EXCEPTION);
169 setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
171 // make sure working copy is in sync
172 workingCopy.updateTimeStamp((CompilationUnit) primary);
173 workingCopy.makeConsistent(this);
177 if (deltaBuilder != null) {
178 deltaBuilder.buildDeltas();
180 // add the deltas to the list of deltas created during this
182 if (deltaBuilder.delta != null) {
183 addDelta(deltaBuilder.delta);
193 * Returns the compilation unit this operation is working on.
195 protected CompilationUnit getCompilationUnit() {
196 return (CompilationUnit) getElementToProcess();
199 protected ISchedulingRule getSchedulingRule() {
200 IResource resource = getElementToProcess().getResource();
201 IWorkspace workspace = resource.getWorkspace();
202 if (resource.exists()) {
203 return workspace.getRuleFactory().modifyRule(resource);
205 return workspace.getRuleFactory().createRule(resource);
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
221 public IJavaModelStatus verify() {
222 CompilationUnit cu = getCompilationUnit();
223 if (!cu.isWorkingCopy()) {
224 return new JavaModelStatus(
225 IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, cu);
227 if (cu.hasResourceChanged() && !this.force) {
228 // axelcl deleted start - force it to VERIFIED_OK, need to be fixed
230 // JavaModelStatus(IJavaModelStatusConstants.UPDATE_CONFLICT);
234 // no read-only check, since some repository adapters can change the
237 return JavaModelStatus.VERIFIED_OK;