Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c6ba5db9b2ee5307c7e3b5c59b656b039fd692cc (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
/**
 * Copyright (c) 2007, 2010, 2013 Borland Software Corporation and others
 * 
 * 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
 *    Michael Golubev (Montages) - #386838 - migrate to Xtend2
 */
package aspects.xpt.editor

import com.google.inject.Inject
import com.google.inject.Singleton
import org.eclipse.gmf.codegen.gmfgen.GenDiagram
import xpt.Externalizer
import xpt.ExternalizerUtils_qvto
import xpt.Common
import xpt.editor.ModelElementSelectionPage
import plugin.Activator
import xpt.editor.DiagramContentInitializer
import xpt.CodeStyle

@Singleton class NewDiagramFileWizard extends xpt.editor.NewDiagramFileWizard {
	
	@Inject extension Common
	@Inject extension CodeStyle
	@Inject extension ExternalizerUtils_qvto;

	@Inject Activator xptActivator
	@Inject DiagramEditorUtil xptDiagramEditorUtil	
	@Inject Externalizer xptExternalizer;
	@Inject DiagramContentInitializer xptDiagramContentInitializer
	@Inject ModelElementSelectionPage xptModelElementSelectionPage
	@Inject xpt.editor.VisualIDRegistry xptVisualIDRegistry

	override i18nValues(GenDiagram it) '''
		«xptExternalizer.messageEntry(nameKey(i18nKeyForNewDiagramFileWizardCreationPage(it)), 'Initialize new diagram file')»
		«xptExternalizer.messageEntry(titleKey(i18nKeyForNewDiagramFileWizardCreationPage(it)), 'Diagram file')»
		«xptExternalizer.messageEntry(descriptionKey(i18nKeyForNewDiagramFileWizardCreationPage(it)),
			'Create new diagram based on {0} model content')»
		«xptExternalizer.messageEntry(nameKey(i18nKeyForNewDiagramFileWizardRootSelectionPage(it)),
			'Select diagram root element')»
		«xptExternalizer.messageEntry(titleKey(i18nKeyForNewDiagramFileWizardRootSelectionPage(it)), 'Diagram root element')»
		«xptExternalizer.messageEntry(descriptionKey(i18nKeyForNewDiagramFileWizardRootSelectionPage(it)),
			'Select semantic model element to be depicted on diagram')»
		«xptExternalizer.messageEntry(i18nKeyForNewDiagramFileWizardRootSelectionPageSelectionTitle(it),
			'Select diagram root element:')»
		«xptExternalizer.messageEntry(i18nKeyForNewDiagramFileWizardRootSelectionPageNoSelectionMessage(it),
			'Diagram root element is not selected')»
		«xptExternalizer.messageEntry(i18nKeyForNewDiagramFileWizardRootSelectionPageInvalidSelectionMessage(it),
			'Invalid diagram root element is selected')»
		«xptExternalizer.messageEntry(i18nKeyForNewDiagramFileWizardInitDiagramCommand(it), 'Initializing diagram contents')»
		«xptExternalizer.messageEntry(i18nKeyForNewDiagramFileWizardIncorrectRootError(it),
			'Incorrect model object stored as a root resource object')»
	'''

	override NewDiagramFileWizard(GenDiagram it) '''
	«copyright(editorGen)»
	package «packageName(it)»;
	
	«generatedClassComment»
	public class «className(it)» extends org.eclipse.jface.wizard.Wizard {
	
		«generatedMemberComment»
		private «creationPage(it)» myFileCreationPage;
	
		«generatedMemberComment»
		private «xptModelElementSelectionPage.qualifiedClassName(it)» diagramRootElementSelectionPage;
	
		«generatedMemberComment»
		private org.eclipse.emf.transaction.TransactionalEditingDomain myEditingDomain;
	
		«generatedMemberComment»
		public «className(it)»(org.eclipse.emf.common.util.URI domainModelURI,
				org.eclipse.emf.ecore.EObject diagramRoot,
				org.eclipse.emf.transaction.TransactionalEditingDomain editingDomain) {
			«_assert('domainModelURI != null : \"Domain model uri must be specified\"')»
		    «_assert('diagramRoot != null : \"Doagram root element must be specified\"')»
		    «_assert('editingDomain != null : \"Editing domain must be specified\"')»
	
			myFileCreationPage = new «creationPage(it)»(«xptExternalizer.accessorCall(editorGen,
			nameKey(i18nKeyForNewDiagramFileWizardCreationPage(it)))», org.eclipse.jface.viewers.StructuredSelection.EMPTY);
			myFileCreationPage.setTitle(«xptExternalizer.accessorCall(editorGen,
			titleKey(i18nKeyForNewDiagramFileWizardCreationPage(it)))»);
			myFileCreationPage.setDescription(org.eclipse.osgi.util.NLS.bind(
					«xptExternalizer.accessorCall(editorGen, descriptionKey(i18nKeyForNewDiagramFileWizardCreationPage(it)))», 
					«VisualIDRegistry::modelID(it)»));
			org.eclipse.core.runtime.IPath filePath;
			String fileName = org.eclipse.emf.common.util.URI.decode(domainModelURI.trimFileExtension().lastSegment());
			if (domainModelURI.isPlatformResource()) {
				filePath = new org.eclipse.core.runtime.Path(domainModelURI.trimSegments(1).toPlatformString(true));
			} else if (domainModelURI.isFile()) {
				filePath = new org.eclipse.core.runtime.Path(domainModelURI.trimSegments(1).toFileString());
			} else {
				// TODO : use some default path
				throw new IllegalArgumentException("Unsupported URI: " + domainModelURI); «nonNLS(1)»
			}
			myFileCreationPage.setContainerFullPath(filePath);
			myFileCreationPage.setFileName(«xptDiagramEditorUtil.qualifiedClassName(it)».getUniqueFileName(
					filePath, fileName, "«editorGen.diagramFileExtension»")); «nonNLS(1)»
	
			diagramRootElementSelectionPage = new DiagramRootElementSelectionPage(«xptExternalizer.accessorCall(editorGen,
			nameKey(i18nKeyForNewDiagramFileWizardRootSelectionPage(it)))»);
			diagramRootElementSelectionPage.setTitle(«xptExternalizer.accessorCall(editorGen,
			titleKey(i18nKeyForNewDiagramFileWizardRootSelectionPage(it)))»);
			diagramRootElementSelectionPage.setDescription(«xptExternalizer.accessorCall(editorGen,
			descriptionKey(i18nKeyForNewDiagramFileWizardRootSelectionPage(it)))»);
			diagramRootElementSelectionPage.setModelElement(diagramRoot);
	
		   	myEditingDomain = editingDomain;
		}
	
		«generatedMemberComment»
		public void addPages() {
			addPage(myFileCreationPage);
			addPage(diagramRootElementSelectionPage);
		}
	
		«generatedMemberComment»
		public boolean performFinish() {
			java.util.LinkedList<org.eclipse.core.resources.IFile> affectedFiles = new java.util.LinkedList<org.eclipse.core.resources.IFile>(); 
			«IF null == editorGen.application»
			org.eclipse.core.resources.IFile diagramFile = myFileCreationPage.createNewFile();
			«xptDiagramEditorUtil.callSetCharset(it, 'diagramFile')»
			affectedFiles.add(diagramFile);
			org.eclipse.emf.common.util.URI diagramModelURI = org.eclipse.emf.common.util.URI.createPlatformResourceURI(diagramFile.getFullPath().toString(), true);
			«ELSE»
			org.eclipse.core.runtime.IPath diagramModelPath = myFileCreationPage.getContainerFullPath().append(myFileCreationPage.getFileName());
			org.eclipse.emf.common.util.URI diagramModelURI = org.eclipse.emf.common.util.URI.createFileURI(diagramModelPath.toString());
			«ENDIF»
			org.eclipse.emf.ecore.resource.ResourceSet resourceSet = myEditingDomain.getResourceSet();
			final org.eclipse.emf.ecore.resource.Resource diagramResource = resourceSet.createResource(diagramModelURI);
			org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand command =
				new org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand(
					myEditingDomain, «xptExternalizer.accessorCall(editorGen, i18nKeyForNewDiagramFileWizardInitDiagramCommand(it))», affectedFiles) {
	
				«overrideC»
				protected org.eclipse.gmf.runtime.common.core.command.CommandResult doExecuteWithResult(
						org.eclipse.core.runtime.IProgressMonitor monitor, org.eclipse.core.runtime.IAdaptable info)
							throws org.eclipse.core.commands.ExecutionException {
					int diagramVID = «xptVisualIDRegistry.getDiagramVisualIDMethodCall(it)»(diagramRootElementSelectionPage.getModelElement());
					if (diagramVID != «VisualIDRegistry::visualID(it)») {
						return org.eclipse.gmf.runtime.common.core.command.CommandResult.newErrorCommandResult(
							«xptExternalizer.accessorCall(editorGen, i18nKeyForNewDiagramFileWizardIncorrectRootError(it))»);
					}
					org.eclipse.gmf.runtime.notation.Diagram diagram =
						org.eclipse.gmf.runtime.diagram.core.services.ViewService.createDiagram(
							diagramRootElementSelectionPage.getModelElement(), «VisualIDRegistry::modelID(it)»,
							«xptActivator.preferenceHintAccess(editorGen)»);
					diagramResource.getContents().add(diagram);
					«IF editorGen.sameFileForDiagramAndModel»
					diagramResource.getContents().add(diagram.getElement());
					«ENDIF»
					«IF !it.synchronized»
					new «xptDiagramContentInitializer.qualifiedClassName(it)»().initDiagramContent(diagram);			
					«ENDIF»
					return org.eclipse.gmf.runtime.common.core.command.CommandResult.newOKCommandResult();
				}
			};
			try {
				org.eclipse.core.commands.operations.OperationHistoryFactory.getOperationHistory().execute(
					command, new org.eclipse.core.runtime.NullProgressMonitor(), null);
				diagramResource.save(«xptDiagramEditorUtil.callGetSaveOptions(it)»);
				«xptDiagramEditorUtil.qualifiedClassName(it)».openDiagram(diagramResource);
			} catch (org.eclipse.core.commands.ExecutionException e) {
				«xptActivator.qualifiedClassName(editorGen.plugin)».getInstance().logError(
					"Unable to create model and diagram", e); «nonNLS(1)»
			} catch (java.io.IOException ex) {
				«xptActivator.qualifiedClassName(editorGen.plugin)».getInstance().logError(
					"Save operation failed for: " + diagramModelURI, ex); «nonNLS(1)»
			} catch (org.eclipse.ui.PartInitException ex) {
				«xptActivator.qualifiedClassName(editorGen.plugin)».getInstance().logError(
					"Unable to open editor", ex); «nonNLS(1)»
			}			
			return true;
		}
	
		«generatedClassComment»
		private static class DiagramRootElementSelectionPage extends «xptModelElementSelectionPage.qualifiedClassName(it)» {
	
			«generatedMemberComment»
			protected DiagramRootElementSelectionPage(String pageName) {
				super(pageName);
			}
	
			«generatedMemberComment»
			protected String getSelectionTitle() {
				return «xptExternalizer.accessorCall(editorGen, i18nKeyForNewDiagramFileWizardRootSelectionPageSelectionTitle(it))»;
			}
	
			«generatedMemberComment»
			protected boolean validatePage() {
				if (getModelElement() == null) {
					setErrorMessage(«xptExternalizer.accessorCall(editorGen,
			i18nKeyForNewDiagramFileWizardRootSelectionPageNoSelectionMessage(it))»);
					return false;
				}
				boolean result = org.eclipse.gmf.runtime.diagram.core.services.ViewService.getInstance().provides(
					new org.eclipse.gmf.runtime.diagram.core.services.view.CreateDiagramViewOperation(
						new org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter(getModelElement()),
						«VisualIDRegistry::modelID(it)», «xptActivator.preferenceHintAccess(editorGen)»));
				setErrorMessage(result ? null : «xptExternalizer.accessorCall(editorGen,
			i18nKeyForNewDiagramFileWizardRootSelectionPageInvalidSelectionMessage(it))»);
				return result;
			}
		}
	}
	'''
}

Back to the top