409c229945906b9daeacf800419243e169be17e7
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / ReconcileWorkingCopyOperation.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.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;
21
22 /**
23  * Reconcile a working copy and signal the changes through a delta.
24  */
25 public class ReconcileWorkingCopyOperation extends JavaModelOperation {
26
27         boolean createAST;
28
29         int astLevel;
30
31         boolean forceProblemDetection;
32
33         WorkingCopyOwner workingCopyOwner;
34
35         // net.sourceforge.phpdt.core.dom.CompilationUnit ast;
36
37         public ReconcileWorkingCopyOperation(IJavaElement workingCopy,
38                         boolean forceProblemDetection) {
39                 super(new IJavaElement[] { workingCopy });
40                 this.forceProblemDetection = forceProblemDetection;
41         }
42
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;
51         }
52
53         /**
54          * @exception JavaModelException
55          *                if setting the source of the original compilation unit
56          *                fails
57          */
58         // protected void executeOperation() throws JavaModelException {
59         // if (fMonitor != null){
60         // if (fMonitor.isCanceled()) return;
61         // fMonitor.beginTask(ProjectPrefUtil.bind("element.reconciling"), 10);
62         // //$NON-NLS-1$
63         // }
64         //      
65         // WorkingCopy workingCopy = getWorkingCopy();
66         // boolean wasConsistent = workingCopy.isConsistent();
67         // JavaElementDeltaBuilder deltaBuilder = null;
68         //      
69         // try {
70         // // create the delta builder (this remembers the current content of the
71         // cu)
72         // if (!wasConsistent){
73         // deltaBuilder = new JavaElementDeltaBuilder(workingCopy);
74         //                              
75         // // update the element infos with the content of the working copy
76         // workingCopy.makeConsistent(fMonitor);
77         // deltaBuilder.buildDeltas();
78         //              
79         // }
80         //      
81         // if (fMonitor != null) fMonitor.worked(2);
82         //                      
83         // // force problem detection? - if structure was consistent
84         // if (forceProblemDetection && wasConsistent){
85         // if (fMonitor != null && fMonitor.isCanceled()) return;
86         //              
87         // IProblemRequestor problemRequestor = workingCopy.problemRequestor;
88         // if (problemRequestor != null && problemRequestor.isActive()){
89         // problemRequestor.beginReporting();
90         // CompilationUnitProblemFinder.process(workingCopy, problemRequestor,
91         // fMonitor);
92         // problemRequestor.endReporting();
93         // }
94         // }
95         //                      
96         // // register the deltas
97         // if (deltaBuilder != null){
98         // if ((deltaBuilder.delta != null) &&
99         // (deltaBuilder.delta.getAffectedChildren().length > 0)) {
100         // addReconcileDelta(workingCopy, deltaBuilder.delta);
101         // }
102         // }
103         // } finally {
104         // if (fMonitor != null) fMonitor.done();
105         // }
106         // }
107         protected void executeOperation() throws JavaModelException {
108                 // TODO jsurfer optimize for PHP
109                 if (progressMonitor != null) {
110                         if (progressMonitor.isCanceled())
111                                 return;
112                         progressMonitor.beginTask(Util.bind("element.reconciling"), 10); //$NON-NLS-1$
113                 }
114
115                 CompilationUnit workingCopy = getWorkingCopy();
116                 boolean wasConsistent = workingCopy.isConsistent();
117                 JavaElementDeltaBuilder deltaBuilder = null;
118
119                 try {
120                         // create the delta builder (this remembers the current content of
121                         // the cu)
122                         if (!wasConsistent) {
123                                 deltaBuilder = new JavaElementDeltaBuilder(workingCopy);
124
125                                 // update the element infos with the content of the working copy
126                                 workingCopy.makeConsistent(progressMonitor);
127                                 deltaBuilder.buildDeltas();
128                         }
129
130                         if (progressMonitor != null)
131                                 progressMonitor.worked(2);
132
133                         // force problem detection? - if structure was consistent
134                         if (forceProblemDetection) {
135                                 if (progressMonitor != null && progressMonitor.isCanceled())
136                                         return;
137                                 CompilationUnitDeclaration unit = null;
138                                 try {
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();
151                                         }
152                                         if (progressMonitor != null)
153                                                 progressMonitor.worked(1);
154                                         if (this.createAST && unit != null) {
155                                                 // Map options =
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);
161                                         }
162                                 } finally {
163                                         if (unit != null) {
164                                                 unit.cleanUp();
165                                         }
166                                 }
167                         }
168
169                         // register the deltas
170                         if (deltaBuilder != null) {
171                                 if ((deltaBuilder.delta != null)
172                                                 && (deltaBuilder.delta.getAffectedChildren().length > 0)) {
173                                         addReconcileDelta(workingCopy, deltaBuilder.delta);
174                                 }
175                         }
176                 } finally {
177                         if (progressMonitor != null)
178                                 progressMonitor.done();
179                 }
180         }
181
182         /**
183          * Returns the working copy this operation is working on.
184          */
185         protected CompilationUnit getWorkingCopy() {
186                 return (CompilationUnit) getElementToProcess();
187         }
188
189         /**
190          * @see JavaModelOperation#isReadOnly
191          */
192         public boolean isReadOnly() {
193                 return true;
194         }
195
196         protected IJavaModelStatus verify() {
197                 IJavaModelStatus status = super.verify();
198                 if (!status.isOK()) {
199                         return status;
200                 }
201                 CompilationUnit workingCopy = getWorkingCopy();
202                 if (!workingCopy.isWorkingCopy()) {
203                         return new JavaModelStatus(
204                                         IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST,
205                                         workingCopy); // was destroyed
206                 }
207                 return status;
208         }
209
210 }