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

importManager.addImport("java.io.IOException");
importManager.addImport("java.util.Collections");
importManager.addImport("java.util.List");
importManager.addImport("java.util.LinkedList");
importManager.addImport("org.eclipse.core.commands.ExecutionException");
importManager.addImport("org.eclipse.core.commands.operations.OperationHistoryFactory");
importManager.addImport("org.eclipse.core.resources.IContainer");
importManager.addImport("org.eclipse.core.resources.IFile");
importManager.addImport("org.eclipse.core.runtime.IProgressMonitor");
importManager.addImport("org.eclipse.core.runtime.IAdaptable");
importManager.addImport("org.eclipse.core.runtime.NullProgressMonitor");
importManager.addImport("org.eclipse.core.runtime.Path");
importManager.addImport("org.eclipse.emf.common.util.URI");
importManager.addImport("org.eclipse.emf.ecore.EObject");
importManager.addImport("org.eclipse.emf.ecore.resource.Resource");
importManager.addImport("org.eclipse.emf.ecore.resource.ResourceSet");
importManager.addImport("org.eclipse.gmf.runtime.common.core.command.CommandResult");
importManager.addImport("org.eclipse.gmf.runtime.notation.Diagram");
importManager.addImport("org.eclipse.gmf.runtime.diagram.core.services.ViewService");
importManager.addImport("org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand");
importManager.addImport("org.eclipse.emf.transaction.TransactionalEditingDomain");
importManager.addImport("org.eclipse.jface.viewers.IStructuredSelection");
importManager.addImport("org.eclipse.jface.wizard.Wizard");
importManager.addImport("org.eclipse.swt.widgets.Composite");
importManager.addImport("org.eclipse.ui.PartInitException");
importManager.addImport("org.eclipse.ui.ide.IDE");
importManager.addImport("org.eclipse.ui.dialogs.WizardNewFileCreationPage");
importManager.addImport("org.eclipse.ui.IWorkbenchPage");

importManager.markImportLocation(stringBuffer);
%>

/**
 * @generated
 */
public class <%=genDiagram.getNewDiagramFileWizardClassName()%> extends Wizard {

	/**
     * @generated
	 */
	private TransactionalEditingDomain myEditingDomain;
		
	/**
     * @generated
	 */
	private WizardNewFileCreationPage myFileCreationPage;
		
	/**
     * @generated
	 */
	private IFile mySelectedModelFile;
		
	/**
     * @generated
	 */
	private IWorkbenchPage myWorkbenchPage;
		
	/**
     * @generated
	 */
	private IStructuredSelection mySelection;
	
	/**
     * @generated
	 */
	private EObject myDiagramRoot;

	/**
	 * @generated
	 */
	public <%=genDiagram.getNewDiagramFileWizardClassName()%>(IFile selectedModelFile, IWorkbenchPage workbenchPage, IStructuredSelection selection, EObject diagramRoot, TransactionalEditingDomain editingDomain) {
		assert selectedModelFile != null : "Null selectedModelFile in <%=genDiagram.getNewDiagramFileWizardClassName()%> constructor"; //$NON-NLS-1$
		assert workbenchPage != null : "Null workbenchPage in <%=genDiagram.getNewDiagramFileWizardClassName()%> constructor"; //$NON-NLS-1$
		assert selection != null : "Null selection in <%=genDiagram.getNewDiagramFileWizardClassName()%> constructor"; //$NON-NLS-1$
	    assert diagramRoot != null : "Null diagramRoot in <%=genDiagram.getNewDiagramFileWizardClassName()%> constructor"; //$NON-NLS-1$
	    assert editingDomain != null : "Null editingDomain in <%=genDiagram.getNewDiagramFileWizardClassName()%> constructor"; //$NON-NLS-1$
	    
	   	mySelectedModelFile = selectedModelFile;
	   	myWorkbenchPage = workbenchPage;
	   	mySelection = selection;
	   	myDiagramRoot = diagramRoot;
	   	myEditingDomain = editingDomain;
	}
	     		
	/**
	 * @generated
	 */
	public void addPages() {
		myFileCreationPage = new WizardNewFileCreationPage("Initialize new Ecore diagram file", mySelection) {

			public void createControl(Composite parent) {
				super.createControl(parent);
				
				IContainer parentContainer = mySelectedModelFile.getParent();
				String originalFileName = mySelectedModelFile.getProjectRelativePath().removeFileExtension().lastSegment();
				String fileExtension = ".<%=editorGen.getDiagramFileExtension()%>"; //$NON-NLS-1$
				String fileName = originalFileName + fileExtension;
				for (int i = 1; parentContainer.getFile(new Path(fileName)).exists(); i++) {
					fileName = originalFileName + i + fileExtension;
				}
				setFileName(fileName);
			}
		
		};
		myFileCreationPage.setTitle("Diagram file");
		myFileCreationPage.setDescription("Create new diagram based on " + <%=importManager.getImportedName(genDiagram.getEditPartQualifiedClassName())%>.MODEL_ID + " model content");
		addPage(myFileCreationPage);
		addPage(new RootElementSelectorPage());
	}

	/**
	 * @generated
	 */
	public boolean performFinish() {
		IFile diagramFile = myFileCreationPage.createNewFile();
		try {
			diagramFile.setCharset("UTF-8", new 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 diagram file", e); //$NON-NLS-1$
		}
		
		ResourceSet resourceSet = myEditingDomain.getResourceSet();
		final Resource diagramResource = resourceSet.createResource(URI.createPlatformResourceURI(diagramFile.getFullPath().toString()));

		List affectedFiles = new LinkedList();
		affectedFiles.add(mySelectedModelFile);
		affectedFiles.add(diagramFile);
			
		AbstractTransactionalCommand command = new AbstractTransactionalCommand(myEditingDomain, "Initializing diagram contents", affectedFiles) { //$NON-NLS-1$
			protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
				int diagramVID = <%=importManager.getImportedName(genDiagram.getVisualIDRegistryQualifiedClassName())%>.getDiagramVisualID(myDiagramRoot);
				if (diagramVID != <%=importManager.getImportedName(genDiagram.getEditPartQualifiedClassName())%>.VISUAL_ID) {
					return CommandResult.newErrorCommandResult("Incorrect model object stored as a root resource object"); //$NON-NLS-1$
				}
				Diagram diagram = ViewService.createDiagram(myDiagramRoot, <%=importManager.getImportedName(genDiagram.getEditPartQualifiedClassName())%>.MODEL_ID, <%=pluginActivatorClass%>.DIAGRAM_PREFERENCES_HINT);
				diagramResource.getContents().add(diagram);
<%if (editorGen.isSameFileForDiagramAndModel()) {%>
				diagramResource.getContents().add(diagram.getElement());
<%
}
if (!genDiagram.isSynchronized()) {
%>
				initDiagramContents(diagram, myDiagramRoot);
<%}%>
				return CommandResult.newOKCommandResult();
			}
		};
			
		try {
			OperationHistoryFactory.getOperationHistory().execute(command, new NullProgressMonitor(), null);
			diagramResource.save(Collections.EMPTY_MAP);
			IDE.openEditor(myWorkbenchPage, diagramFile);
		} catch (ExecutionException e) {
			<%=pluginActivatorClass%>.getInstance().logError("Unable to create model and diagram", e); //$NON-NLS-1$
		} catch (IOException ex) {
			<%=pluginActivatorClass%>.getInstance().logError("Save operation failed for: " + diagramFile.getFullPath().toString(), ex); //$NON-NLS-1$
		} catch (PartInitException ex) {
			<%=pluginActivatorClass%>.getInstance().logError("Unable to open editor", ex); //$NON-NLS-1$
		}			
		return true;
	}
		
<%if (!genDiagram.isSynchronized()) {%>
<%@ include file="../semanticSync.jetinc/initDiagramContents.jetinc"%>
<%}%>	

	/**
     * @generated
	 */
	private class RootElementSelectorPage extends <%=importManager.getImportedName("org.eclipse.jface.wizard.WizardPage")%> implements <%=importManager.getImportedName("org.eclipse.jface.viewers.ISelectionChangedListener")%> {
	
		/**
    	 * @generated
		 */
		protected RootElementSelectorPage() {
			super("Select diagram root element");
			setTitle("Diagram root element");
			setDescription("Select semantic model element to be depicted on diagram");
		}

		/**
    	 * @generated
		 */		
		public void createControl(<%=importManager.getImportedName("org.eclipse.swt.widgets.Composite")%> parent) {
			initializeDialogUnits(parent);
			<%=importManager.getImportedName("org.eclipse.swt.widgets.Composite")%> topLevel = new <%=importManager.getImportedName("org.eclipse.swt.widgets.Composite")%>(parent, <%=importManager.getImportedName("org.eclipse.swt.SWT")%>.NONE);
			topLevel.setLayout(new <%=importManager.getImportedName("org.eclipse.swt.layout.GridLayout")%>());
			topLevel.setLayoutData(new <%=importManager.getImportedName("org.eclipse.swt.layout.GridData")%>(<%=importManager.getImportedName("org.eclipse.swt.layout.GridData")%>.VERTICAL_ALIGN_FILL | <%=importManager.getImportedName("org.eclipse.swt.layout.GridData")%>.HORIZONTAL_ALIGN_FILL));
			topLevel.setFont(parent.getFont());
			setControl(topLevel);
			createModelBrowser(topLevel);
			setPageComplete(validatePage());
		}
		
		/**
		 * @generated
		 */
		private void createModelBrowser(<%=importManager.getImportedName("org.eclipse.swt.widgets.Composite")%> parent) {
			<%=importManager.getImportedName("org.eclipse.swt.widgets.Composite")%> panel = new <%=importManager.getImportedName("org.eclipse.swt.widgets.Composite")%>(parent, <%=importManager.getImportedName("org.eclipse.swt.SWT")%>.NONE);
			panel.setLayoutData(new <%=importManager.getImportedName("org.eclipse.swt.layout.GridData")%>(<%=importManager.getImportedName("org.eclipse.swt.layout.GridData")%>.FILL_BOTH));
			<%=importManager.getImportedName("org.eclipse.swt.layout.GridLayout")%> layout = new <%=importManager.getImportedName("org.eclipse.swt.layout.GridLayout")%>();
			layout.marginWidth = 0;
			panel.setLayout(layout);
			
			<%=importManager.getImportedName("org.eclipse.swt.widgets.Label")%> label = new <%=importManager.getImportedName("org.eclipse.swt.widgets.Label")%>(panel, <%=importManager.getImportedName("org.eclipse.swt.SWT")%>.NONE);
			label.setText("Select diagram root element:");
			label.setLayoutData(new <%=importManager.getImportedName("org.eclipse.swt.layout.GridData")%>(<%=importManager.getImportedName("org.eclipse.swt.layout.GridData")%>.HORIZONTAL_ALIGN_BEGINNING));
		
			<%=importManager.getImportedName("org.eclipse.jface.viewers.TreeViewer")%> treeViewer = new <%=importManager.getImportedName("org.eclipse.jface.viewers.TreeViewer")%>(panel, <%=importManager.getImportedName("org.eclipse.swt.SWT")%>.SINGLE | <%=importManager.getImportedName("org.eclipse.swt.SWT")%>.H_SCROLL | <%=importManager.getImportedName("org.eclipse.swt.SWT")%>.V_SCROLL | <%=importManager.getImportedName("org.eclipse.swt.SWT")%>.BORDER);
			<%=importManager.getImportedName("org.eclipse.swt.layout.GridData")%> layoutData = new <%=importManager.getImportedName("org.eclipse.swt.layout.GridData")%>(<%=importManager.getImportedName("org.eclipse.swt.layout.GridData")%>.FILL_BOTH);
			layoutData.heightHint = 300;
			layoutData.widthHint = 300;
			treeViewer.getTree().setLayoutData(layoutData);
			treeViewer.setContentProvider(new <%=importManager.getImportedName("org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider")%>(<%=importManager.getImportedName(genDiagram.getEditorGen().getPlugin().getActivatorQualifiedClassName())%>.getInstance().getItemProvidersAdapterFactory()));
			treeViewer.setLabelProvider(new <%=importManager.getImportedName("org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider")%>(<%=importManager.getImportedName(genDiagram.getEditorGen().getPlugin().getActivatorQualifiedClassName())%>.getInstance().getItemProvidersAdapterFactory()));
			treeViewer.setInput(myDiagramRoot.eResource());
			treeViewer.setSelection(new <%=importManager.getImportedName("org.eclipse.jface.viewers.StructuredSelection")%>(myDiagramRoot));
			treeViewer.addSelectionChangedListener(this);
		}

		/**
		 * @generated
		 */
		public void selectionChanged(<%=importManager.getImportedName("org.eclipse.jface.viewers.SelectionChangedEvent")%> event) {
			myDiagramRoot = null;
			if (event.getSelection() instanceof <%=importManager.getImportedName("org.eclipse.jface.viewers.IStructuredSelection")%>) {
				<%=importManager.getImportedName("org.eclipse.jface.viewers.IStructuredSelection")%> selection = (<%=importManager.getImportedName("org.eclipse.jface.viewers.IStructuredSelection")%>) event.getSelection();
				if (selection.size() == 1) {
					Object selectedElement = selection.getFirstElement();
					if (selectedElement instanceof <%=importManager.getImportedName("org.eclipse.emf.edit.provider.IWrapperItemProvider")%>) {
						selectedElement = ((<%=importManager.getImportedName("org.eclipse.emf.edit.provider.IWrapperItemProvider")%>) selectedElement).getValue();
					}
					if (selectedElement instanceof <%=importManager.getImportedName("org.eclipse.emf.ecore.util.FeatureMap")%>.Entry) {
						selectedElement = ((<%=importManager.getImportedName("org.eclipse.emf.ecore.util.FeatureMap")%>.Entry) selectedElement).getValue();
					}
					if (selectedElement instanceof EObject) {
						myDiagramRoot = (EObject) selectedElement;
					}
				}
			}
			setPageComplete(validatePage());
		}
		
		/**
    	 * @generated
		 */	
		private boolean validatePage() {
			if (myDiagramRoot == null) {
				setErrorMessage("No diagram root element selected");
				return false;
			}
			boolean result = ViewService.getInstance().provides(new <%=importManager.getImportedName("org.eclipse.gmf.runtime.diagram.core.services.view.CreateDiagramViewOperation")%>(new <%=importManager.getImportedName("org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter")%>(myDiagramRoot), <%=importManager.getImportedName(genDiagram.getEditPartQualifiedClassName())%>.MODEL_ID, <%=importManager.getImportedName(genDiagram.getEditorGen().getPlugin().getActivatorQualifiedClassName())%>.DIAGRAM_PREFERENCES_HINT));
			setErrorMessage(result ? null : "Invalid diagram root element was selected");
			return result;
		}
		
	}
}
<%importManager.emitSortedImports();%>

Back to the top