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.IJavaElement;
14 import net.sourceforge.phpdt.core.IJavaModelStatus;
15 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
16 import net.sourceforge.phpdt.core.IProblemRequestor;
17 import net.sourceforge.phpdt.core.JavaModelException;
18 import net.sourceforge.phpdt.core.WorkingCopyOwner;
19 import net.sourceforge.phpdt.internal.compiler.ast.CompilationUnitDeclaration;
20 import net.sourceforge.phpdt.internal.core.util.Util;
23 * Reconcile a working copy and signal the changes through a delta.
25 public class ReconcileWorkingCopyOperation extends JavaModelOperation {
31 boolean forceProblemDetection;
33 WorkingCopyOwner workingCopyOwner;
35 // net.sourceforge.phpdt.core.dom.CompilationUnit ast;
37 public ReconcileWorkingCopyOperation(IJavaElement workingCopy,
38 boolean forceProblemDetection) {
39 super(new IJavaElement[] { workingCopy });
40 this.forceProblemDetection = forceProblemDetection;
43 public ReconcileWorkingCopyOperation(IJavaElement workingCopy,
44 boolean creatAST, int astLevel, boolean forceProblemDetection,
45 WorkingCopyOwner workingCopyOwner) {
46 super(new IJavaElement[] { workingCopy });
47 this.createAST = creatAST;
48 this.astLevel = astLevel;
49 this.forceProblemDetection = forceProblemDetection;
50 this.workingCopyOwner = workingCopyOwner;
54 * @exception JavaModelException
55 * if setting the source of the original compilation unit
58 // protected void executeOperation() throws JavaModelException {
59 // if (fMonitor != null){
60 // if (fMonitor.isCanceled()) return;
61 // fMonitor.beginTask(ProjectPrefUtil.bind("element.reconciling"), 10);
65 // WorkingCopy workingCopy = getWorkingCopy();
66 // boolean wasConsistent = workingCopy.isConsistent();
67 // JavaElementDeltaBuilder deltaBuilder = null;
70 // // create the delta builder (this remembers the current content of the
72 // if (!wasConsistent){
73 // deltaBuilder = new JavaElementDeltaBuilder(workingCopy);
75 // // update the element infos with the content of the working copy
76 // workingCopy.makeConsistent(fMonitor);
77 // deltaBuilder.buildDeltas();
81 // if (fMonitor != null) fMonitor.worked(2);
83 // // force problem detection? - if structure was consistent
84 // if (forceProblemDetection && wasConsistent){
85 // if (fMonitor != null && fMonitor.isCanceled()) return;
87 // IProblemRequestor problemRequestor = workingCopy.problemRequestor;
88 // if (problemRequestor != null && problemRequestor.isActive()){
89 // problemRequestor.beginReporting();
90 // CompilationUnitProblemFinder.process(workingCopy, problemRequestor,
92 // problemRequestor.endReporting();
96 // // register the deltas
97 // if (deltaBuilder != null){
98 // if ((deltaBuilder.delta != null) &&
99 // (deltaBuilder.delta.getAffectedChildren().length > 0)) {
100 // addReconcileDelta(workingCopy, deltaBuilder.delta);
104 // if (fMonitor != null) fMonitor.done();
107 protected void executeOperation() throws JavaModelException {
108 // TODO jsurfer optimize for PHP
109 if (progressMonitor != null) {
110 if (progressMonitor.isCanceled())
112 progressMonitor.beginTask(Util.bind("element.reconciling"), 10); //$NON-NLS-1$
115 CompilationUnit workingCopy = getWorkingCopy();
116 boolean wasConsistent = workingCopy.isConsistent();
117 JavaElementDeltaBuilder deltaBuilder = null;
120 // create the delta builder (this remembers the current content of
122 if (!wasConsistent) {
123 deltaBuilder = new JavaElementDeltaBuilder(workingCopy);
125 // update the element infos with the content of the working copy
126 workingCopy.makeConsistent(progressMonitor);
127 deltaBuilder.buildDeltas();
130 if (progressMonitor != null)
131 progressMonitor.worked(2);
133 // force problem detection? - if structure was consistent
134 if (forceProblemDetection) {
135 if (progressMonitor != null && progressMonitor.isCanceled())
137 CompilationUnitDeclaration unit = null;
139 IProblemRequestor problemRequestor = workingCopy
140 .getPerWorkingCopyInfo();
141 if (problemRequestor != null && problemRequestor.isActive()) {
142 problemRequestor.beginReporting();
143 char[] contents = workingCopy.getContents();
144 unit = CompilationUnitProblemFinder.process(
145 workingCopy, contents, this.workingCopyOwner,
146 problemRequestor, false/* don't cleanup cu */,
147 this.progressMonitor);
148 CompilationUnitProblemFinder.process(workingCopy,
149 problemRequestor, progressMonitor);
150 problemRequestor.endReporting();
152 if (progressMonitor != null)
153 progressMonitor.worked(1);
154 if (this.createAST && unit != null) {
156 // workingCopy.getJavaProject().getOptions(true);
157 // this.ast = AST.convertCompilationUnit(this.astLevel,
158 // unit, contents, options, this.progressMonitor);
159 if (progressMonitor != null)
160 progressMonitor.worked(1);
169 // register the deltas
170 if (deltaBuilder != null) {
171 if ((deltaBuilder.delta != null)
172 && (deltaBuilder.delta.getAffectedChildren().length > 0)) {
173 addReconcileDelta(workingCopy, deltaBuilder.delta);
177 if (progressMonitor != null)
178 progressMonitor.done();
183 * Returns the working copy this operation is working on.
185 protected CompilationUnit getWorkingCopy() {
186 return (CompilationUnit) getElementToProcess();
190 * @see JavaModelOperation#isReadOnly
192 public boolean isReadOnly() {
196 protected IJavaModelStatus verify() {
197 IJavaModelStatus status = super.verify();
198 if (!status.isOK()) {
201 CompilationUnit workingCopy = getWorkingCopy();
202 if (!workingCopy.isWorkingCopy()) {
203 return new JavaModelStatus(
204 IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST,
205 workingCopy); // was destroyed