Skip to main content
summaryrefslogtreecommitdiffstats
blob: f4e3378bee8d3a85ed44845a82b7fdf380f58c72 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * Copyright (c) 2000, 2003 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.internal.ui.sync.views;

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

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.team.core.subscribers.SyncInfo;

/**
 * This is the UI model object representing a SyncInfo for a resource.
 * The main purpose of this class is to allow menu object contributions
 * to be applied to these resources.
 */
public class SyncResource implements IAdaptable {

	private SyncSet syncSet;
	private IResource resource;

	/**
	 * @param info
	 */
	public SyncResource(SyncSet syncSet, IResource resource) {
		this.syncSet = syncSet;
		this.resource = resource;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
	 */
	public Object getAdapter(Class adapter) {
		if (adapter == IResource.class) {
			return getLocalResource();
		} else if (adapter == SyncInfo.class) {
			return getSyncInfo();
		}
		return null;
	}

	/**
	 * @return
	 */
	public IResource getLocalResource() {
		return resource;
	}

	/**
	 * @return
	 */
	public SyncInfo getSyncInfo() {
		return syncSet.getSyncInfo(resource);
	}
	
	/**
	 * Return an array of all descendants (including the receiver) that have
	 * a non-null sync-info.
	 * @return
	 */
	public SyncResource[] getOutOfSyncDescendants() {
		List result = new ArrayList();
		SyncInfo info = getSyncInfo();
		if (info != null) {
			result.add(this);
		}
		Object[] members = SyncSet.members(syncSet, getLocalResource());
		for (int i = 0; i < members.length; i++) {
			Object object = members[i];
			if (object instanceof SyncResource) {
				SyncResource child = (SyncResource) object;
				result.addAll(Arrays.asList(child.getOutOfSyncDescendants()));
			}
		}
		return (SyncResource[]) result.toArray(new SyncResource[result.size()]);
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object object) {
		if (object instanceof SyncResource) {
			SyncResource syncResource = (SyncResource) object;
			return getLocalResource().equals(syncResource.getLocalResource());
		}
		return super.equals(object);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return getLocalResource().hashCode();
	}

	/**
	 * @return
	 */
	public int getChangeType() {
		return getKind() & SyncInfo.CHANGE_MASK;
	}
	
	/**
	 * @return
	 */
	public int getChangeDirection() {
		return getKind() & SyncInfo.DIRECTION_MASK;
	}

	/**
	 * @return
	 */
	public int getKind() {
		SyncInfo info = getSyncInfo();
		if (info == null) return 0;
		return info.getKind();
	}

	/**
	 * @return
	 */
	public SyncResource getParent() {
		Object parent = SyncSet.getParent(syncSet, this);
		if (parent instanceof SyncResource) {
			return (SyncResource)parent;
		}
		return null;
	}

	/**
	 * @return
	 */
	public IResource getResource() {
		return resource;
	}
	
	public String toString() {
		return "Sync for " + getResource().getFullPath().toString();
	}
}

Back to the top