Skip to main content
summaryrefslogtreecommitdiffstats
blob: 13ed1ff7201b6eb77df3ffabb9decc77e4393580 (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
/******************************************************************************* 
 * Copyright (c) 2011, 2012 Red Hat, Inc. 
 *  All rights reserved. 
 * This program is 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: 
 * Red Hat, Inc. - initial API and implementation 
 *
 * @author Innar Made
 ******************************************************************************/
package org.eclipse.bpmn2.modeler.ui.wizards;

import java.io.File;

import org.eclipse.bpmn2.modeler.core.di.DIUtils;
import org.eclipse.bpmn2.modeler.core.utils.ErrorUtils;
import org.eclipse.bpmn2.modeler.core.utils.FileUtils;
import org.eclipse.bpmn2.modeler.core.utils.ModelUtil.Bpmn2DiagramType;
import org.eclipse.bpmn2.modeler.ui.Activator;
import org.eclipse.bpmn2.modeler.ui.Bpmn2DiagramEditorInput;
import org.eclipse.bpmn2.modeler.ui.editor.BPMN2Editor;
import org.eclipse.bpmn2.modeler.ui.editor.DefaultBPMN2Editor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.URI;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.ui.editor.DiagramEditorInput;
import org.eclipse.graphiti.ui.services.GraphitiUi;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

public class BPMN2DiagramCreator {

	public static Bpmn2DiagramEditorInput createDiagram(URI uri, Bpmn2DiagramType diagramType, String targetNamespace) throws CoreException {
		return createDiagram(null, uri, diagramType, targetNamespace, null);
	}

	public static Bpmn2DiagramEditorInput createDiagram(IEditorInput oldInput, URI modelUri, Bpmn2DiagramType diagramType, String targetNamespace, DefaultBPMN2Editor diagramEditor) {

		// Should we create a new Graphiti diagram file or reuse the one
		// from an already open editor window?
		boolean createNew = true;
		URI diagramUri = null;
		BPMN2Editor otherEditor = BPMN2Editor.findOpenEditor(diagramEditor, oldInput);
		
		String modelName = modelUri.trimFragment().trimFileExtension().lastSegment();
		// We still need to create a Diagram object for this editor
		final Diagram diagram = DIUtils.createDiagram(modelName); //$NON-NLS-1$

		if (otherEditor!=null) {
			// reuse the temp Diagram File from other editor
			diagramUri = otherEditor.getDiagramUri();
			createNew = false;
		}
		else {
			// delete old temp file if necessary
			if (oldInput instanceof Bpmn2DiagramEditorInput) {
				URI oldUri = ((Bpmn2DiagramEditorInput)oldInput).getUri();
				final File oldTempFile = new File(oldUri.toFileString());
				if (oldTempFile!=null && oldTempFile.exists()) {
					// If two or more editor windows are open on the same file
					// when the workbench is first starting up, then deleting
					// the old temp file before all copies of the editor are
					// initialized can cause problems.
					Display.getDefault().asyncExec(new Runnable() {
						@Override
						public void run() {
							try {
								oldTempFile.delete();
							} catch (Exception e) {
							}
						}
					});
				}
			}
			String diagramName = FileUtils.createTempName(modelName);
			diagramUri = URI.createFileURI(diagramName);
			FileService.createEmfFileForDiagram(diagramUri, diagram, diagramEditor);
		}

		String providerId = GraphitiUi.getExtensionManager().getDiagramTypeProviderId(diagram.getDiagramTypeId());
		
		// No need to create a new one if old input is already a Bpmn2DiagramEditorInput,
		// just update it
		Bpmn2DiagramEditorInput newInput;
		if (oldInput instanceof Bpmn2DiagramEditorInput) {
			newInput = (Bpmn2DiagramEditorInput)oldInput;
			newInput.updateUri(diagramUri);
		}
		else if (createNew) {
			newInput = new Bpmn2DiagramEditorInput(modelUri, diagramUri, providerId);
		}
		else {
			newInput = (Bpmn2DiagramEditorInput) otherEditor.getEditorInput();
		}
		
		newInput.setInitialDiagramType(diagramType);
		newInput.setTargetNamespace(targetNamespace);

		if (diagramEditor==null) {
			openEditor(newInput);
		}

		return newInput;
	}

	public static IEditorPart openEditor(final DiagramEditorInput editorInput) {
		final Object result[] = { new Object() };
		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
			@Override
			public void run() {
				try {
					IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
					IEditorPart part = null;
					part = page.findEditor(editorInput);
					if (part!=null) {
						page.activate(part);
					}
					else {
						part = page.openEditor(editorInput, BPMN2Editor.EDITOR_ID);
					}
					result[0] = part;
				} catch (PartInitException e) {
					String error = Messages.BPMN2DiagramCreator_Create_Error;
					IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, error, e);
					ErrorUtils.showErrorWithLogging(status);
				}
			}
		});
		return (IEditorPart)result[0];
	}
}

Back to the top