1 17 package org.eclipse.emf.edit.ui.provider; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 24 import org.eclipse.jface.viewers.ILabelProvider; 25 import org.eclipse.jface.viewers.ILabelProviderListener; 26 import org.eclipse.jface.viewers.ITableLabelProvider; 27 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 28 import org.eclipse.swt.graphics.Image; 29 30 import org.eclipse.emf.common.notify.AdapterFactory; 31 import org.eclipse.emf.common.notify.Notification; 32 import org.eclipse.emf.edit.EMFEditPlugin; 33 import org.eclipse.emf.edit.provider.IChangeNotifier; 34 import org.eclipse.emf.edit.provider.IItemLabelProvider; 35 import org.eclipse.emf.edit.provider.INotifyChangedListener; 36 import org.eclipse.emf.edit.provider.ITableItemLabelProvider; 37 38 39 51 public class AdapterFactoryLabelProvider implements ILabelProvider, ITableLabelProvider, INotifyChangedListener 52 { 53 58 protected AdapterFactory adapterFactory; 59 60 63 protected Collection labelProviderListeners; 64 65 private static final Class IItemLabelProviderClass = IItemLabelProvider.class; 66 private static final Class ITableItemLabelProviderClass = ITableItemLabelProvider.class; 67 68 72 public AdapterFactoryLabelProvider(AdapterFactory adapterFactory) 73 { 74 this.adapterFactory = adapterFactory; 75 if (adapterFactory instanceof IChangeNotifier) 76 { 77 ((IChangeNotifier)adapterFactory).addListener(this); 78 } 79 80 labelProviderListeners = new ArrayList (); 81 } 82 83 86 public AdapterFactory getAdapterFactory() 87 { 88 return adapterFactory; 89 } 90 91 94 public void setAdapterFactory(AdapterFactory adapterFactory) 95 { 96 if (this.adapterFactory instanceof IChangeNotifier) 97 { 98 ((IChangeNotifier)this.adapterFactory).removeListener(this); 99 } 100 101 if (adapterFactory instanceof IChangeNotifier) 102 { 103 ((IChangeNotifier)adapterFactory).addListener(this); 104 } 105 106 this.adapterFactory = adapterFactory; 107 } 108 109 112 public void addListener(ILabelProviderListener listener) 113 { 114 labelProviderListeners.add(listener); 115 } 116 117 120 public void removeListener(ILabelProviderListener listener) 121 { 122 labelProviderListeners.remove(listener); 123 } 124 125 128 public void dispose() 129 { 130 if (this.adapterFactory instanceof IChangeNotifier) 131 { 132 ((IChangeNotifier)adapterFactory).removeListener(this); 133 } 134 } 135 136 139 public boolean isLabelProperty(Object object, String id) 140 { 141 return true; 142 } 143 144 148 public Image getImage(Object object) 149 { 150 IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProviderClass); 153 154 return 155 itemLabelProvider != null ? 156 getImageFromObject(itemLabelProvider.getImage(object)) : 157 getDefaultImage(object); 158 } 159 160 protected Image getDefaultImage(Object object) 161 { 162 String image = "full/obj16/GenericValue"; 163 if (object instanceof String ) 164 { 165 image = "full/obj16/TextValue"; 166 } 167 else if (object instanceof Boolean ) 168 { 169 image = "full/obj16/BooleanValue"; 170 } 171 else if (object instanceof Float || object instanceof Double ) 172 { 173 image = "full/obj16/RealValue"; 174 } 175 else if (object instanceof Integer || object instanceof Short || object instanceof Long || object instanceof Byte ) 176 { 177 image = "full/obj16/RealValue"; 178 } 179 180 return getImageFromObject(EMFEditPlugin.INSTANCE.getImage(image)); 181 } 182 183 protected Image getImageFromObject(Object object) 184 { 185 return ExtendedImageRegistry.getInstance().getImage(object); 186 } 187 188 192 public String getText(Object object) 193 { 194 IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProviderClass); 197 198 return 199 itemLabelProvider != null ? 200 itemLabelProvider.getText(object) : 201 object == null ? 202 "" : 203 object.toString(); 204 } 205 206 213 public Image getColumnImage(Object object, int columnIndex) 214 { 215 ITableItemLabelProvider tableItemLabelProvider = (ITableItemLabelProvider)adapterFactory.adapt(object, ITableItemLabelProviderClass); 218 219 Image result = null; 222 223 if (tableItemLabelProvider != null) 226 { 227 result = getImageFromObject(tableItemLabelProvider.getColumnImage(object, columnIndex)); 230 } 231 else 234 { 235 IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProviderClass); 236 if (itemLabelProvider != null) 237 { 238 result = getImageFromObject(itemLabelProvider.getImage(object)); 241 } 242 } 243 244 return result; 245 } 246 247 254 public String getColumnText(Object object, int columnIndex) 255 { 256 ITableItemLabelProvider tableItemLabelProvider = (ITableItemLabelProvider)adapterFactory.adapt(object, ITableItemLabelProviderClass); 259 260 if (tableItemLabelProvider != null) 263 { 264 return tableItemLabelProvider.getColumnText(object, columnIndex); 267 } 268 else 271 { 272 IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProviderClass); 273 if (itemLabelProvider != null) 274 { 275 return itemLabelProvider.getText(object); 278 } 279 else if (object != null) 282 { 283 return object.toString(); 284 } 285 else 286 { 287 return ""; 288 } 289 } 290 } 291 292 public void fireLabelProviderChanged() 293 { 294 for (Iterator i = labelProviderListeners.iterator(); i.hasNext(); ) 295 { 296 ILabelProviderListener labelProviderListener = (ILabelProviderListener)i.next(); 297 labelProviderListener.labelProviderChanged(new LabelProviderChangedEvent(this)); 298 } 299 } 300 301 public void notifyChanged(Notification notification) 302 { 303 } 305 } 306 | Popular Tags |