Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 590c26f7440e0eeda149cb7dc6cfd9b1c43cd7fa (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/**
 * Copyright (c) 2006, 2014 Borland Software Corporation, CEA, and others
 * 
 * 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:
 *    Alexander Shatalin (Borland) - initial API and implementation
 *    Michael Golubev (Montages) - #386838 - migrate to Xtend2
 *    Emilien Perico (Atos Origin) - add code to refactor some classes
 *    Christian W. Damus (CEA) - bug 430648
 *    Christian W. Damus (CEA) - bug 431023
 */
package aspects.xpt.editor

import aspects.xpt.navigator.NavigatorLinkHelper
import com.google.inject.Inject
import com.google.inject.Singleton
import org.eclipse.gmf.codegen.gmfgen.GenEditorView
import org.eclipse.gmf.codegen.gmfgen.GenNavigator
import org.eclipse.gmf.codegen.gmfgen.Palette
import xpt.Common
import xpt.navigator.Utils_qvto
import xpt.CodeStyle

@Singleton class Editor extends xpt.editor.Editor {
	@Inject extension Common;
	@Inject NavigatorLinkHelper xptNavigatorLinkHelper;
	@Inject extension Utils_qvto;
	@Inject extension CodeStyle

	override extendsList(GenEditorView it) '''extends org.eclipse.papyrus.uml.diagram.common.part.UmlGmfDiagramEditor'''

	override attributes(GenEditorView it) '''
	«generatedMemberComment»
	public static final String ID = "«ID»"; «nonNLS»
	
	«generatedMemberComment»
public static final String CONTEXT_ID = "«contextID»"; «nonNLS»

	
	«««	Documentation. adds listener for papyrus editors
	«generatedMemberComment»
	private org.eclipse.gef.KeyHandler paletteKeyHandler = null;

	«generatedMemberComment»
	private org.eclipse.swt.events.MouseListener paletteMouseListener = null;

	«««	Helps to handle correctly the dirty state
	«generatedMemberComment»
	private org.eclipse.papyrus.commands.util.OperationHistoryDirtyState dirtyState;
	
	«generatedMemberComment»
	private org.eclipse.emf.transaction.TransactionalEditingDomain editingDomain;

	«generatedMemberComment»
    private org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocumentProvider documentProvider;
	'''
	
	override constructor(GenEditorView it) '''
		«generatedMemberComment»
		public «className»(org.eclipse.papyrus.infra.core.services.ServicesRegistry servicesRegistry, org.eclipse.gmf.runtime.notation.Diagram diagram) throws org.eclipse.papyrus.infra.core.services.ServiceException{
		super(servicesRegistry, diagram);

		«««	Documentation. adds listener for papyrus palette service
		// adds a listener to the palette service, which reacts to palette customizations
		org.eclipse.papyrus.uml.diagram.common.service.PapyrusPaletteService.getInstance().addProviderChangeListener(this);
		
		«««Share the same editing domain
		// Share the same editing provider
		editingDomain = servicesRegistry.getService(org.eclipse.emf.transaction.TransactionalEditingDomain.class);
		documentProvider = new org.eclipse.papyrus.infra.gmfdiag.common.GmfMultiDiagramDocumentProvider(editingDomain);
		
		// overrides editing domain created by super constructor
		setDocumentProvider(documentProvider);
		
		«««end of listeners addition
		}
	'''
	
		override getNavigatorSelection(GenNavigator it) '''
		
		«generatedMemberComment»
		private org.eclipse.jface.viewers.ISelection getNavigatorSelection() {
			«IF getDiagramTopReference(it) !=null»
			org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument document = getDiagramDocument();
			«ENDIF»
			«xptNavigatorLinkHelper.findSelectionBody(it)»
		}
	'''

override createPaletteRoot (Palette it)'''
	«generatedMemberComment»
	protected org.eclipse.gef.palette.PaletteRoot createPaletteRoot(org.eclipse.gef.palette.PaletteRoot existingPaletteRoot) {
		org.eclipse.gef.palette.PaletteRoot paletteRoot;
		if (existingPaletteRoot == null) {
			paletteRoot = org.eclipse.papyrus.uml.diagram.common.service.PapyrusPaletteService.getInstance().createPalette(this, getDefaultPaletteContent());
		} else {
			org.eclipse.papyrus.uml.diagram.common.service.PapyrusPaletteService.getInstance().updatePalette(existingPaletteRoot, this, getDefaultPaletteContent());
			paletteRoot = existingPaletteRoot;
		}
		applyCustomizationsToPalette(paletteRoot);
		return paletteRoot;
	}
'''

def createPaletteCustomizer (GenEditorView it)'''
	«generatedMemberComment»
	protected org.eclipse.gef.ui.palette.PaletteCustomizer createPaletteCustomizer() {
		return new org.eclipse.papyrus.uml.diagram.common.part.PapyrusPaletteCustomizer(getPreferenceStore());
	}
'''

override additions (GenEditorView it)'''
	
	«createEditingDomain(it)»
	
	« configureDiagramEditDomain(it)»
	
	« doSave(it)»
	
	« getDirtyState(it)»
	
	« setUndoContext(it)»
	
	« isDirty(it)»
	
	«««Documentation. adds method to handle palette changes
	« handlePaletteChange(it)»
	
	« dispose(it)»
	
	« getPaletteViewer(it)»
	
	«««	Documentation: (RS) advanced customization abilities
	«««	« createPaletteCustomizer»
	
	
	« constructPaletteViewer(it)»
	
	« createPaletteviewerProvider(it)»
	
	«getGraphicalViewer(it)»
	
	«initializeGraphicalViewer(it)»
	
	«selectionChanged(it)»
'''

def handlePaletteChange (GenEditorView it) '''
	«generatedMemberComment»
	public void providerChanged(org.eclipse.gmf.runtime.common.core.service.ProviderChangeEvent event) {
		// update the palette if the palette service has changed
		if (org.eclipse.papyrus.uml.diagram.common.service.PapyrusPaletteService.getInstance().equals(event.getSource())) {
			org.eclipse.papyrus.uml.diagram.common.service.PapyrusPaletteService.getInstance().updatePalette(getPaletteViewer().getPaletteRoot(), this,
					getDefaultPaletteContent());
		}
	}
'''

def constructPaletteViewer (GenEditorView it) '''
	«generatedMemberComment»
	protected org.eclipse.gef.ui.palette.PaletteViewer constructPaletteViewer() {
		return new org.eclipse.papyrus.uml.diagram.common.part.PapyrusPaletteViewer();
	}
'''

override dispose(GenEditorView it)'''
	«generatedMemberComment»
	public void dispose() {
		// remove palette service listener
		// remove preference listener
		org.eclipse.papyrus.uml.diagram.common.service.PapyrusPaletteService.getInstance().removeProviderChangeListener(this);
		
		if(dirtyState != null) {
		    dirtyState.dispose();
		    dirtyState = null;
		}
		
		super.dispose();
	}
'''

def getPaletteViewer (GenEditorView it)'''
	«generatedMemberComment»
	protected org.eclipse.gef.ui.palette.PaletteViewer getPaletteViewer() {
		return getEditDomain().getPaletteViewer();
	}
'''	
	
override implementsList(Iterable<String> it)'''
 implements org.eclipse.gmf.runtime.common.core.service.IProviderChangeListener
	«IF  ! it.isEmpty», 
		«FOR string : it  SEPARATOR ', '»
			«implementsListEntry(string)»
		«ENDFOR»
	«ENDIF»
'''

def implementsListEntry (String it)'''«it»'''

def createPaletteviewerProvider (GenEditorView it)'''
«generatedMemberComment»
protected org.eclipse.gef.ui.palette.PaletteViewerProvider createPaletteViewerProvider() {
		getEditDomain().setPaletteRoot(createPaletteRoot(null));
		return new org.eclipse.gef.ui.palette.PaletteViewerProvider(getEditDomain()) {

			/**
			 * Override to provide the additional behavior for the tools. Will intialize with a
			 * PaletteEditPartFactory that has a TrackDragger that understand how to handle the
			 * mouseDoubleClick event for shape creation tools. Also will initialize the palette
			 * with a defaultTool that is the SelectToolEx that undestands how to handle the enter
			 * key which will result in the creation of the shape also.
			 */
			«overrideC(it.editorGen.diagram)»
			protected void configurePaletteViewer(org.eclipse.gef.ui.palette.PaletteViewer viewer) {
				super.configurePaletteViewer(viewer);

				// customize menu...
				viewer.setContextMenu(new org.eclipse.papyrus.uml.diagram.common.part.PapyrusPaletteContextMenuProvider(viewer));

				viewer.getKeyHandler().setParent(getPaletteKeyHandler());
				viewer.getControl().addMouseListener(getPaletteMouseListener());

				// Add a transfer drag target listener that is supported on
				// palette template entries whose template is a creation tool.
				// This will enable drag and drop of the palette shape creation
				// tools.
				viewer.addDragSourceListener(new org.eclipse.gmf.runtime.diagram.ui.internal.parts.PaletteToolTransferDragSourceListener(viewer));
				viewer.setCustomizer(createPaletteCustomizer());
			}

			«overrideC(it.editorGen.diagram)»
			public org.eclipse.gef.ui.palette.PaletteViewer createPaletteViewer(org.eclipse.swt.widgets.Composite parent) {
				org.eclipse.gef.ui.palette.PaletteViewer pViewer = constructPaletteViewer();
				pViewer.createControl(parent);
				configurePaletteViewer(pViewer);
				hookPaletteViewer(pViewer);
				return pViewer;
			}

			/**
			 * @return Palette Key Handler for the palette
			 */
			private org.eclipse.gef.KeyHandler getPaletteKeyHandler() {

				if (paletteKeyHandler == null) {

					paletteKeyHandler = new org.eclipse.gef.KeyHandler() {

						/**
						 * Processes a <i>key released </i> event. This method is called by the Tool
						 * whenever a key is released, and the Tool is in the proper state. Override
						 * to support pressing the enter key to create a shape or connection
						 * (between two selected shapes)
						 * 
						 * @param event
						 *            the KeyEvent
						 * @return <code>true</code> if KeyEvent was handled in some way
						 */
						«overrideC(it.editorGen.diagram)»
						public boolean keyReleased(org.eclipse.swt.events.KeyEvent event) {

							if (event.keyCode == org.eclipse.swt.SWT.Selection) {

								org.eclipse.gef.Tool tool = getPaletteViewer().getActiveTool().createTool();

								if (toolSupportsAccessibility(tool)) {

									tool.keyUp(event, getDiagramGraphicalViewer());

									// deactivate current selection
									getPaletteViewer().setActiveTool(null);

									return true;
								}

							}
							return super.keyReleased(event);
						}

					};

				}
				return paletteKeyHandler;
			}

			/**
			 * @return Palette Mouse listener for the palette
			 */
			private org.eclipse.swt.events.MouseListener getPaletteMouseListener() {

				if (paletteMouseListener == null) {

					paletteMouseListener = new org.eclipse.swt.events.MouseListener() {

						/**
						 * Flag to indicate that the current active tool should be cleared after a
						 * mouse double-click event.
						 */
						private boolean clearActiveTool = false;

						/**
						 * Override to support double-clicking a palette tool entry to create a
						 * shape or connection (between two selected shapes).
						 * 
						 * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
						 */
						«overrideI(it.editorGen.diagram)»
						public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
							org.eclipse.gef.Tool tool = getPaletteViewer().getActiveTool().createTool();

							if (toolSupportsAccessibility(tool)) {

								tool.setViewer(getDiagramGraphicalViewer());
								tool.setEditDomain(getDiagramGraphicalViewer().getEditDomain());
								tool.mouseDoubleClick(e, getDiagramGraphicalViewer());

								// Current active tool should be deactivated,
								// but if it is down here it will get
								// reactivated deep in GEF palette code after
								// receiving mouse up events.
								clearActiveTool = true;
							}
						}

						«overrideI(it.editorGen.diagram)»
						public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
							// do nothing
						}

						«overrideI(it.editorGen.diagram)»
						public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
							// Deactivate current active tool here if a
							// double-click was handled.
							if (clearActiveTool) {
								getPaletteViewer().setActiveTool(null);
								clearActiveTool = false;
							}

						}
					};

				}
				return paletteMouseListener;
			}

		};
	}
'''

//Not used
override performSaveAs (GenEditorView it)'''
	«generatedMemberComment»
protected void performSaveAs(org.eclipse.core.runtime.IProgressMonitor progressMonitor) {
  // Nothing
}
'''

//Share the same editing domain
override getEditingDomain (GenEditorView it)'''
	«generatedMemberComment»
	public org.eclipse.emf.transaction.TransactionalEditingDomain getEditingDomain() {
		return editingDomain;
	}	
'''

def createEditingDomain (GenEditorView it)'''
«generatedMemberComment»
	protected org.eclipse.emf.transaction.TransactionalEditingDomain createEditingDomain() {
		// Already configured
		return editingDomain;
	}
'''


def configureDiagramEditDomain (GenEditorView it)'''
«generatedMemberComment»
	protected void configureDiagramEditDomain() {
		super.configureDiagramEditDomain();
		getDiagramEditDomain().getDiagramCommandStack().addCommandStackListener(new org.eclipse.gef.commands.CommandStackListener() {

			«overrideI(it.editorGen.diagram)»
			public void commandStackChanged(java.util.EventObject event) {
				firePropertyChange( org.eclipse.ui.IEditorPart.PROP_DIRTY);
			}
		});
	}
'''

def doSave (GenEditorView it)'''
«generatedMemberComment»
	public void doSave(org.eclipse.core.runtime.IProgressMonitor progressMonitor) {
		// The saving of the resource is done by the CoreMultiDiagramEditor
		getDirtyState().saved();
	}
'''

def getDirtyState (GenEditorView it)'''
«generatedMemberComment»
    protected org.eclipse.papyrus.commands.util.OperationHistoryDirtyState getDirtyState() {
        if(dirtyState == null) {
            dirtyState = org.eclipse.papyrus.commands.util.OperationHistoryDirtyState.newInstance(getUndoContext(), getOperationHistory());
        }
        return dirtyState;
    }
'''

def setUndoContext (GenEditorView it)'''
«generatedMemberComment»
    protected void setUndoContext(org.eclipse.core.commands.operations.IUndoContext context) {
        if(dirtyState != null) {
            dirtyState.dispose();
            dirtyState = null;
        }
        
        super.setUndoContext(context);
    }
'''

//Fix the dirty state
def isDirty (GenEditorView it)'''
«generatedMemberComment»
	public boolean isDirty() {
		return getDirtyState().isDirty();
	}
'''

//Code refactoring moved in UMLDiagramEditor
override getDocumentProvider (GenEditorView it)'''
	«generatedMemberComment»
	protected final org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocumentProvider getDocumentProvider(org.eclipse.ui.IEditorInput input) {
		return documentProvider;
	}
'''

override setDocumentProvider (GenEditorView it)'''
	«generatedMemberComment»
	protected final void setDocumentProvider(org.eclipse.ui.IEditorInput input) {
		// Already set in the constructor
	}
'''

def getGraphicalViewer (GenEditorView it)'''
«generatedMemberComment»
	@Override
	public org.eclipse.gef.GraphicalViewer getGraphicalViewer() {
		return super.getGraphicalViewer();
	}
'''


override initializeGraphicalViewer (GenEditorView it)'''
«generatedMemberComment»
	@Override
	protected void initializeGraphicalViewer() {
		super.initializeGraphicalViewer();

		// Enable Drop
		getDiagramGraphicalViewer().addDropTargetListener(
				new org.eclipse.papyrus.uml.diagram.common.listeners.DropTargetListener(getDiagramGraphicalViewer(), org.eclipse.jface.util.LocalSelectionTransfer.getTransfer()) {

					@Override
					protected Object getJavaObject(org.eclipse.swt.dnd.TransferData data) {
						// It is usual for the transfer data not to be set because it is available locally
						return LocalSelectionTransfer.getTransfer().getSelection();
					}

					@Override
					protected org.eclipse.emf.transaction.TransactionalEditingDomain getTransactionalEditingDomain() {
						return getEditingDomain();
					}
				});

	}
'''

def selectionChanged (GenEditorView it)'''
«generatedMemberComment»
	@Override
	public void selectionChanged(org.eclipse.ui.IWorkbenchPart part, org.eclipse.jface.viewers.ISelection selection) {
		if (getSite().getPage().getActiveEditor() instanceof org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor) {
			org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor editor = (org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor) getSite().getPage().getActiveEditor();
			// If not the active editor, ignore selection changed.
			if (this.equals(editor.getActiveEditor())) {
				updateActions(getSelectionActions());
				super.selectionChanged(part, selection);
			} else {
				super.selectionChanged(part, selection);
			}
		} else {
			super.selectionChanged(part, selection);
		}
		// from
		// org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor.selectionChanged(IWorkbenchPart,
		// ISelection)
		if (part == this) {
			rebuildStatusLine();
		}
	}
'''

override getContextID (GenEditorView it)'''
	«generatedMemberComment»
protected String getContextID() {
	return CONTEXT_ID;
}
'''

	override getAdapter(GenEditorView it) '''
		«IF !hasPropertySheet(it) || hasNavigator(it)»
			
			«generatedMemberComment»
			@SuppressWarnings("rawtypes")
			public Object getAdapter(Class type) {
				«IF !hasPropertySheet(it)»
					if (type == org.eclipse.ui.views.properties.IPropertySheetPage.class) {
						return null;
					}
				«ENDIF»
				«IF hasNavigator(it)»
					if (type == org.eclipse.ui.part.IShowInTargetList.class) {
						return new org.eclipse.ui.part.IShowInTargetList() {
							
							«overrideI(it.editorGen.diagram)»
							public String[] getShowInTargetIds() {
								return new String[] { org.eclipse.ui.navigator.resources.ProjectExplorer.VIEW_ID };
							}
						};
					}
				«ENDIF»
				return super.getAdapter(type);
			}
		«ENDIF»
	'''

}

Back to the top