9df9bea92a9266407c1ece96b41026f53dac9b87
[phpeclipse.git] /
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
19 /**
20  * Reconcile a working copy and signal the changes through a delta.
21  */
22 public class ReconcileWorkingCopyOperation extends JavaModelOperation {
23                 
24         boolean forceProblemDetection;
25         
26         public ReconcileWorkingCopyOperation(IJavaElement workingCopy, boolean forceProblemDetection) {
27                 super(new IJavaElement[] {workingCopy});
28                 this.forceProblemDetection = forceProblemDetection;
29         }
30         /**
31          * @exception JavaModelException if setting the source
32          *      of the original compilation unit fails
33          */
34         protected void executeOperation() throws JavaModelException {
35                 if (fMonitor != null){
36                         if (fMonitor.isCanceled()) return;
37                         fMonitor.beginTask(Util.bind("element.reconciling"), 10); //$NON-NLS-1$
38                 }
39         
40                 WorkingCopy workingCopy = getWorkingCopy();
41                 boolean wasConsistent = workingCopy.isConsistent();
42                 JavaElementDeltaBuilder deltaBuilder = null;
43         
44                 try {
45                         // create the delta builder (this remembers the current content of the cu)
46                         if (!wasConsistent){
47                                 deltaBuilder = new JavaElementDeltaBuilder(workingCopy);
48                                 
49                                 // update the element infos with the content of the working copy
50                                 workingCopy.makeConsistent(fMonitor);
51                                 deltaBuilder.buildDeltas();
52                 
53                         }
54         
55                         if (fMonitor != null) fMonitor.worked(2);
56                         
57                         // force problem detection? - if structure was consistent
58                         if (forceProblemDetection && wasConsistent){
59                                 if (fMonitor != null && fMonitor.isCanceled()) return;
60                 
61                                 IProblemRequestor problemRequestor = workingCopy.problemRequestor;
62                                 if (problemRequestor != null && problemRequestor.isActive()){
63                                         problemRequestor.beginReporting();
64                                         CompilationUnitProblemFinder.process(workingCopy, problemRequestor, fMonitor);
65                                         problemRequestor.endReporting();
66                                 }
67                         }
68                         
69                         // register the deltas
70                         if (deltaBuilder != null){
71                                 if ((deltaBuilder.delta != null) && (deltaBuilder.delta.getAffectedChildren().length > 0)) {
72                                         addReconcileDelta(workingCopy, deltaBuilder.delta);
73                                 }
74                         }
75                 } finally {
76                         if (fMonitor != null) fMonitor.done();
77                 }
78         }
79         /**
80          * Returns the working copy this operation is working on.
81          */
82         protected WorkingCopy getWorkingCopy() {
83                 return (WorkingCopy)getElementToProcess();
84         }
85         /**
86          * @see JavaModelOperation#isReadOnly
87          */
88         public boolean isReadOnly() {
89                 return true;
90         }
91         protected IJavaModelStatus verify() {
92                 IJavaModelStatus status = super.verify();
93                 if (!status.isOK()) {
94                         return status;
95                 }
96                 WorkingCopy workingCopy = getWorkingCopy();
97                 if (workingCopy.useCount == 0) {
98                         return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, workingCopy); //was destroyed
99                 }
100                 return status;
101         }
102
103
104 }