Skip to main content
summaryrefslogtreecommitdiffstats
blob: 13752a3e4adee81ed942111344bde3618b3f2078 (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) 2004, 2007 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.jst.jsf.facesconfig.ui.pageflow.command;

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

import org.eclipse.jst.jsf.facesconfig.ui.pageflow.PageflowMessages;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.Pageflow;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowLink;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowNode;

/**
 * This is the delete command for pageflow editpart
 * 
 */
public class DeleteNodeCommand extends AbstractBatchEditCommand {
	/** the deleting pageflow node */
	private PageflowNode child;

	/** parent pageflow */
	private Pageflow parent;

	/** index of pageflow nodes */
	private int index = -1;

	/** source connections with the deleting node */
	private List sourceConnections = new ArrayList();

	private List sourceConnectionSources = new ArrayList();

	private List sourceConnectionTargets = new ArrayList();

	/** target connections with the deleting node */
	private List targetConnections = new ArrayList();

	private List targetConnectionSources = new ArrayList();

	private List targetConnectionTargets = new ArrayList();

	/**
	 * @param pageflow
	 */
	public DeleteNodeCommand(Pageflow pageflow) {
		// Pageflow.Commands.DeleteNodeCommand.Label = Delete
		super(pageflow, PageflowMessages.Pageflow_Commands_DeleteNodeCommand_Label);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Command#execute()
	 */
	public void doExecute() {
		primExecute();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Command#redo()
	 */
	public void doRedo() {
		primExecute();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Command#undo()
	 */
	public void doUndo() {
		parent.getNodes().add(index, child);
		restoreConnections(child);
	}

	/**
	 * delete the connections related with the pageflow node.
	 * 
	 * @param node -
	 *            the deleting pageflow node
	 */
	private void deleteConnections(PageflowNode node) {
		while (node.getOutlinks().size() > 0) {
			// Note:should save the source and target of the link, otherwise
			// source and target will be lost,
			// when the source and target node remove the link.
			PageflowLink link = (PageflowLink) node.getOutlinks().get(0);
			sourceConnections.add(link);
			sourceConnectionSources.add(link.getSource());
			sourceConnectionTargets.add(link.getTarget());

			link.getTarget().getInlinks().remove(link);
			node.getOutlinks().remove(link);
			parent.getLinks().remove(link);
		}

		while (node.getInlinks().size() > 0) {
			// Note: should save the source and target of the link, otherwise
			// source and target will be lost,
			// when the source and target node remove the link.
			PageflowLink link = (PageflowLink) node.getInlinks().get(0);
			targetConnections.add(link);
			targetConnectionSources.add(link.getSource());
			targetConnectionTargets.add(link.getTarget());

			link.getSource().getOutlinks().remove(link);
			node.getInlinks().remove(link);
			parent.getLinks().remove(link);
		}
	}

	/**
	 * execute the delete command
	 * 
	 * 
	 */
	protected void primExecute() {
		deleteConnections(child);
		index = parent.getNodes().indexOf(child);
		parent.getNodes().remove(child);
	}

	/**
	 * restore the connections of the deleted pageflow node
	 * 
	 * @param node -
	 *            the deleted pageflow node
	 */
	private void restoreConnections(PageflowNode node) {
		for (int i = 0; i < sourceConnections.size(); i++) {
			// restore the link with the source and target
			PageflowLink link = (PageflowLink) sourceConnections.get(i);
			PageflowNode source = (PageflowNode) sourceConnectionSources.get(i);
			PageflowNode target = (PageflowNode) sourceConnectionTargets.get(i);
			link.setSource(source);
			link.setTarget(target);

			parent.getLinks().add(link);
			node.getOutlinks().add(link);
			link.getTarget().getInlinks().add(link);
		}
		sourceConnections.clear();
		for (int i = 0; i < targetConnections.size(); i++) {
			// restore the link with the source and target
			PageflowLink link = (PageflowLink) targetConnections.get(i);
			PageflowNode source = (PageflowNode) targetConnectionSources.get(i);
			PageflowNode target = (PageflowNode) targetConnectionTargets.get(i);
			link.setSource(source);
			link.setTarget(target);

			parent.getLinks().add(link);
			node.getInlinks().add(link);
			link.getSource().getOutlinks().add(link);
		}
		targetConnections.clear();
	}

	/**
	 * set the child pageflow node
	 * 
	 * @param c -
	 *            the child pageflow node
	 */
	public void setChild(PageflowNode c) {
		child = c;
	}

	/**
	 * set the parent pageflow
	 * 
	 * @param p -
	 *            the parent pageflow
	 */
	public void setParent(Pageflow p) {
		parent = p;
	}
}

Back to the top