1 | /* |
2 | * Copyright 2007, IPD, SDQ, University of Karlsruhe |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.repository.part; |
5 | |
6 | import java.util.Collections; |
7 | import java.util.Iterator; |
8 | |
9 | import org.eclipse.core.resources.IContainer; |
10 | import org.eclipse.core.resources.IFile; |
11 | import org.eclipse.core.resources.ResourcesPlugin; |
12 | import org.eclipse.core.runtime.IPath; |
13 | import org.eclipse.core.runtime.Path; |
14 | import org.eclipse.emf.common.util.URI; |
15 | import org.eclipse.emf.common.util.WrappedException; |
16 | import org.eclipse.emf.ecore.EObject; |
17 | import org.eclipse.emf.ecore.resource.Resource; |
18 | import org.eclipse.emf.ecore.resource.ResourceSet; |
19 | import org.eclipse.emf.ecore.util.EcoreUtil; |
20 | import org.eclipse.emf.ecore.util.FeatureMap; |
21 | import org.eclipse.emf.edit.provider.IWrapperItemProvider; |
22 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; |
23 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; |
24 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
25 | import org.eclipse.gmf.runtime.diagram.core.services.ViewService; |
26 | import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil; |
27 | import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory; |
28 | import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
29 | import org.eclipse.gmf.runtime.notation.Node; |
30 | import org.eclipse.gmf.runtime.notation.View; |
31 | import org.eclipse.jface.dialogs.Dialog; |
32 | import org.eclipse.jface.dialogs.IDialogConstants; |
33 | import org.eclipse.jface.viewers.ILabelProvider; |
34 | import org.eclipse.jface.viewers.ILabelProviderListener; |
35 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
36 | import org.eclipse.jface.viewers.IStructuredSelection; |
37 | import org.eclipse.jface.viewers.ITreeContentProvider; |
38 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
39 | import org.eclipse.jface.viewers.TreeViewer; |
40 | import org.eclipse.jface.viewers.Viewer; |
41 | import org.eclipse.jface.viewers.ViewerFilter; |
42 | import org.eclipse.swt.SWT; |
43 | import org.eclipse.swt.graphics.Image; |
44 | import org.eclipse.swt.layout.GridData; |
45 | import org.eclipse.swt.widgets.Composite; |
46 | import org.eclipse.swt.widgets.Control; |
47 | import org.eclipse.swt.widgets.Shell; |
48 | import org.eclipse.ui.model.WorkbenchContentProvider; |
49 | import org.eclipse.ui.model.WorkbenchLabelProvider; |
50 | |
51 | /** |
52 | * @generated |
53 | */ |
54 | public class PalladioComponentModelElementChooserDialog extends Dialog { |
55 | |
56 | /** |
57 | * @generated |
58 | */ |
59 | private TreeViewer myTreeViewer; |
60 | |
61 | /** |
62 | * @generated |
63 | */ |
64 | private URI mySelectedModelElementURI; |
65 | |
66 | /** |
67 | * @generated |
68 | */ |
69 | private View myView; |
70 | |
71 | /** |
72 | * @generated |
73 | */ |
74 | private TransactionalEditingDomain myEditingDomain = GMFEditingDomainFactory.INSTANCE |
75 | .createEditingDomain(); |
76 | |
77 | /** |
78 | * @generated |
79 | */ |
80 | public PalladioComponentModelElementChooserDialog(Shell parentShell, |
81 | View view) { |
82 | super(parentShell); |
83 | setShellStyle(getShellStyle() | SWT.RESIZE); |
84 | myView = view; |
85 | } |
86 | |
87 | /** |
88 | * @generated |
89 | */ |
90 | protected Control createDialogArea(Composite parent) { |
91 | Composite composite = (Composite) super.createDialogArea(parent); |
92 | getShell() |
93 | .setText( |
94 | Messages.PalladioComponentModelElementChooserDialog_SelectModelElementTitle); |
95 | createModelBrowser(composite); |
96 | return composite; |
97 | } |
98 | |
99 | /** |
100 | * @generated |
101 | */ |
102 | protected Control createButtonBar(Composite parent) { |
103 | Control buttonBar = super.createButtonBar(parent); |
104 | setOkButtonEnabled(false); |
105 | return buttonBar; |
106 | } |
107 | |
108 | /** |
109 | * @generated |
110 | */ |
111 | private void createModelBrowser(Composite composite) { |
112 | myTreeViewer = new TreeViewer(composite, SWT.SINGLE | SWT.H_SCROLL |
113 | | SWT.V_SCROLL | SWT.BORDER); |
114 | GridData layoutData = new GridData(GridData.FILL_BOTH); |
115 | layoutData.heightHint = 300; |
116 | layoutData.widthHint = 300; |
117 | myTreeViewer.getTree().setLayoutData(layoutData); |
118 | myTreeViewer.setContentProvider(new ModelElementsTreeContentProvider()); |
119 | myTreeViewer.setLabelProvider(new ModelElementsTreeLabelProvider()); |
120 | myTreeViewer.setInput(ResourcesPlugin.getWorkspace().getRoot()); |
121 | myTreeViewer.addFilter(new ModelFilesFilter()); |
122 | myTreeViewer.addSelectionChangedListener(new OkButtonEnabler()); |
123 | } |
124 | |
125 | /** |
126 | * @generated |
127 | */ |
128 | private void setOkButtonEnabled(boolean enabled) { |
129 | getButton(IDialogConstants.OK_ID).setEnabled(enabled); |
130 | } |
131 | |
132 | /** |
133 | * @generated |
134 | */ |
135 | private boolean isValidModelFile(IFile file) { |
136 | String fileExtension = file.getFullPath().getFileExtension(); |
137 | return "repository".equals(fileExtension); //$NON-NLS-1$ |
138 | } |
139 | |
140 | /** |
141 | * @generated |
142 | */ |
143 | public URI getSelectedModelElementURI() { |
144 | return mySelectedModelElementURI; |
145 | } |
146 | |
147 | /** |
148 | * @generated |
149 | */ |
150 | public int open() { |
151 | int result = super.open(); |
152 | for (Iterator it = myEditingDomain.getResourceSet().getResources() |
153 | .iterator(); it.hasNext();) { |
154 | Resource resource = (Resource) it.next(); |
155 | resource.unload(); |
156 | } |
157 | myEditingDomain.dispose(); |
158 | return result; |
159 | } |
160 | |
161 | /** |
162 | * @generated |
163 | */ |
164 | private class ModelElementsTreeContentProvider implements |
165 | ITreeContentProvider { |
166 | |
167 | /** |
168 | * @generated |
169 | */ |
170 | private ITreeContentProvider myWorkbenchContentProvider = new WorkbenchContentProvider(); |
171 | |
172 | /** |
173 | * @generated |
174 | */ |
175 | private AdapterFactoryContentProvider myAdapterFctoryContentProvier = new AdapterFactoryContentProvider( |
176 | PalladioComponentModelRepositoryDiagramEditorPlugin |
177 | .getInstance().getItemProvidersAdapterFactory()); |
178 | |
179 | /** |
180 | * @generated |
181 | */ |
182 | public Object[] getChildren(Object parentElement) { |
183 | Object[] result = myWorkbenchContentProvider |
184 | .getChildren(parentElement); |
185 | if (result != null && result.length > 0) { |
186 | return result; |
187 | } |
188 | if (parentElement instanceof IFile) { |
189 | IFile modelFile = (IFile) parentElement; |
190 | IPath resourcePath = modelFile.getFullPath(); |
191 | ResourceSet resourceSet = myEditingDomain.getResourceSet(); |
192 | try { |
193 | Resource modelResource = resourceSet.getResource(URI |
194 | .createPlatformResourceURI(resourcePath.toString(), |
195 | true), true); |
196 | return myAdapterFctoryContentProvier |
197 | .getChildren(modelResource); |
198 | } catch (WrappedException e) { |
199 | PalladioComponentModelRepositoryDiagramEditorPlugin |
200 | .getInstance() |
201 | .logError( |
202 | "Unable to load resource: " + resourcePath.toString(), e); //$NON-NLS-1$ |
203 | } |
204 | return Collections.EMPTY_LIST.toArray(); |
205 | } |
206 | return myAdapterFctoryContentProvier.getChildren(parentElement); |
207 | } |
208 | |
209 | /** |
210 | * @generated |
211 | */ |
212 | public Object getParent(Object element) { |
213 | Object parent = myWorkbenchContentProvider.getParent(element); |
214 | if (parent != null) { |
215 | return parent; |
216 | } |
217 | if (element instanceof EObject) { |
218 | EObject eObject = (EObject) element; |
219 | if (eObject.eContainer() == null |
220 | && eObject.eResource().getURI().isFile()) { |
221 | String path = eObject.eResource().getURI().path(); |
222 | return ResourcesPlugin.getWorkspace().getRoot() |
223 | .getFileForLocation(new Path(path)); |
224 | } |
225 | return myAdapterFctoryContentProvier.getParent(eObject); |
226 | } |
227 | return null; |
228 | } |
229 | |
230 | /** |
231 | * @generated |
232 | */ |
233 | public boolean hasChildren(Object element) { |
234 | if (element instanceof IFile) { |
235 | return isValidModelFile((IFile) element); |
236 | } |
237 | return myWorkbenchContentProvider.hasChildren(element) |
238 | || myAdapterFctoryContentProvier.hasChildren(element); |
239 | } |
240 | |
241 | /** |
242 | * @generated |
243 | */ |
244 | public Object[] getElements(Object inputElement) { |
245 | Object[] elements = myWorkbenchContentProvider |
246 | .getElements(inputElement); |
247 | return elements; |
248 | } |
249 | |
250 | /** |
251 | * @generated |
252 | */ |
253 | public void dispose() { |
254 | myWorkbenchContentProvider.dispose(); |
255 | myAdapterFctoryContentProvier.dispose(); |
256 | } |
257 | |
258 | /** |
259 | * @generated |
260 | */ |
261 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
262 | myWorkbenchContentProvider.inputChanged(viewer, oldInput, newInput); |
263 | myAdapterFctoryContentProvier.inputChanged(viewer, oldInput, |
264 | newInput); |
265 | } |
266 | |
267 | } |
268 | |
269 | /** |
270 | * @generated |
271 | */ |
272 | private class ModelElementsTreeLabelProvider implements ILabelProvider { |
273 | |
274 | /** |
275 | * @generated |
276 | */ |
277 | private WorkbenchLabelProvider myWorkbenchLabelProvider = new WorkbenchLabelProvider(); |
278 | |
279 | /** |
280 | * @generated |
281 | */ |
282 | private AdapterFactoryLabelProvider myAdapterFactoryLabelProvider = new AdapterFactoryLabelProvider( |
283 | PalladioComponentModelRepositoryDiagramEditorPlugin |
284 | .getInstance().getItemProvidersAdapterFactory()); |
285 | |
286 | /** |
287 | * @generated |
288 | */ |
289 | public Image getImage(Object element) { |
290 | Image result = myWorkbenchLabelProvider.getImage(element); |
291 | return result != null ? result : myAdapterFactoryLabelProvider |
292 | .getImage(element); |
293 | } |
294 | |
295 | /** |
296 | * @generated |
297 | */ |
298 | public String getText(Object element) { |
299 | String result = myWorkbenchLabelProvider.getText(element); |
300 | return result != null && result.length() > 0 ? result |
301 | : myAdapterFactoryLabelProvider.getText(element); |
302 | } |
303 | |
304 | /** |
305 | * @generated |
306 | */ |
307 | public void addListener(ILabelProviderListener listener) { |
308 | myWorkbenchLabelProvider.addListener(listener); |
309 | myAdapterFactoryLabelProvider.addListener(listener); |
310 | } |
311 | |
312 | /** |
313 | * @generated |
314 | */ |
315 | public void dispose() { |
316 | myWorkbenchLabelProvider.dispose(); |
317 | myAdapterFactoryLabelProvider.dispose(); |
318 | } |
319 | |
320 | /** |
321 | * @generated |
322 | */ |
323 | public boolean isLabelProperty(Object element, String property) { |
324 | return myWorkbenchLabelProvider.isLabelProperty(element, property) |
325 | || myAdapterFactoryLabelProvider.isLabelProperty(element, |
326 | property); |
327 | } |
328 | |
329 | /** |
330 | * @generated |
331 | */ |
332 | public void removeListener(ILabelProviderListener listener) { |
333 | myWorkbenchLabelProvider.removeListener(listener); |
334 | myAdapterFactoryLabelProvider.removeListener(listener); |
335 | } |
336 | |
337 | } |
338 | |
339 | /** |
340 | * @generated |
341 | */ |
342 | private class ModelFilesFilter extends ViewerFilter { |
343 | |
344 | /** |
345 | * @generated |
346 | */ |
347 | public boolean select(Viewer viewer, Object parentElement, |
348 | Object element) { |
349 | if (element instanceof IContainer) { |
350 | return true; |
351 | } |
352 | if (element instanceof IFile) { |
353 | IFile file = (IFile) element; |
354 | return isValidModelFile(file); |
355 | } |
356 | return true; |
357 | } |
358 | |
359 | } |
360 | |
361 | /** |
362 | * @generated |
363 | */ |
364 | private class OkButtonEnabler implements ISelectionChangedListener { |
365 | |
366 | /** |
367 | * @generated |
368 | */ |
369 | public void selectionChanged(SelectionChangedEvent event) { |
370 | if (event.getSelection() instanceof IStructuredSelection) { |
371 | IStructuredSelection selection = (IStructuredSelection) event |
372 | .getSelection(); |
373 | if (selection.size() == 1) { |
374 | Object selectedElement = selection.getFirstElement(); |
375 | if (selectedElement instanceof IWrapperItemProvider) { |
376 | selectedElement = ((IWrapperItemProvider) selectedElement) |
377 | .getValue(); |
378 | } |
379 | if (selectedElement instanceof FeatureMap.Entry) { |
380 | selectedElement = ((FeatureMap.Entry) selectedElement) |
381 | .getValue(); |
382 | } |
383 | if (selectedElement instanceof EObject) { |
384 | EObject selectedModelElement = (EObject) selectedElement; |
385 | setOkButtonEnabled(ViewService |
386 | .getInstance() |
387 | .provides( |
388 | Node.class, |
389 | new EObjectAdapter(selectedModelElement), |
390 | myView, |
391 | null, |
392 | ViewUtil.APPEND, |
393 | true, |
394 | PalladioComponentModelRepositoryDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT)); |
395 | mySelectedModelElementURI = EcoreUtil |
396 | .getURI(selectedModelElement); |
397 | return; |
398 | } |
399 | } |
400 | } |
401 | mySelectedModelElementURI = null; |
402 | setOkButtonEnabled(false); |
403 | } |
404 | |
405 | } |
406 | |
407 | } |