Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bc7175f0d1eb430e4542faee07155cc89f6adbd8 (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
/*****************************************************************************
 * Copyright (c) 2015 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.dnd.strategy;

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

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementMatcher;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.IHintedType;
import org.eclipse.gmf.runtime.emf.type.core.ISpecializationType;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.commands.wrappers.CommandProxyWithResult;
import org.eclipse.papyrus.commands.wrappers.GMFtoGEFCommandWrapper;
import org.eclipse.papyrus.infra.gmfdiag.common.expansion.ChildrenListRepresentation;
import org.eclipse.papyrus.infra.gmfdiag.common.expansion.DiagramExpansionSingleton;
import org.eclipse.papyrus.infra.gmfdiag.common.expansion.DiagramExpansionsRegistry;
import org.eclipse.papyrus.infra.gmfdiag.common.expansionmodel.AbstractRepresentation;
import org.eclipse.papyrus.infra.gmfdiag.common.expansionmodel.Representation;
import org.eclipse.papyrus.infra.gmfdiag.dnd.Activator;
import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype;
import org.eclipse.swt.graphics.Image;

/**
 * This strategy has been added in order allow dropping new element done by using expansion model
 * See Requirement #org.eclipse.papyrus.infra.gmfdiag.expansion.Req_020
 *
 */
public class ExpansionElementDropStrategy extends TransactionalDropStrategy {

	private DiagramExpansionsRegistry diagramExpansionRegistry;
	private static final String DEBUG_PREFIX = "[EXPANSION_DIAGRAM]";
	private static final boolean DEBUG_EXPANSION = "true".equalsIgnoreCase(Platform.getDebugOption(
			"org.eclipse.papyrus.infra.gmfdiag.common/debug/expansion"));

	/**
	 * Constructor.
	 *
	 */
	public ExpansionElementDropStrategy() {
		this.diagramExpansionRegistry = DiagramExpansionSingleton.getInstance().getDiagramExpansionRegistry();
	}
	public String getLabel() {
		return "Expansion element drag and drop";
	}

	public String getID() {
		return "org.eclipse.papyrus.infra.gmfdiag.dnd.expansiondropsteategy";
	}

	public String getDescription() {
		return "This strategy is used to allow dropping of expansion of elements in extended diagrams.";
	}

	public Image getImage() {
		return null;
	}

	public int getPriority() {
		return 0;
	}

	public void setOptions(Map<String, Object> options) {
		// Nothing
	}
	/**
	 * get the diagram type from a view.
	 * @param currentView the current view
	 * @return the diagram type it can be also a view point
	 */
	protected String getDiagramType(View currentView) {
		Diagram diagram=currentView.getDiagram();
		String currentDiagramType=null;
		ViewPrototype viewPrototype=org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils.getPrototype(diagram);
		if(viewPrototype!=null){
			currentDiagramType=viewPrototype.getLabel();
		}
		else{
			currentDiagramType=diagram.getType();
		}
		return currentDiagramType;
	}
	@Override
	public Command doGetCommand(Request request, final EditPart targetEditPart) {

		CompositeCommand cc = new CompositeCommand(getLabel());
		if(targetEditPart instanceof GraphicalEditPart){
			IGraphicalEditPart graphicalEditPart= (IGraphicalEditPart)targetEditPart;
			String diagramType= getDiagramType(graphicalEditPart.getNotationView());
			ChildrenListRepresentation listRepresentation=diagramExpansionRegistry.mapChildreen.get(diagramType);
			//to the current diagram, a expansion is added. 
			if(listRepresentation==null){
				return null;
			}
			//look for all possible children for the current target.
			List<String>childrenList =listRepresentation.parentChildrenRelation.get(graphicalEditPart.getNotationView().getType());
			if(childrenList==null){
				return null;
			}
			List<EObject> sourceElements = getSourceEObjects(request);
			if(sourceElements.size()==0 ){
				return null;
			}
			final List<EObject> valuesToAdd = new ArrayList<EObject>(sourceElements.size());
			//getList of accepted ElementType
			final ArrayList<ISpecializationType> acceptedElementTypes= new ArrayList<ISpecializationType>();


			for (String posibleID : childrenList) {
				AbstractRepresentation abstractRepresentation=listRepresentation.IDMap.get(posibleID);
				if( abstractRepresentation instanceof Representation){
					String elementTypeID=((Representation)abstractRepresentation).getGraphicalElementType();
					if( elementTypeID!=null && elementTypeID!=""){
						final IElementType elementType=ElementTypeRegistry.getInstance().getType(elementTypeID);
						if( elementType instanceof ISpecializationType){
							acceptedElementTypes.add((ISpecializationType)elementType);
						}
					}
				}
			}
			if(DEBUG_EXPANSION){
				Activator.log.debug(DEBUG_PREFIX+"try to drop "+ sourceElements+" inside "+graphicalEditPart.getNotationView().getType()+ " accepts "+childrenList);
			}
			// get the sub list of accepted source element that match to elementType
			for (EObject sourceElement : sourceElements) {
				Command cmd=null;
				int acceptedElementTypesIndex=0;
				while (cmd==null &&acceptedElementTypesIndex<acceptedElementTypes.size()){
					final ISpecializationType iSpecializationType = acceptedElementTypes.get(acceptedElementTypesIndex);
					IElementMatcher matcher=iSpecializationType.getMatcher();
					if(matcher!=null && matcher.matches(sourceElement)){
						valuesToAdd.add(sourceElement);
						if(DEBUG_EXPANSION){
							Activator.log.debug(DEBUG_PREFIX+"try to drop command created for "+ sourceElement+ " "+iSpecializationType);
						}
						cmd= new Command() {
							@Override
							public void execute() {
								if( iSpecializationType instanceof IHintedType){
									ViewService.createNode(((GraphicalEditPart) targetEditPart).getNotationView(),valuesToAdd.get(0), ((IHintedType)iSpecializationType).getSemanticHint(), ((GraphicalEditPart) targetEditPart).getDiagramPreferencesHint());
								}
							}

						};
						cc.add(new CommandProxy( cmd));
					}
					else{
						acceptedElementTypesIndex++;
					}

				}

			}
		}

		return cc.canExecute() ? new ICommandProxy(cc.reduce()) : null;
	}


	public String getCategoryID() {
		return "org.eclipse.papyrus.infra.gmfdiag.dnd.expansiondropsteategy";
	}

	public String getCategoryLabel() {
		return "Expansion element drag and drop";
	}
}

Back to the top