5356891bcf716870b4a7bc7a387e3ebd0c648000
[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(fMonitor);
127                         // deltaBuilder.buildDeltas();
128                         //              
129                         // }
130
131                         if (progressMonitor != null)
132                                 progressMonitor.worked(2);
133
134                         // force problem detection? - if structure was consistent
135                         if (forceProblemDetection) {
136                                 if (progressMonitor != null && progressMonitor.isCanceled())
137                                         return;
138                                 CompilationUnitDeclaration unit = null;
139                                 try {
140                                         IProblemRequestor problemRequestor = workingCopy
141                                                         .getPerWorkingCopyInfo();
142                                         if (problemRequestor != null && problemRequestor.isActive()) {
143                                                 problemRequestor.beginReporting();
144                                                 char[] contents = workingCopy.getContents();
145                                                 unit = CompilationUnitProblemFinder.process(
146                                                                 workingCopy, contents, this.workingCopyOwner,
147                                                                 problemRequestor, false/* don't cleanup cu */,
148                                                                 this.progressMonitor);
149                                                 CompilationUnitProblemFinder.process(workingCopy,
150                                                                 problemRequestor, progressMonitor);
151                                                 problemRequestor.endReporting();
152                                         }
153                                         if (progressMonitor != null)
154                                                 progressMonitor.worked(1);
155                                         if (this.createAST && unit != null) {
156                                                 // Map options =
157                                                 // workingCopy.getJavaProject().getOptions(true);
158                                                 // this.ast = AST.convertCompilationUnit(this.astLevel,
159                                                 // unit, contents, options, this.progressMonitor);
160                                                 if (progressMonitor != null)
161                                                         progressMonitor.worked(1);
162                                         }
163                                 } finally {
164                                         if (unit != null) {
165                                                 unit.cleanUp();
166                                         }
167                                 }
168                         }
169
170                         // register the deltas
171                         // if (deltaBuilder != null){
172                         // if ((deltaBuilder.delta != null) &&
173                         // (deltaBuilder.delta.getAffectedChildren().length > 0)) {
174                         // addReconcileDelta(workingCopy, deltaBuilder.delta);
175                         // }
176                         // }
177                 } finally {
178                         if (progressMonitor != null)
179                                 progressMonitor.done();
180                 }
181         }
182
183         /**
184          * Returns the working copy this operation is working on.
185          */
186         protected CompilationUnit getWorkingCopy() {
187                 return (CompilationUnit) getElementToProcess();
188         }
189
190         /**
191          * @see JavaModelOperation#isReadOnly
192          */
193         public boolean isReadOnly() {
194                 return true;
195         }
196
197         protected IJavaModelStatus verify() {
198                 IJavaModelStatus status = super.verify();
199                 if (!status.isOK()) {
200                         return status;
201                 }
202                 CompilationUnit workingCopy = getWorkingCopy();
203                 if (!workingCopy.isWorkingCopy()) {
204                         return new JavaModelStatus(
205                                         IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST,
206                                         workingCopy); // was destroyed
207                 }
208                 return status;
209         }
210
211 }