Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3075e8249408b763cfe7ef055898c2619fff91db (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
/*******************************************************************************
 * 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.wst.wsdl.Message;
import org.eclipse.wst.wsdl.Part;
import org.eclipse.wst.wsdl.ui.internal.Messages;
import org.eclipse.wst.wsdl.ui.internal.adapters.basic.W11ParameterForAttribute;
import org.eclipse.wst.wsdl.ui.internal.adapters.basic.W11ParameterForElement;
import org.eclipse.wst.wsdl.ui.internal.adapters.basic.W11ParameterForPart;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IParameter;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDParticle;

public class W11ReorderParametersCommand extends W11TopLevelElementCommand {
    protected IParameter leftSibling;
    protected IParameter rightSibling;
    protected IParameter movingParameter;
    
    public W11ReorderParametersCommand(IParameter leftSibling, IParameter rightSibling, IParameter movingParameter) {
        super(Messages._UI_ACTION_REORDER_PART, null);
        this.leftSibling = leftSibling;
        this.rightSibling = rightSibling;
        this.movingParameter = movingParameter;
    }
    
    public void execute() {
    	if (leftSibling instanceof W11ParameterForPart ||
    		rightSibling instanceof W11ParameterForPart ||
    		movingParameter instanceof W11ParameterForPart) {
    		executeForPart();
    	}
    	else if (leftSibling instanceof W11ParameterForElement ||
        		 rightSibling instanceof W11ParameterForElement ||
        		 movingParameter instanceof W11ParameterForElement) {
    		executeForElement();
        }
    	else if (leftSibling instanceof W11ParameterForAttribute ||
       		     rightSibling instanceof W11ParameterForAttribute ||
    		     movingParameter instanceof W11ParameterForAttribute) {
//    		executeForElement();
    	}
    }
    
    private void executeForElement() {
    	XSDElementDeclaration leftSibElement = null;
    	XSDElementDeclaration rightSibElement = null;
    	XSDElementDeclaration movingChild = null;
    	
		XSDParticle movingParticle = null;
		XSDParticle leftParticle = null;
		XSDParticle rightParticle = null;

		if (leftSibling instanceof W11ParameterForElement) {
			leftSibElement = (XSDElementDeclaration) ((W11ParameterForElement) leftSibling).getTarget();
			leftParticle = (XSDParticle) leftSibElement.eContainer();
		}
		if (rightSibling instanceof W11ParameterForElement) {
			rightSibElement = (XSDElementDeclaration) ((W11ParameterForElement) rightSibling).getTarget();
			rightParticle = (XSDParticle) rightSibElement.eContainer();
		}
		if (movingParameter instanceof W11ParameterForElement) {
			movingChild = (XSDElementDeclaration) ((W11ParameterForElement) movingParameter).getTarget();
			movingParticle = (XSDParticle) movingChild.eContainer();
		}
		
		if (movingChild.equals(leftSibElement) || movingChild.equals(rightSibElement)) {
			return;
		}
    	
    	if (movingChild != null) {
    		try {
    			beginRecording(movingParticle.getElement());

    			XSDModelGroup container = (XSDModelGroup) movingParticle.getContainer();
    			List particles = container.getContents();
    			
    			particles.remove(movingParticle);

    			int leftIndex = -1, rightIndex = -1;
    			if (leftParticle != null) {
    				leftIndex = particles.indexOf(leftParticle);
    			}
    			if (rightParticle!= null) {
    				rightIndex = particles.indexOf(rightParticle);
    			}

    			if (leftIndex == -1) {
    				// Add moving child to the front
    				particles.add(0, movingParticle);  				
    			}
    			else if (rightIndex == -1) {
    				// Add moving child to the end
    				particles.add(movingParticle);
    			}
    			else {
    				// Add moving child after the occurence of the left sibling
    				particles.add(leftIndex + 1, movingParticle);
    			}
    		}
    		finally {
    			endRecording(movingParticle.getElement());
    		}
    	}
    }
    
    private void executeForPart() {
		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) {
    		try {
    			beginRecording(movingChild.getElement());
        		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);
    			}
    		}
    		finally {
    			endRecording(movingChild.getElement());
    		}
    	}
    }
}

Back to the top