Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8c5a3e91c8ad4f45106223ddd4ee10ce9bdad8b6 (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
/*******************************************************************************
 * Copyright (c) 2001, 2006 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsdl.ui.internal.asd.actions;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Polyline;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IASDObject;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IMessageReference;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IOperation;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IParameter;

public class ASDDragReorderAction extends ASDDragAction {
	  protected IASDObject movingChild;
	  protected IASDObject newParent;
	  protected GraphicalEditPart leftSiblingEditPart;
	  protected GraphicalEditPart rightSiblingEditPart;
	  protected IASDObject leftSibElement;
	  protected IASDObject rightSibElement;
	  protected boolean canNeverExecute = false;
	  
	  public ASDDragReorderAction(GraphicalEditPart movingChildEditPart, EditPart pointerEditPart, Point pointerLocation) {
		  super(null);
	  	this.movingChildEditPart = movingChildEditPart;
	  	this.pointerLocation = pointerLocation;

		EditPart possibleParent = getParentEditPart(pointerEditPart);
		
		if (!(possibleParent instanceof GraphicalEditPart)) {
			canNeverExecute = true;
			return;
		}
	  	
		GraphicalEditPart targetParentEditPart = (GraphicalEditPart) possibleParent;
		
		if (pointerEditPart.getParent() != null) {
			// Sort the siblings (and itself)
			List targetEditPartSiblings = pointerEditPart.getParent().getChildren();
			Collections.sort(targetEditPartSiblings, new VerticalEditPartComparator());
	  
			// Get 'left' and 'right' siblings
			leftSiblingEditPart = getVerticalLeftSibling(targetEditPartSiblings, movingChildEditPart, pointerLocation);
			rightSiblingEditPart = getVerticalRightSibling(targetEditPartSiblings, movingChildEditPart, pointerLocation);           	

			if (movingChildEditPart != null)
				movingChild = (IASDObject) movingChildEditPart.getModel();
			if (leftSiblingEditPart != null)
				leftSibElement = (IASDObject) leftSiblingEditPart.getModel();
			if (rightSiblingEditPart != null)
				rightSibElement = (IASDObject) rightSiblingEditPart.getModel();
			
			newParent = (IASDObject) targetParentEditPart.getModel();
		}
		else {
			newParent = (IASDObject) targetParentEditPart.getModel();
			leftSibElement = null;
			rightSibElement= null;
		}
	  }
	            
	  public boolean canExecute()
	  {
	    boolean result = false;
	    
	    if (canNeverExecute || newParent == null || movingChild == null) {
	    	return false;
	    }
	    
	    if (movingChild instanceof IMessageReference) {
	    	IMessageReference messageRef = (IMessageReference) movingChild;
	    	if (messageRef.getKind() == IMessageReference.KIND_INPUT || messageRef.getKind() == IMessageReference.KIND_OUTPUT) {
	    		IMessageReference leftSibMessageRef = (IMessageReference) leftSibElement;

	    		result = true;
	    		if (leftSibMessageRef != null) {
	    			if (leftSibMessageRef.getKind() == IMessageReference.KIND_FAULT) {
	    				result = false;
	    			}
	    		}
	    		if (!(result && newParent.equals(messageRef.getOwnerOperation()))) {
	    			result = false;
	    		}
	    	}
	    }
	    else if (movingChild instanceof IParameter) {
	    	if (newParent instanceof IMessageReference && newParent.equals(((IParameter) movingChild).getOwner())) {
	    		result = true;
	    	}
	    }
	    
	    return result;
	  }           

	  /*
	   * @see IAction#run()
	   */
	  public void run()
	  {                            
//	    beginRecording();
	  	if (canExecute()) {
	  		if (movingChild instanceof IMessageReference) {
	  			Object messageRefOwner = ((IMessageReference) movingChild).getOwnerOperation();
	  			if (messageRefOwner instanceof IOperation) {
	  				IMessageReference leftSib = (IMessageReference) leftSibElement;
	  				IMessageReference rightSib = (IMessageReference) rightSibElement;
	  				IMessageReference movingSib = (IMessageReference) movingChild;
	  				Command command = ((IOperation) messageRefOwner).getReorderMessageReferencesCommand(leftSib, rightSib, movingSib);
	  				command.execute();
	  			}
	  		}
	  		else if (movingChild instanceof IParameter) {
	  			Object paramOwner = ((IParameter) movingChild).getOwner();
	  			if (paramOwner instanceof IMessageReference) {
	  				IParameter leftSib = (IParameter) leftSibElement;
	  				IParameter rightSib = (IParameter) rightSibElement;
	  				IParameter movingSib = (IParameter) movingChild;
	  				Command command = ((IMessageReference) paramOwner).getReorderParametersCommand(leftSib, rightSib, movingSib);
	  				command.execute();
	  			}
	  		}
//	  		wsdlEditor.getSelectionManager().setSelection(new StructuredSelection(movingChild));
	  	}
//	    endRecording();
	  }
	  
	  public IFigure getFeedbackFigure() {
	  	IFigure feedbackFigure = null;
	    if (feedbackFigure == null && movingChildEditPart instanceof GraphicalEditPart)
	    {                                       
	      feedbackFigure = new Polyline();
	      ((Polyline)feedbackFigure).setLineWidth(2);	
	      drawLines((Polyline) feedbackFigure);
	    }
	    
	  	return feedbackFigure;
	  }

	  /* Return a PointList which will be used to add a feedback figure.  The feedback figure will be a
	   * Polyline composed of the Points in the PointList
	   */
	  private PointList drawLines(Polyline polyLine) {
	    PointList pointList = new PointList(); 
	    
	    if (leftSiblingEditPart != null) {
	    	Rectangle leftRectangle = leftSiblingEditPart.getFigure().getBounds();
		   	int xCoord = leftRectangle.x;
		  	int yCoord = leftRectangle.y;
		   	int height = leftRectangle.height;
		   	int width = leftRectangle.width;

		   	// Draw left end line
		   	addLineToPolyline(polyLine, xCoord, yCoord + height + 3, xCoord, yCoord + height - 3);
		   	addLineToPolyline(polyLine, xCoord, yCoord + height - 3, xCoord, yCoord + height);
		   	
		   	// Draw horizontal line
		   	addLineToPolyline(polyLine, xCoord, yCoord + height, xCoord + width, yCoord + height);
		   	
		   	// Draw right end line
		   	addLineToPolyline(polyLine, xCoord + width, yCoord + height, xCoord + width, yCoord + height - 3);
		  	addLineToPolyline(polyLine, xCoord + width, yCoord + height, xCoord + width, yCoord + height + 3);   	
	    }
	    else if (rightSiblingEditPart != null) {
	    	Rectangle rightRectangle = rightSiblingEditPart.getFigure().getBounds();
	    	int xCoord = rightRectangle.x;
	    	int yCoord = rightRectangle.y;
	   		int width = rightRectangle.width;	

	   		// Draw left end line
		   	addLineToPolyline(polyLine, xCoord, yCoord + 3, xCoord, yCoord - 3);
		   	addLineToPolyline(polyLine, xCoord, yCoord - 3, xCoord, yCoord);
		   	
		   	// Draw horizontal line
		   	addLineToPolyline(polyLine, xCoord, yCoord, xCoord + width, yCoord);
		   	
		   	// Draw right end line
		   	addLineToPolyline(polyLine, xCoord + width, yCoord, xCoord + width, yCoord - 3);
		  	addLineToPolyline(polyLine, xCoord + width, yCoord, xCoord + width, yCoord + 3);   
	    }

	    return pointList;
	  }                    
	  
	  protected Polyline addLineToPolyline(Polyline polyline, int x1, int y1, int x2, int y2) {
		polyline.addPoint(new Point(x1, y1));
		polyline.addPoint(new Point(x2, y2));

		return polyline;
	  }                                          
	  
	  /*
	   * Pre-condition: The List siblings should be sorted by it's y-coordinate 
	   */
	  private GraphicalEditPart getVerticalRightSibling(List siblings, GraphicalEditPart movingEditPart, Point pointerLocation) {
	  	GraphicalEditPart rightSibling = null;
	  	int pointerYLocation = pointerLocation.y;

	  	for (int index = 0; index < siblings.size(); index++) {
	  		GraphicalEditPart sibling = (GraphicalEditPart) siblings.get(index);
	  		int siblingYLocation = sibling.getFigure().getBounds().getCenter().y;

	  		if (siblingYLocation > pointerYLocation) {
	  			rightSibling = sibling;
	  			break;
	  		}
	  	}
	  	
	  	return rightSibling;
	  }

	  /*
	   * Pre-condition: The List siblings should be sorted by it's y-coordinate 
	   */
	  private GraphicalEditPart getVerticalLeftSibling(List siblings, GraphicalEditPart movingEditPart, Point pointerLocation) {
	  	GraphicalEditPart leftSibling = null;
	  	int pointerYLocation = pointerLocation.y;

	  	int index;
	  	for (index = 0; index < siblings.size(); index++) {
	  		GraphicalEditPart sibling = (GraphicalEditPart) siblings.get(index);
	  		int siblingYLocation = sibling.getFigure().getBounds().getCenter().y;
	  	  	
	  		if (siblingYLocation > pointerYLocation) {
	  			if (index > 0) {
	  				leftSibling = (GraphicalEditPart) siblings.get(index - 1);
	  			}

	  			break;
	  		}
	  	}

	  	if (index == siblings.size()) {
	  		leftSibling = (GraphicalEditPart) siblings.get(index - 1);
	  	}

	  	return leftSibling;
	  }
	  
	  protected EditPart getParentEditPart(EditPart target)
	  {
	  	return target.getParent();
	  }

	  private class VerticalEditPartComparator implements Comparator {
	  	
	  	public VerticalEditPartComparator() {
	  	}
	  	
	  	public int compare(Object part1, Object part2) {
	  		int compareValue = 0;

	  		if (part1 instanceof GraphicalEditPart && part2 instanceof GraphicalEditPart) {
	  			GraphicalEditPart editPart1 = (GraphicalEditPart) part1;
	  			GraphicalEditPart editPart2 = (GraphicalEditPart) part2;
	  			
	  			int editPart1Y = editPart1.getFigure().getBounds().getCenter().y;
	  			int editPart2Y = editPart2.getFigure().getBounds().getCenter().y;
	  			
	  			Integer integerPart1 = new Integer(editPart1Y);
	  			Integer integerPart2 = new Integer(editPart2Y);
	  			
	  			compareValue = integerPart1.compareTo(integerPart2);
	  		}

	  		return compareValue;
	  	}
	  }
}

Back to the top