/*******************************************************************************
* Copyright (c) 2000, 2017 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.team.internal.ui.mapping;
import java.util.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.resources.mapping.*;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.team.core.diff.*;
import org.eclipse.team.core.mapping.*;
import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
import org.eclipse.team.internal.ui.*;
import org.eclipse.team.ui.mapping.ITeamContentProviderManager;
import org.eclipse.team.ui.mapping.SynchronizationContentProvider;
import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
/**
* This content provider displays the mappings as a flat list
* of elements.
*
* There are three use-cases we need to consider. The first is when there
* are resource level mappings to be displayed. The second is when there
* are mappings from a model provider that does not have a content provider
* registered. The third is for the case where a resource mapping does not
* have a model provider registered (this may be considered an error case).
*
*/
public class ResourceModelContentProvider extends SynchronizationContentProvider implements ITreePathContentProvider {
private WorkbenchContentProvider provider;
@Override
protected ITreeContentProvider getDelegateContentProvider() {
if (provider == null)
provider = new WorkbenchContentProvider();
return provider;
}
@Override
protected String getModelProviderId() {
return ModelProvider.RESOURCE_MODEL_PROVIDER_ID;
}
@Override
protected Object getModelRoot() {
return ResourcesPlugin.getWorkspace().getRoot();
}
@Override
protected boolean isInScope(ISynchronizationScope scope, Object parent, Object elementOrPath) {
Object object = internalGetElement(elementOrPath);
if (object instanceof IResource) {
IResource resource = (IResource) object;
if (!resource.getProject().isAccessible())
return false;
if (scope.contains(resource))
return true;
if (hasChildrenInScope(scope, object, resource)) {
return true;
}
}
return false;
}
private boolean hasChildrenInScope(ISynchronizationScope scope, Object object, IResource resource) {
if (!resource.isAccessible())
return false;
IResource[] roots = scope.getRoots();
for (int i = 0; i < roots.length; i++) {
IResource root = roots[i];
if (resource.getFullPath().isPrefixOf(root.getFullPath()))
return true;
}
return false;
}
@Override
public void init(ICommonContentExtensionSite site) {
super.init(site);
TeamUIPlugin.getPlugin().getPreferenceStore().addPropertyChangeListener(this);
}
@Override
public void dispose() {
if (provider != null)
provider.dispose();
super.dispose();
TeamUIPlugin.getPlugin().getPreferenceStore().removePropertyChangeListener(this);
}
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getProperty().equals(IPreferenceIds.SYNCVIEW_DEFAULT_LAYOUT)) {
refresh();
}
super.propertyChange(event);
}
@Override
protected Object[] getChildrenInContext(ISynchronizationContext context, Object parentOrPath, Object[] children) {
Object parent = internalGetElement(parentOrPath);
if (parent instanceof IResource) {
IResource resource = (IResource) parent;
if (resource.getType() == IResource.PROJECT && !resource.getProject().isAccessible())
return new Object[0];
IResourceDiffTree diffTree = context.getDiffTree();
// TODO: Could optimize this to a single pass over the children instead of 3
children = getTraversalCalculator().filterChildren(diffTree, resource, parentOrPath, children);
if (children.length != 0)
children = getChildrenInScope(context.getScope(), parentOrPath, children);
if (children.length != 0)
children = internalGetChildren(context, parentOrPath, children);
return children;
}
return super.getChildrenInContext(context, parentOrPath, children);
}
private Object[] internalGetChildren(ISynchronizationContext context, Object parent, Object[] children) {
List