1 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.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 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 [] NO_DESCRIPTOR_IDS = new String [0]; 45 46 private static final CommonWizardDescriptor[] NO_DESCRIPTORS = new CommonWizardDescriptor[0]; 47 48 51 public static final String WIZARD_TYPE_NEW = "new"; 53 private Map commonWizardDescriptors = new HashMap (); 54 55 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); } 81 synchronized (commonWizardDescriptors) { 82 Set descriptors = (Set ) commonWizardDescriptors.get(aDesc 83 .getType()); 84 if (descriptors == null) { 85 commonWizardDescriptors.put(aDesc.getType(), descriptors = new HashSet ()); 86 } 87 if (!descriptors.contains(aDesc)) { 88 descriptors.add(aDesc); 89 } 90 } 91 } 92 93 106 public String [] getEnabledCommonWizardDescriptorIds(Object anElement, 107 String aType, INavigatorContentService aContentService) { 108 109 Set commonDescriptors = (Set ) commonWizardDescriptors.get(aType); 110 if (commonDescriptors == null) { 111 return NO_DESCRIPTOR_IDS; 112 } 113 114 List descriptorIds = new ArrayList (); 115 for (Iterator 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 [] wizardIds = new String [descriptorIds.size()]; 126 return (String []) descriptorIds.toArray(wizardIds); 127 } 128 129 130 143 public CommonWizardDescriptor[] getEnabledCommonWizardDescriptors(Object anElement, 144 String aType, INavigatorContentService aContentService) { 145 146 Set commonDescriptors = (Set ) commonWizardDescriptors.get(aType); 147 if (commonDescriptors == null) { 148 return NO_DESCRIPTORS; 149 } 150 151 List descriptors = new ArrayList (); 152 for (Iterator 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 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 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 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 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 |