Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7f0c4016f42124cb2328bf053602fcf77d2c1e84 (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
<%@ 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.Collections;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.OperationHistoryFactory;
import org.eclipse.core.resources.IFile;
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.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.ui.PartInitException;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
<%importManager.markImportLocation(stringBuffer);%>

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

	/**
	 * @generated
	 */
	public static boolean openDiagram(Resource diagram) throws PartInitException {
<%if (editorGen.getApplication() == null) {%>
		return <%=importManager.getImportedName("org.eclipse.emf.edit.ui.util.EditUIUtil")%>.openEditor((EObject) diagram.getContents().get(0));
<%} 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());
<%}%>
	}

	/**
	 * @generated
	 */
	private static void setCharset(IPath path) {
		IFile file = <%=importManager.getImportedName("org.eclipse.core.resources.ResourcesPlugin")%>.getWorkspace().getRoot().getFile(path);
		try {
			file.setCharset("UTF-8", new <%=importManager.getImportedName("org.eclipse.core.runtime.NullProgressMonitor")%>()); //$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 file " + path, e); //$NON-NLS-1$
		}
	}

	/**
<%if (editorGen.getApplication() == null) {%>
	 * <p>
	 * This method should be called within a workspace modify operation since it creates resources.
	 * </p>
<%}%>
	 * @generated
	 * @return the created resource, or <code>null</code> if the resource was not created
	 */
	public static final Resource createDiagram(IPath containerFullPath, final String fileName, IProgressMonitor progressMonitor) {
		TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE.createEditingDomain();
		progressMonitor.beginTask("Creating diagram and model files", 3); //$NON-NLS-1$
		IPath diagramPath = containerFullPath.append(fileName);
		final Resource diagramResource = editingDomain.getResourceSet().createResource(URI.createPlatformResourceURI(diagramPath.toString()));
<%
final boolean standaloneDomainModel = !editorGen.isSameFileForDiagramAndModel() && genDiagram.getDomainDiagramElement() != null;
if (standaloneDomainModel) {
%>
		IPath modelPath = diagramPath.removeFileExtension().addFileExtension("taipan"); //$NON-NLS-1$
		final Resource modelResource = editingDomain.getResourceSet().createResource(URI.createPlatformResourceURI(modelPath.toString()));
<%}%>
		AbstractTransactionalCommand command = new AbstractTransactionalCommand(editingDomain, "Creating diagram and model", Collections.EMPTY_LIST) { //$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(fileName);
<%if (genDiagram.getDomainDiagramElement() != null) {%>
					diagram.setElement(<%if (genDiagram.getDomainDiagramElement().isExternalInterface()) {%>(<%=importManager.getImportedName("org.eclipse.emf.ecore.EObject")%>) <%}%>model);
<%}%>
				}
			try {
				<%=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$
<%if (standaloneDomainModel) {%>
				modelResource.save(options);
<%}%>
				diagramResource.save(options);
			} 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) {%>
		setCharset(modelPath);
<%}%>
		setCharset(diagramPath);
		return diagramResource;
	}
<%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 */%>
}
<%importManager.emitSortedImports();%>

Back to the top