Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4481be47be7477ca4c5692651925737bfca3eb07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * Copyright (c) 2007, 2008 Borland Software Corporation
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Alexander Shatalin (Borland) - initial API and implementation
 */

«IMPORT 'http://www.eclipse.org/gmf/2008/GenModel'»
«IMPORT 'http://www.eclipse.org/emf/2002/Ecore'»
«EXTENSION xpt::editor::Utils»

«DEFINE ElementChooser FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::copyright FOR editorGen-»
package «editorGen.editor.packageName»;

	«EXPAND xpt::Common::generatedClassComment»
public class «elementChooserClassName» extends org.eclipse.jface.dialogs.Dialog {

	«EXPAND attributes-»
	
	«EXPAND _constructor-»
	
	«EXPAND createDialogArea-»
	
	«EXPAND createButtonBar-»
	
	«EXPAND createModelBrowser-»
	
	«EXPAND setOkButtonEnabled-»
	
	«EXPAND isValidModelFile-»
	
	«EXPAND getSelectedModelElementURI-»
	
	«EXPAND open-»
	
	«EXPAND ModelElementsTreeContentProvider-»

	«EXPAND ModelElementsTreeLabelProvider-»

	«EXPAND ModelFilesFilter-»

	«EXPAND OkButtonEnabler-»	

	«EXPAND additions-»
}
«ENDDEFINE»

«DEFINE attributes FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
private org.eclipse.jface.viewers.TreeViewer myTreeViewer;

	«EXPAND xpt::Common::generatedMemberComment»
private org.eclipse.emf.common.util.URI mySelectedModelElementURI;

	«EXPAND xpt::Common::generatedMemberComment»
private org.eclipse.gmf.runtime.notation.View myView;
	
	«EXPAND xpt::Common::generatedMemberComment»
private org.eclipse.emf.transaction.TransactionalEditingDomain myEditingDomain = org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory.INSTANCE.createEditingDomain();
«ENDDEFINE»

«DEFINE _constructor FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public «elementChooserClassName»(org.eclipse.swt.widgets.Shell parentShell, org.eclipse.gmf.runtime.notation.View view) {
	super(parentShell);
	setShellStyle(getShellStyle() | org.eclipse.swt.SWT.RESIZE);
	myView = view;
}
«ENDDEFINE»

«DEFINE createDialogArea FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected org.eclipse.swt.widgets.Control createDialogArea(org.eclipse.swt.widgets.Composite parent) {
	org.eclipse.swt.widgets.Composite composite = (org.eclipse.swt.widgets.Composite) super.createDialogArea(parent);
	getShell().setText(«EXPAND xpt::Externalizer::accessorCall(titleKey(i18nKeyForSelectModelElement(self))) FOR editorGen»);
	createModelBrowser(composite);
	return composite;
}
«ENDDEFINE»

«DEFINE createButtonBar FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected org.eclipse.swt.widgets.Control createButtonBar(org.eclipse.swt.widgets.Composite parent) {
	org.eclipse.swt.widgets.Control buttonBar = super.createButtonBar(parent);
	setOkButtonEnabled(false);
	return buttonBar;
}
«ENDDEFINE»

«DEFINE createModelBrowser FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
private void createModelBrowser(org.eclipse.swt.widgets.Composite composite) {
	myTreeViewer = new org.eclipse.jface.viewers.TreeViewer(composite, org.eclipse.swt.SWT.SINGLE | org.eclipse.swt.SWT.H_SCROLL | org.eclipse.swt.SWT.V_SCROLL | org.eclipse.swt.SWT.BORDER);
	org.eclipse.swt.layout.GridData layoutData = new org.eclipse.swt.layout.GridData(org.eclipse.swt.layout.GridData.FILL_BOTH);
	layoutData.heightHint = 300;
	layoutData.widthHint = 300;
	myTreeViewer.getTree().setLayoutData(layoutData);
	myTreeViewer.setContentProvider(new ModelElementsTreeContentProvider());
	myTreeViewer.setLabelProvider(new ModelElementsTreeLabelProvider());
	myTreeViewer.setInput(org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot());
	myTreeViewer.addFilter(new ModelFilesFilter());
	myTreeViewer.addSelectionChangedListener(new OkButtonEnabler());
}
«ENDDEFINE»

«DEFINE setOkButtonEnabled FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
private void setOkButtonEnabled(boolean enabled) {
	getButton(org.eclipse.jface.dialogs.IDialogConstants.OK_ID).setEnabled(enabled);
}
«ENDDEFINE»

«DEFINE isValidModelFile FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
private boolean isValidModelFile(org.eclipse.core.resources.IFile file) {
	String fileExtension = file.getFullPath().getFileExtension();
	return «EXPAND compareModelFileExtension FOREACH containsShortcutsTo SEPARATOR ' || '»; «EXPAND xpt::Common::nonNLS FOR containsShortcutsTo->asSequence()»
}
«ENDDEFINE»

«DEFINE compareModelFileExtension FOR String»"«self»".equals(fileExtension)«ENDDEFINE»

«DEFINE getSelectedModelElementURI FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public org.eclipse.emf.common.util.URI getSelectedModelElementURI() {
	return mySelectedModelElementURI;
}
«ENDDEFINE»

«DEFINE open FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public int open() {
	int result = super.open();
	for (java.util.Iterator it = myEditingDomain.getResourceSet().getResources().iterator(); it.hasNext();) {
		org.eclipse.emf.ecore.resource.Resource resource = (org.eclipse.emf.ecore.resource.Resource) it.next();
		resource.unload();
	}
	myEditingDomain.dispose();
	return result;
}
«ENDDEFINE»

«DEFINE ModelElementsTreeContentProvider FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedClassComment»
private class ModelElementsTreeContentProvider implements org.eclipse.jface.viewers.ITreeContentProvider {

	«EXPAND METCP_attributes-»
	
	«EXPAND METCP_getChildren-»

	«EXPAND METCP_getParent-»

	«EXPAND METCP_hasChildren-»

	«EXPAND METCP_getElements-»

	«EXPAND METCP_dispose-»

	«EXPAND METCP_inputChanged-»	

	«EXPAND METCP_additions-»
}
«ENDDEFINE»

«DEFINE METCP_attributes FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
private org.eclipse.jface.viewers.ITreeContentProvider myWorkbenchContentProvider = new «IF null = editorGen.application»org.eclipse.ui.model.WorkbenchContentProvider«ELSE»org.eclipse.ui.model.BaseWorkbenchContentProvider«ENDIF»();

	«EXPAND xpt::Common::generatedMemberComment»
private org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider myAdapterFctoryContentProvier = new org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider(«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().getItemProvidersAdapterFactory());
«ENDDEFINE»

«DEFINE METCP_getChildren FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public Object[] getChildren(Object parentElement) {
	Object[] result = myWorkbenchContentProvider.getChildren(parentElement);
	if (result != null && result.length > 0) {
		return result;
	}
	if (parentElement instanceof org.eclipse.core.resources.IFile) {
		org.eclipse.core.resources.IFile modelFile = (org.eclipse.core.resources.IFile) parentElement;
		org.eclipse.core.runtime.IPath resourcePath = modelFile.getFullPath();
		org.eclipse.emf.ecore.resource.ResourceSet resourceSet = myEditingDomain.getResourceSet();
		try {
			org.eclipse.emf.ecore.resource.Resource modelResource = resourceSet.getResource(org.eclipse.emf.common.util.URI.createPlatformResourceURI(resourcePath.toString(), true), true);
			return myAdapterFctoryContentProvier.getChildren(modelResource);
		} catch (org.eclipse.emf.common.util.WrappedException e) {
			«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError("Unable to load resource: " + resourcePath.toString(), e); «EXPAND xpt::Common::nonNLS»
		}
		return java.util.Collections.EMPTY_LIST.toArray();
	}
	return myAdapterFctoryContentProvier.getChildren(parentElement);
}
«ENDDEFINE»

«DEFINE METCP_getParent FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public Object getParent(Object element) {
	Object parent = myWorkbenchContentProvider.getParent(element);
	if (parent != null) {
		return parent;
	}
	if (element instanceof org.eclipse.emf.ecore.EObject) {
		org.eclipse.emf.ecore.EObject eObject = (org.eclipse.emf.ecore.EObject) element;
		if (eObject.eContainer() == null && eObject.eResource().getURI().isFile()) {
			String path = eObject.eResource().getURI().path();
			return org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new org.eclipse.core.runtime.Path(path));
		}
		return myAdapterFctoryContentProvier.getParent(eObject);
	}
	return null;
}
«ENDDEFINE»

«DEFINE METCP_hasChildren FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public boolean hasChildren(Object element) {
	if (element instanceof org.eclipse.core.resources.IFile) {
		return isValidModelFile((org.eclipse.core.resources.IFile) element);
	}
	return myWorkbenchContentProvider.hasChildren(element) || myAdapterFctoryContentProvier.hasChildren(element);
}
«ENDDEFINE»

«DEFINE METCP_getElements FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public Object[] getElements(Object inputElement) {
	Object[] elements = myWorkbenchContentProvider.getElements(inputElement);
	return elements;
}
«ENDDEFINE»

«DEFINE METCP_dispose FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public void dispose() {
	myWorkbenchContentProvider.dispose();
	myAdapterFctoryContentProvier.dispose();
}
«ENDDEFINE»

«DEFINE METCP_inputChanged FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public void inputChanged(org.eclipse.jface.viewers.Viewer viewer, Object oldInput, Object newInput) {
	myWorkbenchContentProvider.inputChanged(viewer, oldInput, newInput);
	myAdapterFctoryContentProvier.inputChanged(viewer, oldInput, newInput);
}
«ENDDEFINE»

«DEFINE ModelElementsTreeLabelProvider FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedClassComment»
private class ModelElementsTreeLabelProvider implements org.eclipse.jface.viewers.ILabelProvider {

	«EXPAND METLP_attributes-»
	
	«EXPAND METLP_getImage-»
	
	«EXPAND METLP_getText-»
	
	«EXPAND METLP_addListener-»
	
	«EXPAND METLP_dispose-»
	
	«EXPAND METLP_isLabelProperty-»
	
	«EXPAND METLP_removeListener-»

	«EXPAND METLP_additions-»
}
«ENDDEFINE»


«DEFINE METLP_attributes FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
		private org.eclipse.ui.model.WorkbenchLabelProvider myWorkbenchLabelProvider = new org.eclipse.ui.model.WorkbenchLabelProvider();

	«EXPAND xpt::Common::generatedMemberComment»
		private org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider myAdapterFactoryLabelProvider = new org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider(«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().getItemProvidersAdapterFactory());
«ENDDEFINE»
		
«DEFINE METLP_getImage FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public org.eclipse.swt.graphics.Image getImage(Object element) {
	org.eclipse.swt.graphics.Image result = myWorkbenchLabelProvider.getImage(element);
	return result != null ? result : myAdapterFactoryLabelProvider.getImage(element);
}
«ENDDEFINE»

«DEFINE METLP_getText FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public String getText(Object element) {
	String result = myWorkbenchLabelProvider.getText(element);
	return result != null && result.length() > 0 ? result : myAdapterFactoryLabelProvider.getText(element);
}
«ENDDEFINE»

«DEFINE METLP_addListener FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public void addListener(org.eclipse.jface.viewers.ILabelProviderListener listener) {
	myWorkbenchLabelProvider.addListener(listener);
	myAdapterFactoryLabelProvider.addListener(listener);
}
«ENDDEFINE»

«DEFINE METLP_dispose FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public void dispose() {
	myWorkbenchLabelProvider.dispose();
	myAdapterFactoryLabelProvider.dispose();
}
«ENDDEFINE»

«DEFINE METLP_isLabelProperty FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public boolean isLabelProperty(Object element, String property) {
	return myWorkbenchLabelProvider.isLabelProperty(element, property) || myAdapterFactoryLabelProvider.isLabelProperty(element, property);
}
«ENDDEFINE»

«DEFINE METLP_removeListener FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public void removeListener(org.eclipse.jface.viewers.ILabelProviderListener listener) {
	myWorkbenchLabelProvider.removeListener(listener);
	myAdapterFactoryLabelProvider.removeListener(listener);
}
«ENDDEFINE»

«DEFINE ModelFilesFilter FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedClassComment»
private class ModelFilesFilter extends org.eclipse.jface.viewers.ViewerFilter {

	«EXPAND MFF_select-»

	«EXPAND MFF_additions-»
}
«ENDDEFINE»

«DEFINE MFF_select FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public boolean select(org.eclipse.jface.viewers.Viewer viewer, Object parentElement, Object element) {
	if (element instanceof org.eclipse.core.resources.IContainer) {
		return true;
	}
	if (element instanceof org.eclipse.core.resources.IFile) {
		org.eclipse.core.resources.IFile file = (org.eclipse.core.resources.IFile) element;
		return isValidModelFile(file);
	}
	return true;
}
«ENDDEFINE»

«DEFINE OkButtonEnabler FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedClassComment»
private class OkButtonEnabler implements org.eclipse.jface.viewers.ISelectionChangedListener {

	«EXPAND OBE_selectionChanged-»

	«EXPAND OBE_additions-»
}
«ENDDEFINE»

«DEFINE OBE_selectionChanged FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public void selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent event) {
	if (event.getSelection() instanceof org.eclipse.jface.viewers.IStructuredSelection) {
		org.eclipse.jface.viewers.IStructuredSelection selection = (org.eclipse.jface.viewers.IStructuredSelection) event.getSelection();
		if (selection.size() == 1) {
			Object selectedElement = selection.getFirstElement();
			if (selectedElement instanceof org.eclipse.emf.edit.provider.IWrapperItemProvider) {
				selectedElement = ((org.eclipse.emf.edit.provider.IWrapperItemProvider) selectedElement).getValue();
			}
			if (selectedElement instanceof org.eclipse.emf.ecore.util.FeatureMap.Entry) {
				selectedElement = ((org.eclipse.emf.ecore.util.FeatureMap.Entry) selectedElement).getValue();
			}
			if (selectedElement instanceof org.eclipse.emf.ecore.EObject) {
				org.eclipse.emf.ecore.EObject selectedModelElement = (org.eclipse.emf.ecore.EObject) selectedElement;
				setOkButtonEnabled(org.eclipse.gmf.runtime.diagram.core.services.ViewService.getInstance().provides(org.eclipse.gmf.runtime.notation.Node.class, new org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter(selectedModelElement), myView, null, org.eclipse.gmf.runtime.diagram.core.util.ViewUtil.APPEND, true, «EXPAND xpt::plugin::Activator::preferenceHintAccess FOR editorGen»));
				mySelectedModelElementURI = org.eclipse.emf.ecore.util.EcoreUtil.getURI(selectedModelElement);
				return;
			}
		}
	}
	mySelectedModelElementURI = null;
	setOkButtonEnabled(false);
}
«ENDDEFINE»

«DEFINE METCP_additions FOR gmfgen::GenDiagram-»
«ENDDEFINE»

«DEFINE METLP_additions FOR gmfgen::GenDiagram-»
«ENDDEFINE»

«DEFINE MFF_additions FOR gmfgen::GenDiagram-»
«ENDDEFINE»

«DEFINE OBE_additions FOR gmfgen::GenDiagram-»
«ENDDEFINE»

«DEFINE additions FOR gmfgen::GenDiagram-»
«ENDDEFINE»

«DEFINE i18nValues FOR gmfgen::GenDiagram-»
«EXPAND xpt::Externalizer::messageEntry(titleKey(i18nKeyForSelectModelElement(self)), 'Select model element')-»
«ENDDEFINE»

«DEFINE i18nAccessors FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Externalizer::accessorField(titleKey(i18nKeyForSelectModelElement(self)))-»
«ENDDEFINE»

Back to the top