Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c52cc1ae80fa6149672e59326d8e2ec39a02c6f5 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*******************************************************************************
 * Copyright (c) 2006, 2007 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.core.subscribers;

import java.util.*;

import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.jobs.ILock;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.team.internal.core.Policy;

/**
 * A change set manager that batches change event notification.
 */
public class BatchingChangeSetManager extends ChangeSetManager {

	private ILock lock = Job.getJobManager().newLock();

	public static class CollectorChangeEvent {

		Set added = new HashSet();
		Set removed = new HashSet();
		Map changed = new HashMap();
		private final BatchingChangeSetManager collector;

		public CollectorChangeEvent(BatchingChangeSetManager collector) {
			this.collector = collector;
		}

		private void setAdded(ChangeSet set) {
			added.add(set);
			removed.remove(set);
		}

		private void setRemoved(ChangeSet set) {
			added.remove(set);
			removed.add(set);
			// Do not clear the changes list since clients may want to know what resources
			// were changed before the set was removed
		}

		private void changed(ChangeSet changeSet, IPath[] allAffectedResources) {
			if (added.contains(changeSet))
				return;
			IPath[] paths = (IPath[])changed.get(changeSet);
			if (paths == null) {
				changed.put(changeSet, allAffectedResources);
			} else {
				Set allPaths = new HashSet();
				for (int i = 0; i < paths.length; i++) {
					IPath path = paths[i];
					allPaths.add(path);
				}
				for (int i = 0; i < allAffectedResources.length; i++) {
					IPath path = allAffectedResources[i];
					allPaths.add(path);
				}
				changed.put(changeSet, (IPath[]) allPaths.toArray(new IPath[allPaths.size()]));
			}
		}

		public boolean isEmpty() {
			return changed.isEmpty() && added.isEmpty() && removed.isEmpty();
		}

		public ChangeSet[] getAddedSets() {
			return (ChangeSet[]) added.toArray(new ChangeSet[added.size()]);
		}

		public ChangeSet[] getRemovedSets() {
			return (ChangeSet[]) removed.toArray(new ChangeSet[removed.size()]);
		}

		public ChangeSet[] getChangedSets() {
			return (ChangeSet[]) changed.keySet().toArray(new ChangeSet[changed.size()]);
		}

		public IPath[] getChangesFor(ChangeSet set) {
			return (IPath[])changed.get(set);
		}

		public BatchingChangeSetManager getSource() {
			return collector;
		}
	}

	public static interface IChangeSetCollectorChangeListener {
		public void changeSetChanges(CollectorChangeEvent event, IProgressMonitor monitor);
	}

	private CollectorChangeEvent changes = new CollectorChangeEvent(this);

	public void beginInput() {
		lock.acquire();
	}

	public void endInput(IProgressMonitor monitor) {
		try {
			if (lock.getDepth() == 1) {
				// Remain locked while firing the events so the handlers
				// can expect the set to remain constant while they process the events
				fireChanges(Policy.monitorFor(monitor));
			}
		} finally {
			lock.release();
		}
	}

    private void fireChanges(final IProgressMonitor monitor) {
    	if (changes.isEmpty()) {
    		return;
    	}
    	final CollectorChangeEvent event = changes;
    	changes = new CollectorChangeEvent(this);
        Object[] listeners = getListeners();
        for (int i = 0; i < listeners.length; i++) {
            final IChangeSetChangeListener listener = (IChangeSetChangeListener)listeners[i];
            if (listener instanceof IChangeSetCollectorChangeListener) {
				final IChangeSetCollectorChangeListener csccl = (IChangeSetCollectorChangeListener) listener;
				SafeRunner.run(new ISafeRunnable() {
					public void handleException(Throwable exception) {
						// Exceptions are logged by the platform
					}
					public void run() throws Exception {
						csccl.changeSetChanges(event, monitor);
					}
				});
			}
        }
	}

    public void add(ChangeSet set) {
    	try {
    		beginInput();
    		super.add(set);
    		changes.setAdded(set);
    	} finally {
    		endInput(null);
    	}
    }

    public void remove(ChangeSet set) {
    	try {
    		beginInput();
    		super.remove(set);
    		changes.setRemoved(set);
    	} finally {
    		endInput(null);
    	}
    }

    protected void fireResourcesChangedEvent(ChangeSet changeSet, IPath[] allAffectedResources) {
    	super.fireResourcesChangedEvent(changeSet, allAffectedResources);
    	try {
    		beginInput();
    		changes.changed(changeSet, allAffectedResources);
    	} finally {
    		endInput(null);
    	}
    }

    protected void initializeSets() {
    	// Nothing to do
    }
}

Back to the top