Debian Codegraph @

plugins/org.eclipse.emf.edit/src/org/eclipse/emf/edit/provider/resource/ResourceItemProvider.java

raw
1 /**
2 * Copyright (c) 2002-2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.edit.provider.resource;
12
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18
19 import org.eclipse.emf.common.notify.AdapterFactory;
20 import org.eclipse.emf.common.notify.Notification;
21 import org.eclipse.emf.common.util.EList;
22 import org.eclipse.emf.common.util.ResourceLocator;
23 import org.eclipse.emf.common.util.URI;
24 import org.eclipse.emf.ecore.EObject;
25 import org.eclipse.emf.ecore.EStructuralFeature;
26 import org.eclipse.emf.ecore.resource.Resource;
27 import org.eclipse.emf.ecore.resource.ResourceSet;
28 import org.eclipse.emf.edit.EMFEditPlugin;
29 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
30 import org.eclipse.emf.edit.domain.EditingDomain;
31 import org.eclipse.emf.edit.provider.ComposedImage;
32 import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
33 import org.eclipse.emf.edit.provider.IItemLabelProvider;
34 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
35 import org.eclipse.emf.edit.provider.IItemPropertySource;
36 import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
37 import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
38 import org.eclipse.emf.edit.provider.ItemProviderAdapter;
39 import org.eclipse.emf.edit.provider.ViewerNotification;
40
41
42 /**
43 * This is the item provider adapter for a {@link org.eclipse.emf.ecore.resource.Resource} object.
44 */
45 public class ResourceItemProvider
46 extends ItemProviderAdapter
47 implements
48 IEditingDomainItemProvider,
49 IStructuredItemContentProvider,
50 ITreeItemContentProvider,
51 IItemLabelProvider,
52 IItemPropertySource
53 {
54 /**
55 * This constructs an instance from a factory and a notifier.
56 */
57 public ResourceItemProvider(AdapterFactory adapterFactory)
58 {
59 super(adapterFactory);
60 }
61
62 /**
63 * This returns the property descriptors for the adapted class.
64 */
65 @Override
66 public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object)
67 {
68 return super.getPropertyDescriptors(object);
69 }
70
71 @Override
72 public Collection<?> getChildren(Object object)
73 {
74 // Don't include controlled children here, they'll show up under their container.
75 //
76 Resource resource = (Resource)object;
77 Object[] contents = getContents(resource);
78
79 Collection<Object> result = new ArrayList<Object>(contents.length);
80 for (Object o : contents)
81 {
82 if (!AdapterFactoryEditingDomain.isControlled(o))
83 {
84 result.add(o);
85 }
86 }
87 return result;
88 }
89
90 /**
91 * Copies the contents of the resource to an array while holding a lock on the resource and subsequently its resource set, if the resource is contained by a resource set.
92 * @since 2.9
93 */
94 protected EObject[] getContents(Resource resource)
95 {
96 synchronized (resource)
97 {
98 ResourceSet resourceSet = resource.getResourceSet();
99 if (resourceSet != null)
100 {
101 synchronized (resourceSet)
102 {
103 EList<EObject> contents = resource.getContents();
104 return contents.toArray(new EObject[contents.size()]);
105 }
106 }
107 else
108 {
109 EList<EObject> contents = resource.getContents();
110 return contents.toArray(new EObject[contents.size()]);
111 }
112 }
113 }
114
115 /**
116 * This specifies how to implement {@link #getChildren} and is used to deduce an appropriate feature for an
117 * {@link org.eclipse.emf.edit.command.AddCommand}, {@link org.eclipse.emf.edit.command.RemoveCommand} or
118 * {@link org.eclipse.emf.edit.command.MoveCommand} in {@link #createCommand(Object, EditingDomain, Class, org.eclipse.emf.edit.command.CommandParameter) createCommand}.
119 */
120 @Override
121 public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object)
122 {
123 if (childrenFeatures == null)
124 {
125 super.getChildrenFeatures(object);
126 // Resource resource = (Resource)object;
127 // childrenFeatures.add(ResourcePackage.eINSTANCE.getResource_Contents());
128 }
129 return childrenFeatures;
130 }
131
132 /**
133 * This returns the parent of the Resource.
134 */
135 @Override
136 public Object getParent(Object object)
137 {
138 return ((Resource)object).getResourceSet();
139 }
140
141 /**
142 * This returns Resource.gif.
143 */
144 @Override
145 public Object getImage(Object object)
146 {
147 Resource resource = (Resource)object;
148 Object image = URI.createURI(getResourceLocator().getImage("full/obj16/Resource").toString() + "#" + resource.getURI().fileExtension());
149
150 // Overlay if the resource is the target for any controlled objects.
151 //
152 for (Object o : getContents(resource))
153 {
154 if (AdapterFactoryEditingDomain.isControlled(o))
155 {
156 List<Object> images = new ArrayList<Object>(2);
157 images.add(image);
158 images.add(getImage("full/ovr16/ControlledObjectTarget"));
159 image = new ComposedImage(images);
160 break;
161 }
162 }
163 return image;
164 }
165
166 /**
167 * This returns the label text for the adapted class.
168 */
169 @Override
170 public String getText(Object object)
171 {
172 Resource resource = (Resource)object;
173 return resource.getURI() == null ? "" : resource.getURI().toString();
174 }
175
176 /**
177 * This handles notification by calling {@link #fireNotifyChanged(Notification) fireNotifyChanged}.
178 */
179 @Override
180 public void notifyChanged(Notification notification)
181 {
182 switch (notification.getFeatureID(Resource.class))
183 {
184 case Resource.RESOURCE__URI:
185 // case Resource.RESOURCE__IS_MODIFIED:
186 // case Resource.RESOURCE__IS_LOADED:
187 // case Resource.RESOURCE__IS_TRACKING_MODIFICATION:
188 // case Resource.RESOURCE__RESOURCE_SET:
189 {
190 fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
191 return;
192 }
193 // When an object is controlled, the only change will be in the new resource's contents, so it must
194 // refresh the whole viewer to hit the object's label.
195 //
196 case Resource.RESOURCE__CONTENTS:
197 {
198 fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
199 return;
200 }
201 }
202 super.notifyChanged(notification);
203 }
204
205 @Override
206 public Collection<?> getNewChildDescriptors(Object object, EditingDomain editingDomain, Object sibling)
207 {
208 return Collections.emptyList();
209 }
210
211 /**
212 * This adds {@link org.eclipse.emf.edit.command.CommandParameter}s describing the children
213 * that can be created under this object.
214 */
215 @Override
216 protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object)
217 {
218 super.collectNewChildDescriptors(newChildDescriptors, object);
219 }
220
221 /**
222 * Return the resource locator for this item provider's resources.
223 */
224 @Override
225 public ResourceLocator getResourceLocator()
226 {
227 return EMFEditPlugin.INSTANCE;
228 }
229 }