1 11 package org.eclipse.ui.internal.navigator; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.core.runtime.ListenerList; 23 import org.eclipse.core.runtime.Preferences; 24 import org.eclipse.ui.internal.navigator.extensions.NavigatorContentDescriptor; 25 import org.eclipse.ui.internal.navigator.extensions.NavigatorContentDescriptorManager; 26 import org.eclipse.ui.navigator.IExtensionActivationListener; 27 import org.eclipse.ui.navigator.INavigatorActivationService; 28 import org.eclipse.ui.navigator.INavigatorContentDescriptor; 29 import org.eclipse.ui.navigator.INavigatorContentService; 30 31 43 public final class NavigatorActivationService implements 44 INavigatorActivationService { 45 46 private static final String ACTIVATED_EXTENSIONS = ".activatedExtensions"; 48 private static final NavigatorContentDescriptorManager CONTENT_DESCRIPTOR_REGISTRY = NavigatorContentDescriptorManager 49 .getInstance(); 50 51 private static final INavigatorContentDescriptor[] NO_DESCRIPTORS = new INavigatorContentDescriptor[0]; 52 53 private static final String DELIM = ";"; 55 private static final char EQUALS = '='; 56 57 60 62 65 private final Map activatedExtensionsMap = new HashMap (); 66 67 70 private final ListenerList listeners = new ListenerList(); 71 72 private INavigatorContentService contentService; 73 74 80 public NavigatorActivationService(INavigatorContentService aContentService) { 81 contentService = aContentService; 82 revertExtensionActivations(); 83 } 84 85 95 public boolean isNavigatorExtensionActive(String aNavigatorExtensionId) { 96 Boolean b = (Boolean ) activatedExtensionsMap.get(aNavigatorExtensionId); 97 if(b != null) 98 return b.booleanValue(); 99 synchronized (activatedExtensionsMap) { 100 NavigatorContentDescriptor descriptor = CONTENT_DESCRIPTOR_REGISTRY.getContentDescriptor(aNavigatorExtensionId); 101 if(descriptor.isActiveByDefault()) 102 activatedExtensionsMap.put(aNavigatorExtensionId, Boolean.TRUE); 103 else 104 activatedExtensionsMap.put(aNavigatorExtensionId, Boolean.FALSE); 105 return descriptor.isActiveByDefault(); 106 } 107 } 109 110 135 public void setActive( 136 String aNavigatorExtensionId, boolean toEnable) { 137 138 boolean currentlyActive = isNavigatorExtensionActive(aNavigatorExtensionId); 139 if (currentlyActive == toEnable) { 140 return; 141 } 142 143 if (toEnable) { 144 activatedExtensionsMap.put(aNavigatorExtensionId, Boolean.TRUE); 146 } else { 147 activatedExtensionsMap.put(aNavigatorExtensionId, Boolean.FALSE); 149 } 150 notifyListeners(new String [] { aNavigatorExtensionId }, toEnable); 151 152 } 153 154 174 public void setActive(String [] aNavigatorExtensionIds, 175 boolean toEnable) { 176 177 if (toEnable) { 178 for (int i = 0; i < aNavigatorExtensionIds.length; i++) { 179 activatedExtensionsMap.put(aNavigatorExtensionIds[i], Boolean.TRUE); 181 } 182 } else { 183 for (int i = 0; i < aNavigatorExtensionIds.length; i++) { 184 activatedExtensionsMap.put(aNavigatorExtensionIds[i], Boolean.FALSE); 186 } 187 } 188 notifyListeners(aNavigatorExtensionIds, toEnable); 189 190 } 191 192 196 public void persistExtensionActivations() { 197 198 Preferences preferences = NavigatorPlugin.getDefault() 199 .getPluginPreferences(); 200 201 synchronized (activatedExtensionsMap) { 203 Iterator activatedExtensionsIterator = activatedExtensionsMap.keySet().iterator(); 205 206 207 StringBuffer preferenceValue = new StringBuffer (); 208 String navigatorExtensionId = null; 209 boolean isActive = false; 210 while (activatedExtensionsIterator.hasNext()) { 211 navigatorExtensionId = (String ) activatedExtensionsIterator.next(); 212 isActive = isNavigatorExtensionActive(navigatorExtensionId); 213 preferenceValue.append(navigatorExtensionId) 214 .append(EQUALS) 215 .append( isActive ? Boolean.TRUE : Boolean.FALSE ) 216 .append(DELIM); 217 } 218 preferences.setValue(getPreferenceKey(), preferenceValue.toString()); 219 } 220 NavigatorPlugin.getDefault().savePluginPreferences(); 221 } 222 223 230 public void addExtensionActivationListener( 231 IExtensionActivationListener aListener) { 232 listeners.add(aListener); 233 } 234 235 241 public void removeExtensionActivationListener( 242 IExtensionActivationListener aListener) { 243 listeners.remove(aListener); 244 } 245 246 private void notifyListeners(String [] navigatorExtensionIds, 247 boolean toEnable) { 248 249 if(navigatorExtensionIds != null) { if(navigatorExtensionIds.length > 1) 251 Arrays.sort(navigatorExtensionIds); 252 253 Object [] listenerArray = listeners.getListeners(); 254 for (int i = 0; i < listenerArray.length; i++) { 255 ((IExtensionActivationListener) listenerArray[i]) 256 .onExtensionActivation(contentService.getViewerId(), 257 navigatorExtensionIds, toEnable); 258 } 259 } 260 261 } 262 263 private void revertExtensionActivations() { 264 265 Preferences preferences = NavigatorPlugin.getDefault() 266 .getPluginPreferences(); 267 268 String activatedExtensionsString = preferences 269 .getString(getPreferenceKey()); 270 271 if (activatedExtensionsString != null 272 && activatedExtensionsString.length() > 0) { 273 String [] contentExtensionIds = activatedExtensionsString 274 .split(DELIM); 275 276 String id = null; 277 String booleanString = null; 278 int indx=0; 279 for (int i = 0; i < contentExtensionIds.length; i++) { 280 if( (indx = contentExtensionIds[i].indexOf(EQUALS)) > -1) { 282 id = contentExtensionIds[i].substring(0, indx); 284 booleanString = contentExtensionIds[i].substring(indx+1, contentExtensionIds[i].length()); 285 activatedExtensionsMap.put(id, Boolean.valueOf(booleanString)); 286 } else { 287 NavigatorContentDescriptor descriptor = CONTENT_DESCRIPTOR_REGISTRY.getContentDescriptor(contentExtensionIds[i]); 289 if(descriptor != null) 290 activatedExtensionsMap.put(id, Boolean.valueOf(descriptor.isActiveByDefault())); 291 } 292 } 293 294 } else { 295 301 INavigatorContentDescriptor[] contentDescriptors = CONTENT_DESCRIPTOR_REGISTRY 302 .getAllContentDescriptors(); 303 for (int i = 0; i < contentDescriptors.length; i++) { 304 if (contentDescriptors[i].isActiveByDefault()) { 305 activatedExtensionsMap.put(contentDescriptors[i].getId(), Boolean.TRUE); 307 } 308 } 309 } 310 } 311 312 private String getPreferenceKey() { 313 return contentService.getViewerId() + ACTIVATED_EXTENSIONS; 314 } 315 316 317 public INavigatorContentDescriptor[] activateExtensions( 318 String [] extensionIds, boolean toDeactivateAllOthers) { 319 320 Set activatedDescriptors = new HashSet (); 321 setActive(extensionIds, true); 322 for (int extId = 0; extId < extensionIds.length; extId++) { 323 activatedDescriptors.add(CONTENT_DESCRIPTOR_REGISTRY 324 .getContentDescriptor(extensionIds[extId])); 325 } 326 327 if (toDeactivateAllOthers) { 328 NavigatorContentDescriptor[] descriptors = CONTENT_DESCRIPTOR_REGISTRY 329 .getAllContentDescriptors(); 330 List descriptorList = new ArrayList (Arrays.asList(descriptors)); 331 332 for (int descriptorIndx = 0; descriptorIndx < descriptors.length; descriptorIndx++) { 333 for (int extId = 0; extId < extensionIds.length; extId++) { 334 if (descriptors[descriptorIndx].getId().equals( 335 extensionIds[extId])) { 336 descriptorList.remove(descriptors[descriptorIndx]); 337 } 338 } 339 } 340 341 String [] deactivatedExtensions = new String [descriptorList.size()]; 342 for (int i = 0; i < descriptorList.size(); i++) { 343 INavigatorContentDescriptor descriptor = (INavigatorContentDescriptor) descriptorList 344 .get(i); 345 deactivatedExtensions[i] = descriptor.getId(); 346 } 347 setActive(deactivatedExtensions, false); 348 } 349 350 if (activatedDescriptors.size() == 0) { 351 return NO_DESCRIPTORS; 352 } 353 return (INavigatorContentDescriptor[]) activatedDescriptors 354 .toArray(new NavigatorContentDescriptor[activatedDescriptors 355 .size()]); 356 } 357 358 public INavigatorContentDescriptor[] deactivateExtensions( 359 String [] extensionIds, boolean toEnableAllOthers) { 360 361 Set activatedDescriptors = new HashSet (); 362 setActive(extensionIds, false); 363 364 if (toEnableAllOthers) { 365 NavigatorContentDescriptor[] descriptors = CONTENT_DESCRIPTOR_REGISTRY 366 .getAllContentDescriptors(); 367 List descriptorList = new ArrayList (Arrays.asList(descriptors)); 368 369 for (int descriptorIndx = 0; descriptorIndx < descriptors.length; descriptorIndx++) { 370 for (int extId = 0; extId < extensionIds.length; extId++) { 371 if (descriptors[descriptorIndx].getId().equals( 372 extensionIds[extId])) { 373 descriptorList.remove(descriptors[descriptorIndx]); 374 } 375 } 376 } 377 378 String [] activatedExtensions = new String [descriptorList.size()]; 379 for (int i = 0; i < descriptorList.size(); i++) { 380 NavigatorContentDescriptor descriptor = (NavigatorContentDescriptor) descriptorList 381 .get(i); 382 activatedExtensions[i] = descriptor.getId(); 383 activatedDescriptors.add(descriptor); 384 } 385 setActive(activatedExtensions, true); 386 } 387 if (activatedDescriptors.size() == 0) { 388 return NO_DESCRIPTORS; 389 } 390 391 return (INavigatorContentDescriptor[]) activatedDescriptors 392 .toArray(new NavigatorContentDescriptor[activatedDescriptors 393 .size()]); 394 } 395 396 397 } 398 | Popular Tags |