Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: df76d78fa31d064b542fe518c53c4dc4a3ebee5a (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
273
274
275
276
277
/*****************************************************************************
 * Copyright (c) 2016 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:
 *   CEA LIST - Initial API and implementation
 *   Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.part;

import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.KeyHandler;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.gmfdiag.common.SynchronizableGmfDiagramEditor;
import org.eclipse.papyrus.infra.gmfdiag.common.helper.DiagramHelper;
import org.eclipse.papyrus.infra.internationalization.InternationalizationPackage;
import org.eclipse.papyrus.infra.internationalization.common.editor.IInternationalizationEditor;
import org.eclipse.papyrus.infra.internationalization.utils.utils.LabelInternationalization;
import org.eclipse.papyrus.infra.internationalization.utils.utils.LabelInternationalizationUtils;
import org.eclipse.papyrus.infra.ui.lifecycleevents.ISaveAndDirtyService;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.contexts.IContextService;

/**
 * Common ancestor of GMF based editors for UML. This class allows to declare
 * stuff commons to all this kind of editors.
 *
 * @author cedric dumoulin
 *
 */
public class UmlGmfDiagramEditor extends SynchronizableGmfDiagramEditor implements IInternationalizationEditor {

	/**
	 * The associated Diagram.
	 */
	private Diagram diagram;

	/**
	 * Object used to synchronize the name of the editor with the name of the
	 * diagram.
	 */
	private PartNameSynchronizer partNameSynchronizer;

	/**
	 * service registry of the backbone
	 */
	private ServicesRegistry servicesRegistry;

	/**
	 * Constructor.
	 *
	 * @param servicesRegistry
	 * @param diagram
	 * @throws ServiceException
	 */
	public UmlGmfDiagramEditor(ServicesRegistry servicesRegistry, Diagram diagram) throws ServiceException {
		super(true);
		this.diagram = diagram;
		this.servicesRegistry = servicesRegistry;
		// Install synchronizer
		partNameSynchronizer = new PartNameSynchronizer(diagram);

		// Need to manage the part label synchronizer for the table labels
		LabelInternationalizationUtils.managePartLabelSynchronizer(diagram, this);

		// Register this part to the ISaveAndDirtyService.
		// This will allows to be notified of saveAs events, and the isDirty
		// flag will be taken into
		// account.
		ISaveAndDirtyService saveAndDirtyService = servicesRegistry.getService(ISaveAndDirtyService.class);
		saveAndDirtyService.registerIsaveablePart(this);
	}

	/**
	 * Dispose services used in this part.
	 *
	 * @see org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor#dispose()
	 *
	 */
	@Override
	public void dispose() {
		this.setUndoContext(new IUndoContext() {

			@Override
			public String getLabel() {
				return "Disposed undo context";
			}

			@Override
			public boolean matches(IUndoContext context) {
				return false;
			}

		}); // Avoid disposing the shared UndoContext when this nestedEditor is dispose
		// Super.dispose() will try to dispose the IUndoContext

		super.dispose();

		ISaveAndDirtyService saveAndDirtyService;
		try {
			saveAndDirtyService = servicesRegistry.getService(ISaveAndDirtyService.class);
			saveAndDirtyService.removeIsaveablePart(this);

		} catch (ServiceException e) {
			// the service can't be found. Maybe it is already disposed.
			// Do nothing
		}

		partNameSynchronizer = null;
		diagram = null;
		servicesRegistry = null;
	}

	@Override
	public Object getAdapter(Class type) {
		if (type == ServicesRegistry.class) {
			return servicesRegistry;
		}
		return super.getAdapter(type);
	}

	@Override
	protected void stopListening() {
		super.stopListening();
	}

	/**
	 *
	 * @return the backbone service registry. it cannot return null.
	 */
	public ServicesRegistry getServicesRegistry() {
		return servicesRegistry;
	}

	/**
	 * Set the associated Diagram.
	 */
	public void setDiagram(Diagram diagram) {
		this.diagram = diagram;
		partNameSynchronizer.setDiagram(diagram);
	}

	/**
	 * Get the associated Diagram
	 */
	@Override
	public Diagram getDiagram() {
		return diagram;
	}

	/**
	 *
	 * @see org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor#getKeyHandler()
	 *
	 * @return
	 */
	@Override
	protected KeyHandler getKeyHandler() {
		// we remove all keybinding provided by GMF
		KeyHandler keyHandler = new KeyHandler();
		return keyHandler;
	}

	/**
	 * A class taking in charge the synchronization of the partName and the
	 * diagram name. When diagram name change, the other is automatically
	 * updated.
	 *
	 * @author cedric dumoulin
	 *
	 */
	public class PartNameSynchronizer {

		Diagram diagram;

		/**
		 * Listener on diagram name change.
		 */
		private Adapter diagramNameListener = new Adapter() {

			@Override
			public void notifyChanged(Notification notification) {
				if (notification.getFeatureID(Diagram.class) == NotationPackage.DIAGRAM__NAME && notification.getNotifier() == diagram) {
					setPartName(LabelInternationalization.getInstance().getDiagramLabel(diagram));
				} else if (notification.getFeature() == InternationalizationPackage.eINSTANCE.getInternationalizationEntry_Value() && notification.getNotifier() == diagram) {
					setPartName(LabelInternationalization.getInstance().getDiagramLabel(diagram));
				}

			}

			@Override
			public Notifier getTarget() {
				return null;
			}

			@Override
			public void setTarget(Notifier newTarget) {
			}

			@Override
			public boolean isAdapterForType(Object type) {
				return false;
			}

		};

		/**
		 *
		 * Constructor.
		 *
		 * @param diagram
		 */
		PartNameSynchronizer(Diagram diagram) {
			setDiagram(diagram);
		}

		/**
		 * Change the associated diagram.
		 *
		 * @param diagram
		 */
		public void setDiagram(Diagram diagram) {
			// Remove from old diagram, if any
			if (this.diagram != null) {
				diagram.eAdapters().remove(diagramNameListener);
			}

			// Set new Diagram
			this.diagram = diagram;
			// Set editor name
			setPartName(LabelInternationalization.getInstance().getDiagramLabel(diagram));
			// Listen to name change
			diagram.eAdapters().add(diagramNameListener);
		}
	}

	@Override
	public void createPartControl(Composite parent) {
		IContextService contextService = (IContextService) getSite().getService(IContextService.class);
		// FIXME : before Eclipse Juno, this line was not necessary
		// see bug 367816 and bug 382218
		contextService.activateContext("org.eclipse.gmf.runtime.diagram.ui.diagramContext"); //$NON-NLS-1$
		super.createPartControl(parent);

	}

	/**
	 * @see org.eclipse.papyrus.infra.internationalization.common.editor.IInternationalizationEditor#modifyPartName(java.lang.String)
	 *
	 * @param name
	 */
	@Override
	public void modifyPartName(final String name) {
		setPartName(name);
	}
	
	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.infra.internationalization.common.editor.IInternationalizationEditor#refreshEditorPart()
	 */
	@Override
	public void refreshEditorPart(){
		DiagramHelper.forceRefresh(getDiagramEditPart());
	}
}

Back to the top