Project

General

Profile

Download (4.29 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2003, 2010 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.extensions;
13

    
14
import java.util.ArrayList;
15
import java.util.Iterator;
16
import java.util.List;
17

    
18
import org.eclipse.core.runtime.IConfigurationElement;
19
import org.eclipse.core.runtime.SafeRunner;
20
import org.eclipse.ui.IEditorInput;
21
import org.eclipse.ui.internal.navigator.NavigatorPlugin;
22
import org.eclipse.ui.internal.navigator.NavigatorSafeRunnable;
23
import org.eclipse.ui.navigator.INavigatorContentService;
24

    
25
/**
26
 * @since 3.2
27
 *
28
 */
29
public class LinkHelperManager {
30

    
31
	private static final LinkHelperManager instance = new LinkHelperManager();
32

    
33
	private static final LinkHelperDescriptor[] NO_DESCRIPTORS = new LinkHelperDescriptor[0];
34

    
35
	private List descriptors;
36

    
37
	/**
38
	 * @return the singleton instance.
39
	 */
40
	public static LinkHelperManager getInstance() {
41
		return instance;
42
	}
43

    
44
	private LinkHelperManager() {
45
		new LinkHelperRegistry().readRegistry();
46
	}
47

    
48
	/**
49
	 * Return the link helper descriptors for a given selection and content
50
	 * service.
51
	 *
52
	 * @param anObject
53
	 *            An object from the viewer.
54
	 * @param aContentService
55
	 *            The content service to use for visibility filtering. Link
56
	 *            Helpers are filtered by contentExtension elements in
57
	 *            viewerContentBindings.
58
	 * @return An array of <i>visible</i> and <i>enabled</i> Link Helpers or
59
	 *         an empty array.
60
	 */
61
	public LinkHelperDescriptor[] getLinkHelpersFor(
62
			Object anObject,
63
			INavigatorContentService aContentService) {
64

    
65
		List helpersList = new ArrayList();
66
		LinkHelperDescriptor descriptor = null;
67
		for (Iterator itr = getDescriptors().iterator(); itr.hasNext();) {
68
			descriptor = (LinkHelperDescriptor) itr.next();
69
			if (aContentService.isVisible(descriptor.getId())
70
					&& descriptor.isEnabledFor(anObject)) {
71
				helpersList.add(descriptor);
72
			}
73
		}
74
		if (helpersList.size() == 0) {
75
			return NO_DESCRIPTORS;
76
		}
77
		return (LinkHelperDescriptor[]) helpersList
78
				.toArray(new LinkHelperDescriptor[helpersList.size()]);
79

    
80
	}
81

    
82
	/**
83
	 * Return the link helper descriptors for a given selection and content
84
	 * service.
85
	 *
86
	 * @param anInput
87
	 *            The input of the active viewer.
88
	 * @param aContentService
89
	 *            The content service to use for visibility filtering. Link
90
	 *            Helpers are filtered by contentExtension elements in
91
	 *            viewerContentBindings.
92
	 * @return An array of <i>visible</i> and <i>enabled</i> Link Helpers or
93
	 *         an empty array.
94
	 */
95
	public LinkHelperDescriptor[] getLinkHelpersFor(IEditorInput anInput,
96
			INavigatorContentService aContentService) {
97

    
98
		List helpersList = new ArrayList();
99
		LinkHelperDescriptor descriptor = null;
100
		for (Iterator itr = getDescriptors().iterator(); itr.hasNext();) {
101
			descriptor = (LinkHelperDescriptor) itr.next();
102
			if (aContentService.isVisible(descriptor.getId())
103
					&& descriptor.isEnabledFor(anInput)) {
104
				helpersList.add(descriptor);
105
			}
106
		}
107
		if (helpersList.size() == 0) {
108
			return NO_DESCRIPTORS;
109
		}
110
		return (LinkHelperDescriptor[]) helpersList
111
				.toArray(new LinkHelperDescriptor[helpersList.size()]);
112

    
113
	}
114

    
115
	protected List getDescriptors() {
116
		if (descriptors == null) {
117
			descriptors = new ArrayList();
118
		}
119
		return descriptors;
120
	}
121

    
122
	private class LinkHelperRegistry extends RegistryReader implements
123
			ILinkHelperExtPtConstants {
124

    
125
		private LinkHelperRegistry() {
126
			super(NavigatorPlugin.NAVIGATOR_PLUGIN_ID, LINK_HELPER);
127
		}
128

    
129
		public boolean readElement(final IConfigurationElement element) {
130
			if (LINK_HELPER.equals(element.getName())) {
131
				final boolean retValue[] = new boolean[1];
132
				SafeRunner.run(new NavigatorSafeRunnable(element) {
133
					public void run() throws Exception {
134
						getDescriptors().add(new LinkHelperDescriptor(element));
135
						retValue[0] = true;
136
					}
137
				});
138
				return retValue[0];
139
			}
140
			return false;
141
		}
142
	}
143
}
(14-14/29)