1 17 package org.eclipse.emf.edit.provider.resource; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 23 import org.eclipse.emf.common.notify.Adapter; 24 import org.eclipse.emf.common.notify.Notification; 25 import org.eclipse.emf.common.notify.Notifier; 26 import org.eclipse.emf.common.notify.impl.AdapterFactoryImpl; 27 import org.eclipse.emf.ecore.EObject; 28 import org.eclipse.emf.ecore.resource.Resource; 29 import org.eclipse.emf.ecore.resource.ResourceSet; 30 import org.eclipse.emf.edit.provider.ChangeNotifier; 31 import org.eclipse.emf.edit.provider.ComposeableAdapterFactory; 32 import org.eclipse.emf.edit.provider.ComposedAdapterFactory; 33 import org.eclipse.emf.edit.provider.Disposable; 34 import org.eclipse.emf.edit.provider.IChangeNotifier; 35 import org.eclipse.emf.edit.provider.IDisposable; 36 import org.eclipse.emf.edit.provider.IEditingDomainItemProvider; 37 import org.eclipse.emf.edit.provider.IItemLabelProvider; 38 import org.eclipse.emf.edit.provider.IItemPropertySource; 39 import org.eclipse.emf.edit.provider.INotifyChangedListener; 40 import org.eclipse.emf.edit.provider.IStructuredItemContentProvider; 41 import org.eclipse.emf.edit.provider.ITableItemLabelProvider; 42 import org.eclipse.emf.edit.provider.ITreeItemContentProvider; 43 44 45 52 public class ResourceItemProviderAdapterFactory 53 extends AdapterFactoryImpl 54 implements ComposeableAdapterFactory, IChangeNotifier, IDisposable 55 { 56 protected static Package resourcePackage = Resource.class.getPackage(); 57 58 62 protected ComposedAdapterFactory parentAdapterFactory; 63 64 68 protected IChangeNotifier changeNotifier = new ChangeNotifier(); 69 70 74 protected Collection supportedTypes = new ArrayList (); 75 76 79 protected Disposable disposable = new Disposable(); 80 81 85 public ResourceItemProviderAdapterFactory() 86 { 87 supportedTypes.add(IStructuredItemContentProvider.class); 88 supportedTypes.add(ITreeItemContentProvider.class); 89 supportedTypes.add(IItemPropertySource.class); 90 supportedTypes.add(IEditingDomainItemProvider.class); 91 supportedTypes.add(IItemLabelProvider.class); 92 supportedTypes.add(ITableItemLabelProvider.class); 93 } 94 95 public Adapter createAdapter(Notifier target) 96 { 97 if (target instanceof Resource) 98 { 99 return createResourceAdapter(); 100 } 101 else 102 { 103 return createResourceSetAdapter(); 104 } 105 } 106 107 111 public Adapter createResourceAdapter() 112 { 113 return new ResourceItemProvider(this); 114 } 115 116 120 public Adapter createResourceSetAdapter() 121 { 122 return new ResourceSetItemProvider(this); 123 } 124 125 129 public ComposeableAdapterFactory getRootAdapterFactory() 130 { 131 return parentAdapterFactory == null ? this : parentAdapterFactory.getRootAdapterFactory(); 132 } 133 134 138 public void setParentAdapterFactory(ComposedAdapterFactory parentAdapterFactory) 139 { 140 this.parentAdapterFactory = parentAdapterFactory; 141 } 142 143 151 public boolean isFactoryForType(Object type) 152 { 153 return type == resourcePackage || type instanceof Resource || type instanceof ResourceSet || supportedTypes.contains(type); 154 } 155 156 160 public Adapter adapt(Notifier notifier, Object type) 161 { 162 return super.adapt(notifier, this); 163 } 164 165 168 public Object adapt(Object object, Object type) 169 { 170 if (object instanceof EObject && ((EObject)object).eClass() == null) 173 { 174 return null; 175 } 176 177 if (isFactoryForType(type)) 178 { 179 Object adapter = super.adapt(object, type); 180 if (!(type instanceof Class ) || (((Class )type).isInstance(adapter))) 181 { 182 return adapter; 183 } 184 } 185 186 return null; 187 } 188 189 193 public void addListener(INotifyChangedListener notifyChangedListener) 194 { 195 changeNotifier.addListener(notifyChangedListener); 196 } 197 198 202 public void removeListener(INotifyChangedListener notifyChangedListener) 203 { 204 changeNotifier.removeListener(notifyChangedListener); 205 } 206 207 211 public void fireNotifyChanged(Notification notification) 212 { 213 changeNotifier.fireNotifyChanged(notification); 214 215 if (parentAdapterFactory != null) 216 { 217 parentAdapterFactory.fireNotifyChanged(notification); 218 } 219 } 220 221 protected void associate(Adapter adapter, Notifier target) 222 { 223 super.associate(adapter, target); 224 if (adapter != null) 225 { 226 disposable.add(adapter); 227 } 228 } 229 230 public void dispose() 231 { 232 disposable.dispose(); 233 } 234 } 235 | Popular Tags |