Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab7c11522719221161ca6f371ba471909d220923 (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
package org.eclipse.papyrus.connectionpointreference.editor.xtext.ui.contributions;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.connectionpointreference.editor.xtext.uMLConnectionPointReference.ConnectionPointReferenceRule;
import org.eclipse.papyrus.connectionpointreference.editor.xtext.ui.internal.UMLConnectionPointReferenceActivator;
import org.eclipse.papyrus.connectionpointreference.editor.xtext.validation.UMLConnectionPointReferenceJavaValidator;
import org.eclipse.papyrus.core.utils.EditorUtils;
import org.eclipse.papyrus.extensionpoints.editors.ui.IPopupEditorHelper;
import org.eclipse.uml2.uml.ConnectionPointReference;
import org.eclipse.uml2.uml.Pseudostate;
import org.eclipse.xtext.gmf.glue.PopupEditorConfiguration;
import org.eclipse.xtext.gmf.glue.edit.part.DefaultXtextSemanticValidator;
import org.eclipse.xtext.gmf.glue.edit.part.IXtextEMFReconciler;

import com.google.inject.Injector;

public class ConnectionPointReferencePopupEditorConfiguration extends PopupEditorConfiguration {

	private ConnectionPointReference connectionPoint ;

	private List<Pseudostate> newEntries = new ArrayList<Pseudostate>() ;
	private List<Pseudostate> newExits = new ArrayList<Pseudostate>() ;
	
	public ConnectionPointReferencePopupEditorConfiguration() {
		super() ;
	}

	@Override
	public String getTextToEdit(Object editedObject) {
		if (editedObject instanceof ConnectionPointReference) {
			ConnectionPointReference ref = (ConnectionPointReference)editedObject ;
			String label = "" ;
			if (!ref.getEntries().isEmpty()) {
				label += "entry " ;
				boolean first = true ;
				for (Pseudostate p : ref.getEntries()) {
					if (!first) {
						label += ", " ;
					}
					else {
						first = false ;
					}
					label += p.getName() ;
				}
			}
			else if (!ref.getExits().isEmpty()) {
				label += "exit " ;
				boolean first = true ;
				for (Pseudostate p : ref.getExits()) {
					if (!first) {
						label += ", " ;
					}
					else {
						first = false ;
					}
					label += p.getName() ;
				}
			}
			return label ;
		}
		return "not a ConnectionPointReference" ;
	}

	@Override
	public IPopupEditorHelper createPopupEditorHelper(Object editPart) {
		// resolves the edit part, and the associated semantic element
		IGraphicalEditPart graphicalEditPart = null;
		if(!(editPart instanceof IGraphicalEditPart))
			return null;
		graphicalEditPart = (IGraphicalEditPart)editPart;
		if(!(graphicalEditPart.resolveSemanticElement() instanceof ConnectionPointReference))
			return null;
		connectionPoint = (ConnectionPointReference)graphicalEditPart.resolveSemanticElement();

		UMLConnectionPointReferenceJavaValidator.init(connectionPoint) ;
		
		// retrieves the XText injector
		Injector injector = UMLConnectionPointReferenceActivator.getInstance().getInjector("org.eclipse.papyrus.connectionpointreference.editor.xtext.UMLConnectionPointReference");

		// builds the text content and extension for a temporary file, to be edited by the xtext editor
		String textToEdit = "" + this.getTextToEdit(graphicalEditPart.resolveSemanticElement());
		String fileExtension = "" + ".conncectionpointreference";

		// builds a new IXtextEMFReconciler.
		// Its purpose is to extract any relevant information from the textual specification,
		// and then merge it in the context UML model if necessary
		IXtextEMFReconciler reconciler = new IXtextEMFReconciler() {

			public void reconcile(EObject modelObject, EObject xtextObject) {
				
				newEntries = new ArrayList<Pseudostate>() ;
				newExits = new ArrayList<Pseudostate>() ;
				
				ConnectionPointReferenceRule rule = (ConnectionPointReferenceRule)xtextObject ;
				if (! rule.getEntry().isEmpty()) {
					newEntries.addAll(rule.getEntry()) ;
				}
				else if (! rule.getExit().isEmpty()) {
					newExits.addAll(rule.getExit()) ;
				}
				
				// Creates and executes the update command
				UpdateConnectionPointReferenceCommand updateCommand = new UpdateConnectionPointReferenceCommand(connectionPoint);
				
				TransactionalEditingDomain dom = EditorUtils.getTransactionalEditingDomain();
				dom.getCommandStack().execute(new GMFtoEMFCommandWrapper(updateCommand));
			}
		};
		return super.createPopupEditorHelper(graphicalEditPart, injector, reconciler, textToEdit, fileExtension, new DefaultXtextSemanticValidator());
	}
	
	/**
	 * @author CEA LIST
	 *
	 * A command for updating the context UML model
	 */
	protected class UpdateConnectionPointReferenceCommand extends AbstractTransactionalCommand {

		private ConnectionPointReference connectionPointReference ;
		
		/* (non-Javadoc)
		 * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
		 */
		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor arg0,
				IAdaptable arg1) throws ExecutionException {
			
			connectionPointReference.getEntries().clear() ;
			connectionPointReference.getExits().clear() ;
			
			connectionPointReference.getEntries().addAll(newEntries) ;
			connectionPointReference.getExits().addAll(newExits) ;
			
			return CommandResult.newOKCommandResult(connectionPointReference);
		}
		
		public UpdateConnectionPointReferenceCommand(ConnectionPointReference connectionPointReference) {
			super(EditorUtils.getTransactionalEditingDomain(), 
					"ConnectionPointReference Update", 
					getWorkspaceFiles(connectionPointReference));
			this.connectionPointReference = connectionPointReference ;
		}	
	}
}

Back to the top