1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2009, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
******************************************************************************/
|
11
|
|
12
|
package org.eclipse.ui.internal.navigator;
|
13
|
|
14
|
import java.util.Iterator;
|
15
|
import java.util.LinkedHashSet;
|
16
|
|
17
|
import org.eclipse.ui.navigator.INavigatorContentDescriptor;
|
18
|
|
19
|
/**
|
20
|
* Used to associate the NavigatorContentDescriptor (NCD) with an object that it contributes.
|
21
|
*
|
22
|
* The NCD/object association is tracked using the NavigatorContentService.rememberContribution().
|
23
|
*
|
24
|
* @since 3.2
|
25
|
*
|
26
|
*/
|
27
|
public class ContributorTrackingSet extends LinkedHashSet {
|
28
|
|
29
|
|
30
|
private static final long serialVersionUID = 2516241537206281972L;
|
31
|
|
32
|
private INavigatorContentDescriptor contributor;
|
33
|
private INavigatorContentDescriptor firstClassContributor;
|
34
|
private NavigatorContentService contentService;
|
35
|
|
36
|
/**
|
37
|
* Construct a tracking set.
|
38
|
*
|
39
|
* @param aContentService
|
40
|
*/
|
41
|
public ContributorTrackingSet(NavigatorContentService aContentService) {
|
42
|
contentService = aContentService;
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Construct a tracking set.
|
47
|
*
|
48
|
* @param aContentService
|
49
|
* @param elements
|
50
|
*/
|
51
|
public ContributorTrackingSet(NavigatorContentService aContentService, Object[] elements) {
|
52
|
|
53
|
for (int i = 0; i < elements.length; i++)
|
54
|
super.add(elements[i]);
|
55
|
|
56
|
contentService = aContentService;
|
57
|
}
|
58
|
|
59
|
public boolean add(Object o) {
|
60
|
if (contributor != null) {
|
61
|
contentService.rememberContribution(contributor, firstClassContributor, o);
|
62
|
}
|
63
|
return super.add(o);
|
64
|
}
|
65
|
|
66
|
public boolean remove(Object o) {
|
67
|
contentService.forgetContribution(o);
|
68
|
return super.remove(o);
|
69
|
}
|
70
|
|
71
|
|
72
|
public void clear() {
|
73
|
Iterator it = iterator();
|
74
|
while (it.hasNext())
|
75
|
contentService.forgetContribution(it.next());
|
76
|
super.clear();
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
*
|
81
|
* @return The current contributor.
|
82
|
*/
|
83
|
public INavigatorContentDescriptor getContributor() {
|
84
|
return contributor;
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
*
|
89
|
* @return The current contributor.
|
90
|
*/
|
91
|
public INavigatorContentDescriptor getFirstClassContributor() {
|
92
|
return firstClassContributor;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
*
|
97
|
* @param newContributor The contributor to record for the next series of adds.
|
98
|
* @param theFirstClassContributor The first class contributor associated with the newContributor.
|
99
|
*/
|
100
|
public void setContributor(INavigatorContentDescriptor newContributor, INavigatorContentDescriptor theFirstClassContributor) {
|
101
|
contributor = newContributor;
|
102
|
firstClassContributor = theFirstClassContributor;
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* @param contents
|
107
|
*/
|
108
|
public void setContents(Object[] contents) {
|
109
|
super.clear();
|
110
|
if(contents != null)
|
111
|
for (int i = 0; i < contents.length; i++)
|
112
|
add(contents[i]);
|
113
|
|
114
|
}
|
115
|
|
116
|
public Iterator iterator() {
|
117
|
return new Iterator() {
|
118
|
|
119
|
Iterator delegateIterator = ContributorTrackingSet.super.iterator();
|
120
|
Object current;
|
121
|
|
122
|
public boolean hasNext() {
|
123
|
return delegateIterator.hasNext();
|
124
|
}
|
125
|
|
126
|
public Object next() {
|
127
|
current = delegateIterator.next();
|
128
|
return current;
|
129
|
}
|
130
|
|
131
|
public void remove() {
|
132
|
delegateIterator.remove();
|
133
|
contentService.forgetContribution(current);
|
134
|
}
|
135
|
};
|
136
|
}
|
137
|
|
138
|
}
|