Search in sources :

Example 1 with IResourceDelta

use of org.eclipse.core.resources.IResourceDelta in project che by eclipse.

the class DeltaProcessor method traverseDelta.

/*
     * Converts an <code>IResourceDelta</code> and its children into
     * the corresponding <code>IJavaElementDelta</code>s.
     */
private void traverseDelta(IResourceDelta delta, int elementType, RootInfo rootInfo, OutputsInfo outputsInfo) {
    IResource res = delta.getResource();
    // set stack of elements
    if (this.currentElement == null && rootInfo != null) {
        this.currentElement = rootInfo.project;
    }
    // process current delta
    boolean processChildren = true;
    if (res instanceof IProject) {
        // reset source element parser cache
        this.sourceElementParserCache = null;
        processChildren = updateCurrentDeltaAndIndex(delta, elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT ? // case of prj=src
        IJavaElement.JAVA_PROJECT : elementType, rootInfo);
    } else if (rootInfo != null) {
        processChildren = updateCurrentDeltaAndIndex(delta, elementType, rootInfo);
    } else {
        // not yet inside a package fragment root
        processChildren = true;
    }
    // process children if needed
    if (processChildren) {
        IResourceDelta[] children = (IResourceDelta[]) delta.getAffectedChildren();
        boolean oneChildOnClasspath = false;
        int length = children.length;
        IResourceDelta[] orphanChildren = null;
        Openable parent = null;
        boolean isValidParent = true;
        for (int i = 0; i < length; i++) {
            IResourceDelta child = children[i];
            IResource childRes = child.getResource();
            // check source attachment change
            checkSourceAttachmentChange(child, childRes);
            // find out whether the child is a package fragment root of the current project
            IPath childPath = externalPath(childRes);
            int childKind = child.getKind();
            RootInfo childRootInfo = rootInfo(childPath, childKind);
            RootInfo originalChildRootInfo = childRootInfo;
            if (childRootInfo != null && !childRootInfo.isRootOfProject(childPath)) {
                // package fragment root of another project (dealt with later)
                childRootInfo = null;
            }
            // compute child type
            int childType = elementType(childRes, childKind, elementType, rootInfo == null ? childRootInfo : rootInfo);
            // is childRes in the output folder and is it filtered out ?
            boolean isResFilteredFromOutput = isResFilteredFromOutput(rootInfo, outputsInfo, childRes, childType);
            boolean isNestedRoot = rootInfo != null && childRootInfo != null;
            if (!isResFilteredFromOutput && !isNestedRoot) {
                // do not treat as non-java rsc if nested root
                traverseDelta(child, childType, rootInfo == null ? childRootInfo : rootInfo, // traverse delta for child in the same project
                outputsInfo);
                if (childType == NON_JAVA_RESOURCE) {
                    if (rootInfo != null) {
                        // if inside a package fragment root
                        if (!isValidParent)
                            continue;
                        if (parent == null) {
                            // find the parent of the non-java resource to attach to
                            if (this.currentElement == null || !rootInfo.project.equals(this.currentElement.getJavaProject())) {
                                // note if currentElement is the
                                // IJavaModel, getJavaProject() is null
                                // force the currentProject to be used
                                this.currentElement = rootInfo.project;
                            }
                            if (elementType == IJavaElement.JAVA_PROJECT || (elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT && res instanceof IProject)) {
                                // NB: attach non-java resource to project (not to its package fragment root)
                                parent = rootInfo.project;
                            } else {
                                parent = createElement(res, elementType, rootInfo);
                            }
                            if (parent == null) {
                                isValidParent = false;
                                continue;
                            }
                        }
                        // add child as non java resource
                        try {
                            nonJavaResourcesChanged(parent, child);
                        } catch (JavaModelException e) {
                        // ignore
                        }
                    } else {
                        // the non-java resource (or its parent folder) will be attached to the java project
                        if (orphanChildren == null)
                            orphanChildren = new IResourceDelta[length];
                        orphanChildren[i] = child;
                    }
                } else {
                    if (rootInfo == null && childRootInfo == null) {
                        // the non-java resource (or its parent folder) will be attached to the java project
                        if (orphanChildren == null)
                            orphanChildren = new IResourceDelta[length];
                        orphanChildren[i] = child;
                    }
                }
            } else {
                // to avoid reporting child delta as non-java resource delta
                oneChildOnClasspath = true;
            }
            // but it is a package fragment root of another project, traverse delta too
            if (isNestedRoot || (childRootInfo == null && originalChildRootInfo != null)) {
                traverseDelta(child, IJavaElement.PACKAGE_FRAGMENT_ROOT, originalChildRootInfo, // binary output of childRootInfo.project cannot be this root
                null);
            }
            // if the child is a package fragment root of one or several other projects
            ArrayList rootList;
            if ((rootList = otherRootsInfo(childPath, childKind)) != null) {
                Iterator iterator = rootList.iterator();
                while (iterator.hasNext()) {
                    originalChildRootInfo = (RootInfo) iterator.next();
                    this.currentElement = // ensure that 2 roots refering to the same resource don't share the current element (see
                    null;
                    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=210746 )
                    traverseDelta(child, IJavaElement.PACKAGE_FRAGMENT_ROOT, originalChildRootInfo, // binary output of childRootInfo.project cannot be this root
                    null);
                }
            }
        }
        if (orphanChildren != null && (// orphan children are siblings of a package fragment root
        oneChildOnClasspath || res instanceof IProject)) {
        // non-java resource directly under a project
        //				// attach orphan children
        //				IProject rscProject = res.getProject();
        //				JavaProject adoptiveProject = (JavaProject)JavaCore.create(rscProject);
        //				if (adoptiveProject != null
        //						&& JavaProject.hasJavaNature(rscProject)) { // delta iff Java project (18698)
        //					for (int i = 0; i < length; i++) {
        //						if (orphanChildren[i] != null) {
        //							try {
        //								nonJavaResourcesChanged(adoptiveProject, orphanChildren[i]);
        //							} catch (JavaModelException e) {
        //								// ignore
        //							}
        //						}
        //					}
        //				}
        }
    // else resource delta will be added by parent
    }
// else resource delta will be added by parent
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject) IResourceDelta(org.eclipse.core.resources.IResourceDelta)

Example 2 with IResourceDelta

use of org.eclipse.core.resources.IResourceDelta in project che by eclipse.

the class DeltaProcessor method updateRootIndex.

/*
     * Updates the index of the given root (assuming it's an addition or a removal).
     * This is done recusively, pkg being the current package.
     */
private void updateRootIndex(PackageFragmentRoot root, String[] pkgName, IResourceDelta delta) {
    Openable pkg = root.getPackageFragment(pkgName);
    updateIndex(pkg, delta);
    IResourceDelta[] children = (IResourceDelta[]) delta.getAffectedChildren();
    for (int i = 0, length = children.length; i < length; i++) {
        IResourceDelta child = children[i];
        IResource resource = child.getResource();
        if (resource instanceof IFolder) {
            String[] subpkgName = Util.arrayConcat(pkgName, resource.getName());
            updateRootIndex(root, subpkgName, child);
        }
    }
}
Also used : IResource(org.eclipse.core.resources.IResource) IResourceDelta(org.eclipse.core.resources.IResourceDelta) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with IResourceDelta

use of org.eclipse.core.resources.IResourceDelta in project flux by eclipse.

the class Activator method initCoreService.

private void initCoreService(String userChannel) throws CoreException {
    repository = new Repository(messageConnector, userChannel);
    liveEditCoordinator = new LiveEditCoordinator(messageConnector);
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    resourceListener = new CloudSyncResourceListener(repository);
    workspace.addResourceChangeListener(resourceListener, IResourceChangeEvent.POST_CHANGE);
    metadataListener = new CloudSyncMetadataListener(repository);
    workspace.addResourceChangeListener(metadataListener, IResourceChangeEvent.POST_BUILD);
    this.repositoryListener = new IRepositoryListener() {

        @Override
        public void projectDisconnected(IProject project) {
            removeConnectedProjectPreference(project.getName());
        }

        @Override
        public void projectConnected(IProject project) {
            addConnectedProjectPreference(project.getName());
        }

        @Override
        public void resourceChanged(IResource resource) {
        // nothing
        }
    };
    getRepository().addRepositoryListener(repositoryListener);
    workspaceListener = new IResourceChangeListener() {

        public void resourceChanged(IResourceChangeEvent event) {
            if (event.getResource() instanceof IProject) {
                IResourceDelta delta = event.getDelta();
                if (delta == null) {
                    return;
                }
                if (delta.getKind() == IResourceDelta.REMOVED) {
                    IProject project = (IProject) event.getResource();
                    removeConnectedProjectPreference(project.getName());
                } else if (delta.getKind() == IResourceDelta.CHANGED) {
                // TODO, we aren't handling project renaming yet
                // IProject project = (IProject) event.getResource();
                // String oldName =
                // delta.getMovedFromPath().lastSegment();
                // removeConnectedProjectPreference(oldName);
                // addConnectedProjectPreference(project.getName());
                }
            }
        }
    };
    workspace.addResourceChangeListener(workspaceListener);
    updateProjectConnections();
}
Also used : IResourceChangeEvent(org.eclipse.core.resources.IResourceChangeEvent) IWorkspace(org.eclipse.core.resources.IWorkspace) CloudSyncMetadataListener(org.eclipse.flux.core.internal.CloudSyncMetadataListener) IResourceChangeListener(org.eclipse.core.resources.IResourceChangeListener) CloudSyncResourceListener(org.eclipse.flux.core.internal.CloudSyncResourceListener) IProject(org.eclipse.core.resources.IProject) IResource(org.eclipse.core.resources.IResource) IResourceDelta(org.eclipse.core.resources.IResourceDelta)

Example 4 with IResourceDelta

use of org.eclipse.core.resources.IResourceDelta in project translationstudio8 by heartsome.

the class ResourceExtensionContentProvider method processDelta.

/**
	 * Process a resource delta. Add any runnables
	 */
private void processDelta(IResourceDelta delta, Collection runnables) {
    //he widget may have been destroyed
    // by the time this is run. Check for this and do nothing if so.
    Control ctrl = viewer.getControl();
    if (ctrl == null || ctrl.isDisposed()) {
        return;
    }
    // Get the affected resource
    final IResource resource = delta.getResource();
    // If any children have changed type, just do a full refresh of this
    // parent,
    // since a simple update on such children won't work,
    // and trying to map the change to a remove and add is too dicey.
    // The case is: folder A renamed to existing file B, answering yes to
    // overwrite B.
    IResourceDelta[] affectedChildren = delta.getAffectedChildren(IResourceDelta.CHANGED);
    for (int i = 0; i < affectedChildren.length; i++) {
        if ((affectedChildren[i].getFlags() & IResourceDelta.TYPE) != 0) {
            runnables.add(getRefreshRunnable(resource));
            return;
        }
    }
    // Check the flags for changes the Navigator cares about.
    // See ResourceLabelProvider for the aspects it cares about.
    // Notice we don't care about F_CONTENT or F_MARKERS currently.
    int changeFlags = delta.getFlags();
    if ((changeFlags & (IResourceDelta.OPEN | IResourceDelta.SYNC | IResourceDelta.TYPE | IResourceDelta.DESCRIPTION)) != 0) {
        //			Runnable updateRunnable =  new Runnable(){
        //				public void run() {
        //					((StructuredViewer) viewer).update(resource, null);
        //				}
        //			};
        //			runnables.add(updateRunnable);
        /* support the Closed Projects filter; 
			 * when a project is closed, it may need to be removed from the view.
			 */
        runnables.add(getRefreshRunnable(resource.getParent()));
    }
    // Replacing a resource may affect its label and its children
    if ((changeFlags & IResourceDelta.REPLACED) != 0) {
        runnables.add(getRefreshRunnable(resource));
        return;
    }
    // Handle changed children .
    for (int i = 0; i < affectedChildren.length; i++) {
        processDelta(affectedChildren[i], runnables);
    }
    // @issue several problems here:
    //  - should process removals before additions, to avoid multiple equal
    // elements in viewer
    //   - Kim: processing removals before additions was the indirect cause of
    // 44081 and its varients
    //   - Nick: no delta should have an add and a remove on the same element,
    // so processing adds first is probably OK
    //  - using setRedraw will cause extra flashiness
    //  - setRedraw is used even for simple changes
    //  - to avoid seeing a rename in two stages, should turn redraw on/off
    // around combined removal and addition
    //   - Kim: done, and only in the case of a rename (both remove and add
    // changes in one delta).
    IResourceDelta[] addedChildren = delta.getAffectedChildren(IResourceDelta.ADDED);
    IResourceDelta[] removedChildren = delta.getAffectedChildren(IResourceDelta.REMOVED);
    if (addedChildren.length == 0 && removedChildren.length == 0) {
        return;
    }
    final Object[] addedObjects;
    final Object[] removedObjects;
    // Process additions before removals as to not cause selection
    // preservation prior to new objects being added
    // Handle added children. Issue one update for all insertions.
    int numMovedFrom = 0;
    int numMovedTo = 0;
    if (addedChildren.length > 0) {
        addedObjects = new Object[addedChildren.length];
        for (int i = 0; i < addedChildren.length; i++) {
            addedObjects[i] = addedChildren[i].getResource();
            if ((addedChildren[i].getFlags() & IResourceDelta.MOVED_FROM) != 0) {
                ++numMovedFrom;
            }
        }
    } else {
        addedObjects = new Object[0];
    }
    // Handle removed children. Issue one update for all removals.
    if (removedChildren.length > 0) {
        removedObjects = new Object[removedChildren.length];
        for (int i = 0; i < removedChildren.length; i++) {
            removedObjects[i] = removedChildren[i].getResource();
            if ((removedChildren[i].getFlags() & IResourceDelta.MOVED_TO) != 0) {
                ++numMovedTo;
            }
        }
    } else {
        removedObjects = new Object[0];
    }
    // heuristic test for items moving within same folder (i.e. renames)
    final boolean hasRename = numMovedFrom > 0 && numMovedTo > 0;
    Runnable addAndRemove = new Runnable() {

        public void run() {
            if (viewer instanceof AbstractTreeViewer) {
                AbstractTreeViewer treeViewer = (AbstractTreeViewer) viewer;
                // rename case)
                if (hasRename) {
                    treeViewer.getControl().setRedraw(false);
                }
                try {
                    if (addedObjects.length > 0) {
                        treeViewer.add(resource, addedObjects);
                    }
                    if (removedObjects.length > 0) {
                        treeViewer.remove(removedObjects);
                    }
                } finally {
                    if (hasRename) {
                        treeViewer.getControl().setRedraw(true);
                    }
                }
            } else {
                ((StructuredViewer) viewer).refresh(resource);
            }
        }
    };
    runnables.add(addAndRemove);
}
Also used : Control(org.eclipse.swt.widgets.Control) AbstractTreeViewer(org.eclipse.jface.viewers.AbstractTreeViewer) StructuredViewer(org.eclipse.jface.viewers.StructuredViewer) IResource(org.eclipse.core.resources.IResource) IResourceDelta(org.eclipse.core.resources.IResourceDelta)

Example 5 with IResourceDelta

use of org.eclipse.core.resources.IResourceDelta in project tdi-studio-se by Talend.

the class ResourceExtensionContentProvider method processDelta.

/**
     * Process a resource delta. Add any runnables
     */
private void processDelta(IResourceDelta delta, Collection runnables) {
    // he widget may have been destroyed
    // by the time this is run. Check for this and do nothing if so.
    Control ctrl = viewer.getControl();
    if (ctrl == null || ctrl.isDisposed()) {
        return;
    }
    // Get the affected resource
    final IResource resource = delta.getResource();
    // If any children have changed type, just do a full refresh of this
    // parent,
    // since a simple update on such children won't work,
    // and trying to map the change to a remove and add is too dicey.
    // The case is: folder A renamed to existing file B, answering yes to
    // overwrite B.
    IResourceDelta[] affectedChildren = delta.getAffectedChildren(IResourceDelta.CHANGED);
    for (int i = 0; i < affectedChildren.length; i++) {
        if ((affectedChildren[i].getFlags() & IResourceDelta.TYPE) != 0) {
            runnables.add(getRefreshRunnable(resource));
            return;
        }
    }
    // Check the flags for changes the Navigator cares about.
    // See ResourceLabelProvider for the aspects it cares about.
    // Notice we don't care about F_CONTENT or F_MARKERS currently.
    int changeFlags = delta.getFlags();
    if ((changeFlags & (IResourceDelta.OPEN | IResourceDelta.SYNC | IResourceDelta.TYPE | IResourceDelta.DESCRIPTION)) != 0) {
        // Runnable updateRunnable = new Runnable(){
        // public void run() {
        // ((StructuredViewer) viewer).update(resource, null);
        // }
        // };
        // runnables.add(updateRunnable);
        /*
             * support the Closed Projects filter; when a project is closed, it may need to be removed from the view.
             */
        runnables.add(getRefreshRunnable(resource.getParent()));
    }
    // Replacing a resource may affect its label and its children
    if ((changeFlags & IResourceDelta.REPLACED) != 0) {
        runnables.add(getRefreshRunnable(resource));
        return;
    }
    // Handle changed children .
    for (int i = 0; i < affectedChildren.length; i++) {
        processDelta(affectedChildren[i], runnables);
    }
    // @issue several problems here:
    // - should process removals before additions, to avoid multiple equal
    // elements in viewer
    // - Kim: processing removals before additions was the indirect cause of
    // 44081 and its varients
    // - Nick: no delta should have an add and a remove on the same element,
    // so processing adds first is probably OK
    // - using setRedraw will cause extra flashiness
    // - setRedraw is used even for simple changes
    // - to avoid seeing a rename in two stages, should turn redraw on/off
    // around combined removal and addition
    // - Kim: done, and only in the case of a rename (both remove and add
    // changes in one delta).
    IResourceDelta[] addedChildren = delta.getAffectedChildren(IResourceDelta.ADDED);
    IResourceDelta[] removedChildren = delta.getAffectedChildren(IResourceDelta.REMOVED);
    if (addedChildren.length == 0 && removedChildren.length == 0) {
        return;
    }
    final Object[] addedObjects;
    final Object[] removedObjects;
    // Process additions before removals as to not cause selection
    // preservation prior to new objects being added
    // Handle added children. Issue one update for all insertions.
    int numMovedFrom = 0;
    int numMovedTo = 0;
    if (addedChildren.length > 0) {
        addedObjects = new Object[addedChildren.length];
        for (int i = 0; i < addedChildren.length; i++) {
            addedObjects[i] = addedChildren[i].getResource();
            if ((addedChildren[i].getFlags() & IResourceDelta.MOVED_FROM) != 0) {
                ++numMovedFrom;
            }
        }
    } else {
        addedObjects = new Object[0];
    }
    // Handle removed children. Issue one update for all removals.
    if (removedChildren.length > 0) {
        removedObjects = new Object[removedChildren.length];
        for (int i = 0; i < removedChildren.length; i++) {
            removedObjects[i] = removedChildren[i].getResource();
            if ((removedChildren[i].getFlags() & IResourceDelta.MOVED_TO) != 0) {
                ++numMovedTo;
            }
        }
    } else {
        removedObjects = new Object[0];
    }
    // heuristic test for items moving within same folder (i.e. renames)
    final boolean hasRename = numMovedFrom > 0 && numMovedTo > 0;
    Runnable addAndRemove = new Runnable() {

        public void run() {
            if (viewer instanceof AbstractTreeViewer) {
                AbstractTreeViewer treeViewer = (AbstractTreeViewer) viewer;
                // rename case)
                if (hasRename) {
                    treeViewer.getControl().setRedraw(false);
                }
                try {
                    if (addedObjects.length > 0) {
                        treeViewer.add(resource, addedObjects);
                    }
                    if (removedObjects.length > 0) {
                        treeViewer.remove(removedObjects);
                    }
                } finally {
                    if (hasRename) {
                        treeViewer.getControl().setRedraw(true);
                    }
                }
            } else {
                ((StructuredViewer) viewer).refresh(resource);
            }
        }
    };
    runnables.add(addAndRemove);
}
Also used : Control(org.eclipse.swt.widgets.Control) AbstractTreeViewer(org.eclipse.jface.viewers.AbstractTreeViewer) StructuredViewer(org.eclipse.jface.viewers.StructuredViewer) IResource(org.eclipse.core.resources.IResource) IResourceDelta(org.eclipse.core.resources.IResourceDelta)

Aggregations

IResourceDelta (org.eclipse.core.resources.IResourceDelta)33 IResource (org.eclipse.core.resources.IResource)21 IPath (org.eclipse.core.runtime.IPath)12 IProject (org.eclipse.core.resources.IProject)10 CoreException (org.eclipse.core.runtime.CoreException)9 IResourceDeltaVisitor (org.eclipse.core.resources.IResourceDeltaVisitor)8 ArrayList (java.util.ArrayList)7 IFile (org.eclipse.core.resources.IFile)5 HashSet (java.util.HashSet)4 List (java.util.List)4 Iterator (java.util.Iterator)3 IResourceChangeListener (org.eclipse.core.resources.IResourceChangeListener)3 Project (aQute.bnd.build.Project)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IFolder (org.eclipse.core.resources.IFolder)2 IResourceChangeEvent (org.eclipse.core.resources.IResourceChangeEvent)2 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2