1 17 package org.eclipse.emf.edit.ui.provider; 18 19 20 import java.util.Collections ; 21 22 import org.eclipse.jface.viewers.ITreeContentProvider; 23 import org.eclipse.jface.viewers.StructuredViewer; 24 import org.eclipse.jface.viewers.Viewer; 25 import org.eclipse.ui.views.properties.IPropertySource; 26 import org.eclipse.ui.views.properties.IPropertySourceProvider; 27 import org.eclipse.swt.widgets.Display; 28 29 import org.eclipse.emf.common.notify.AdapterFactory; 30 import org.eclipse.emf.common.notify.Notification; 31 import org.eclipse.emf.ecore.EObject; 32 import org.eclipse.emf.edit.provider.IChangeNotifier; 33 import org.eclipse.emf.edit.provider.IItemPropertySource; 34 import org.eclipse.emf.edit.provider.INotifyChangedListener; 35 import org.eclipse.emf.edit.provider.IStructuredItemContentProvider; 36 import org.eclipse.emf.edit.provider.ITreeItemContentProvider; 37 import org.eclipse.emf.edit.provider.IViewerNotification; 38 39 40 50 public class AdapterFactoryContentProvider 51 implements 52 ITreeContentProvider, 53 IPropertySourceProvider, 54 INotifyChangedListener 55 { 56 61 protected AdapterFactory adapterFactory; 62 63 66 protected Viewer viewer; 67 68 private static final Class IStructuredItemContentProviderClass = IStructuredItemContentProvider.class; 69 private static final Class ITreeItemContentProviderClass = ITreeItemContentProvider.class; 70 private static final Class IItemPropertySourceClass = IItemPropertySource.class; 71 72 76 public AdapterFactoryContentProvider(AdapterFactory adapterFactory) 77 { 78 this.adapterFactory = adapterFactory; 79 80 if (adapterFactory instanceof IChangeNotifier) 81 { 82 ((IChangeNotifier)adapterFactory).addListener(this); 83 } 84 } 85 86 89 public void setAdapterFactory(AdapterFactory adapterFactory) 90 { 91 if (this.adapterFactory instanceof IChangeNotifier) 92 { 93 ((IChangeNotifier)this.adapterFactory).removeListener(this); 94 } 95 96 if (adapterFactory instanceof IChangeNotifier) 97 { 98 ((IChangeNotifier)adapterFactory).addListener(this); 99 } 100 101 this.adapterFactory = adapterFactory; 102 } 103 104 107 public AdapterFactory getAdapterFactory() 108 { 109 return adapterFactory; 110 } 111 112 115 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) 116 { 117 this.viewer = viewer; 120 } 121 122 127 public Object [] getElements(Object object) 128 { 129 IStructuredItemContentProvider structuredItemContentProvider = 132 (IStructuredItemContentProvider)adapterFactory.adapt(object, IStructuredItemContentProviderClass); 133 134 return 137 (structuredItemContentProvider != null ? 138 structuredItemContentProvider.getElements(object) : 139 Collections.EMPTY_LIST).toArray(); 140 } 141 142 146 public Object [] getChildren(Object object) 147 { 148 ITreeItemContentProvider treeItemContentProvider = 151 (ITreeItemContentProvider)adapterFactory.adapt(object, ITreeItemContentProviderClass); 152 153 return 156 (treeItemContentProvider != null ? 157 treeItemContentProvider.getChildren(object) : 158 Collections.EMPTY_LIST).toArray(); 159 } 160 161 165 public boolean hasChildren(Object object) 166 { 167 ITreeItemContentProvider treeItemContentProvider = 170 (ITreeItemContentProvider)adapterFactory.adapt(object, ITreeItemContentProviderClass); 171 172 return 175 treeItemContentProvider != null && 176 treeItemContentProvider.hasChildren(object); 177 } 178 179 183 public Object getParent(Object object) 184 { 185 ITreeItemContentProvider treeItemContentProvider = 188 (ITreeItemContentProvider)adapterFactory.adapt(object, ITreeItemContentProviderClass); 189 190 return 193 treeItemContentProvider != null ? 194 treeItemContentProvider.getParent(object) : 195 null; 196 } 197 198 201 public void dispose() 202 { 203 if (adapterFactory instanceof IChangeNotifier) 204 { 205 ((IChangeNotifier)adapterFactory).removeListener(this); 206 } 207 viewer = null; 208 } 209 210 214 public IPropertySource getPropertySource(Object object) 215 { 216 if (object instanceof IPropertySource) 217 { 218 return (IPropertySource)object; 219 } 220 else 221 { 222 IItemPropertySource itemPropertySource = 223 (IItemPropertySource) 224 (object instanceof EObject && ((EObject)object).eClass() == null ? 225 null : 226 adapterFactory.adapt(object, IItemPropertySourceClass)); 227 228 return 229 itemPropertySource != null ? createPropertySource(object, itemPropertySource) : null; 230 } 231 } 232 233 protected IPropertySource createPropertySource(Object object, IItemPropertySource itemPropertySource) 234 { 235 return new PropertySource(object, itemPropertySource); 236 } 237 238 public void notifyChanged(Notification notification) 239 { 240 if (viewer != null && viewer.getControl() != null && !viewer.getControl().isDisposed()) 241 { 242 if (notification instanceof IViewerNotification) 247 { 248 ViewerRefresh viewerRefresh = new ViewerRefresh(viewer, (IViewerNotification)notification); 249 250 Display d = viewer.getControl().getDisplay(); 251 if (d != Display.getCurrent()) 252 { 253 d.asyncExec(viewerRefresh); 254 } 255 else 256 { 257 viewerRefresh.run(); 258 } 259 } 260 else 261 { 262 NotifyChangedToViewerRefresh.handleNotifyChanged( 263 viewer, 264 notification.getNotifier(), 265 notification.getEventType(), 266 notification.getFeature(), 267 notification.getOldValue(), 268 notification.getNewValue(), 269 notification.getPosition()); 270 } 271 } 272 } 273 274 278 public static class ViewerRefresh implements Runnable 279 { 280 Viewer viewer; 281 IViewerNotification notification; 282 283 public ViewerRefresh(Viewer viewer, IViewerNotification notification) 284 { 285 this.viewer = viewer; 286 this.notification = notification; 287 } 288 289 public void run() 290 { 291 if (notification.getEventType() == Notification.RESOLVE) return; 294 295 Object element = notification.getElement(); 296 297 if (viewer instanceof StructuredViewer) 298 { 299 StructuredViewer structuredViewer = (StructuredViewer)viewer; 300 301 if (element != null) 302 { 303 if (notification.isContentRefresh()) 304 { 305 structuredViewer.refresh(element, notification.isLabelUpdate()); 306 } 307 else if (notification.isLabelUpdate()) 308 { 309 structuredViewer.update(element, null); 310 } 311 } 312 else 313 { 314 structuredViewer.refresh(notification.isLabelUpdate()); 315 } 316 } 317 else 318 { 319 viewer.refresh(); 320 } 321 } 322 } 323 } 324 | Popular Tags |