Skip to main content
summaryrefslogtreecommitdiffstats
blob: 743868dca576c765cef7a245420b38174bdcd9a4 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*******************************************************************************
 * Copyright (c) 2001, 2004 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.actions;


import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.widgets.Shell;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.xsd.XSDSchema;

import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.SelectionEvent;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.eclipse.wst.wsdl.Definition;
import org.eclipse.wst.wsdl.Fault;
import org.eclipse.wst.wsdl.Message;
import org.eclipse.wst.wsdl.Operation;
import org.eclipse.wst.wsdl.Part;
import org.eclipse.wst.wsdl.PortType;
import org.eclipse.wst.wsdl.WSDLElement;
import org.eclipse.wst.wsdl.ui.internal.WSDLEditorPlugin;
import org.eclipse.wst.xml.core.document.IDOMNode;

public class DeleteAction extends BaseNodeAction {
	 protected List list;
	 protected String deleteString = WSDLEditorPlugin.getWSDLString("_UI_ACTION_DELETE") + " "; //$NON-NLS-1$
//	 private Object object;
//	 private Node node;
	
	public DeleteAction(Object object, Node node) {
		setText(WSDLEditorPlugin.getWSDLString("_UI_ACTION_DELETE"));  //$NON-NLS-1$
	    list = new Vector();
	    list.add(object);
	}
	
	public DeleteAction(List deleteList) {
		setText(WSDLEditorPlugin.getWSDLString("_UI_ACTION_DELETE"));  //$NON-NLS-1$
	    list = deleteList;
	}

	private Vector getReferencedMessages(Operation operation) {
		Vector messages = new Vector();
		messages.addAll(getMessages(operation.getEFaults()));
		
		if (operation.getEInput() != null && operation.getEInput().getEMessage() != null)
			messages.addElement(operation.getEInput().getEMessage());
		if (operation.getEOutput() != null && operation.getEOutput().getEMessage() != null)
			messages.addElement(operation.getEOutput().getEMessage());
		
		return messages;
	}
	
	public void run() {
		// We want to delete Operations first.  So we need to sort the list
		List sortedList = new ArrayList(list.size());
		Iterator listIterator = list.iterator();
		
		while (listIterator.hasNext()) {
			Object unsortedObject = listIterator.next();
			if (unsortedObject instanceof Operation) {
				sortedList.add(0, unsortedObject);
			}
			else {
				sortedList.add(unsortedObject);
			}
		}
		
		Node recordingNode = null;
		if (sortedList.size() > 1) {
			recordingNode = getElement(sortedList.get(0));
			beginRecording(recordingNode, WSDLEditorPlugin.getWSDLString("_UI_ACTION_MULTIPLE_DELETE"));
		}
		
		Iterator iterator = sortedList.iterator();
		while (iterator.hasNext()) {
			Object object = iterator.next();
			
			if (object instanceof Operation) {
				deleteOperation((Operation) object);
			}
			else if (object instanceof Part) {
				deletePart((Part) object);
			}
			else {
				DeleteNodeAction deleteNodeAction = new DeleteNodeAction(getElement(object));
				deleteNodeAction.run();
			}
		}
		
		if (recordingNode != null) {
			endRecording(recordingNode);
		}
	}
	
	private Vector getMessages(List faults) {
		Vector v = new Vector();
		Iterator it = faults.iterator();
		
		while (it.hasNext()) {
			Fault fault = (Fault) it.next();
			if (fault.getEMessage() != null) {
				v.addElement(fault.getEMessage());
			}
		}
		
		return v;
	}
	
	private Vector getParts(Vector messages) {
		Vector parts = new Vector();
		Iterator it = messages.iterator();
		
		while (it.hasNext()) {
			Message message = (Message) it.next();
			
			if (message.getEParts() != null) {
				parts.addAll(message.getEParts());
			}
		}
		
		return parts;
	}
	
	public Node getNode()
	{
	  return list.size() > 0 ? (Node) getElement(list.get(0)) : null;
	} 

	public String getUndoDescription()
	{
	  return WSDLEditorPlugin.getWSDLString("_UI_ACTION_DELETE"); //$NON-NLS-1$
	}
	
	private void deleteOperation(Operation operation) {
		DeleteOperationDialog dialog = new DeleteOperationDialog(WSDLEditorPlugin.getShell(), operation.getName());
	    int returnCode = dialog.createAndOpen();
	    
	    if (returnCode == IDialogConstants.OK_ID) {
	    	if (dialog.deleteMessagesAndParts()) {
	    		// Get all associated messages
	    		Vector messages = getReferencedMessages(operation);
	    		
	    		// Determine which Messages are referenced from 'outside' this operation
	    		Vector referencedMessages = new Vector();
	    		if (operation.getEnclosingDefinition().getEPortTypes() != null) {
	    			Iterator portTypeIterator = operation.getEnclosingDefinition().getEPortTypes().iterator();
	    			Vector operations = new Vector();
	    			
	    			while (portTypeIterator.hasNext()) {
	    				PortType portType = (PortType) portTypeIterator.next();
	    				
	    				if (portType.getEOperations() != null) {
	    					Iterator operationIterator = portType.getEOperations().iterator();
	    					while (operationIterator.hasNext()) {
	    						Operation nextOperation = (Operation) operationIterator.next();
	    						if (!nextOperation.equals(operation))
	    							operations.add(nextOperation);
	    					}
	    				}
	    			}
	    			
	    			for (int index = 0; index < operations.size(); index++) {
	    				referencedMessages.addAll(getReferencedMessages((Operation) operations.elementAt(index)));
	    			}
	    		
	    			// Filter which Messages need to be deleted
	    			for (int index = 0; index < messages.size(); index++) {
	    				int foundIndex = referencedMessages.indexOf(messages.elementAt(index));
	    				
	    				if (foundIndex != -1) {
	    					// Message is referenced elsewhere.  Do not delete
	    					messages.remove(index);
	    				}
	    			}
	    		}
	    		
	    		// Get all associated parts
	    		Vector parts = getParts(messages);
	    		
	    		// Remove our list of Messages and Parts
	    		PortType portType = (PortType) operation.eContainer();
	    		Node recordingNode = portType.getEnclosingDefinition().getElement();
	    		beginRecording(recordingNode, deleteString + WSDLEditorPlugin.getWSDLString("_UI_LABEL_OPERATION"));
	    		
	    		for (int index = 0; index < messages.size(); index++) {
	    			Message message = (Message) messages.elementAt(index);
	    			Definition definition = (Definition) message.eContainer();
	    			definition.getEMessages().remove(message);
	    		}

	    		for (int index = 0; index < parts.size(); index++) {
	    			Part part = (Part) parts.elementAt(index);
	    			Message message = (Message) part.eContainer();
	    			message.getEParts().remove(part);
	    		}
	    		
	    		// Delete the Operation
	    		portType.getEOperations().remove(operation);

	    		endRecording(recordingNode);
	    	}
	    	else {
	    		// Only delete the Operation
	    		PortType portType = (PortType) operation.eContainer();
	    		Node recordingNode = portType.getElement();
	    		
	    		beginRecording(recordingNode, deleteString + WSDLEditorPlugin.getWSDLString("_UI_LABEL_OPERATION")); // Translate this!!!
	    		portType.getEOperations().remove(operation);
	    		endRecording(recordingNode);
	    	}
	    }
	}
	
	private void deletePart(Part part) {
		Message message = (Message) part.eContainer();
		if (message != null) {
			beginRecording(message.getElement(), deleteString + WSDLEditorPlugin.getWSDLString("_UI_LABEL_PART"));
			message.getEParts().remove(part);
			endRecording(message.getElement());
		}
	}
	
	private Element getElement(Object object) {
		Element element = null;
		if (object instanceof WSDLElement) {
			element = ((WSDLElement) object).getElement();
		}
		else if (object instanceof XSDSchema) {
			element = ((XSDSchema) object).getElement();
		}
		
		return element;
	}
	
	/*
	 * The following method should be used to begin recording changes.
	 * This should eventually replace BaseNodeAction.beginRecording()
	 * when all deletes are model driven.
	 */
	  private void beginRecording(Node node, String undoDescription)
	  {    
	    if (node instanceof IDOMNode)
	    {
	      ((IDOMNode)node).getModel().beginRecording(this, undoDescription);  
	    }
	  }

	/*
	 * The following method should be used to begin recording changes.
	 * This should eventually replace BaseNodeAction.beginRecording()
	 * when all deletes are model driven.
	 */
	  public void endRecording(Node node)
	  {
	    if (node instanceof IDOMNode)
	    {
	      ((IDOMNode)node).getModel().endRecording(this);  
	    }
	  }
	
	private class DeleteOperationDialog extends Dialog implements SelectionListener{
		private Button checkButton = null;
		private boolean deleteMessagesAndParts = true;
		private String operationName;
		 
		public DeleteOperationDialog(Shell shell, String name) {
			super(shell);
			operationName = name;
		}
		
		protected Control createDialogArea(Composite parent) 
		  {
		    Composite dialogArea = (Composite)super.createDialogArea(parent);

		    Composite composite = new Composite(dialogArea, SWT.NONE);
		    GridLayout layout = new GridLayout();
		    layout.numColumns = 2;
		    layout.marginWidth = 0;
		    layout.marginHeight = 0;
		    layout.verticalSpacing = 0;

		    composite.setLayout(layout);

		    GridData gdFill= new GridData();
		    gdFill.horizontalAlignment= GridData.FILL;
		    gdFill.grabExcessHorizontalSpace= true;
		    gdFill.verticalAlignment= GridData.FILL;
		    gdFill.grabExcessVerticalSpace= true;
		    composite.setLayoutData(gdFill);

		    Label label = new Label(composite, SWT.NONE);
		    label.setText(operationName);
//		    label.setText(WSDLEditorPlugin.getWSDLString("_UI_LABEL_OPERATION") + " " + operationName);
		    GridData labelData = new GridData();
		    labelData.horizontalSpan = 2;
		    
		    label.setLayoutData(labelData);
		    
		    Label emptyLabel = new Label(composite, SWT.NONE);
		    GridData emptyData = new GridData();
		    emptyData.horizontalSpan = 2;
		    emptyLabel.setLayoutData(emptyData);

		    checkButton = new Button(composite, SWT.CHECK);
		    checkButton.setSelection(deleteMessagesAndParts);
		    checkButton.addSelectionListener(this);
		    Label nameLabel = new Label(composite, SWT.NONE);
		    nameLabel.setText(WSDLEditorPlugin.getWSDLString("_UI_DELETE_ASSOCIATED_MSG_AND_PARTS"));
		    
		    return dialogArea;
		  }
		
		public boolean deleteMessagesAndParts() {
			return deleteMessagesAndParts;
		}
		
		public void widgetDefaultSelected(SelectionEvent e)  {}

		public void widgetSelected(SelectionEvent e)  {
			if (e.widget == checkButton) {
				if (checkButton.getSelection()) {
					deleteMessagesAndParts = true;
				}
				else {
					deleteMessagesAndParts = false;
				}
			}
		}

		public int createAndOpen() {
			create();
		    getShell().setText(WSDLEditorPlugin.getWSDLString("_UI_DELETE_OPERATION_TITLE"));
		    setBlockOnOpen(true);
		    return open();
		}
		
	}
}

Back to the top