Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 93dd632324ed86c3e26e29e758e6dd621d3dad82 (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
<%@ jet package="org.eclipse.gmf.codegen.templates.editor" class="DiagramEditorUtilGenerator"
    imports="org.eclipse.gmf.codegen.gmfgen.* org.eclipse.gmf.common.codegen.* org.eclipse.emf.codegen.ecore.genmodel.* org.eclipse.emf.ecore.*"%>
<%
final GenDiagram genDiagram = (GenDiagram) ((Object[]) argument)[0];
final GenEditorGenerator editorGen = genDiagram.getEditorGen();
final ImportAssistant importManager = (ImportAssistant) ((Object[]) argument)[1];
%>
<%@ include file="../copyright4java.jetinc"%>
<%importManager.emitPackageStatement(stringBuffer);%>

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.OperationHistoryFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
<%importManager.markImportLocation(stringBuffer);%>

/**
 * @generated
 */
public class <%=importManager.getCompilationUnitName()%> {

	/**
	 * @generated
	 */
	public static final URI createAndOpenDiagram(IPath containerPath, String fileName, IWorkbenchWindow window, IProgressMonitor progressMonitor, boolean openEditor, boolean saveDiagram) {
		IFile diagramFile = createNewDiagramFile(containerPath, fileName, window.getShell(), progressMonitor);
		if (diagramFile != null && openEditor) {
			openDiagramEditor(window, diagramFile, saveDiagram, progressMonitor);
		}
		return URI.createPlatformResourceURI(diagramFile.getFullPath().toString(), true);
	}

	/**
	 * @generated
	 */
	public static final IEditorPart openDiagramEditor(IWorkbenchWindow window, IFile file, boolean saveDiagram, IProgressMonitor progressMonitor) {
		IEditorPart editorPart = null;
		try {
			IWorkbenchPage page = window.getActivePage();
			if (page != null) {
				editorPart = openDiagramEditor(page, file);
				if (saveDiagram) {
					editorPart.doSave(progressMonitor);
				}
			}
			file.refreshLocal(IResource.DEPTH_ZERO, null);
			return editorPart;
		} catch (Exception e) {
			<%=importManager.getImportedName(editorGen.getPlugin().getActivatorQualifiedClassName())%>.getInstance().logError("Error opening diagram", e);
		}
		return null;
	}

	/**
	 * @generated
	 */
	public static final IEditorPart openDiagramEditor(IWorkbenchPage page, IFile file) throws PartInitException {
<%if (editorGen.getApplication() == null) {%>
		return <%=importManager.getImportedName("org.eclipse.ui.ide.IDE")%>.openEditor(page, file);
<%} else {%>
		URI fileURI = URI.createFileURI(file.getLocation().toString());
		<%=importManager.getImportedName("org.eclipse.ui.IEditorDescriptor")%> editorDescriptor = page.getWorkbenchWindow().getWorkbench().getEditorRegistry().getDefaultEditor(fileURI.toFileString());
		if (editorDescriptor == null) {
			<%=importManager.getImportedName("org.eclipse.jface.dialogs.MessageDialog")%>.openError(page.getWorkbenchWindow().getShell(), "Error",
					<%=importManager.getImportedName("org.eclipse.osgi.util.NLS")%>.bind("There is no editor registered for the file ''{0}''.", fileURI.toFileString()));
			return null;
		}
		return page.openEditor(new <%=importManager.getImportedName("org.eclipse.emf.common.ui.URIEditorInput")%>(fileURI), editorDescriptor.getId());
<%}%>
	}

	/**
	 * <p>
	 * This method should be called within a workspace modify operation since it creates resources.
	 * </p>
	 * @generated
	 * @return the created file resource, or <code>null</code> if the file was not created
	 */
	public static final IFile createNewDiagramFile(IPath containerFullPath, String fileName, Shell shell, IProgressMonitor progressMonitor) {
		TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE.createEditingDomain();
		ResourceSet resourceSet = editingDomain.getResourceSet();
		progressMonitor.beginTask("Creating diagram and model files", 3); //$NON-NLS-1$
		final IFile diagramFile = createNewFile(containerFullPath, fileName, shell);
		final Resource diagramResource = resourceSet.createResource(URI.createPlatformResourceURI(diagramFile.getFullPath().toString(), true));
		List affectedFiles = new ArrayList();
		affectedFiles.add(diagramFile);
<%
final boolean standaloneDomainModel = !editorGen.isSameFileForDiagramAndModel() && genDiagram.getDomainDiagramElement() != null;
if (standaloneDomainModel) {
%>
		IPath modelFileRelativePath = diagramFile.getFullPath().removeFileExtension().addFileExtension("<%=editorGen.getDomainFileExtension()%>"); //$NON-NLS-1$
		IFile modelFile = diagramFile.getParent().getFile(new <%=importManager.getImportedName("org.eclipse.core.runtime.Path")%>(modelFileRelativePath.lastSegment()));
		final Resource modelResource = resourceSet.createResource(URI.createPlatformResourceURI(modelFile.getFullPath().toString(), true));
		affectedFiles.add(modelFile);
<%}%>
		AbstractTransactionalCommand command = new AbstractTransactionalCommand(editingDomain, "Creating diagram and model", affectedFiles) { //$NON-NLS-1$
			protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
<%
if (genDiagram.getDomainDiagramElement() != null) {%>
				<%=importManager.getImportedName(genDiagram.getDomainDiagramElement().getQualifiedInterfaceName())%> model = createInitialModel();
<%	if (standaloneDomainModel) {%>
				modelResource.getContents().add(createInitialRoot(model));
<%	} else {%>
				diagramResource.getContents().add(model);
<%
	}
}
%>
				Diagram diagram = ViewService.createDiagram(
<%if (genDiagram.getDomainDiagramElement() != null) {%>
				<%if (genDiagram.getDomainDiagramElement().isExternalInterface()) {%>(<%=importManager.getImportedName("org.eclipse.emf.ecore.EObject")%>) <%}%>model, 
<%}%>
					<%=importManager.getImportedName(genDiagram.getEditPartQualifiedClassName())%>.MODEL_ID,
					<%=genDiagram.getEditorGen().getPlugin().getActivatorClassName()%>.DIAGRAM_PREFERENCES_HINT);
				if (diagram != null) {
					diagramResource.getContents().add(diagram);
					diagram.setName(diagramFile.getName());
<%if (genDiagram.getDomainDiagramElement() != null) {%>
					diagram.setElement(<%if (genDiagram.getDomainDiagramElement().isExternalInterface()) {%>(<%=importManager.getImportedName("org.eclipse.emf.ecore.EObject")%>) <%}%>model);
<%}%>
				}
			try {
<%if (standaloneDomainModel) {%>
				<%=importManager.getImportedName("java.util.Map")%> options = new <%=importManager.getImportedName("java.util.HashMap")%>();
				options.put(<%=importManager.getImportedName("org.eclipse.emf.ecore.xmi.XMIResource")%>.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$
				modelResource.save(options);
<%}%>
				diagramResource.save(Collections.EMPTY_MAP);
			} catch (IOException e) {
				<%// TODO CommandResult.newErrorCommandResult(e) would be better? %>
				<%=importManager.getImportedName(genDiagram.getEditorGen().getPlugin().getActivatorQualifiedClassName())%>.getInstance().logError("Unable to store model and diagram resources", e); //$NON-NLS-1$
			}
				return CommandResult.newOKCommandResult();
			}
		};
		
		try {
			OperationHistoryFactory.getOperationHistory().execute(command, new SubProgressMonitor(progressMonitor, 1), null);
		} catch (ExecutionException e) {
			<%=importManager.getImportedName(genDiagram.getEditorGen().getPlugin().getActivatorQualifiedClassName())%>.getInstance().logError("Unable to create model and diagram", e); //$NON-NLS-1$
		}
		
<%if (standaloneDomainModel) {%>
		try {
			modelFile.setCharset("UTF-8", new SubProgressMonitor(progressMonitor, 1)); //$NON-NLS-1$
		} catch (<%=importManager.getImportedName("org.eclipse.core.runtime.CoreException")%> e) {
			<%=importManager.getImportedName(genDiagram.getEditorGen().getPlugin().getActivatorQualifiedClassName())%>.getInstance().logError("Unable to set charset for model file", e); //$NON-NLS-1$
		}
<%}%>
		try {
			diagramFile.setCharset("UTF-8", new SubProgressMonitor(progressMonitor, 1)); //$NON-NLS-1$
		} catch (<%=importManager.getImportedName("org.eclipse.core.runtime.CoreException")%> e) {
			<%=importManager.getImportedName(genDiagram.getEditorGen().getPlugin().getActivatorQualifiedClassName())%>.getInstance().logError("Unable to set charset for diagram file", e); //$NON-NLS-1$
		}
		
		return diagramFile;
	}
<%if (genDiagram.getDomainDiagramElement() != null) {
final String diagramElementImportedName = importManager.getImportedName(genDiagram.getDomainDiagramElement().getQualifiedInterfaceName());
final GenPackage domainGenPackage = genDiagram.getDomainDiagramElement().getGenPackage();
%>
	/**
	 * Create a new instance of domain element associated with canvas.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	private static <%=diagramElementImportedName%> createInitialModel() {
		return <%=importManager.getImportedName(domainGenPackage.getQualifiedFactoryInterfaceName())%>.<%=domainGenPackage.getFactoryInstanceName()%>.create<%=genDiagram.getDomainDiagramElement().getName()%>();
	}

<%if (standaloneDomainModel /*may need docRoot only if separate file for domain model*/) {%>
	/**
	 * @generated
	 */
	private static <%=importManager.getImportedName("org.eclipse.emf.ecore.EObject")%> createInitialRoot(<%=diagramElementImportedName%> model) {
<%if (!domainGenPackage.hasDocumentRoot()) { %>
		return <%if (genDiagram.getDomainDiagramElement().isExternalInterface()) {%>(<%=importManager.getImportedName("org.eclipse.emf.ecore.EObject")%>) <%}%>model;
<%} else {
// would be better to get GenClass for docRoot and directly use setter
GenClass docRoot = domainGenPackage.getDocumentRoot();
String featureAccessor = "null"; // Alternative is: genDiagram.getDomainDiagramElement().getInterfaceName(); although it's a hack. Seems better to fail right away
for (java.util.Iterator it = docRoot.getGenFeatures().iterator(); it.hasNext(); ) {
	GenFeature genFeature = (GenFeature) it.next();
	boolean unspecifiedUpperBound = ETypedElement.UNSPECIFIED_MULTIPLICITY == genFeature.getEcoreFeature().getUpperBound();
	// Perhaps, makes sense to check instanceof EReference && isContainment
	if (genFeature.isSet() && unspecifiedUpperBound && genDiagram.getDomainDiagramElement().equals(genFeature.getTypeGenClass())) {
		featureAccessor = genFeature.getAccessorName();
		break;
	}
} /*for*/
%>
		<%=importManager.getImportedName(docRoot.getQualifiedInterfaceName())%> docRoot =
				<%=importManager.getImportedName(domainGenPackage.getQualifiedFactoryInterfaceName())%>.<%=domainGenPackage.getFactoryInstanceName()%>.create<%=docRoot.getName()%>();
		docRoot.set<%=featureAccessor%>(model);
		return docRoot;
<%}%>
	}
<%} /*if standaloneDomainModel*/
} /* domainDiagramElement != null */%>

	/**
	 * @generated
	 */
	public static IFile createNewFile(IPath containerPath, String fileName, Shell shell) {
		IPath newFilePath = containerPath.append(fileName);
		IFile newFileHandle = <%=importManager.getImportedName("org.eclipse.core.resources.ResourcesPlugin")%>.getWorkspace().getRoot().getFile(newFilePath);
		try {
			createFile(newFileHandle);
		} catch (<%=importManager.getImportedName("org.eclipse.core.runtime.CoreException")%> e) {
			<%=importManager.getImportedName("org.eclipse.jface.dialogs.ErrorDialog")%>.openError(shell, "Creation Problems", null, e.getStatus());
			return null;
		}
		return newFileHandle;
	}

	/**
	 * @generated
	 */
	protected static void createFile(IFile fileHandle) throws <%=importManager.getImportedName("org.eclipse.core.runtime.CoreException")%> {
		try {
			fileHandle.create(new <%=importManager.getImportedName("java.io.ByteArrayInputStream")%>(new byte[0]),
					false, new <%=importManager.getImportedName("org.eclipse.core.runtime.NullProgressMonitor")%>());
		} catch (<%=importManager.getImportedName("org.eclipse.core.runtime.CoreException")%> e) {
			// If the file already existed locally, just refresh to get contents
			if (e.getStatus().getCode() == <%=importManager.getImportedName("org.eclipse.core.resources.IResourceStatus")%>.PATH_OCCUPIED) {
				fileHandle.refreshLocal(<%=importManager.getImportedName("org.eclipse.core.resources.IResource")%>.DEPTH_ZERO, null);
			} else {
				throw e;
			}
		}
	}
}
<%importManager.emitSortedImports();%>

Back to the top