Skip to main content
summaryrefslogtreecommitdiffstats
blob: dffa69160e0b967977b9ac2b25236add1ae44af9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.ui.synchronize;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.ui.IMemento;

/**
 * A synchronize scope whose roots are a set of resources.
 * <p>
 * Clients are not expected to subclass this class.
 * @since 3.0
 */
public class ResourceScope extends AbstractSynchronizeScope {
	
	/*
	 * Constants used to save and restore this scope
	 */
	private final static String CTX_ROOT = "resource_scope_roots"; //$NON-NLS-1$
	private final static String CTX_ROOT_PATH = "resource_scope_root_resource"; //$NON-NLS-1$
	
	/*
	 * The resources that define this scope
	 */
	private IResource[] resources;
	
	/**
	 * Create the resource scope for the given resources
	 * @param resources the resources that define this scope
	 */
	public ResourceScope(IResource[] resources) {
		this.resources = resources;
	}
	
	/** 
	 * Create this scope from it's previously saved state
	 * @param memento
	 */
	protected ResourceScope(IMemento memento) {
		super(memento);
	}
	
	/**
	 * Set the resources that define this scope
	 * @param resources the resources that define this scope
	 */
	public void setResources(IResource[] resources) {
		this.resources = resources;
		fireRootsChanges();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ui.synchronize.ScopableSubscriberParticipant.ISynchronizeScope#getName()
	 */
	public String getName() {
		return Utils.convertSelection(resources, 4);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ui.synchronize.ScopableSubscriberParticipant.ISynchronizeScope#getRoots()
	 */
	public IResource[] getRoots() {
		return resources;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ui.synchronize.ScopableSubscriberParticipant.ISynchronizeScope#dispose()
	 */
	public void dispose() {
		// Nothing to dispose
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ui.synchronize.ScopableSubscriberParticipant.ISynchronizeScope#saveState(org.eclipse.ui.IMemento)
	 */
	public void saveState(IMemento memento) {
		if (resources != null) {
			for (int i = 0; i < resources.length; i++) {
				IResource resource = resources[i];
				IMemento rootNode = memento.createChild(CTX_ROOT);
				rootNode.putString(CTX_ROOT_PATH, resource.getFullPath().toString());
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeScope#init(org.eclipse.ui.IMemento)
	 */
	protected void init(IMemento memento) {
		IMemento[] rootNodes = memento.getChildren(CTX_ROOT);
		if(rootNodes != null) {
			List resources = new ArrayList();
			for (int i = 0; i < rootNodes.length; i++) {
				IMemento rootNode = rootNodes[i];
				IPath path = new Path(rootNode.getString(CTX_ROOT_PATH)); //$NON-NLS-1$
				IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path, true /* include phantoms */);
				if(resource != null) {
					resources.add(resource);
				}
			}
			this.resources = (IResource[]) resources.toArray(new IResource[resources.size()]);
		}
	}
}

Back to the top