1 11 package org.eclipse.ui.internal.activities.ws; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.jface.resource.DeviceResourceException; 17 import org.eclipse.jface.resource.ImageDescriptor; 18 import org.eclipse.jface.resource.JFaceResources; 19 import org.eclipse.jface.resource.LocalResourceManager; 20 import org.eclipse.jface.viewers.LabelProvider; 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.ui.PlatformUI; 23 import org.eclipse.ui.activities.IActivity; 24 import org.eclipse.ui.activities.ICategory; 25 import org.eclipse.ui.activities.NotDefinedException; 26 27 33 public class ActivityCategoryLabelProvider extends LabelProvider { 34 35 private LocalResourceManager manager; 36 private Map descriptorMap = new HashMap (); 37 38 41 public ActivityCategoryLabelProvider() { 42 manager = new LocalResourceManager(JFaceResources.getResources()); 43 } 44 45 48 public Image getImage(Object element) { 49 try { 50 ImageDescriptor descriptor = getDescriptor(element); 51 if (descriptor != null) { 52 return manager.createImage(descriptor); 53 } 54 } 55 catch (DeviceResourceException e) { 56 } 58 return null; 59 } 60 61 private ImageDescriptor getDescriptor(Object element) { 62 ImageDescriptor descriptor = (ImageDescriptor) descriptorMap.get(element); 63 if (descriptor != null) { 64 return descriptor; 65 } 66 67 if (element instanceof ICategory) { 68 ICategory category = (ICategory) element; 69 descriptor = PlatformUI.getWorkbench().getActivitySupport() 70 .getImageDescriptor(category); 71 if (descriptor != null) { 72 descriptorMap.put(element, descriptor); 73 } 74 } else if (element instanceof IActivity) { 75 IActivity activity = (IActivity) element; 76 descriptor = PlatformUI.getWorkbench().getActivitySupport() 77 .getImageDescriptor(activity); 78 if (descriptor != null) { 79 descriptorMap.put(element, descriptor); 80 } 81 } 82 return descriptor; 83 } 84 85 88 public String getText(Object element) { 89 if (element instanceof IActivity) { 90 IActivity activity = (IActivity) element; 91 try { 92 return activity.getName(); 93 } catch (NotDefinedException e) { 94 return activity.getId(); 95 } 96 } else if (element instanceof ICategory) { 97 ICategory category = ((ICategory) element); 98 try { 99 return category.getName(); 100 } catch (NotDefinedException e) { 101 return category.getId(); 102 } 103 } 104 return super.getText(element); 105 } 106 107 110 public void dispose() { 111 manager.dispose(); 112 descriptorMap.clear(); 113 } 114 } 115 | Popular Tags |