Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 743a2f74485d9f3f5060dcaed2a08787628f3d37 (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
/*****************************************************************************
 * Copyright (c) 2010 Atos Origin.
 *
 *    
 * 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:
 *   Mathieu Velten (Atos Origin) mathieu.velten@atosorigin.com - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.documentation.view.papyrus;

import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.papyrus.infra.core.adaptor.gmf.DiagramsUtil;
import org.eclipse.papyrus.infra.core.editor.CoreMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageMngr;
import org.eclipse.papyrus.views.documentation.DocumentationManager;
import org.eclipse.papyrus.views.documentation.IDocumentationManager;
import org.eclipse.papyrus.views.documentation.view.IDocumentationPartHandler;
import org.eclipse.papyrus.views.documentation.view.SelectResourceDialog;
import org.eclipse.papyrus.views.modelexplorer.MoDiscoContentProvider;
import org.eclipse.papyrus.views.modelexplorer.MoDiscoLabelProvider;
import org.eclipse.papyrus.views.modelexplorer.ModelExplorerPageBookView;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;


public class PapyrusDocumentationPartHandler implements IDocumentationPartHandler {

	public boolean canHandlePart(IWorkbenchPart part) {
		return part instanceof CoreMultiDiagramEditor || part instanceof ModelExplorerPageBookView;
	}

	public IDocumentationManager getDocumentationManager() {
		return DocumentationManager.getInstance();
	}

	public void executeCommand(IWorkbenchPart part, Command cmd) {
		CoreMultiDiagramEditor editor = getPapyrusEditor(part);
		if(editor != null && cmd != null) {
			TransactionalEditingDomain domain = (TransactionalEditingDomain)editor.getEditingDomain();
			domain.getCommandStack().execute(cmd);
		}
	}

	public EObject getAssociatedDiagram(IWorkbenchPart part, EObject eObject) {
		if(eObject instanceof Diagram) {
			return null;
		}

		if(eObject != null) {
			if(part instanceof IDiagramWorkbenchPart) {
				Diagram currentDiagram = ((IDiagramWorkbenchPart)part).getDiagram();
				if(currentDiagram != null && eObject.equals(currentDiagram.getElement())) {
					return currentDiagram;
				}
			}
		}

		return null;
	}

	public void openElement(IWorkbenchPart part, URI elementUri) {
		try {
			CoreMultiDiagramEditor editor = getPapyrusEditor(part);
			if(editor != null) {
				EditingDomain ed = editor.getEditingDomain();
				if(ed != null) {
					EObject eObject = ed.getResourceSet().getEObject(elementUri, false);
					Diagram diagram = null;
					if(eObject instanceof Diagram) {
						diagram = (Diagram)eObject;
					} else {
						List<Diagram> diagrams = DiagramsUtil.getAssociatedDiagrams(eObject, null);
						if(!diagrams.isEmpty()) {
							diagram = diagrams.get(0);
						}
					}
					if(diagram != null) {
						IPageMngr pageMngr = (IPageMngr)editor.getAdapter(IPageMngr.class);
						if(pageMngr != null) {
							if(pageMngr.isOpen(diagram)) {
								pageMngr.closePage(diagram);
							}
							pageMngr.openPage(diagram);
						}
					}
				}
			}
		} catch (Exception e) {
		}
	}

	public EObject openElementSelectionDialog(IWorkbenchPart part) {
		Object selectedElement = null;
		CoreMultiDiagramEditor editor = getPapyrusEditor(part);
		if(editor != null) {
			ISelectionStatusValidator validator = new ISelectionStatusValidator() {

				public IStatus validate(Object[] selectedElements) {
					boolean enableOK = false;
					if(selectedElements.length == 1) {
						Object selectedElement = selectedElements[0];
						if(adapt(selectedElement) != null) {
							enableOK = true;
						}
					}

					String msg = "";
					if(enableOK == false) {
						msg = "Only one EObject can be selected";
					}
					return enableOK ? new Status(IStatus.OK, "org.eclipse.emf.common.ui", 0, msg, null) : new Status(IStatus.ERROR, "org.eclipse.emf.common.ui", 0, msg, null);
				}
			};
			selectedElement = SelectResourceDialog.openElementSelection(editor.getServicesRegistry(), new MoDiscoLabelProvider(), new MoDiscoContentProvider(), validator, null, true);
		}
		return adapt(selectedElement);
	}

	public boolean isReadOnly(IWorkbenchPart part, EObject eObject) {
		CoreMultiDiagramEditor editor = getPapyrusEditor(part);
		if(editor != null && eObject != null) {
			TransactionalEditingDomain domain = (TransactionalEditingDomain)editor.getEditingDomain();
			return domain.isReadOnly(eObject.eResource());
		}
		return false;
	}

	private static EObject adapt(Object obj) {
		if(obj instanceof EObject) {
			return (EObject)obj;
		} else if(obj instanceof IAdaptable) {
			EObject adapted = (EObject)((IAdaptable)obj).getAdapter(EObject.class);
			return adapted;
		}
		return null;
	}

	private static CoreMultiDiagramEditor getPapyrusEditor(IWorkbenchPart part) {
		if(part instanceof CoreMultiDiagramEditor) {
			return (CoreMultiDiagramEditor)part;
		}
		IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if(activeEditor instanceof CoreMultiDiagramEditor) {
			return (CoreMultiDiagramEditor)activeEditor;
		}
		return null;
	}
}

Back to the top