Project

General

Profile

Download (7.08 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 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
package org.eclipse.ui.internal.navigator.wizards;
12

    
13
import java.util.ArrayList;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.Iterator;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.Set;
20

    
21
import org.eclipse.core.runtime.IConfigurationElement;
22
import org.eclipse.core.runtime.SafeRunner;
23
import org.eclipse.ui.activities.WorkbenchActivityHelper;
24
import org.eclipse.ui.internal.navigator.NavigatorPlugin;
25
import org.eclipse.ui.internal.navigator.NavigatorSafeRunnable;
26
import org.eclipse.ui.internal.navigator.extensions.NavigatorContentRegistryReader;
27
import org.eclipse.ui.navigator.INavigatorContentService;
28

    
29
/**
30
 * @since 3.2
31
 */
32
public class CommonWizardDescriptorManager {
33

    
34
	private static final CommonWizardDescriptorManager INSTANCE = new CommonWizardDescriptorManager();
35

    
36
	private static boolean isInitialized = false;
37

    
38
	private static final String[] NO_DESCRIPTOR_IDS = new String[0];
39

    
40
	private static final CommonWizardDescriptor[] NO_DESCRIPTORS = new CommonWizardDescriptor[0];
41
	
42
	/**
43
	 * Find wizards of type 'new'.
44
	 */
45
	public static final String WIZARD_TYPE_NEW = "new"; //$NON-NLS-1$
46

    
47
	private Map commonWizardDescriptors = new HashMap();
48

    
49
	/**
50
	 * @return the singleton instance of the registry
51
	 */
52
	public static CommonWizardDescriptorManager getInstance() {
53
		if (isInitialized) {
54
			return INSTANCE;
55
		}
56
		synchronized (INSTANCE) {
57
			if (!isInitialized) {
58
				INSTANCE.init();
59
				isInitialized = true;
60
			}
61
		}
62
		return INSTANCE;
63
	}
64

    
65
	private void init() {
66
		new CommonWizardRegistry().readRegistry();
67
	}
68

    
69
	private void addCommonWizardDescriptor(CommonWizardDescriptor aDesc) {
70
		if (aDesc == null) {
71
			return;
72
		} else if(aDesc.getWizardId() == null) {
73
			NavigatorPlugin.logError(0, "A null wizardId was supplied for a commonWizard in " + aDesc.getNamespace(), null); //$NON-NLS-1$
74
		}
75
		synchronized (commonWizardDescriptors) {
76
			Set descriptors = (Set) commonWizardDescriptors.get(aDesc
77
					.getType());
78
			if (descriptors == null) { 
79
				commonWizardDescriptors.put(aDesc.getType(), descriptors = new HashSet());
80
			}
81
			if (!descriptors.contains(aDesc)) {
82
				descriptors.add(aDesc);
83
			}
84
		}
85
	}
86

    
87
	/**
88
	 * 
89
	 * Returns all wizard id(s) which enable for the given element.
90
	 * 
91
	 * @param anElement
92
	 *            the element to return the best content descriptor for
93
	 * @param aType
94
	 *            The type of wizards to locate (e.g. 'new', 'import', or
95
	 *            'export' etc).
96
	 * @param aContentService 
97
	 * 			 The content service to use when deciding visibility.   
98
	 * @return The set of commonWizard ids for the given element
99
	 */
100
	public String[] getEnabledCommonWizardDescriptorIds(Object anElement,
101
			String aType, INavigatorContentService aContentService) {
102

    
103
		Set commonDescriptors = (Set) commonWizardDescriptors.get(aType);
104
		if (commonDescriptors == null) {
105
			return NO_DESCRIPTOR_IDS;
106
		}
107
		/* Find other Common Wizard providers which enable for this object */
108
		List descriptorIds = new ArrayList();
109
		for (Iterator commonWizardDescriptorsItr = commonDescriptors.iterator(); commonWizardDescriptorsItr
110
				.hasNext();) {
111
			CommonWizardDescriptor descriptor = (CommonWizardDescriptor) commonWizardDescriptorsItr
112
					.next();
113

    
114
			if (isVisible(aContentService, descriptor)
115
					&& descriptor.isEnabledFor(anElement)) {
116
				descriptorIds.add(descriptor.getWizardId());
117
			}
118
		}
119
		String[] wizardIds = new String[descriptorIds.size()];
120
		return (String[]) descriptorIds.toArray(wizardIds); 
121
	}
122
	
123

    
124
	/**
125
	 * 
126
	 * Returns all wizard descriptor(s) which enable for the given element.
127
	 * 
128
	 * @param anElement
129
	 *            the element to return the best content descriptor for
130
	 * @param aType
131
	 *            The type of wizards to locate (e.g. 'new', 'import', or
132
	 *            'export' etc).
133
	 * @param aContentService 
134
	 * 			 The content service to use when deciding visibility.   
135
	 * @return The set of commonWizard descriptors for the element
136
	 */
137
	public CommonWizardDescriptor[] getEnabledCommonWizardDescriptors(Object anElement,
138
			String aType, INavigatorContentService aContentService) {
139

    
140
		Set commonDescriptors = (Set) commonWizardDescriptors.get(aType);
141
		if (commonDescriptors == null) {
142
			return NO_DESCRIPTORS;
143
		}
144
		/* Find other Common Wizard providers which enable for this object */
145
		List descriptors = new ArrayList();
146
		for (Iterator commonWizardDescriptorsItr = commonDescriptors.iterator(); commonWizardDescriptorsItr
147
				.hasNext();) {
148
			CommonWizardDescriptor descriptor = (CommonWizardDescriptor) commonWizardDescriptorsItr
149
					.next();
150

    
151
			if (isVisible(aContentService, descriptor)
152
					&& descriptor.isEnabledFor(anElement)) {
153
				descriptors.add(descriptor);
154
			}
155
		}
156
		CommonWizardDescriptor[] enabledDescriptors = new CommonWizardDescriptor[descriptors.size()];
157
		return (CommonWizardDescriptor[]) descriptors.toArray(enabledDescriptors);  
158
	}
159

    
160
	/**
161
	 * @param aContentService
162
	 * @param descriptor
163
	 * @return True if the descriptor is visible to the given content service.
164
	 */
165
	private boolean isVisible(INavigatorContentService aContentService, CommonWizardDescriptor descriptor) {
166
		return !WorkbenchActivityHelper.filterItem(descriptor) && 
167
					(aContentService == null || 
168
							(descriptor.getId() == null || 
169
									( aContentService.isVisible(descriptor.getId()) && 
170
											aContentService.isActive(descriptor.getId())
171
									)
172
							)
173
					);
174
	}
175
  
176
	private class CommonWizardRegistry extends NavigatorContentRegistryReader {
177

    
178
		protected boolean readElement(final IConfigurationElement anElement) {
179
			final boolean[] retValue = new boolean[1];
180

    
181
			if (TAG_COMMON_WIZARD.equals(anElement.getName())) {
182
				SafeRunner.run(new NavigatorSafeRunnable(anElement) {
183
					public void run() throws Exception {
184
						addCommonWizardDescriptor(new CommonWizardDescriptor(anElement));
185
						retValue[0] = true;
186
					}
187
				});
188
				return retValue[0];
189
			}
190
			if (TAG_NAVIGATOR_CONTENT.equals(anElement.getName())) {
191
				IConfigurationElement[] commonWizards = anElement.getChildren(TAG_COMMON_WIZARD);
192
				final String contentExtensionId = anElement.getAttribute(ATT_ID);
193
				// In case there are none
194
				retValue[0] = true;
195
				for (int i = 0; i < commonWizards.length; i++) {
196
					final IConfigurationElement element = commonWizards[i];
197
					// Assume it did not work
198
					retValue[0] = false;
199
					SafeRunner.run(new NavigatorSafeRunnable(element) {
200
						public void run() throws Exception {
201
							addCommonWizardDescriptor(new CommonWizardDescriptor(element,
202
									contentExtensionId));
203
							retValue[0] = true;
204
						}
205
					});
206
					if (!retValue[0])
207
						break;
208
				}
209
				return retValue[0];
210
			}
211
			return super.readElement(anElement);
212
		}
213
	}
214

    
215
}
(2-2/3)