1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.pcmbench.navigator; |
5 | |
6 | import java.io.IOException; |
7 | import java.util.ArrayList; |
8 | import java.util.Collection; |
9 | import java.util.HashMap; |
10 | import java.util.Hashtable; |
11 | import java.util.Iterator; |
12 | import java.util.List; |
13 | |
14 | import org.eclipse.core.resources.IProject; |
15 | import org.eclipse.core.resources.ResourcesPlugin; |
16 | import org.eclipse.core.runtime.CoreException; |
17 | import org.eclipse.core.runtime.IAdaptable; |
18 | import org.eclipse.core.runtime.IProgressMonitor; |
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.ecore.EObject; |
23 | import org.eclipse.emf.ecore.resource.Resource; |
24 | import org.eclipse.emf.ecore.resource.ResourceSet; |
25 | import org.eclipse.emf.ecore.util.EContentAdapter; |
26 | import org.eclipse.emf.ecore.util.EcoreUtil; |
27 | import org.eclipse.emf.edit.provider.ComposedAdapterFactory; |
28 | import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory; |
29 | import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory; |
30 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; |
31 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
32 | import org.eclipse.jface.resource.ImageDescriptor; |
33 | import org.eclipse.jface.viewers.ITreeContentProvider; |
34 | import org.eclipse.jface.viewers.StructuredViewer; |
35 | import org.eclipse.jface.viewers.Viewer; |
36 | import org.eclipse.ui.Saveable; |
37 | import org.eclipse.ui.navigator.SaveablesProvider; |
38 | |
39 | import de.uka.ipd.sdq.pcm.repository.RepositoryPackage; |
40 | import de.uka.ipd.sdq.pcm.repository.provider.RepositoryItemProviderAdapterFactory; |
41 | import de.uka.ipd.sdq.pcm.seff.provider.SeffItemProviderAdapterFactory; |
42 | import de.uka.ipd.sdq.pcmbench.ui.provider.categoryaware.CategoryAwareItemProviderAdapterFactory; |
43 | import de.uka.ipd.sdq.pcmbench.ui.provider.categoryaware.GenericCategoryItemProvider; |
44 | import de.uka.ipd.sdq.pcmbench.ui.provider.categoryaware.PalladioCategoryDescriptions; |
45 | |
46 | |
47 | class MySaveable extends Saveable |
48 | { |
49 | |
50 | private Resource myResource; |
51 | |
52 | public MySaveable(Resource r) { |
53 | myResource = r; |
54 | r.setTrackingModification(true); |
55 | } |
56 | |
57 | @Override |
58 | public void doSave(IProgressMonitor monitor) throws CoreException { |
59 | try { |
60 | myResource.save(null); |
61 | } catch (IOException e) { |
62 | throw new CoreException(null); |
63 | } |
64 | } |
65 | |
66 | public Resource getResource() |
67 | { |
68 | return myResource; |
69 | } |
70 | |
71 | @Override |
72 | public boolean equals(Object object) { |
73 | if (object instanceof MySaveable) |
74 | { |
75 | MySaveable other = (MySaveable) object; |
76 | if (other.getResource() == myResource) |
77 | return true; |
78 | } |
79 | return false; |
80 | } |
81 | |
82 | @Override |
83 | public ImageDescriptor getImageDescriptor() { |
84 | return null; |
85 | } |
86 | |
87 | @Override |
88 | public String getName() { |
89 | return myResource.toString(); |
90 | } |
91 | |
92 | @Override |
93 | public String getToolTipText() { |
94 | return myResource.toString(); |
95 | } |
96 | |
97 | @Override |
98 | public int hashCode() { |
99 | return myResource.hashCode(); |
100 | } |
101 | |
102 | @Override |
103 | public boolean isDirty() { |
104 | return myResource.isModified(); |
105 | } |
106 | } |
107 | |
108 | class MySaveablesProvider extends SaveablesProvider { |
109 | |
110 | private ResourceSet rs = null; |
111 | |
112 | public MySaveablesProvider(ResourceSet resourceSet) { |
113 | rs = resourceSet; |
114 | EContentAdapter dirtyListeners = new EContentAdapter() { |
115 | public void notifyChanged(Notification notification) { |
116 | if (notification.getNotifier() instanceof EObject) |
117 | { |
118 | EObject eObject = (EObject)notification.getNotifier(); |
119 | Resource r = eObject.eResource(); |
120 | MySaveable s = new MySaveable(r); |
121 | MySaveablesProvider.this.fireSaveablesDirtyChanged(new Saveable[]{s}); |
122 | } |
123 | } |
124 | }; |
125 | resourceSet.eAdapters().add(dirtyListeners); |
126 | |
127 | EContentAdapter repositoryResourceListeners = new EContentAdapter() { |
128 | public void notifyChanged(Notification notification) { |
129 | if (notification.getNotifier() instanceof ResourceSet) { |
130 | switch (notification.getFeatureID(ResourceSet.class)) { |
131 | case ResourceSet.RESOURCE_SET__RESOURCES: |
132 | if (notification.getEventType() == Notification.ADD) { |
133 | Resource r = (Resource)notification.getNewValue(); |
134 | MySaveable s = new MySaveable(r); |
135 | MySaveablesProvider.this.fireSaveablesOpened(new Saveable[]{s}); |
136 | } |
137 | break; |
138 | } |
139 | } else { |
140 | super.notifyChanged(notification); |
141 | } |
142 | } |
143 | |
144 | protected void setTarget(ResourceSet target) { |
145 | basicSetTarget(target); |
146 | } |
147 | |
148 | protected void unsetTarget(ResourceSet target) { |
149 | basicUnsetTarget(target); |
150 | } |
151 | }; |
152 | repositoryResourceListeners.setTarget(resourceSet); |
153 | resourceSet.eAdapters().add( |
154 | repositoryResourceListeners); |
155 | } |
156 | |
157 | @Override |
158 | public Object[] getElements(Saveable saveable) { |
159 | MySaveable s = (MySaveable)saveable; |
160 | if (!s.getResource().isLoaded()) { |
161 | try { |
162 | s.getResource().load(null); |
163 | } catch (IOException e) { |
164 | // TODO Auto-generated catch block |
165 | e.printStackTrace(); |
166 | } |
167 | } |
168 | EList objects = s.getResource().getContents(); |
169 | ArrayList result = new ArrayList(); |
170 | for (EObject o : (List<EObject>)objects) |
171 | { |
172 | result.add(o); |
173 | } |
174 | return result.toArray(); |
175 | } |
176 | |
177 | @Override |
178 | public Saveable getSaveable(Object element) { |
179 | if (element instanceof EObject) |
180 | { |
181 | EObject eElement = (EObject)element; |
182 | Resource r = eElement.eResource(); |
183 | Saveable saveable = new MySaveable(r); |
184 | return saveable; |
185 | } |
186 | return null; |
187 | } |
188 | |
189 | @Override |
190 | public Saveable[] getSaveables() { |
191 | ArrayList<MySaveable> result = new ArrayList<MySaveable>(); |
192 | for (Resource r : (List<Resource>) rs.getResources()) |
193 | { |
194 | result.add(new MySaveable(r)); |
195 | } |
196 | return result.toArray(new Saveable[]{}); |
197 | } |
198 | }; |
199 | |
200 | |
201 | /** |
202 | * @author Snowball |
203 | * This class is OBSOLETE and only left for demonstration purposes. |
204 | */ |
205 | public class PCMNavigatorContentProvider implements ITreeContentProvider, IAdaptable { |
206 | |
207 | private Object input; |
208 | final protected TransactionalEditingDomain editingDomain = null; |
209 | // TransactionalEditingDomain.Registry.INSTANCE |
210 | // .getEditingDomain(EditingDomainFactory.EDITING_DOMAIN_ID); |
211 | private StructuredViewer viewer; |
212 | private ComposedAdapterFactory adapterFactory; |
213 | private AdapterFactoryContentProvider contentProvider; |
214 | private MySaveablesProvider saveProvider = null; |
215 | private Hashtable<IProject,LinkedRepositoriesNode> myLinkedRepositoriesNodes = new Hashtable<IProject,LinkedRepositoriesNode>(); |
216 | |
217 | public PCMNavigatorContentProvider() |
218 | { |
219 | //saveProvider = new MySaveablesProvider(editingDomain.getResourceSet()); |
220 | |
221 | EContentAdapter repositoryResourceListeners = new EContentAdapter() { |
222 | public void notifyChanged(Notification notification) { |
223 | if (notification.getNotifier() instanceof ResourceSet) { |
224 | switch (notification.getFeatureID(ResourceSet.class)) { |
225 | case ResourceSet.RESOURCE_SET__RESOURCES: |
226 | if (notification.getEventType() == Notification.ADD) { |
227 | System.out.println("New Resource loaded..."); |
228 | viewer.refresh(); |
229 | } |
230 | break; |
231 | } |
232 | } else if (notification.getNotifier() instanceof Resource) { |
233 | switch (notification.getFeatureID(Resource.class)) { |
234 | case Resource.RESOURCE__IS_LOADED: |
235 | if (notification.getEventType() == Notification.SET) { |
236 | System.out.println("New Resource loaded..."); |
237 | viewer.refresh(); |
238 | } |
239 | break; |
240 | } |
241 | } |
242 | else { |
243 | super.notifyChanged(notification); |
244 | } |
245 | } |
246 | }; |
247 | repositoryResourceListeners.setTarget(editingDomain.getResourceSet()); |
248 | editingDomain.getResourceSet().eAdapters().add( |
249 | repositoryResourceListeners); |
250 | |
251 | adapterFactory = new ComposedAdapterFactory(); |
252 | adapterFactory |
253 | .addAdapterFactory(new ResourceItemProviderAdapterFactory()); |
254 | adapterFactory |
255 | .addAdapterFactory(new RepositoryItemProviderAdapterFactory()); |
256 | adapterFactory.addAdapterFactory(new SeffItemProviderAdapterFactory()); |
257 | adapterFactory |
258 | .addAdapterFactory(new ReflectiveItemProviderAdapterFactory()); |
259 | |
260 | AdapterFactory decoratorFactory = new CategoryAwareItemProviderAdapterFactory(adapterFactory, new PalladioCategoryDescriptions()); |
261 | |
262 | contentProvider = new AdapterFactoryContentProvider(decoratorFactory); |
263 | } |
264 | |
265 | /* (non-Javadoc) |
266 | * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) |
267 | */ |
268 | public Object[] getChildren(Object parentElement) { |
269 | if (parentElement instanceof IProject) |
270 | { |
271 | ArrayList children = new ArrayList(); |
272 | children.add(myLinkedRepositoriesNodes.get(parentElement)); |
273 | return children.toArray(); |
274 | } |
275 | if (parentElement instanceof LinkedRepositoriesNode) |
276 | { |
277 | ArrayList resourceList = new ArrayList(); |
278 | for (Iterator it=editingDomain.getResourceSet().getResources().iterator(); |
279 | it.hasNext();) |
280 | { |
281 | Resource r = (Resource)it.next(); |
282 | if (!r.isLoaded()) |
283 | try { |
284 | r.load(new HashMap()); |
285 | } catch (IOException e) { |
286 | // TODO Auto-generated catch block |
287 | e.printStackTrace(); |
288 | } |
289 | if (r.isLoaded()) |
290 | { |
291 | Collection repositories = EcoreUtil.getObjectsByType(r.getContents(), RepositoryPackage.eINSTANCE.getRepository()); |
292 | resourceList.addAll(repositories); |
293 | } |
294 | } |
295 | return resourceList.toArray(); |
296 | } |
297 | if (parentElement instanceof EObject || parentElement instanceof GenericCategoryItemProvider) |
298 | { |
299 | return contentProvider.getChildren(parentElement); |
300 | } |
301 | return null; |
302 | } |
303 | |
304 | /* (non-Javadoc) |
305 | * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) |
306 | */ |
307 | public Object getParent(Object element) { |
308 | if (element instanceof LinkedRepositoriesNode) |
309 | return ((LinkedRepositoriesNode)element).getParent(); |
310 | if (element instanceof EObject) { |
311 | Object originalParent = contentProvider.getParent(element); |
312 | if (originalParent instanceof Resource) |
313 | return myLinkedRepositoriesNodes.get(ResourcesPlugin.getWorkspace().getRoot().getProjects()[0]); //TODO: Works only with one project in the workspace |
314 | return originalParent; |
315 | } |
316 | return null; |
317 | } |
318 | |
319 | /* (non-Javadoc) |
320 | * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) |
321 | */ |
322 | public boolean hasChildren(Object element) { |
323 | if (getChildren(element) == null) |
324 | return false; |
325 | return getChildren(element).length > 0; |
326 | } |
327 | |
328 | /* (non-Javadoc) |
329 | * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) |
330 | */ |
331 | public Object[] getElements(Object inputElement) { |
332 | return getChildren(inputElement); |
333 | } |
334 | |
335 | /* (non-Javadoc) |
336 | * @see org.eclipse.jface.viewers.IContentProvider#dispose() |
337 | */ |
338 | public void dispose() { |
339 | } |
340 | |
341 | /* (non-Javadoc) |
342 | * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) |
343 | */ |
344 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
345 | input = newInput; |
346 | this.viewer = (StructuredViewer)viewer; |
347 | contentProvider.inputChanged(viewer, oldInput, newInput); |
348 | populateHashtable(); |
349 | } |
350 | |
351 | private void |
352 | populateHashtable() { |
353 | myLinkedRepositoriesNodes.clear(); |
354 | for (IProject p : ResourcesPlugin.getWorkspace().getRoot().getProjects()) |
355 | { |
356 | myLinkedRepositoriesNodes.put(p,new LinkedRepositoriesNode(p)); |
357 | } |
358 | } |
359 | |
360 | public Object getAdapter(Class adapter) { |
361 | //if (adapter == SaveablesProvider.class) |
362 | // return saveProvider; |
363 | return null; |
364 | } |
365 | |
366 | } |