Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c25947a78b55b4a70bf2b016ad0f813250faa3d6 (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
/*****************************************************************************
 * Copyright (c) 2008 CEA LIST.
 *
 * 
 * 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.emftree;

import java.util.EventObject;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.common.command.CommandStackListener;
import org.eclipse.emf.common.ui.viewer.IViewerProvider;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.papyrus.infra.core.editor.BackboneException;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.ide.IGotoMarker;
import org.eclipse.ui.views.properties.IPropertySheetPage;

/**
 * This is an example of a EMF UML model editor. <!-- begin-user-doc --> <!-- end-user-doc -->
 * 
 * @generated
 */
public class UMLEditor extends org.eclipse.uml2.uml.editor.presentation.UMLEditor implements IEditingDomainProvider, ISelectionProvider, IMenuListener, IViewerProvider, IGotoMarker {

	/**
	 * ServiceRegistry used to retrieve needed services. This registry is provided by the multieditor.
	 */
	protected ServicesRegistry servicesRegistry;

	protected CommandStackListener commandStackListener;

	/**
	 * 
	 * Constructor. Create an DiEditor using the {@link ServicesRegistry}.
	 * 
	 * @param servicesRegistry
	 */
	public UMLEditor(ServicesRegistry servicesRegistry) throws ServiceException, BackboneException {
		this.servicesRegistry = servicesRegistry;
		initEditor();
	}

	/**
	 * Init the editor from the specified context.
	 * 
	 * @param editorContext
	 * @throws ServiceException
	 */
	private void initEditor() throws ServiceException {

		TransactionalEditingDomain papyrusEditingDomain = servicesRegistry.getService(TransactionalEditingDomain.class);
		CommandStack commandStack = papyrusEditingDomain.getCommandStack();

		// Add a listener to set the most recent command's affected objects to be the selection of
		// the viewer with focus.
		//

		commandStackListener = new CommandStackListener() {

			public void commandStackChanged(final EventObject event) {
				if(getContainer().isDisposed()) {
					return;
				}
				getContainer().getDisplay().asyncExec(new Runnable() {

					public void run() {
						firePropertyChange(IEditorPart.PROP_DIRTY);

						// Try to select the affected objects.
						//
						Command mostRecentCommand = ((CommandStack)event.getSource()).getMostRecentCommand();
						if(mostRecentCommand != null) {
							setSelectionToViewer(mostRecentCommand.getAffectedObjects());
						}
						if(propertySheetPage != null && !propertySheetPage.getControl().isDisposed()) {
							propertySheetPage.refresh();
						}
					}
				});
			}

		};

		commandStack.addCommandStackListener(commandStackListener);

		// Create the editing domain with a special command stack.
		//
		// Set the resourceSet wth our own resourceSet.
		// editingDomain = new AdapterFactoryEditingDomain(adapterFactory, commandStack, new
		// HashMap<Resource, Boolean>());

		ResourceSet resourceSet = papyrusEditingDomain.getResourceSet();
		editingDomain = new AdapterFactoryEditingDomain(adapterFactory, commandStack, resourceSet);

		// editingDomain = editorContext.getTransactionalEditingDomain();
		// editingDomain.setResourceToReadOnlyMap(new HashMap<Resource, Boolean>());
	}

	/**
	 * This is the method called to load a resource into the editing domain's resource set based on
	 * the editor's input. Put the existing model into the editingDomain. <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void createModel() {
		// model is already created and loaded. Do nothing.
	}

	/**
	 * This is for implementing {@link IEditorPart} and simply saves the model file. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void doSave(IProgressMonitor progressMonitor) {
	}

	/**
	 * This also changes the editor's input. <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void doSaveAs() {
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	protected void doSaveAs(URI uri, IEditorInput editorInput) {
	}

	@Override
	public void init(IEditorSite site, IEditorInput editorInput) {
		System.out.println(this.getClass().getSimpleName() + ".init()");
		super.init(site, editorInput);
		setPartName("emf tree");
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public IActionBars getActionBars() {
		try {
			return getActionBarContributor().getActionBars();
		} catch (NullPointerException e) {
			// If we are nested, we have not access to ActionBarContributor()
			return null;
		}
	}

	@Override
	public void dispose() {
		//super.dispose(); //FIXME: Fails on a ClassCastException: The ActionBarContributor is not accessible
		try {
			TransactionalEditingDomain papyrusEditingDomain = servicesRegistry.getService(TransactionalEditingDomain.class);
			CommandStack commandStack = papyrusEditingDomain.getCommandStack();
			commandStack.removeCommandStackListener(commandStackListener);
		} catch (ServiceException e) {
			Activator.log.error(e);
		}
	}

	/**
	 * Has we have no actionBarContributor, we can't get the status line. So skip this method.
	 */
	@Override
	public void setStatusLineManager(ISelection selection) {

	}

	/**
	 * Disable the native property sheet page.
	 */
	@Override
	public IPropertySheetPage getPropertySheetPage() {

		return null;
	}
}

Back to the top