Lines 18-27
Link Here
|
18 |
|
18 |
|
19 |
import org.eclipse.core.runtime.IConfigurationElement; |
19 |
import org.eclipse.core.runtime.IConfigurationElement; |
20 |
import org.eclipse.core.runtime.IProgressMonitor; |
20 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
|
21 |
import org.eclipse.core.runtime.Platform; |
22 |
import org.eclipse.core.runtime.jobs.IJobManager; |
21 |
import org.eclipse.core.runtime.jobs.Job; |
23 |
import org.eclipse.core.runtime.jobs.Job; |
22 |
import org.eclipse.gmf.runtime.common.core.service.ExecutionStrategy; |
24 |
import org.eclipse.gmf.runtime.common.core.service.ExecutionStrategy; |
23 |
import org.eclipse.gmf.runtime.common.core.service.IOperation; |
25 |
import org.eclipse.gmf.runtime.common.core.service.IOperation; |
24 |
import org.eclipse.gmf.runtime.common.core.service.Service; |
26 |
import org.eclipse.gmf.runtime.common.core.service.Service; |
|
|
27 |
import org.eclipse.gmf.runtime.common.core.util.StringStatics; |
25 |
import org.eclipse.gmf.runtime.common.ui.services.internal.CommonUIServicesPlugin; |
28 |
import org.eclipse.gmf.runtime.common.ui.services.internal.CommonUIServicesPlugin; |
26 |
import org.eclipse.gmf.runtime.common.ui.services.internal.elementselection.ElementSelectionList; |
29 |
import org.eclipse.gmf.runtime.common.ui.services.internal.elementselection.ElementSelectionList; |
27 |
import org.eclipse.gmf.runtime.common.ui.services.internal.elementselection.MatchingObjectsOperation; |
30 |
import org.eclipse.gmf.runtime.common.ui.services.internal.elementselection.MatchingObjectsOperation; |
Lines 59-69
Link Here
|
59 |
} |
62 |
} |
60 |
} |
63 |
} |
61 |
|
64 |
|
62 |
private IElementSelectionInput elementSelectionInput; |
65 |
protected class JobData { |
|
|
66 |
public IElementSelectionInput elementSelectionInput; |
63 |
|
67 |
|
64 |
private IElementSelectionListener elementSelectionListener; |
68 |
public IElementSelectionListener elementSelectionListener; |
65 |
|
69 |
|
66 |
private HashMap jobs = new HashMap(); |
70 |
public HashMap jobs = new HashMap(); |
|
|
71 |
} |
72 |
|
73 |
private Map jobs2Data = new HashMap(); |
74 |
|
75 |
public JobData getJobData() { |
76 |
Job currentJob = jobManager.currentJob(); |
77 |
assert currentJob != null; |
78 |
|
79 |
if(currentJob == null) { |
80 |
return null; |
81 |
} |
82 |
|
83 |
JobData data = null; |
84 |
synchronized(jobs2Data) { |
85 |
data = (JobData)jobs2Data.get(currentJob); |
86 |
} |
87 |
|
88 |
return data; |
89 |
} |
67 |
|
90 |
|
68 |
/** |
91 |
/** |
69 |
* The singleton instance of the type selection service. |
92 |
* The singleton instance of the type selection service. |
Lines 107-119
Link Here
|
107 |
*/ |
130 |
*/ |
108 |
public ElementSelectionServiceJob getMatchingObjects( |
131 |
public ElementSelectionServiceJob getMatchingObjects( |
109 |
IElementSelectionInput input, IElementSelectionListener listener) { |
132 |
IElementSelectionInput input, IElementSelectionListener listener) { |
110 |
elementSelectionInput = input; |
|
|
111 |
elementSelectionListener = listener; |
112 |
ElementSelectionServiceJob job = createSelectionJob(); |
133 |
ElementSelectionServiceJob job = createSelectionJob(); |
|
|
134 |
JobData data = new JobData(); |
135 |
data.elementSelectionInput = input; |
136 |
data.elementSelectionListener = listener; |
137 |
job.setName(getJobName(data)); |
138 |
synchronized(jobs2Data) { |
139 |
jobs2Data.put(job, data); |
140 |
} |
113 |
job.schedule(); |
141 |
job.schedule(); |
114 |
return job; |
142 |
return job; |
115 |
} |
143 |
} |
116 |
|
144 |
|
|
|
145 |
|
146 |
protected String getJobName() { |
147 |
return StringStatics.BLANK; |
148 |
} |
149 |
|
117 |
/** |
150 |
/** |
118 |
* Creates the selection service job that manages the individual provider |
151 |
* Creates the selection service job that manages the individual provider |
119 |
* search jobs. This method should configure the new job with the appropriate |
152 |
* search jobs. This method should configure the new job with the appropriate |
Lines 126-139
Link Here
|
126 |
job.setPriority(Job.SHORT); |
159 |
job.setPriority(Job.SHORT); |
127 |
return job; |
160 |
return job; |
128 |
} |
161 |
} |
|
|
162 |
|
163 |
public static final IJobManager jobManager = Platform.getJobManager(); |
129 |
|
164 |
|
130 |
/** |
165 |
/** |
131 |
* {@inheritDoc} |
166 |
* {@inheritDoc} |
132 |
*/ |
167 |
*/ |
133 |
public void run(IProgressMonitor monitor) { |
168 |
public void run(IProgressMonitor monitor) { |
|
|
169 |
JobData data = getJobData(); |
170 |
if(data == null) |
171 |
return; |
172 |
|
134 |
List results = new ArrayList(); |
173 |
List results = new ArrayList(); |
135 |
IOperation operation = new MatchingObjectsOperation( |
174 |
IOperation operation = new MatchingObjectsOperation( |
136 |
elementSelectionInput); |
175 |
data.elementSelectionInput); |
137 |
|
176 |
|
138 |
/** |
177 |
/** |
139 |
* Get the list of element selection providers based on the input. |
178 |
* Get the list of element selection providers based on the input. |
Lines 151-165
Link Here
|
151 |
IElementSelectionProvider provider = (IElementSelectionProvider) i |
190 |
IElementSelectionProvider provider = (IElementSelectionProvider) i |
152 |
.next(); |
191 |
.next(); |
153 |
|
192 |
|
154 |
addJob(provider); |
193 |
addJob(data, provider); |
155 |
} |
194 |
} |
156 |
|
195 |
|
157 |
/** |
196 |
/** |
158 |
* Start the provider jobs. |
197 |
* Start the provider jobs. |
159 |
*/ |
198 |
*/ |
160 |
HashMap jobsClone; |
199 |
HashMap jobsClone; |
161 |
synchronized (jobs) { |
200 |
synchronized (data) { |
162 |
jobsClone = (HashMap)jobs.clone(); |
201 |
jobsClone = (HashMap)data.jobs.clone(); |
163 |
} |
202 |
} |
164 |
for (Iterator i = jobsClone.entrySet().iterator(); i.hasNext();) { |
203 |
for (Iterator i = jobsClone.entrySet().iterator(); i.hasNext();) { |
165 |
Map.Entry entry = (Map.Entry) i.next(); |
204 |
Map.Entry entry = (Map.Entry) i.next(); |
Lines 174-183
Link Here
|
174 |
/** |
213 |
/** |
175 |
* Now loop, waiting for the provider jobs to complete. |
214 |
* Now loop, waiting for the provider jobs to complete. |
176 |
*/ |
215 |
*/ |
177 |
monitor.beginTask(getJobName(), 1000); |
216 |
monitor.beginTask(getJobName(data), 1000); |
178 |
while (true) { |
217 |
while (true) { |
179 |
synchronized (jobs) { |
218 |
synchronized (data) { |
180 |
if (jobs.size() == 0) { |
219 |
if (data.jobs.size() == 0) { |
181 |
break; |
220 |
break; |
182 |
} |
221 |
} |
183 |
} |
222 |
} |
Lines 186-193
Link Here
|
186 |
* if the progress monitor is canceled, then cancel the running jobs. |
225 |
* if the progress monitor is canceled, then cancel the running jobs. |
187 |
*/ |
226 |
*/ |
188 |
if (monitor.isCanceled()) { |
227 |
if (monitor.isCanceled()) { |
189 |
cancelAllJobs(); |
228 |
synchronized(data) { |
190 |
break; |
229 |
// nullify the element selection listener. |
|
|
230 |
data.elementSelectionListener = null; |
231 |
cancelAllJobs(); |
232 |
break; |
233 |
} |
191 |
} |
234 |
} |
192 |
} |
235 |
} |
193 |
monitor.done(); |
236 |
monitor.done(); |
Lines 219-231
Link Here
|
219 |
* |
262 |
* |
220 |
* @return the name for the job. |
263 |
* @return the name for the job. |
221 |
*/ |
264 |
*/ |
222 |
protected String getJobName() { |
265 |
protected String getJobName(JobData data) { |
223 |
String providerName = getClass().getName().substring( |
266 |
if((getJobName() != null && getJobName().equals(StringStatics.BLANK)) && data != null) { |
224 |
getClass().getName().lastIndexOf('.') + 1); |
267 |
String providerName = getClass().getName().substring( |
225 |
String filter = elementSelectionInput.getInput(); |
268 |
getClass().getName().lastIndexOf('.') + 1); |
226 |
return NLS.bind( |
269 |
String filter = data.elementSelectionInput.getInput(); |
227 |
CommonUIServicesMessages.ElementSelectionService_JobName, |
270 |
return NLS.bind( |
228 |
new String[] {providerName, filter}); |
271 |
CommonUIServicesMessages.ElementSelectionService_JobName, |
|
|
272 |
new String[] {providerName, filter}); |
273 |
} |
274 |
return getJobName(); |
229 |
} |
275 |
} |
230 |
|
276 |
|
231 |
/** |
277 |
/** |
Lines 233-243
Link Here
|
233 |
* |
279 |
* |
234 |
* @param provider an element selection provider. |
280 |
* @param provider an element selection provider. |
235 |
*/ |
281 |
*/ |
236 |
private void addJob(IElementSelectionProvider provider) { |
282 |
private void addJob(JobData data, IElementSelectionProvider provider) { |
237 |
ElementSelectionServiceJob job = provider.getMatchingObjects( |
283 |
ElementSelectionServiceJob job = provider.getMatchingObjects( |
238 |
elementSelectionInput, this); |
284 |
data.elementSelectionInput, this); |
239 |
synchronized (jobs) { |
285 |
synchronized (data) { |
240 |
jobs.put(provider, job); |
286 |
data.jobs.put(provider, job); |
|
|
287 |
} |
288 |
|
289 |
synchronized(jobs2Data) { |
290 |
jobs2Data.put(job, data); |
241 |
} |
291 |
} |
242 |
} |
292 |
} |
243 |
|
293 |
|
Lines 246-265
Link Here
|
246 |
* |
296 |
* |
247 |
* @param provider an element selection provider. |
297 |
* @param provider an element selection provider. |
248 |
*/ |
298 |
*/ |
249 |
private void removeJob(IElementSelectionProvider provider) { |
299 |
private void removeJob(JobData data, IElementSelectionProvider provider) { |
250 |
boolean end_of_matches = false; |
300 |
boolean end_of_matches = false; |
251 |
synchronized (jobs) { |
301 |
Object job = null; |
252 |
jobs.remove(provider); |
302 |
synchronized (data) { |
253 |
if (jobs.size() == 0) { |
303 |
job = data.jobs.remove(provider); |
|
|
304 |
if (data.jobs.size() == 0) { |
254 |
end_of_matches = true; |
305 |
end_of_matches = true; |
255 |
} |
306 |
} |
256 |
} |
307 |
} |
|
|
308 |
|
257 |
/** |
309 |
/** |
258 |
* All the jobs have finished, send end of matches event. |
310 |
* All the jobs have finished, send end of matches event. |
259 |
*/ |
311 |
*/ |
260 |
if (end_of_matches) { |
312 |
if (end_of_matches) { |
261 |
fireEndOfMatchesEvent(); |
313 |
fireEndOfMatchesEvent(); |
262 |
} |
314 |
} |
|
|
315 |
|
316 |
synchronized(jobs2Data) { |
317 |
jobs2Data.remove(job); |
318 |
} |
263 |
} |
319 |
} |
264 |
|
320 |
|
265 |
/** |
321 |
/** |
Lines 270-280
Link Here
|
270 |
*/ |
326 |
*/ |
271 |
protected void fireMatchingObjectEvent( |
327 |
protected void fireMatchingObjectEvent( |
272 |
final IMatchingObjectEvent matchingObjectEvent) { |
328 |
final IMatchingObjectEvent matchingObjectEvent) { |
|
|
329 |
final Job currentJob = jobManager.currentJob(); |
330 |
if(currentJob == null) |
331 |
return; |
332 |
|
333 |
JobData data = null; |
334 |
synchronized(jobs2Data) { |
335 |
data = (JobData)jobs2Data.get(currentJob); |
336 |
} |
337 |
|
338 |
if(data == null) |
339 |
return; |
340 |
|
341 |
final JobData finalData = data; |
273 |
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { |
342 |
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { |
274 |
|
343 |
|
275 |
public void run() { |
344 |
public void run() { |
276 |
elementSelectionListener |
345 |
synchronized(finalData) { |
277 |
.matchingObjectEvent(matchingObjectEvent); |
346 |
if(finalData.elementSelectionListener != null) { |
|
|
347 |
finalData.elementSelectionListener |
348 |
.matchingObjectEvent(matchingObjectEvent); |
349 |
} |
350 |
} |
278 |
} |
351 |
} |
279 |
}); |
352 |
}); |
280 |
} |
353 |
} |
Lines 294-313
Link Here
|
294 |
* {@inheritDoc} |
367 |
* {@inheritDoc} |
295 |
*/ |
368 |
*/ |
296 |
public void matchingObjectEvent(IMatchingObjectEvent matchingObjectEvent) { |
369 |
public void matchingObjectEvent(IMatchingObjectEvent matchingObjectEvent) { |
|
|
370 |
JobData data = getJobData(); |
371 |
if(data == null) |
372 |
return; |
297 |
if (matchingObjectEvent.getEventType() == MatchingObjectEventType.END_OF_MATCHES) { |
373 |
if (matchingObjectEvent.getEventType() == MatchingObjectEventType.END_OF_MATCHES) { |
298 |
removeJob(matchingObjectEvent.getMatchingObject().getProvider()); |
374 |
removeJob(data, matchingObjectEvent.getMatchingObject().getProvider()); |
299 |
} else { |
375 |
} else { |
300 |
fireMatchingObjectEvent(matchingObjectEvent); |
376 |
fireMatchingObjectEvent(matchingObjectEvent); |
301 |
} |
377 |
} |
302 |
} |
378 |
} |
303 |
|
379 |
|
304 |
/** |
380 |
/** |
305 |
* Cancel the jobs running for the element selection service. |
381 |
* Cancel the jobs running for the element selection service. |
306 |
*/ |
382 |
*/ |
307 |
protected void cancelAllJobs() { |
383 |
protected void cancelAllJobs() { |
|
|
384 |
JobData data = getJobData(); |
308 |
HashMap jobsClone; |
385 |
HashMap jobsClone; |
309 |
synchronized (jobs) { |
386 |
synchronized (data) { |
310 |
jobsClone = (HashMap) jobs.clone(); |
387 |
jobsClone = (HashMap) data.jobs.clone(); |
311 |
} |
388 |
} |
312 |
for (Iterator i = jobsClone.entrySet().iterator(); i.hasNext();) { |
389 |
for (Iterator i = jobsClone.entrySet().iterator(); i.hasNext();) { |
313 |
Map.Entry entry = (Map.Entry) i.next(); |
390 |
Map.Entry entry = (Map.Entry) i.next(); |
Lines 315-321
Link Here
|
315 |
ElementSelectionServiceJob job = (ElementSelectionServiceJob) entry |
392 |
ElementSelectionServiceJob job = (ElementSelectionServiceJob) entry |
316 |
.getValue(); |
393 |
.getValue(); |
317 |
job.cancel(); |
394 |
job.cancel(); |
318 |
removeJob(provider); |
395 |
removeJob(data, provider); |
319 |
} |
396 |
} |
320 |
} |
397 |
} |
321 |
|
398 |
|
Lines 333-336
Link Here
|
333 |
CommonUIServicesPlugin.getPluginId(), |
410 |
CommonUIServicesPlugin.getPluginId(), |
334 |
"elementSelectionProviders"); //$NON-NLS-1$ |
411 |
"elementSelectionProviders"); //$NON-NLS-1$ |
335 |
} |
412 |
} |
|
|
413 |
|
414 |
public void cancelJob(ElementSelectionServiceJob job) { |
415 |
JobData data = null; |
416 |
synchronized(jobs2Data) { |
417 |
data = (JobData)jobs2Data.get(job); |
418 |
} |
419 |
|
420 |
synchronized(data) { |
421 |
data.elementSelectionListener = null; |
422 |
} |
423 |
job.cancel(); |
424 |
} |
336 |
} |
425 |
} |