Debian Codegraph @

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

raw
1 /**
2 * Copyright (c) 2002-2012 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.command.AbstractCommand;
20 import org.eclipse.emf.common.command.Command;
21 import org.eclipse.emf.common.notify.AdapterFactory;
22 import org.eclipse.emf.common.notify.Notification;
23 import org.eclipse.emf.common.util.ResourceLocator;
24 import org.eclipse.emf.common.util.URI;
25 import org.eclipse.emf.ecore.resource.Resource;
26 import org.eclipse.emf.ecore.resource.ResourceSet;
27 import org.eclipse.emf.edit.EMFEditPlugin;
28 import org.eclipse.emf.edit.command.AbstractOverrideableCommand;
29 import org.eclipse.emf.edit.domain.EditingDomain;
30 import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
31 import org.eclipse.emf.edit.provider.IItemLabelProvider;
32 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
33 import org.eclipse.emf.edit.provider.IItemPropertySource;
34 import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
35 import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
36 import org.eclipse.emf.edit.provider.ItemProviderAdapter;
37 import org.eclipse.emf.edit.provider.ViewerNotification;
38
39
40 /**
41 * This is the item provider adapter for a {@link org.eclipse.emf.ecore.resource.ResourceSet} object.
42 */
43 public class ResourceSetItemProvider
44 extends ItemProviderAdapter
45 implements
46 IEditingDomainItemProvider,
47 IStructuredItemContentProvider,
48 ITreeItemContentProvider,
49 IItemLabelProvider,
50 IItemPropertySource
51 {
52 /**
53 * This constructs an instance from a factory and a notifier.
54 */
55 public ResourceSetItemProvider(AdapterFactory adapterFactory)
56 {
57 super(adapterFactory);
58 }
59
60 /**
61 * This returns the property descriptors for the adapted class.
62 */
63 @Override
64 public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object)
65 {
66 if (itemPropertyDescriptors == null)
67 {
68 super.getPropertyDescriptors(object);
69 }
70 return itemPropertyDescriptors;
71 }
72
73 @Override
74 public Collection<?> getChildren(Object object)
75 {
76 ResourceSet resourceSet = (ResourceSet)object;
77 synchronized (resourceSet)
78 {
79 return new ArrayList<Resource>(resourceSet.getResources());
80 }
81 }
82
83 /**
84 * This returns the parent of the ResourceSet.
85 */
86 @Override
87 public Object getParent(Object object)
88 {
89 return null;
90 }
91
92 /**
93 * This returns ResourceSet.gif.
94 */
95 @Override
96 public Object getImage(Object object)
97 {
98 return getResourceLocator().getImage("full/obj16/ResourceSet");
99 }
100
101 /**
102 * This returns the label text for the adapted class.
103 */
104 @Override
105 public String getText(Object object)
106 {
107 return EMFEditPlugin.INSTANCE.getString("_UI_ResourceSet_label");
108 }
109
110 /**
111 * This handles notification by calling {@link #fireNotifyChanged(Notification) fireNotifyChanged}.
112 */
113 @Override
114 public void notifyChanged(Notification notification)
115 {
116 switch (notification.getFeatureID(ResourceSet.class))
117 {
118 case ResourceSet.RESOURCE_SET__RESOURCES:
119 {
120 fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
121 return;
122 }
123 }
124 super.notifyChanged(notification);
125 }
126
127 @Override
128 public Collection<?> getNewChildDescriptors(Object object, EditingDomain editingDomain, Object sibling)
129 {
130 return Collections.emptyList();
131 }
132
133 /**
134 * This adds {@link org.eclipse.emf.edit.command.CommandParameter}s describing the children
135 * that can be created under this object.
136 */
137 @Override
138 protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object)
139 {
140 super.collectNewChildDescriptors(newChildDescriptors, object);
141 }
142
143 @Override
144 protected Command createDragAndDropCommand(EditingDomain domain, Object owner, float location, int operations, int operation, final Collection<?> collection)
145 {
146 final ResourceSet resourceSet = (ResourceSet)owner;
147
148 class LoadResourceCommand extends AbstractOverrideableCommand implements AbstractCommand.NonDirtying
149 {
150 protected LoadResourceCommand(EditingDomain domain)
151 {
152 super(domain);
153 }
154
155 protected List<Resource> resources;
156
157 @Override
158 protected boolean prepare()
159 {
160 for (Object object : collection)
161 {
162 if (!(object instanceof URI))
163 {
164 return false;
165 }
166 }
167 return true;
168 }
169
170 @Override
171 public void doExecute()
172 {
173 resources = new ArrayList<Resource>();
174 for (Object object : collection)
175 {
176 URI uri = (URI)object;
177 Resource resource = resourceSet.getResource(uri, false);
178 if (resource == null)
179 {
180 try
181 {
182 resource = resourceSet.getResource(uri, true);
183 }
184 catch (RuntimeException exception)
185 {
186 resource = resourceSet.getResource(uri, false);
187 EMFEditPlugin.INSTANCE.log(exception);
188 }
189 }
190 if (resource != null)
191 {
192 resources.add(resource);
193 }
194 }
195 }
196
197 @Override
198 public void doUndo()
199 {
200 resourceSet.getResources().removeAll(resources);
201 resources = null;
202 }
203
204 @Override
205 public void doRedo()
206 {
207 doExecute();
208 }
209
210 @Override
211 public Collection<?> doGetAffectedObjects()
212 {
213 return resources == null ? Collections.singleton(resourceSet) : resources;
214 }
215
216 @Override
217 public String doGetDescription()
218 {
219 return EMFEditPlugin.INSTANCE.getString("_UI_LoadResources_description");
220 }
221
222 @Override
223 public String doGetLabel()
224 {
225 return EMFEditPlugin.INSTANCE.getString("_UI_LoadResources_label");
226 }
227 }
228
229 return new LoadResourceCommand(domain);
230 }
231
232 /**
233 * Return the resource locator for this item provider's resources.
234 */
235 @Override
236 public ResourceLocator getResourceLocator()
237 {
238 return EMFEditPlugin.INSTANCE;
239 }
240 }