1 | /* |
2 | * Copyright 2006 SDQ Research Group, University of Karlsruhe (TH) |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.seff.navigator; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.Collection; |
8 | import java.util.HashMap; |
9 | import java.util.Iterator; |
10 | |
11 | import org.eclipse.core.resources.IFile; |
12 | import org.eclipse.emf.common.util.URI; |
13 | import org.eclipse.emf.ecore.EObject; |
14 | import org.eclipse.emf.ecore.resource.Resource; |
15 | import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; |
16 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; |
17 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
18 | import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; |
19 | import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory; |
20 | import org.eclipse.jface.viewers.Viewer; |
21 | import org.eclipse.ui.IMemento; |
22 | import org.eclipse.ui.navigator.ICommonContentExtensionSite; |
23 | import org.eclipse.ui.navigator.ICommonContentProvider; |
24 | |
25 | import de.uka.ipd.sdq.pcm.gmf.seff.part.PalladioComponentModelSeffDiagramEditorPlugin; |
26 | |
27 | /** |
28 | * @generated |
29 | */ |
30 | public class PalladioComponentModelDomainNavigatorContentProvider implements |
31 | ICommonContentProvider { |
32 | |
33 | /** |
34 | * @generated |
35 | */ |
36 | private AdapterFactoryContentProvider myAdapterFctoryContentProvier; |
37 | |
38 | /** |
39 | * @generated |
40 | */ |
41 | private static final Object[] EMPTY_ARRAY = new Object[0]; |
42 | |
43 | /** |
44 | * @generated |
45 | */ |
46 | private Viewer myViewer; |
47 | |
48 | /** |
49 | * @generated |
50 | */ |
51 | private AdapterFactoryEditingDomain myEditingDomain; |
52 | |
53 | /** |
54 | * @generated |
55 | */ |
56 | private WorkspaceSynchronizer myWorkspaceSynchronizer; |
57 | |
58 | /** |
59 | * @generated |
60 | */ |
61 | private Runnable myViewerRefreshRunnable; |
62 | |
63 | /** |
64 | * @generated |
65 | */ |
66 | public PalladioComponentModelDomainNavigatorContentProvider() { |
67 | myAdapterFctoryContentProvier = new AdapterFactoryContentProvider( |
68 | PalladioComponentModelSeffDiagramEditorPlugin.getInstance() |
69 | .getItemProvidersAdapterFactory()); |
70 | TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE |
71 | .createEditingDomain(); |
72 | myEditingDomain = (AdapterFactoryEditingDomain) editingDomain; |
73 | myEditingDomain.setResourceToReadOnlyMap(new HashMap() { |
74 | public Object get(Object key) { |
75 | if (!containsKey(key)) { |
76 | put(key, Boolean.TRUE); |
77 | } |
78 | return super.get(key); |
79 | } |
80 | }); |
81 | myViewerRefreshRunnable = new Runnable() { |
82 | public void run() { |
83 | if (myViewer != null) { |
84 | myViewer.refresh(); |
85 | } |
86 | } |
87 | }; |
88 | myWorkspaceSynchronizer = new WorkspaceSynchronizer(editingDomain, |
89 | new WorkspaceSynchronizer.Delegate() { |
90 | public void dispose() { |
91 | } |
92 | |
93 | public boolean handleResourceChanged(final Resource resource) { |
94 | for (Iterator it = myEditingDomain.getResourceSet() |
95 | .getResources().iterator(); it.hasNext();) { |
96 | Resource nextResource = (Resource) it.next(); |
97 | nextResource.unload(); |
98 | } |
99 | if (myViewer != null) { |
100 | myViewer.getControl().getDisplay().asyncExec( |
101 | myViewerRefreshRunnable); |
102 | } |
103 | return true; |
104 | } |
105 | |
106 | public boolean handleResourceDeleted(Resource resource) { |
107 | for (Iterator it = myEditingDomain.getResourceSet() |
108 | .getResources().iterator(); it.hasNext();) { |
109 | Resource nextResource = (Resource) it.next(); |
110 | nextResource.unload(); |
111 | } |
112 | if (myViewer != null) { |
113 | myViewer.getControl().getDisplay().asyncExec( |
114 | myViewerRefreshRunnable); |
115 | } |
116 | return true; |
117 | } |
118 | |
119 | public boolean handleResourceMoved(Resource resource, |
120 | final URI newURI) { |
121 | for (Iterator it = myEditingDomain.getResourceSet() |
122 | .getResources().iterator(); it.hasNext();) { |
123 | Resource nextResource = (Resource) it.next(); |
124 | nextResource.unload(); |
125 | } |
126 | if (myViewer != null) { |
127 | myViewer.getControl().getDisplay().asyncExec( |
128 | myViewerRefreshRunnable); |
129 | } |
130 | return true; |
131 | } |
132 | }); |
133 | } |
134 | |
135 | /** |
136 | * @generated |
137 | */ |
138 | public void dispose() { |
139 | myWorkspaceSynchronizer.dispose(); |
140 | myWorkspaceSynchronizer = null; |
141 | myViewerRefreshRunnable = null; |
142 | for (Iterator it = myEditingDomain.getResourceSet().getResources() |
143 | .iterator(); it.hasNext();) { |
144 | Resource resource = (Resource) it.next(); |
145 | resource.unload(); |
146 | } |
147 | ((TransactionalEditingDomain) myEditingDomain).dispose(); |
148 | myEditingDomain = null; |
149 | } |
150 | |
151 | /** |
152 | * @generated |
153 | */ |
154 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
155 | myViewer = viewer; |
156 | } |
157 | |
158 | /** |
159 | * @generated |
160 | */ |
161 | public Object[] getElements(Object inputElement) { |
162 | return getChildren(inputElement); |
163 | } |
164 | |
165 | /** |
166 | * @generated |
167 | */ |
168 | public void restoreState(IMemento aMemento) { |
169 | } |
170 | |
171 | /** |
172 | * @generated |
173 | */ |
174 | public void saveState(IMemento aMemento) { |
175 | } |
176 | |
177 | /** |
178 | * @generated |
179 | */ |
180 | public void init(ICommonContentExtensionSite aConfig) { |
181 | } |
182 | |
183 | /** |
184 | * @generated |
185 | */ |
186 | public Object[] getChildren(Object parentElement) { |
187 | if (parentElement instanceof IFile) { |
188 | IFile file = (IFile) parentElement; |
189 | URI fileURI = URI.createPlatformResourceURI(file.getFullPath() |
190 | .toString(), true); |
191 | Resource resource = myEditingDomain.getResourceSet().getResource( |
192 | fileURI, true); |
193 | return wrapEObjects(myAdapterFctoryContentProvier |
194 | .getChildren(resource), parentElement); |
195 | } |
196 | |
197 | if (parentElement instanceof PalladioComponentModelDomainNavigatorItem) { |
198 | return wrapEObjects( |
199 | myAdapterFctoryContentProvier |
200 | .getChildren(((PalladioComponentModelDomainNavigatorItem) parentElement) |
201 | .getEObject()), parentElement); |
202 | } |
203 | return EMPTY_ARRAY; |
204 | } |
205 | |
206 | /** |
207 | * @generated |
208 | */ |
209 | public Object[] wrapEObjects(Object[] objects, Object parentElement) { |
210 | Collection result = new ArrayList(); |
211 | for (int i = 0; i < objects.length; i++) { |
212 | if (objects[i] instanceof EObject) { |
213 | result.add(new PalladioComponentModelDomainNavigatorItem( |
214 | (EObject) objects[i], parentElement, |
215 | myAdapterFctoryContentProvier)); |
216 | } |
217 | } |
218 | return result.toArray(); |
219 | } |
220 | |
221 | /** |
222 | * @generated |
223 | */ |
224 | public Object getParent(Object element) { |
225 | if (element instanceof PalladioComponentModelAbstractNavigatorItem) { |
226 | PalladioComponentModelAbstractNavigatorItem abstractNavigatorItem = (PalladioComponentModelAbstractNavigatorItem) element; |
227 | return abstractNavigatorItem.getParent(); |
228 | } |
229 | return null; |
230 | } |
231 | |
232 | /** |
233 | * @generated |
234 | */ |
235 | public boolean hasChildren(Object element) { |
236 | return element instanceof IFile || getChildren(element).length > 0; |
237 | } |
238 | |
239 | } |