|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2006 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.incubator; |
| 13 |
|
| 14 |
import java.util.Collections; |
| 15 |
import java.util.HashSet; |
| 16 |
import java.util.LinkedList; |
| 17 |
import java.util.Set; |
| 18 |
|
| 19 |
import org.eclipse.core.databinding.observable.Diffs; |
| 20 |
import org.eclipse.core.databinding.observable.set.ObservableSet; |
| 21 |
import org.eclipse.core.databinding.util.IFilter; |
| 22 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 23 |
import org.eclipse.core.runtime.IStatus; |
| 24 |
import org.eclipse.core.runtime.Status; |
| 25 |
import org.eclipse.core.runtime.jobs.Job; |
| 26 |
import org.eclipse.jface.databinding.swt.SWTObservables; |
| 27 |
import org.eclipse.swt.widgets.Display; |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 3.3 |
| 31 |
* |
| 32 |
*/ |
| 33 |
public class IncrementalFilterSet extends ObservableSet { |
| 34 |
|
| 35 |
/** |
| 36 |
* |
| 37 |
*/ |
| 38 |
private static final int MAX_WORK = 100; |
| 39 |
|
| 40 |
public static interface IElementStream { |
| 41 |
public void accept(IStreamVisitor visitor); |
| 42 |
} |
| 43 |
|
| 44 |
public static interface IStreamVisitor { |
| 45 |
public boolean visit(Object element); |
| 46 |
} |
| 47 |
|
| 48 |
protected static final boolean DEBUG = true; |
| 49 |
|
| 50 |
private IFilter filter; |
| 51 |
|
| 52 |
private static Object CLEAR_ALL = new Object(); |
| 53 |
private static Object MODE_ADD = new Object(); |
| 54 |
private static Object MODE_REMOVE = new Object(); |
| 55 |
/** |
| 56 |
* CLEAR_ALL element in this list represents a clear all operation, MODE_ADD |
| 57 |
* changes the mode so that from now on, other elements represent an add |
| 58 |
* operation with that element, and MODE_REMOVE changes the mode so that |
| 59 |
* from now on, other elements represent a remove operation of that element |
| 60 |
*/ |
| 61 |
private LinkedList workQueue = new LinkedList(); |
| 62 |
// the workQueue writer's current state |
| 63 |
boolean writerAdding = true; |
| 64 |
// the workQueue reader's current state |
| 65 |
boolean readerAdding = true; |
| 66 |
|
| 67 |
private static Object REQUEST_RECALCULATE = new Object(); |
| 68 |
private static Object REQUEST_FILTER = new Object(); |
| 69 |
private static Object REQUEST_CLEAR = new Object(); |
| 70 |
private LinkedList requestQueue = new LinkedList(); |
| 71 |
boolean consistent = false; |
| 72 |
|
| 73 |
private Job filterJob = new Job("Filtering") { //$NON-NLS-1$ |
| 74 |
Object request; |
| 75 |
|
| 76 |
protected IStatus run(IProgressMonitor monitor) { |
| 77 |
try { |
| 78 |
request = dequeue(requestQueue); |
| 79 |
while (request != null) { |
| 80 |
if (request == REQUEST_CLEAR) { |
| 81 |
if (DEBUG) { |
| 82 |
System.out.println("clearing"); //$NON-NLS-1$ |
| 83 |
} |
| 84 |
request = null; |
| 85 |
enqueueWorkQueueClearAll(); |
| 86 |
consistent = false; |
| 87 |
scheduleWorker(); |
| 88 |
} else if (consistent && request == REQUEST_FILTER) { |
| 89 |
if (DEBUG) { |
| 90 |
System.out |
| 91 |
.println("filtering, wrappedSet.size=" + wrappedSet.size()); //$NON-NLS-1$ |
| 92 |
} |
| 93 |
int filtered = 0; |
| 94 |
request = null; |
| 95 |
boolean removing; |
| 96 |
synchronized (workQueue) { |
| 97 |
removing = workQueue.isEmpty(); |
| 98 |
} |
| 99 |
if (!removing) { |
| 100 |
enqueueWorkQueueClearAll(); |
| 101 |
scheduleWorker(); |
| 102 |
} |
| 103 |
Object[] elements = wrappedSet |
| 104 |
.toArray(new Object[wrappedSet.size()]); |
| 105 |
for (int i = 0; request == null && i < elements.length; i++) { |
| 106 |
Object element = elements[i]; |
| 107 |
if (!filter.select(element)) { |
| 108 |
if (removing) { |
| 109 |
if (writerAdding) { |
| 110 |
enqueue(workQueue, MODE_REMOVE); |
| 111 |
writerAdding = false; |
| 112 |
} |
| 113 |
enqueue(workQueue, element); |
| 114 |
scheduleWorker(); |
| 115 |
filtered++; |
| 116 |
} |
| 117 |
} else { |
| 118 |
if (!removing) { |
| 119 |
if (!writerAdding) { |
| 120 |
enqueue(workQueue, MODE_ADD); |
| 121 |
writerAdding = true; |
| 122 |
} |
| 123 |
enqueue(workQueue, element); |
| 124 |
scheduleWorker(); |
| 125 |
filtered++; |
| 126 |
} |
| 127 |
} |
| 128 |
request = dequeue(requestQueue); |
| 129 |
} |
| 130 |
if (DEBUG) { |
| 131 |
System.out |
| 132 |
.println("filtered: " + (removing ? "removed " : "added ") + filtered + " elements"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
| 133 |
} |
| 134 |
} else if (request == REQUEST_RECALCULATE |
| 135 |
|| (request == REQUEST_FILTER && !consistent)) { |
| 136 |
if (DEBUG) { |
| 137 |
System.out.println("recalculating"); //$NON-NLS-1$ |
| 138 |
} |
| 139 |
request = null; |
| 140 |
enqueueWorkQueueClearAll(); |
| 141 |
consistent = false; |
| 142 |
scheduleWorker(); |
| 143 |
stream.accept(new IStreamVisitor() { |
| 144 |
public boolean visit(Object element) { |
| 145 |
if (request == null) { |
| 146 |
if (filter.select(element)) { |
| 147 |
if (!writerAdding) { |
| 148 |
enqueue(workQueue, MODE_ADD); |
| 149 |
writerAdding = true; |
| 150 |
} |
| 151 |
enqueue(workQueue, element); |
| 152 |
scheduleWorker(); |
| 153 |
} |
| 154 |
request = dequeue(requestQueue); |
| 155 |
} |
| 156 |
return request == null; |
| 157 |
} |
| 158 |
}); |
| 159 |
if (request == null) { |
| 160 |
consistent = true; |
| 161 |
} |
| 162 |
} else { |
| 163 |
throw new RuntimeException("unhandled case"); //$NON-NLS-1$ |
| 164 |
} |
| 165 |
if (request == null) { |
| 166 |
request = dequeue(requestQueue); |
| 167 |
} |
| 168 |
} |
| 169 |
} catch (Exception ex) { |
| 170 |
ex.printStackTrace(); |
| 171 |
} |
| 172 |
return Status.OK_STATUS; |
| 173 |
} |
| 174 |
}; |
| 175 |
|
| 176 |
private Runnable worker; |
| 177 |
|
| 178 |
private Display display; |
| 179 |
|
| 180 |
private final IElementStream stream; |
| 181 |
|
| 182 |
private void enqueue(LinkedList queue, Object element) { |
| 183 |
synchronized (queue) { |
| 184 |
queue.addLast(element); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* non-blocking - returns null if no element in queue |
| 190 |
* |
| 191 |
* @param queue |
| 192 |
* TODO |
| 193 |
*/ |
| 194 |
Object dequeue(LinkedList queue) { |
| 195 |
synchronized (queue) { |
| 196 |
return queue.isEmpty() ? null : queue.removeFirst(); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
*/ |
| 202 |
protected IncrementalFilterSet(Display display, IElementStream stream, |
| 203 |
IFilter filter) { |
| 204 |
super(SWTObservables.getRealm(display), new HashSet(), Object.class); |
| 205 |
this.display = display; |
| 206 |
this.stream = stream; |
| 207 |
this.filter = filter; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* May be called from any thread. Non-blocking. |
| 212 |
* |
| 213 |
* @param stream |
| 214 |
* @throws InterruptedException |
| 215 |
*/ |
| 216 |
public void requestFilter() { |
| 217 |
enqueue(requestQueue, REQUEST_FILTER); |
| 218 |
filterJob.schedule(); |
| 219 |
} |
| 220 |
|
| 221 |
public void requestRecalculation() { |
| 222 |
enqueue(requestQueue, REQUEST_RECALCULATE); |
| 223 |
filterJob.schedule(); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* |
| 228 |
*/ |
| 229 |
protected void scheduleWorker() { |
| 230 |
if (worker == null) { |
| 231 |
worker = new Runnable() { |
| 232 |
public void run() { |
| 233 |
Object work; |
| 234 |
Set additions = new HashSet(); |
| 235 |
Set removals = new HashSet(); |
| 236 |
int worked = 0; |
| 237 |
while (++worked < MAX_WORK |
| 238 |
&& (work = dequeue(workQueue)) != null) { |
| 239 |
if (work == CLEAR_ALL) { |
| 240 |
fireSetChange(Diffs.createSetDiff( |
| 241 |
Collections.EMPTY_SET, wrappedSet)); |
| 242 |
wrappedSet = new HashSet(); |
| 243 |
additions=new HashSet(); |
| 244 |
removals=new HashSet(); |
| 245 |
worked = MAX_WORK; |
| 246 |
} else if (work == MODE_ADD) { |
| 247 |
readerAdding = true; |
| 248 |
if (removals.size() > 0) { |
| 249 |
processRemovals(removals); |
| 250 |
worked = MAX_WORK; |
| 251 |
} |
| 252 |
} else if (work == MODE_REMOVE) { |
| 253 |
readerAdding = false; |
| 254 |
if (additions.size() > 0) { |
| 255 |
processAdditions(additions); |
| 256 |
worked = MAX_WORK; |
| 257 |
} |
| 258 |
} else if (readerAdding) { |
| 259 |
additions.add(work); |
| 260 |
} else { |
| 261 |
removals.add(work); |
| 262 |
} |
| 263 |
} |
| 264 |
processRemovals(removals); |
| 265 |
processAdditions(additions); |
| 266 |
synchronized (workQueue) { |
| 267 |
if (!workQueue.isEmpty()) { |
| 268 |
scheduleWorker(); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
private void processAdditions(Set additions) { |
| 274 |
if (!additions.isEmpty()) { |
| 275 |
if (DEBUG) { |
| 276 |
System.out.println("processing " + additions.size() //$NON-NLS-1$ |
| 277 |
+ " additions"); //$NON-NLS-1$ |
| 278 |
} |
| 279 |
wrappedSet.addAll(additions); |
| 280 |
fireSetChange(Diffs.createSetDiff(additions, |
| 281 |
Collections.EMPTY_SET)); |
| 282 |
additions = new HashSet(); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
private void processRemovals(Set removals) { |
| 287 |
if (!removals.isEmpty()) { |
| 288 |
if (DEBUG) { |
| 289 |
System.out.println("processing " + removals.size() //$NON-NLS-1$ |
| 290 |
+ " removals"); //$NON-NLS-1$ |
| 291 |
} |
| 292 |
wrappedSet.removeAll(removals); |
| 293 |
fireSetChange(Diffs.createSetDiff( |
| 294 |
Collections.EMPTY_SET, removals)); |
| 295 |
removals = new HashSet(); |
| 296 |
} |
| 297 |
} |
| 298 |
}; |
| 299 |
} |
| 300 |
display.asyncExec(worker); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* |
| 305 |
*/ |
| 306 |
public void requestClear() { |
| 307 |
enqueue(requestQueue, REQUEST_CLEAR); |
| 308 |
filterJob.schedule(); |
| 309 |
} |
| 310 |
|
| 311 |
private void enqueueWorkQueueClearAll() { |
| 312 |
// enqueue clear all operation |
| 313 |
synchronized (workQueue) { |
| 314 |
workQueue.clear(); |
| 315 |
workQueue.addLast(CLEAR_ALL); |
| 316 |
} |
| 317 |
} |
| 318 |
} |