Skip to main content
summaryrefslogtreecommitdiffstats
blob: 06b6b807f5b8d5775766e54cd365e8d77bcd954e (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
/*******************************************************************************
 * 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.adapters.commands;

import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.wst.wsdl.Message;
import org.eclipse.wst.wsdl.Part;
import org.eclipse.wst.wsdl.ui.internal.adapters.basic.W11ParameterForPart;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IParameter;

public class W11ReorderParametersCommand extends Command {
    protected IParameter leftSibling;
    protected IParameter rightSibling;
    protected IParameter movingParameter;
    
    public W11ReorderParametersCommand(IParameter leftSibling, IParameter rightSibling, IParameter movingParameter) {
        super("");  // TODO: Need to add String here...
        this.leftSibling = leftSibling;
        this.rightSibling = rightSibling;
        this.movingParameter = movingParameter;
    }
    
    public void execute() {
		Part leftSibElement = null;
		Part rightSibElement = null;
		Part movingChild = null;
		
		if (leftSibling instanceof W11ParameterForPart) {
			leftSibElement = (Part) ((W11ParameterForPart) leftSibling).getTarget(); 
		}
		if (rightSibling instanceof W11ParameterForPart) {
			rightSibElement = (Part) ((W11ParameterForPart) rightSibling).getTarget();
		}
		if (movingParameter instanceof W11ParameterForPart) {
			movingChild = (Part) ((W11ParameterForPart) movingParameter).getTarget();
		}
		
		if (movingChild.equals(leftSibElement) || movingChild.equals(rightSibElement)) {
			return;
		}
    	
    	if (movingChild != null) {    		
    		Message message = (Message) movingChild.eContainer();
			List parts = message.getEParts();

			parts.remove(movingChild);

			int leftIndex = -1, rightIndex = -1;
			if (leftSibElement != null) {
				leftIndex = parts.indexOf(leftSibElement);
			}
			if (rightSibElement != null) {
				rightIndex = parts.indexOf(rightSibElement);
			}

			if (leftIndex == -1) {
				// Add moving child to the front
				parts.add(0, movingChild);  				
			}
			else if (rightIndex == -1) {
				// Add moving child to the end
				parts.add(movingChild);
			}
			else {
				// Add moving child after the occurence of the left sibling
				parts.add(leftIndex + 1, movingChild);
			}
    	}
    }
}

Back to the top