1 /*******************************************************************************
2  * Copyright (c) 2000, 2010 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.team.examples.localhistory;
15 
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
19 
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IFileState;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.team.core.TeamException;
28 import org.eclipse.team.core.subscribers.Subscriber;
29 import org.eclipse.team.core.synchronize.SyncInfo;
30 import org.eclipse.team.core.variants.IResourceVariant;
31 import org.eclipse.team.core.variants.IResourceVariantComparator;
32 
33 public class LocalHistorySubscriber extends Subscriber {
34 
35 	private LocalHistoryVariantComparator comparator;
36 
LocalHistorySubscriber()37 	public LocalHistorySubscriber() {
38 		this.comparator = new LocalHistoryVariantComparator();
39 	}
40 
41 	@Override
getName()42 	public String getName() {
43 		return "Local History Subscriber"; //$NON-NLS-1$
44 	}
45 
46 	/**
47 	 * @param resource the resource being tested
48 	 */
49 	@Override
isSupervised(IResource resource)50 	public boolean isSupervised(IResource resource) {
51 		// all resources in the workspace can potentially have resource history
52 		return true;
53 	}
54 
55 	@Override
members(IResource resource)56 	public IResource[] members(IResource resource) throws TeamException {
57 		try {
58 			if(resource.getType() == IResource.FILE)
59 				return new IResource[0];
60 			IContainer container = (IContainer)resource;
61 			List<IResource> existingChildren = new ArrayList<>(Arrays.asList(container.members()));
62 			existingChildren.addAll(Arrays.asList(container.findDeletedMembersWithHistory(IResource.DEPTH_INFINITE, null)));
63 			return existingChildren.toArray(new IResource[existingChildren.size()]);
64 		} catch (CoreException e) {
65 			throw TeamException.asTeamException(e);
66 		}
67 	}
68 
69 	@Override
roots()70 	public IResource[] roots() {
71 		return ResourcesPlugin.getWorkspace().getRoot().getProjects();
72 	}
73 
74 	@Override
getSyncInfo(IResource resource)75 	public SyncInfo getSyncInfo(IResource resource) throws TeamException {
76 		try {
77 			IResourceVariant variant = null;
78 			if(resource.getType() == IResource.FILE) {
79 				IFile file = (IFile)resource;
80 				IFileState[] states = file.getHistory(null);
81 				if(states.length > 0) {
82 					// last state only
83 					variant = new LocalHistoryVariant(states[0]);
84 				}
85 			}
86 			SyncInfo info = new LocalHistorySyncInfo(resource, variant, comparator);
87 			info.init();
88 			return info;
89 		} catch (CoreException e) {
90 			throw TeamException.asTeamException(e);
91 		}
92 	}
93 
94 	@Override
getResourceComparator()95 	public IResourceVariantComparator getResourceComparator() {
96 		return comparator;
97 	}
98 
99 	/**
100 	 * @param resources
101 	 *            the resources to refresh
102 	 * @param depth
103 	 *            the depth
104 	 * @param monitor
105 	 *            progress monitor, or <code>null</code> if progress reporting
106 	 *            and cancellation are not desired
107 	 */
108 	@Override
refresh(IResource[] resources, int depth, IProgressMonitor monitor)109 	public void refresh(IResource[] resources, int depth, IProgressMonitor monitor) {
110 		// do nothing
111 	}
112 }
113