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 java.io.InputStream;
14 import java.util.ArrayList;
15 import java.util.HashMap;
17 import net.sourceforge.phpdt.core.ICompilationUnit;
18 import net.sourceforge.phpdt.core.IJavaElement;
19 import net.sourceforge.phpdt.core.IJavaElementDelta;
20 import net.sourceforge.phpdt.core.IJavaModel;
21 import net.sourceforge.phpdt.core.IJavaModelStatus;
22 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
23 import net.sourceforge.phpdt.core.IPackageFragment;
24 import net.sourceforge.phpdt.core.IWorkingCopy;
25 import net.sourceforge.phpdt.core.JavaModelException;
26 import net.sourceforge.phpdt.internal.core.util.PerThreadObject;
28 import org.eclipse.core.resources.IContainer;
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.core.resources.IFolder;
31 import org.eclipse.core.resources.IResource;
32 import org.eclipse.core.resources.IResourceStatus;
33 import org.eclipse.core.resources.IWorkspace;
34 import org.eclipse.core.resources.IWorkspaceRunnable;
35 import org.eclipse.core.runtime.CoreException;
36 import org.eclipse.core.runtime.IPath;
37 import org.eclipse.core.runtime.IProgressMonitor;
38 import org.eclipse.core.runtime.OperationCanceledException;
39 import org.eclipse.core.runtime.Path;
40 import org.eclipse.core.runtime.SubProgressMonitor;
44 * Defines behavior common to all Java Model operations
46 public abstract class JavaModelOperation implements IWorkspaceRunnable, IProgressMonitor {
47 protected interface IPostAction {
49 * Returns the id of this action.
50 * @see JavaModelOperation#postAction
56 void run() throws JavaModelException;
59 * Constants controlling the insertion mode of an action.
60 * @see JavaModelOperation#postAction
62 protected static final int APPEND = 1; // insert at the end
63 protected static final int REMOVEALL_APPEND = 2; // remove all existing ones with same ID, and add new one at the end
64 protected static final int KEEP_EXISTING = 3; // do not insert if already existing with same ID
67 * Whether tracing post actions is enabled.
69 protected static boolean POST_ACTION_VERBOSE;
72 * A list of IPostActions.
74 protected IPostAction[] actions;
75 protected int actionsStart = 0;
76 protected int actionsEnd = -1;
78 * A HashMap of attributes that can be used by operations
80 protected HashMap attributes;
82 public static final String HAS_MODIFIED_RESOURCE_ATTR = "hasModifiedResource"; //$NON-NLS-1$
83 public static final String TRUE = "true"; //$NON-NLS-1$
84 //public static final String FALSE = "false"; //$NON-NLS-1$
87 * The elements this operation operates on,
88 * or <code>null</code> if this operation
89 * does not operate on specific elements.
91 protected IJavaElement[] fElementsToProcess;
93 * The parent elements this operation operates with
94 * or <code>null</code> if this operation
95 * does not operate with specific parent elements.
97 protected IJavaElement[] fParentElements;
99 * An empty collection of <code>IJavaElement</code>s - the common
100 * empty result if no elements are created, or if this
101 * operation is not actually executed.
103 protected static IJavaElement[] fgEmptyResult= new IJavaElement[] {};
107 * The elements created by this operation - empty
108 * until the operation actually creates elements.
110 protected IJavaElement[] fResultElements= fgEmptyResult;
113 * The progress monitor passed into this operation
115 protected IProgressMonitor fMonitor= null;
117 * A flag indicating whether this operation is nested.
119 protected boolean fNested = false;
121 * Conflict resolution policy - by default do not force (fail on a conflict).
123 protected boolean fForce= false;
126 * A per thread stack of java model operations (PerThreadObject of ArrayList).
128 protected static PerThreadObject operationStacks = new PerThreadObject();
129 protected JavaModelOperation() {
132 * A common constructor for all Java Model operations.
134 protected JavaModelOperation(IJavaElement[] elements) {
135 fElementsToProcess = elements;
138 * Common constructor for all Java Model operations.
140 protected JavaModelOperation(IJavaElement[] elementsToProcess, IJavaElement[] parentElements) {
141 fElementsToProcess = elementsToProcess;
142 fParentElements= parentElements;
145 * A common constructor for all Java Model operations.
147 protected JavaModelOperation(IJavaElement[] elementsToProcess, IJavaElement[] parentElements, boolean force) {
148 fElementsToProcess = elementsToProcess;
149 fParentElements= parentElements;
153 * A common constructor for all Java Model operations.
155 protected JavaModelOperation(IJavaElement[] elements, boolean force) {
156 fElementsToProcess = elements;
161 * Common constructor for all Java Model operations.
163 protected JavaModelOperation(IJavaElement element) {
164 fElementsToProcess = new IJavaElement[]{element};
167 * A common constructor for all Java Model operations.
169 protected JavaModelOperation(IJavaElement element, boolean force) {
170 fElementsToProcess = new IJavaElement[]{element};
175 * Registers the given action at the end of the list of actions to run.
177 protected void addAction(IPostAction action) {
178 int length = this.actions.length;
179 if (length == ++this.actionsEnd) {
180 System.arraycopy(this.actions, 0, this.actions = new IPostAction[length*2], 0, length);
182 this.actions[this.actionsEnd] = action;
185 * Registers the given delta with the Java Model Manager.
187 protected void addDelta(IJavaElementDelta delta) {
188 JavaModelManager.getJavaModelManager().registerJavaModelDelta(delta);
191 * Registers the given reconcile delta with the Java Model Manager.
193 protected void addReconcileDelta(IWorkingCopy workingCopy, IJavaElementDelta delta) {
194 HashMap reconcileDeltas = JavaModelManager.getJavaModelManager().reconcileDeltas;
195 JavaElementDelta previousDelta = (JavaElementDelta)reconcileDeltas.get(workingCopy);
196 if (previousDelta != null) {
197 IJavaElementDelta[] children = delta.getAffectedChildren();
198 for (int i = 0, length = children.length; i < length; i++) {
199 JavaElementDelta child = (JavaElementDelta)children[i];
200 previousDelta.insertDeltaTree(child.getElement(), child);
203 reconcileDeltas.put(workingCopy, delta);
207 * Deregister the reconcile delta for the given working copy
209 protected void removeReconcileDelta(IWorkingCopy workingCopy) {
210 JavaModelManager.getJavaModelManager().reconcileDeltas.remove(workingCopy);
213 * @see IProgressMonitor
215 public void beginTask(String name, int totalWork) {
216 if (fMonitor != null) {
217 fMonitor.beginTask(name, totalWork);
221 * Checks with the progress monitor to see whether this operation
222 * should be canceled. An operation should regularly call this method
223 * during its operation so that the user can cancel it.
225 * @exception OperationCanceledException if cancelling the operation has been requested
226 * @see IProgressMonitor#isCanceled
228 protected void checkCanceled() {
230 throw new OperationCanceledException(Util.bind("operation.cancelled")); //$NON-NLS-1$
234 * Common code used to verify the elements this operation is processing.
235 * @see JavaModelOperation#verify()
237 protected IJavaModelStatus commonVerify() {
238 if (fElementsToProcess == null || fElementsToProcess.length == 0) {
239 return new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS);
241 for (int i = 0; i < fElementsToProcess.length; i++) {
242 if (fElementsToProcess[i] == null) {
243 return new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS);
246 return JavaModelStatus.VERIFIED_OK;
249 * Convenience method to copy resources
251 protected void copyResources(IResource[] resources, IPath destinationPath) throws JavaModelException {
252 IProgressMonitor subProgressMonitor = getSubProgressMonitor(resources.length);
253 IWorkspace workspace = resources[0].getWorkspace();
255 workspace.copy(resources, destinationPath, false, subProgressMonitor);
256 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
257 } catch (CoreException e) {
258 throw new JavaModelException(e);
262 * Convenience method to create a file
264 protected void createFile(IContainer folder, String name, InputStream contents, boolean force) throws JavaModelException {
265 IFile file= folder.getFile(new Path(name));
269 force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY,
270 getSubProgressMonitor(1));
271 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
272 } catch (CoreException e) {
273 throw new JavaModelException(e);
277 * Convenience method to create a folder
279 protected void createFolder(IContainer parentFolder, String name, boolean force) throws JavaModelException {
280 IFolder folder= parentFolder.getFolder(new Path(name));
282 // we should use true to create the file locally. Only VCM should use tru/false
284 force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY,
286 getSubProgressMonitor(1));
287 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
288 } catch (CoreException e) {
289 throw new JavaModelException(e);
293 * Convenience method to delete an empty package fragment
295 protected void deleteEmptyPackageFragment(
296 IPackageFragment fragment,
298 IResource rootResource)
299 throws JavaModelException {
301 IContainer resource = (IContainer) fragment.getResource();
305 force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY,
306 getSubProgressMonitor(1));
307 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
308 while (resource instanceof IFolder) {
309 // deleting a package: delete the parent if it is empty (eg. deleting x.y where folder x doesn't have resources but y)
310 // without deleting the package fragment root
311 resource = resource.getParent();
312 if (!resource.equals(rootResource) && resource.members().length == 0) {
314 force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY,
315 getSubProgressMonitor(1));
316 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
319 } catch (CoreException e) {
320 throw new JavaModelException(e);
324 * Convenience method to delete a resource
326 protected void deleteResource(IResource resource,int flags) throws JavaModelException {
328 resource.delete(flags, getSubProgressMonitor(1));
329 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
330 } catch (CoreException e) {
331 throw new JavaModelException(e);
335 * Convenience method to delete resources
337 protected void deleteResources(IResource[] resources, boolean force) throws JavaModelException {
338 if (resources == null || resources.length == 0) return;
339 IProgressMonitor subProgressMonitor = getSubProgressMonitor(resources.length);
340 IWorkspace workspace = resources[0].getWorkspace();
344 force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY,
346 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
347 } catch (CoreException e) {
348 throw new JavaModelException(e);
352 * @see IProgressMonitor
355 if (fMonitor != null) {
360 * Returns whether the given path is equals to one of the given other paths.
362 protected boolean equalsOneOf(IPath path, IPath[] otherPaths) {
363 for (int i = 0, length = otherPaths.length; i < length; i++) {
364 if (path.equals(otherPaths[i])) {
371 * Verifies the operation can proceed and executes the operation.
372 * Subclasses should override <code>#verify</code> and
373 * <code>executeOperation</code> to implement the specific operation behavior.
375 * @exception JavaModelException The operation has failed.
377 protected void execute() throws JavaModelException {
378 IJavaModelStatus status= verify();
380 // if first time here, computes the root infos before executing the operation
381 DeltaProcessor deltaProcessor = JavaModelManager.getJavaModelManager().deltaProcessor;
382 if (deltaProcessor.roots == null) {
383 // TODO khartlage temp-del
384 deltaProcessor.initializeRoots();
388 throw new JavaModelException(status);
392 * Convenience method to run an operation within this operation
394 public void executeNestedOperation(JavaModelOperation operation, int subWorkAmount) throws JavaModelException {
395 IProgressMonitor subProgressMonitor = getSubProgressMonitor(subWorkAmount);
396 // fix for 1FW7IKC, part (1)
398 operation.setNested(true);
399 operation.run(subProgressMonitor);
400 } catch (CoreException ce) {
401 if (ce instanceof JavaModelException) {
402 throw (JavaModelException)ce;
404 // translate the core exception to a java model exception
405 if (ce.getStatus().getCode() == IResourceStatus.OPERATION_FAILED) {
406 Throwable e = ce.getStatus().getException();
407 if (e instanceof JavaModelException) {
408 throw (JavaModelException) e;
411 throw new JavaModelException(ce);
416 * Performs the operation specific behavior. Subclasses must override.
418 protected abstract void executeOperation() throws JavaModelException;
420 * Returns the attribute registered at the given key with the top level operation.
421 * Returns null if no such attribute is found.
423 protected Object getAttribute(Object key) {
424 ArrayList stack = this.getCurrentOperationStack();
425 if (stack.size() == 0) return null;
426 JavaModelOperation topLevelOp = (JavaModelOperation)stack.get(0);
427 if (topLevelOp.attributes == null) {
430 return topLevelOp.attributes.get(key);
434 * Returns the compilation unit the given element is contained in,
435 * or the element itself (if it is a compilation unit),
436 * otherwise <code>null</code>.
438 protected ICompilationUnit getCompilationUnitFor(IJavaElement element) {
440 return ((JavaElement)element).getCompilationUnit();
443 * Returns the stack of operations running in the current thread.
444 * Returns an empty stack if no operations are currently running in this thread.
446 protected ArrayList getCurrentOperationStack() {
447 ArrayList stack = (ArrayList)operationStacks.getCurrent();
449 stack = new ArrayList();
450 operationStacks.setCurrent(stack);
455 * Returns the elements to which this operation applies,
456 * or <code>null</code> if not applicable.
458 protected IJavaElement[] getElementsToProcess() {
459 return fElementsToProcess;
462 * Returns the element to which this operation applies,
463 * or <code>null</code> if not applicable.
465 protected IJavaElement getElementToProcess() {
466 if (fElementsToProcess == null || fElementsToProcess.length == 0) {
469 return fElementsToProcess[0];
472 * Returns the Java Model this operation is operating in.
474 public IJavaModel getJavaModel() {
475 if (fElementsToProcess == null || fElementsToProcess.length == 0) {
476 return getParentElement().getJavaModel();
478 return fElementsToProcess[0].getJavaModel();
481 // protected IPath[] getNestedFolders(IPackageFragmentRoot root) throws JavaModelException {
482 // IPath rootPath = root.getPath();
483 // IClasspathEntry[] classpath = root.getJavaProject().getRawClasspath();
484 // int length = classpath.length;
485 // IPath[] result = new IPath[length];
487 // for (int i = 0; i < length; i++) {
488 // IPath path = classpath[i].getPath();
489 // if (rootPath.isPrefixOf(path) && !rootPath.equals(path)) {
490 // result[index++] = path;
493 // if (index < length) {
494 // System.arraycopy(result, 0, result = new IPath[index], 0, index);
499 * Returns the parent element to which this operation applies,
500 * or <code>null</code> if not applicable.
502 protected IJavaElement getParentElement() {
503 if (fParentElements == null || fParentElements.length == 0) {
506 return fParentElements[0];
509 * Returns the parent elements to which this operation applies,
510 * or <code>null</code> if not applicable.
512 protected IJavaElement[] getParentElements() {
513 return fParentElements;
516 * Returns the elements created by this operation.
518 public IJavaElement[] getResultElements() {
519 return fResultElements;
522 * Creates and returns a subprogress monitor if appropriate.
524 protected IProgressMonitor getSubProgressMonitor(int workAmount) {
525 IProgressMonitor sub = null;
526 if (fMonitor != null) {
527 sub = new SubProgressMonitor(fMonitor, workAmount, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
533 * Returns whether this operation has performed any resource modifications.
534 * Returns false if this operation has not been executed yet.
536 public boolean hasModifiedResource() {
537 return !this.isReadOnly() && this.getAttribute(HAS_MODIFIED_RESOURCE_ATTR) == TRUE;
539 public void internalWorked(double work) {
540 if (fMonitor != null) {
541 fMonitor.internalWorked(work);
545 * @see IProgressMonitor
547 public boolean isCanceled() {
548 if (fMonitor != null) {
549 return fMonitor.isCanceled();
554 * Returns <code>true</code> if this operation performs no resource modifications,
555 * otherwise <code>false</code>. Subclasses must override.
557 public boolean isReadOnly() {
561 * Returns whether this operation is the first operation to run in the current thread.
563 protected boolean isTopLevelOperation() {
566 (stack = this.getCurrentOperationStack()).size() > 0
567 && stack.get(0) == this;
570 * Returns the index of the first registered action with the given id, starting from a given position.
571 * Returns -1 if not found.
573 protected int firstActionWithID(String id, int start) {
574 for (int i = start; i <= this.actionsEnd; i++) {
575 if (this.actions[i].getID().equals(id)) {
583 * Convenience method to move resources
585 protected void moveResources(IResource[] resources, IPath destinationPath) throws JavaModelException {
586 IProgressMonitor subProgressMonitor = null;
587 if (fMonitor != null) {
588 subProgressMonitor = new SubProgressMonitor(fMonitor, resources.length, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
590 IWorkspace workspace = resources[0].getWorkspace();
592 workspace.move(resources, destinationPath, false, subProgressMonitor);
593 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
594 } catch (CoreException e) {
595 throw new JavaModelException(e);
599 * Creates and returns a new <code>IJavaElementDelta</code>
602 public JavaElementDelta newJavaElementDelta() {
603 return new JavaElementDelta(getJavaModel());
606 * Removes the last pushed operation from the stack of running operations.
607 * Returns the poped operation or null if the stack was empty.
609 protected JavaModelOperation popOperation() {
610 ArrayList stack = getCurrentOperationStack();
611 int size = stack.size();
613 if (size == 1) { // top level operation
614 operationStacks.setCurrent(null); // release reference (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=33927)
616 return (JavaModelOperation)stack.remove(size-1);
622 * Registers the given action to be run when the outer most java model operation has finished.
623 * The insertion mode controls whether:
624 * - the action should discard all existing actions with the same id, and be queued at the end (REMOVEALL_APPEND),
625 * - the action should be ignored if there is already an action with the same id (KEEP_EXISTING),
626 * - the action should be queued at the end without looking at existing actions (APPEND)
628 protected void postAction(IPostAction action, int insertionMode) {
629 if (POST_ACTION_VERBOSE) {
630 System.out.print("(" + Thread.currentThread() + ") [JavaModelOperation.postAction(IPostAction, int)] Posting action " + action.getID()); //$NON-NLS-1$ //$NON-NLS-2$
631 switch(insertionMode) {
632 case REMOVEALL_APPEND:
633 System.out.println(" (REMOVEALL_APPEND)"); //$NON-NLS-1$
636 System.out.println(" (KEEP_EXISTING)"); //$NON-NLS-1$
639 System.out.println(" (APPEND)"); //$NON-NLS-1$
644 JavaModelOperation topLevelOp = (JavaModelOperation)getCurrentOperationStack().get(0);
645 IPostAction[] postActions = topLevelOp.actions;
646 if (postActions == null) {
647 topLevelOp.actions = postActions = new IPostAction[1];
648 postActions[0] = action;
649 topLevelOp.actionsEnd = 0;
651 String id = action.getID();
652 switch (insertionMode) {
653 case REMOVEALL_APPEND :
654 int index = this.actionsStart-1;
655 while ((index = topLevelOp.firstActionWithID(id, index+1)) >= 0) {
656 // remove action[index]
657 System.arraycopy(postActions, index+1, postActions, index, topLevelOp.actionsEnd - index);
658 postActions[topLevelOp.actionsEnd--] = null;
660 topLevelOp.addAction(action);
663 if (topLevelOp.firstActionWithID(id, 0) < 0) {
664 topLevelOp.addAction(action);
668 topLevelOp.addAction(action);
674 * Returns whether the given path is the prefix of one of the given other paths.
676 protected boolean prefixesOneOf(IPath path, IPath[] otherPaths) {
677 for (int i = 0, length = otherPaths.length; i < length; i++) {
678 if (path.isPrefixOf(otherPaths[i])) {
685 * Pushes the given operation on the stack of operations currently running in this thread.
687 protected void pushOperation(JavaModelOperation operation) {
688 getCurrentOperationStack().add(operation);
692 * Main entry point for Java Model operations. Executes this operation
693 * and registers any deltas created.
695 * @see IWorkspaceRunnable
696 * @exception CoreException if the operation fails
698 public void run(IProgressMonitor monitor) throws CoreException {
699 JavaModelManager manager = JavaModelManager.getJavaModelManager();
700 int previousDeltaCount = manager.javaModelDeltas.size();
707 if (this.isTopLevelOperation()) {
708 this.runPostActions();
714 // TODO khartlage temp-del
715 // update JavaModel using deltas that were recorded during this operation
716 // for (int i = previousDeltaCount, size = manager.javaModelDeltas.size(); i < size; i++) {
717 // manager.updateJavaModel((IJavaElementDelta)manager.javaModelDeltas.get(i));
721 // - the operation is a top level operation
722 // - the operation did produce some delta(s)
723 // - but the operation has not modified any resource
724 if (this.isTopLevelOperation()) {
725 if ((manager.javaModelDeltas.size() > previousDeltaCount || !manager.reconcileDeltas.isEmpty())
726 && !this.hasModifiedResource()) {
727 manager.fire(null, JavaModelManager.DEFAULT_CHANGE_EVENT);
728 } // else deltas are fired while processing the resource delta
735 protected void runPostActions() throws JavaModelException {
736 while (this.actionsStart <= this.actionsEnd) {
737 IPostAction postAction = this.actions[this.actionsStart++];
738 if (POST_ACTION_VERBOSE) {
739 System.out.println("(" + Thread.currentThread() + ") [JavaModelOperation.runPostActions()] Running action " + postAction.getID()); //$NON-NLS-1$ //$NON-NLS-2$
745 * Registers the given attribute at the given key with the top level operation.
747 protected void setAttribute(Object key, Object attribute) {
748 JavaModelOperation topLevelOp = (JavaModelOperation)this.getCurrentOperationStack().get(0);
749 if (topLevelOp.attributes == null) {
750 topLevelOp.attributes = new HashMap();
752 topLevelOp.attributes.put(key, attribute);
755 * @see IProgressMonitor
757 public void setCanceled(boolean b) {
758 if (fMonitor != null) {
759 fMonitor.setCanceled(b);
763 * Sets whether this operation is nested or not.
764 * @see CreateElementInCUOperation#checkCanceled
766 protected void setNested(boolean nested) {
770 * @see IProgressMonitor
772 public void setTaskName(String name) {
773 if (fMonitor != null) {
774 fMonitor.setTaskName(name);
778 * @see IProgressMonitor
780 public void subTask(String name) {
781 if (fMonitor != null) {
782 fMonitor.subTask(name);
786 * Returns a status indicating if there is any known reason
787 * this operation will fail. Operations are verified before they
790 * Subclasses must override if they have any conditions to verify
791 * before this operation executes.
793 * @see IJavaModelStatus
795 protected IJavaModelStatus verify() {
796 return commonVerify();
800 * @see IProgressMonitor
802 public void worked(int work) {
803 if (fMonitor != null) {
804 fMonitor.worked(work);