1 | package de.uka.ipd.sdq.pcm.dialogs.selection; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | |
6 | import org.eclipse.emf.common.util.URI; |
7 | import org.eclipse.emf.ecore.EObject; |
8 | import org.eclipse.emf.ecore.EReference; |
9 | import org.eclipse.emf.ecore.resource.ResourceSet; |
10 | import org.eclipse.emf.edit.provider.ComposedAdapterFactory; |
11 | import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory; |
12 | import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory; |
13 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; |
14 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; |
15 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
16 | import org.eclipse.emf.transaction.util.TransactionUtil; |
17 | import org.eclipse.jface.dialogs.MessageDialog; |
18 | import org.eclipse.swt.widgets.Shell; |
19 | |
20 | import de.uka.ipd.sdq.dialogs.selection.FilteredItemsAdapterFactory; |
21 | import de.uka.ipd.sdq.dialogs.selection.SelectEObjectDialog; |
22 | import de.uka.ipd.sdq.pcm.allocation.provider.AllocationItemProviderAdapterFactory; |
23 | import de.uka.ipd.sdq.pcm.core.composition.provider.CompositionItemProviderAdapterFactory; |
24 | import de.uka.ipd.sdq.pcm.core.provider.CoreItemProviderAdapterFactory; |
25 | import de.uka.ipd.sdq.pcm.parameter.provider.ParameterItemProviderAdapterFactory; |
26 | import de.uka.ipd.sdq.pcm.repository.provider.RepositoryItemProviderAdapterFactory; |
27 | import de.uka.ipd.sdq.pcm.resourceenvironment.provider.ResourceenvironmentItemProviderAdapterFactory; |
28 | import de.uka.ipd.sdq.pcm.resourcetype.provider.ResourcetypeItemProviderAdapterFactory; |
29 | import de.uka.ipd.sdq.pcm.seff.provider.SeffItemProviderAdapterFactory; |
30 | import de.uka.ipd.sdq.pcm.system.provider.SystemItemProviderAdapterFactory; |
31 | import de.uka.ipd.sdq.pcm.subsystem.provider.SubsystemItemProviderAdapterFactory; |
32 | import de.uka.ipd.sdq.pcm.usagemodel.provider.UsagemodelItemProviderAdapterFactory; |
33 | import de.uka.ipd.sdq.pcmbench.ui.provider.PalladioItemProviderAdapterFactory; |
34 | |
35 | /** |
36 | * @author Snowball |
37 | * |
38 | */ |
39 | public class PalladioSelectEObjectDialog extends SelectEObjectDialog { |
40 | |
41 | private Object input; |
42 | private AdapterFactoryContentProvider contentProvider; |
43 | |
44 | /** |
45 | * Creates a selection dialog |
46 | * @param parent Shell of active workbench window |
47 | * @param filterList Objects to show (positive filter). Also include respective super types here |
48 | * @param additionalChildReferences Usually this should be the EReference which should be set |
49 | * @param input ResourceSet or object of which resource set should be taken |
50 | */ |
51 | public PalladioSelectEObjectDialog(Shell parent, |
52 | Collection<Object> filterList, |
53 | Collection<EReference> additionalChildReferences, Object input) { |
54 | super(parent); |
55 | this.input = input; |
56 | create(); |
57 | ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(); |
58 | adapterFactory |
59 | .addAdapterFactory(new ResourceItemProviderAdapterFactory()); |
60 | adapterFactory |
61 | .addAdapterFactory(new RepositoryItemProviderAdapterFactory()); |
62 | adapterFactory |
63 | .addAdapterFactory(new SeffItemProviderAdapterFactory()); |
64 | adapterFactory |
65 | .addAdapterFactory(new SystemItemProviderAdapterFactory()); |
66 | adapterFactory |
67 | .addAdapterFactory(new AllocationItemProviderAdapterFactory()); |
68 | adapterFactory |
69 | .addAdapterFactory(new UsagemodelItemProviderAdapterFactory()); |
70 | adapterFactory |
71 | .addAdapterFactory(new ResourceenvironmentItemProviderAdapterFactory()); |
72 | adapterFactory |
73 | .addAdapterFactory(new ParameterItemProviderAdapterFactory()); |
74 | adapterFactory |
75 | .addAdapterFactory(new ResourcetypeItemProviderAdapterFactory()); |
76 | adapterFactory |
77 | .addAdapterFactory(new CoreItemProviderAdapterFactory()); |
78 | adapterFactory |
79 | .addAdapterFactory(new CompositionItemProviderAdapterFactory()); |
80 | adapterFactory |
81 | .addAdapterFactory(new ReflectiveItemProviderAdapterFactory()); |
82 | adapterFactory |
83 | .addAdapterFactory(new SubsystemItemProviderAdapterFactory()); |
84 | |
85 | contentProvider = new AdapterFactoryContentProvider( |
86 | new FilteredItemsAdapterFactory(adapterFactory, filterList, |
87 | additionalChildReferences)); |
88 | setViewerContentProvider(contentProvider); |
89 | |
90 | setViewerLabelProvider(new AdapterFactoryLabelProvider( |
91 | new PalladioItemProviderAdapterFactory(adapterFactory))); |
92 | setViewerInput(input); |
93 | setInputDialogResourceName(filterList); |
94 | } |
95 | |
96 | public PalladioSelectEObjectDialog(Shell parent, |
97 | Collection<Object> filterList, Object input) { |
98 | this(parent,filterList,new ArrayList<EReference>(),input); |
99 | } |
100 | |
101 | /* (non-Javadoc) |
102 | * @see de.uka.ipd.sdq.dialogs.selection.SelectEObjectDialog#setResourceName(java.util.Collection) |
103 | */ |
104 | @Override |
105 | protected void setInputDialogResourceName(Collection<Object> filterList) { |
106 | String system = "System"; |
107 | String repository = "Repository"; |
108 | String resourceRepository = "ResourceRepository"; |
109 | |
110 | ArrayList<Object> list = new ArrayList<Object>(); |
111 | list.addAll(filterList); |
112 | |
113 | if (!list.isEmpty()) { |
114 | for (Object object : list) { |
115 | Class<?> clazz = (Class<?>) object; |
116 | String name = clazz.getSimpleName(); |
117 | if (name.equals(system) || name.equals(repository) |
118 | || name.equals(resourceRepository)) |
119 | setResourceName(correctionResourceRepositoryName(name)); |
120 | } |
121 | } |
122 | } |
123 | |
124 | /** Correction the ResourceRepository to ResourceType */ |
125 | private String correctionResourceRepositoryName(String name){ |
126 | if (name.equals("ResourceRepository")) |
127 | return "ResourceType"; |
128 | return name; |
129 | } |
130 | |
131 | |
132 | /* (non-Javadoc) |
133 | * @see de.uka.ipd.sdq.dialogs.selection.SelectEObjectDialog#addResourceToEditingDomain(org.eclipse.swt.widgets.Shell, java.lang.String) |
134 | */ |
135 | @Override |
136 | protected void addModelToResourceSet(Shell shell, String uri) { |
137 | ResourceSet resourceSet = getResourceSet(input); |
138 | |
139 | if (resourceSet != null) { |
140 | URI model = URI.createURI(uri); |
141 | try { |
142 | resourceSet.getResource(model, true); |
143 | } catch (Throwable t) { |
144 | MessageDialog.openInformation(shell, "Resource Loader Error", t |
145 | .getMessage()); |
146 | } |
147 | } |
148 | } |
149 | |
150 | /** |
151 | * Get a resource set from dialog input object |
152 | * |
153 | * @param object - |
154 | * dialog input object |
155 | */ |
156 | protected ResourceSet getResourceSet(Object object) { |
157 | /** ResourceSet */ |
158 | if (object instanceof ResourceSet) |
159 | return (ResourceSet) object; |
160 | /** EObject */ |
161 | if (object instanceof EObject) { |
162 | TransactionalEditingDomain editingDomain = TransactionUtil |
163 | .getEditingDomain((EObject) object); |
164 | if (editingDomain != null) |
165 | return editingDomain.getResourceSet(); |
166 | } |
167 | return null; |
168 | } |
169 | |
170 | /** |
171 | * The method supplies the main knots of, in this dialogue the represented, |
172 | * tree |
173 | */ |
174 | public Object getViewerRootElement() { |
175 | Object object = null; |
176 | |
177 | if (contentProvider != null){ |
178 | object = (contentProvider.getElements(input))[0]; |
179 | } |
180 | |
181 | return object; |
182 | } |
183 | } |