Project

General

Profile

Download (2.74 KB) Statistics
| Branch: | Tag: | Revision:
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.navigator.dnd;
13

    
14
import java.lang.ref.WeakReference;
15
import java.util.HashSet;
16
import java.util.Iterator;
17
import java.util.Set;
18

    
19
import org.eclipse.ui.navigator.INavigatorContentService;
20

    
21
/**
22
 * Caches instances of {@link INavigatorContentService} to facilitate the
23
 * handling of drop operations in other viewers.
24
 * 
25
 * @since 3.2
26
 * 
27
 */
28
public class NavigatorContentServiceTransfer {
29
	
30
	private static final NavigatorContentServiceTransfer instance = new NavigatorContentServiceTransfer(); 
31
	
32
	/**
33
	 * 
34
	 * @return The singleton instance of the transfer service.
35
	 */
36
	public static NavigatorContentServiceTransfer getInstance() {
37
		return instance;
38
	}
39

    
40
	private final Set registeredContentServices = new HashSet();
41
	
42
	/**
43
	 *  
44
	 * @param aContentService The Content Service to register.
45
	 */
46
	public synchronized void registerContentService(INavigatorContentService aContentService) { 
47
		if(findService(aContentService.getViewerId()) == null) {
48
			registeredContentServices.add(new WeakReference(aContentService));
49
		}
50
	}
51
	
52
	/**
53
	 *  
54
	 * @param aContentService The Content Service to unregister.
55
	 */
56
	public synchronized void unregisterContentService(INavigatorContentService aContentService) { 
57
  
58
		for (Iterator iter = registeredContentServices.iterator(); iter.hasNext();) {
59
			WeakReference ref = (WeakReference) iter.next();
60
			if(ref.get() == null) {
61
				iter.remove();
62
			} else { 
63
				if(ref.get() == aContentService) {
64
					iter.remove(); 
65
					return;
66
				}
67
			}
68
		} 
69
	}
70
	
71
	/**
72
	 * 
73
	 * @param aViewerId A viewer id that should have previously been registered with the service.
74
	 * @return The registered content service for the given viewer id.
75
	 */
76
	public synchronized INavigatorContentService findService(String aViewerId) {
77
		if(aViewerId == null || aViewerId.length() == 0) {
78
			return null;
79
		}
80
		INavigatorContentService contentService;
81
		for (Iterator iter = registeredContentServices.iterator(); iter.hasNext();) {
82
			WeakReference ref = (WeakReference) iter.next();
83
			if(ref.get() == null) {
84
				iter.remove();
85
			} else {
86
				contentService = (INavigatorContentService)ref.get();
87
				if(aViewerId.equals(contentService.getViewerId())) {
88
					return contentService;
89
				} 
90
			}
91
		}
92
		return null;
93
	} 
94
	
95

    
96
}
(3-3/6)