KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > wizards > CommonWizardDescriptorManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.ui.internal.navigator.wizards;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.ui.WorkbenchException;
23 import org.eclipse.ui.activities.WorkbenchActivityHelper;
24 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
25 import org.eclipse.ui.internal.navigator.extensions.NavigatorContentRegistryReader;
26 import org.eclipse.ui.navigator.INavigatorContentService;
27
28 /**
29  * <p>
30  * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
31  * part of a work in progress. There is a guarantee neither that this API will
32  * work nor that it will remain the same. Please do not use this API without
33  * consulting with the Platform/UI team.
34  * </p>
35  *
36  * @since 3.2
37  */

38 public class CommonWizardDescriptorManager {
39
40     private static final CommonWizardDescriptorManager INSTANCE = new CommonWizardDescriptorManager();
41
42     private static boolean isInitialized = false;
43
44     private static final String JavaDoc[] NO_DESCRIPTOR_IDS = new String JavaDoc[0];
45
46     private static final CommonWizardDescriptor[] NO_DESCRIPTORS = new CommonWizardDescriptor[0];
47     
48     /**
49      * Find wizards of type 'new'.
50      */

51     public static final String JavaDoc WIZARD_TYPE_NEW = "new"; //$NON-NLS-1$
52

53     private Map JavaDoc commonWizardDescriptors = new HashMap JavaDoc();
54
55     /**
56      * @return the singleton instance of the registry
57      */

58     public static CommonWizardDescriptorManager getInstance() {
59         if (isInitialized) {
60             return INSTANCE;
61         }
62         synchronized (INSTANCE) {
63             if (!isInitialized) {
64                 INSTANCE.init();
65                 isInitialized = true;
66             }
67         }
68         return INSTANCE;
69     }
70
71     private void init() {
72         new CommonWizardRegistry().readRegistry();
73     }
74
75     private void addCommonWizardDescriptor(CommonWizardDescriptor aDesc) {
76         if (aDesc == null) {
77             return;
78         } else if(aDesc.getWizardId() == null) {
79             NavigatorPlugin.logError(0, "A null wizardId was supplied for a commonWizard in " + aDesc.getNamespace(), null); //$NON-NLS-1$
80
}
81         synchronized (commonWizardDescriptors) {
82             Set JavaDoc descriptors = (Set JavaDoc) commonWizardDescriptors.get(aDesc
83                     .getType());
84             if (descriptors == null) {
85                 commonWizardDescriptors.put(aDesc.getType(), descriptors = new HashSet JavaDoc());
86             }
87             if (!descriptors.contains(aDesc)) {
88                 descriptors.add(aDesc);
89             }
90         }
91     }
92
93     /**
94      *
95      * Returns all wizard id(s) which enable for the given element.
96      *
97      * @param anElement
98      * the element to return the best content descriptor for
99      * @param aType
100      * The type of wizards to locate (e.g. 'new', 'import', or
101      * 'export' etc).
102      * @param aContentService
103      * The content service to use when deciding visibility.
104      * @return The set of commonWizard ids for the given element
105      */

106     public String JavaDoc[] getEnabledCommonWizardDescriptorIds(Object JavaDoc anElement,
107             String JavaDoc aType, INavigatorContentService aContentService) {
108
109         Set JavaDoc commonDescriptors = (Set JavaDoc) commonWizardDescriptors.get(aType);
110         if (commonDescriptors == null) {
111             return NO_DESCRIPTOR_IDS;
112         }
113         /* Find other Common Wizard providers which enable for this object */
114         List JavaDoc descriptorIds = new ArrayList JavaDoc();
115         for (Iterator JavaDoc commonWizardDescriptorsItr = commonDescriptors.iterator(); commonWizardDescriptorsItr
116                 .hasNext();) {
117             CommonWizardDescriptor descriptor = (CommonWizardDescriptor) commonWizardDescriptorsItr
118                     .next();
119
120             if (isVisible(aContentService, descriptor)
121                     && descriptor.isEnabledFor(anElement)) {
122                 descriptorIds.add(descriptor.getWizardId());
123             }
124         }
125         String JavaDoc[] wizardIds = new String JavaDoc[descriptorIds.size()];
126         return (String JavaDoc[]) descriptorIds.toArray(wizardIds);
127     }
128     
129
130     /**
131      *
132      * Returns all wizard descriptor(s) which enable for the given element.
133      *
134      * @param anElement
135      * the element to return the best content descriptor for
136      * @param aType
137      * The type of wizards to locate (e.g. 'new', 'import', or
138      * 'export' etc).
139      * @param aContentService
140      * The content service to use when deciding visibility.
141      * @return The set of commonWizard descriptors for the element
142      */

143     public CommonWizardDescriptor[] getEnabledCommonWizardDescriptors(Object JavaDoc anElement,
144             String JavaDoc aType, INavigatorContentService aContentService) {
145
146         Set JavaDoc commonDescriptors = (Set JavaDoc) commonWizardDescriptors.get(aType);
147         if (commonDescriptors == null) {
148             return NO_DESCRIPTORS;
149         }
150         /* Find other Common Wizard providers which enable for this object */
151         List JavaDoc descriptors = new ArrayList JavaDoc();
152         for (Iterator JavaDoc commonWizardDescriptorsItr = commonDescriptors.iterator(); commonWizardDescriptorsItr
153                 .hasNext();) {
154             CommonWizardDescriptor descriptor = (CommonWizardDescriptor) commonWizardDescriptorsItr
155                     .next();
156
157             if (isVisible(aContentService, descriptor)
158                     && descriptor.isEnabledFor(anElement)) {
159                 descriptors.add(descriptor);
160             }
161         }
162         CommonWizardDescriptor[] enabledDescriptors = new CommonWizardDescriptor[descriptors.size()];
163         return (CommonWizardDescriptor[]) descriptors.toArray(enabledDescriptors);
164     }
165
166     /**
167      * @param aContentService
168      * @param descriptor
169      * @return True if the descriptor is visible to the given content service.
170      */

171     private boolean isVisible(INavigatorContentService aContentService, CommonWizardDescriptor descriptor) {
172         return !WorkbenchActivityHelper.filterItem(descriptor) &&
173                     (aContentService == null ||
174                             (descriptor.getId() == null ||
175                                     ( aContentService.isVisible(descriptor.getId()) &&
176                                             aContentService.isActive(descriptor.getId())
177                                     )
178                             )
179                     );
180     }
181   
182     private class CommonWizardRegistry extends NavigatorContentRegistryReader {
183  
184
185         protected boolean readElement(IConfigurationElement anElement) {
186             if (TAG_COMMON_WIZARD.equals(anElement.getName())) {
187                 try {
188                     addCommonWizardDescriptor(new CommonWizardDescriptor(
189                             anElement));
190                 } catch (WorkbenchException e) {
191                     // log an error since its not safe to open a dialog here
192
NavigatorPlugin
193                             .logError(0, e.getMessage(), e);
194                     return false;
195                 }
196                 return true;
197             } if(TAG_NAVIGATOR_CONTENT.equals(anElement.getName())) {
198                 
199                 IConfigurationElement[] commonWizards = anElement.getChildren(TAG_COMMON_WIZARD);
200                 
201                 String JavaDoc contentExtensionId = anElement.getAttribute(ATT_ID);
202                 for (int i = 0; i < commonWizards.length; i++) {
203                     try {
204                         addCommonWizardDescriptor(new CommonWizardDescriptor(
205                                     commonWizards[i], contentExtensionId));
206                     } catch (WorkbenchException e) {
207                         // log an error since its not safe to open a dialog here
208
NavigatorPlugin
209                                 .logError(0, e.getMessage(), e);
210                         return false;
211                     }
212                 }
213                 return true;
214             }
215             return super.readElement(anElement);
216         }
217     }
218      
219 }
220
Popular Tags