View | Details | Raw Unified | Return to bug 158368
Collapse All | Expand All

(-)src/org/eclipse/gmf/runtime/common/ui/services/elementselection/ElementSelectionComposite.java (-31 / +36 lines)
Lines 17-23 Link Here
17
import java.util.regex.Pattern;
17
import java.util.regex.Pattern;
18
18
19
import org.eclipse.core.runtime.IProgressMonitor;
19
import org.eclipse.core.runtime.IProgressMonitor;
20
import org.eclipse.core.runtime.jobs.Job;
21
import org.eclipse.gmf.runtime.common.core.util.StringStatics;
20
import org.eclipse.gmf.runtime.common.core.util.StringStatics;
22
import org.eclipse.gmf.runtime.common.ui.services.internal.l10n.CommonUIServicesMessages;
21
import org.eclipse.gmf.runtime.common.ui.services.internal.l10n.CommonUIServicesMessages;
23
import org.eclipse.jface.dialogs.Dialog;
22
import org.eclipse.jface.dialogs.Dialog;
Lines 92-98 Link Here
92
    /**
91
    /**
93
     * The job running the element selection service.
92
     * The job running the element selection service.
94
     */
93
     */
95
    private Job job;
94
    private ElementSelectionServiceJob job;
96
    
95
    
97
    /**
96
    /**
98
     * The element selection service to use to search for elements.
97
     * The element selection service to use to search for elements.
Lines 289-333 Link Here
289
        if (filterText.getText().equals(StringStatics.BLANK)) {
288
        if (filterText.getText().equals(StringStatics.BLANK)) {
290
            /* no filter, no results */
289
            /* no filter, no results */
291
            cancel();
290
            cancel();
292
            tableViewer.getTable().removeAll();
293
            matchingObjects.clear();
291
            matchingObjects.clear();
292
            tableViewer.getTable().removeAll();            
294
            firstCharacter = Character.MIN_VALUE;
293
            firstCharacter = Character.MIN_VALUE;
295
            return;
294
            return;
296
        }
295
        }
297
        
296
        
298
        if (this.input.getScope().intValue() != this.lastScopeSearchedFor) {
299
            //scope changes, start from scratch...
300
            
301
            tableViewer.getTable().removeAll();
302
            matchingObjects.clear();
303
        }
304
        
305
        /*
306
         * clear the existing matches in the table and refilter results we have
307
         * received
308
         */
309
        String filter = validatePattern(filterText.getText());
297
        String filter = validatePattern(filterText.getText());
310
        pattern = Pattern.compile(filter);
298
        pattern = Pattern.compile(filter);
311
        tableViewer.getTable().removeAll();
299
        if (firstCharacter != filterText.getText().charAt(0) ||
312
        for (Iterator i = matchingObjects.iterator(); i.hasNext();) {
300
                this.input.getScope().intValue() != this.lastScopeSearchedFor ||
313
            IMatchingObject matchingObject = (IMatchingObject) i.next();
301
                !filterText.getText().startsWith(lastSearchedFor)) {
314
            Matcher matcher = pattern.matcher(matchingObject.getName()
302
            //scope changes, start from scratch...
315
                .toLowerCase());
303
            cancel();
316
            if (matcher.matches()) {
304
            matchingObjects.clear();
317
                tableViewer.add(matchingObject);
305
            tableViewer.getTable().removeAll();
318
                setSelection();
319
            }
320
        }
321
322
        if ((firstCharacter == Character.MIN_VALUE) ||
323
        	(firstCharacter != filterText.getText().charAt(0)) ||
324
        	(filterText.getText().indexOf(lastSearchedFor) == -1) ||
325
            this.input.getScope().intValue() != this.lastScopeSearchedFor) {
326
            
306
            
327
            firstCharacter = filterText.getText().charAt(0);
307
            firstCharacter = filterText.getText().charAt(0);
328
            this.lastScopeSearchedFor = this.input.getScope().intValue();
308
            this.lastScopeSearchedFor = this.input.getScope().intValue();
329
            
309
            
330
            startElementSelectionService();
310
            startElementSelectionService();
311
        } else {
312
            /*
313
             * clear the existing matches in the table and refilter results we have
314
             * received
315
             */
316
            tableViewer.getTable().removeAll();
317
            for (Iterator i = matchingObjects.iterator(); i.hasNext();) {
318
                IMatchingObject matchingObject = (IMatchingObject) i.next();
319
                Matcher matcher = pattern.matcher(matchingObject.getName()
320
                    .toLowerCase());
321
                if (matcher.matches()) {
322
                    tableViewer.add(matchingObject);
323
                    setSelection();
324
                }
325
            }
331
        }
326
        }
332
    }
327
    }
333
328
Lines 414-423 Link Here
414
                    .getMatchingObject();
409
                    .getMatchingObject();
415
                progressBar.worked(1);
410
                progressBar.worked(1);
416
                progressBar.subTask(matchingObject.getName());
411
                progressBar.subTask(matchingObject.getName());
412
                matchingObjects.add(matchingObject);
417
                Matcher matcher = pattern.matcher(matchingObject.getName()
413
                Matcher matcher = pattern.matcher(matchingObject.getName()
418
                    .toLowerCase());
414
                    .toLowerCase());
419
                if (matcher.matches()) {
415
                if (matcher.matches()) {
420
                    matchingObjects.add(matchingObject);
421
                    tableViewer.add(matchingObject);
416
                    tableViewer.add(matchingObject);
422
                    setSelection();
417
                    setSelection();
423
                }
418
                }
Lines 430-437 Link Here
430
     */
425
     */
431
    public void cancel() {
426
    public void cancel() {
432
        if (job != null) {
427
        if (job != null) {
433
            job.cancel();
428
            elementSelectionService.cancelJob(job);
434
            job = null;
429
            job = null;
430
            progressBar.done();
431
            progressBar.setVisible(false);
435
        }
432
        }
436
    }
433
    }
437
434
Lines 477-480 Link Here
477
            handleSelectionChange();
474
            handleSelectionChange();
478
        }
475
        }
479
    }
476
    }
477
478
    
479
    /**
480
     * @return the job
481
     */
482
    public ElementSelectionServiceJob getSelectionServiceJob() {
483
        return job;
484
    }
480
}
485
}
(-)src/org/eclipse/gmf/runtime/common/ui/services/elementselection/ElementSelectionService.java (-36 / +125 lines)
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
}
(-)src/org/eclipse/gmf/runtime/common/ui/services/elementselection/AbstractElementSelectionProvider.java (-1 / +1 lines)
Lines 50-57 Link Here
50
    public ElementSelectionServiceJob getMatchingObjects(
50
    public ElementSelectionServiceJob getMatchingObjects(
51
            IElementSelectionInput input, IElementSelectionListener listener) {
51
            IElementSelectionInput input, IElementSelectionListener listener) {
52
        elementSelectionInput = input;
52
        elementSelectionInput = input;
53
        elementSelectionListener = listener;
54
        ElementSelectionServiceJob job = createSelectionJob();
53
        ElementSelectionServiceJob job = createSelectionJob();
54
        elementSelectionListener = listener;
55
        return job;
55
        return job;
56
    }
56
    }
57
    
57
    

Return to bug 158368