3m9 compatible;
[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 java.util.Map;
14
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IJavaModelStatus;
17 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
18 import net.sourceforge.phpdt.core.IProblemRequestor;
19 import net.sourceforge.phpdt.core.JavaModelException;
20 import net.sourceforge.phpdt.core.WorkingCopyOwner;
21 import net.sourceforge.phpdt.internal.core.util.Util;
22 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
23
24 /**
25  * Reconcile a working copy and signal the changes through a delta.
26  */
27 public class ReconcileWorkingCopyOperation extends JavaModelOperation {
28                 
29         boolean createAST;
30         int astLevel;
31         boolean forceProblemDetection;
32         WorkingCopyOwner workingCopyOwner;
33 //      org.eclipse.jdt.core.dom.CompilationUnit ast;
34         
35         public ReconcileWorkingCopyOperation(IJavaElement workingCopy, boolean forceProblemDetection) {
36                 super(new IJavaElement[] {workingCopy});
37                 this.forceProblemDetection = forceProblemDetection;
38         }
39         public ReconcileWorkingCopyOperation(IJavaElement workingCopy, boolean creatAST, int astLevel, boolean forceProblemDetection, WorkingCopyOwner workingCopyOwner) {
40                 super(new IJavaElement[] {workingCopy});
41                 this.createAST = creatAST;
42                 this.astLevel = astLevel;
43                 this.forceProblemDetection = forceProblemDetection;
44                 this.workingCopyOwner = workingCopyOwner;
45         }
46         /**
47          * @exception JavaModelException if setting the source
48          *      of the original compilation unit fails
49          */
50 //      protected void executeOperation() throws JavaModelException {
51 //              if (fMonitor != null){
52 //                      if (fMonitor.isCanceled()) return;
53 //                      fMonitor.beginTask(Util.bind("element.reconciling"), 10); //$NON-NLS-1$
54 //              }
55 //      
56 //              WorkingCopy workingCopy = getWorkingCopy();
57 //              boolean wasConsistent = workingCopy.isConsistent();
58 //              JavaElementDeltaBuilder deltaBuilder = null;
59 //      
60 //              try {
61 //                      // create the delta builder (this remembers the current content of the cu)
62 //                      if (!wasConsistent){
63 //                              deltaBuilder = new JavaElementDeltaBuilder(workingCopy);
64 //                              
65 //                              // update the element infos with the content of the working copy
66 //                              workingCopy.makeConsistent(fMonitor);
67 //                              deltaBuilder.buildDeltas();
68 //              
69 //                      }
70 //      
71 //                      if (fMonitor != null) fMonitor.worked(2);
72 //                      
73 //                      // force problem detection? - if structure was consistent
74 //                      if (forceProblemDetection && wasConsistent){
75 //                              if (fMonitor != null && fMonitor.isCanceled()) return;
76 //              
77 //                              IProblemRequestor problemRequestor = workingCopy.problemRequestor;
78 //                              if (problemRequestor != null && problemRequestor.isActive()){
79 //                                      problemRequestor.beginReporting();
80 //                                      CompilationUnitProblemFinder.process(workingCopy, problemRequestor, fMonitor);
81 //                                      problemRequestor.endReporting();
82 //                              }
83 //                      }
84 //                      
85 //                      // register the deltas
86 //                      if (deltaBuilder != null){
87 //                              if ((deltaBuilder.delta != null) && (deltaBuilder.delta.getAffectedChildren().length > 0)) {
88 //                                      addReconcileDelta(workingCopy, deltaBuilder.delta);
89 //                              }
90 //                      }
91 //              } finally {
92 //                      if (fMonitor != null) fMonitor.done();
93 //              }
94 //      }
95         protected void executeOperation() throws JavaModelException {
96           // TODO jsurfer optimize for PHP
97                 if (progressMonitor != null){
98                         if (progressMonitor.isCanceled()) return;
99                         progressMonitor.beginTask(Util.bind("element.reconciling"), 10); //$NON-NLS-1$
100                 }
101         
102                 CompilationUnit workingCopy = getWorkingCopy();
103 //              boolean wasConsistent = workingCopy.isConsistent();
104 //              JavaElementDeltaBuilder deltaBuilder = null;
105         
106                 try {
107                         // create the delta builder (this remembers the current content of the cu)
108 //                      if (!wasConsistent){
109 //                              deltaBuilder = new JavaElementDeltaBuilder(workingCopy);
110 //                              
111 //                              // update the element infos with the content of the working copy
112 //                              workingCopy.makeConsistent(fMonitor);
113 //                              deltaBuilder.buildDeltas();
114 //              
115 //                      }
116         
117                         if (progressMonitor != null) progressMonitor.worked(2);
118                         
119                         // force problem detection? - if structure was consistent
120                         if (forceProblemDetection){
121                                 if (progressMonitor != null && progressMonitor.isCanceled()) return;
122                                 CompilationUnitDeclaration unit = null;
123                                 try {
124                                 IProblemRequestor problemRequestor = workingCopy.getPerWorkingCopyInfo();
125                                 if (problemRequestor != null && problemRequestor.isActive()){
126                                         problemRequestor.beginReporting();
127                                         char[] contents = workingCopy.getContents();
128                                         unit = CompilationUnitProblemFinder.process(workingCopy, contents, this.workingCopyOwner, problemRequestor, false/*don't cleanup cu*/, this.progressMonitor);
129                                         
130                                         CompilationUnitProblemFinder.process(workingCopy, problemRequestor, progressMonitor);
131                                         problemRequestor.endReporting();
132                                 }
133                                 if (progressMonitor != null) progressMonitor.worked(1);
134                                 if (this.createAST && unit != null) {
135 //                                      Map options = workingCopy.getJavaProject().getOptions(true);
136 //                                      this.ast = AST.convertCompilationUnit(this.astLevel, unit, contents, options, this.progressMonitor);
137                                         if (progressMonitor != null) progressMonitor.worked(1);
138                                 }
139                                 } finally {
140                                 if (unit != null) {
141                                     unit.cleanUp();
142                                 }
143                             }
144                         }
145                         
146                         // register the deltas
147 //                      if (deltaBuilder != null){
148 //                              if ((deltaBuilder.delta != null) && (deltaBuilder.delta.getAffectedChildren().length > 0)) {
149 //                                      addReconcileDelta(workingCopy, deltaBuilder.delta);
150 //                              }
151 //                      }
152                 } finally {
153                         if (progressMonitor != null) progressMonitor.done();
154                 }
155         }
156         /**
157          * Returns the working copy this operation is working on.
158          */
159         protected CompilationUnit getWorkingCopy() {
160                 return (CompilationUnit)getElementToProcess();
161         }
162         /**
163          * @see JavaModelOperation#isReadOnly
164          */
165         public boolean isReadOnly() {
166                 return true;
167         }
168         
169         protected IJavaModelStatus verify() {
170                 IJavaModelStatus status = super.verify();
171                 if (!status.isOK()) {
172                         return status;
173                 }
174                 CompilationUnit workingCopy = getWorkingCopy();
175                 if (!workingCopy.isWorkingCopy()) {
176                         return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, workingCopy); //was destroyed
177                 }
178                 return status;
179         }
180
181 }