Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0cb7b282fdf22d11c1a6166c48a250d404f1f386 (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
/*******************************************************************************
 * Copyright (c) 2004, 2005 Sybase, Inc. 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

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

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;
import org.eclipse.jface.window.Window;
import org.eclipse.jst.jsf.common.ui.internal.dialogs.CommonResourceDialog;
import org.eclipse.jst.jsf.facesconfig.ui.EditorPlugin;
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.PageflowNode;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowPage;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.util.PageflowValidation;
import org.eclipse.jst.jsf.facesconfig.ui.util.WebrootUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;

/**
 * This is the creation command for pageflow editpart
 * 
 * @author Xiao-guang Zhang
 */
public class CreateNodeCommand extends Command implements IPreExecuteCommand {
	private static final String UNNAMED_NODE = "unnamed"; //$NON-NLS-1$

	/** new pageflow node */
	private PageflowNode child;

	/** size of pageflow node figure */
	private Rectangle rect;

	/** parent pageflow */
	private Pageflow parent;

	/** index of pageflow nodes */
	// private int _index = -1;
	public CreateNodeCommand() {
		// Pageflow.Commands.CreateNodeCommand.Label = Create new node
		super(PageflowMessages.Pageflow_Commands_CreateNodeCommand_Label);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	public boolean canExecute() {
		return child != null && parent != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Command#execute()
	 */
	public void execute() {
		if (rect != null) {
			child.setX(rect.x);
			child.setY(rect.y);
			if (!rect.isEmpty()) {
				child.setHeight(rect.getSize().height);
				child.setWidth(rect.getSize().width);
			}
		}

		if (child.getName() == UNNAMED_NODE) {
			child.setName(parent.getDefaultNodeName(child.getClass()));
		}

		parent.getNodes().add(child);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Command#redo()
	 */
	public void redo() {
		if (rect != null) {
			child.setX(rect.x);
			child.setY(rect.y);
			if (!rect.isEmpty()) {
				child.setHeight(rect.getSize().height);
				child.setWidth(rect.getSize().width);
			}
		}

		parent.getNodes().add(child);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Command#undo()
	 */
	public void undo() {
		parent.getNodes().remove(child);
	}

	/**
	 * select a jsp page in current project.
	 * 
	 * @return
	 */
	private boolean selectJSPPage() {

		Shell shell = null;
		IWorkbenchWindow workbenchWindow = EditorPlugin
				.getActiveWorkbenchWindow();
		if (workbenchWindow.getShell() != null) {
			shell = workbenchWindow.getShell();
		} else {
			shell = new Shell();
		}
		IProject project = WebrootUtil.getProject(getParent());

		CommonResourceDialog dialog = new CommonResourceDialog(shell, project,
				SWT.NONE);

		dialog
				.setTitle(PageflowMessages.Pageflow_Commands_CreateNodeCommand_SelectJSPDialog_Title);
		// no suffix filter
		dialog.setSuffixs(null);
		dialog
				.setResourceDescription(PageflowMessages.Pageflow_Commands_CreateNodeCommand_SelectJSPDialog_Description);

		if (dialog.open() == Window.OK) {
			Object[] result = dialog.getResult();
			if (result != null) {
				IFile jspFile = (IFile) result[0];
				if (jspFile != null) {
					// get the project path for the new created file, i.e.,
					// /project/webroot/*.jsp
					String jsfSelection = WebrootUtil.getWebPath(jspFile
							.getFullPath());
					if (jsfSelection != null && jsfSelection.length() > 0) {
						if (PageflowValidation.getInstance().isExistedPage(
								this.parent, jsfSelection)) {
							// Pageflow.PageflowEditor.Alert.DNDResourceTitle =
							// Pageflow Creation Error
							// Pageflow.PageflowEditor.Alert.ExistingPage = The
							// web page {0} is already existed in current
							// PageFlow.
							EditorPlugin
									.getAlerts()
									.error(
											"Pageflow.PageflowEditor.Alert.DNDResourceTitle", //$NON-NLS-1$
											"Pageflow.PageflowEditor.Alert.ExistingPage", //$NON-NLS-1$
											jsfSelection);
							return false;
						}

						((PageflowPage) (child)).setPath(jsfSelection);
						((PageflowPage) (child)).setName(WebrootUtil
								.getPageNameFromWebPath(jsfSelection));
					}

					return true;
				}
			}
		}
		return false;
	}

	/**
	 * call the JSF wizard to create a new jsf page.
	 * @return the parent
	 * 
	 */
	public Pageflow getParent() {
		return parent;
	}

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

	/**
	 * set the location of the new pageflow node
	 * 
	 * @param r -
	 *            location of the new pageflow node
	 */
	public void setLocation(Rectangle r) {
		rect = r;
	}

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sybase.stf.jmt.editors.pageflow.commands.IPreExecuteCommand#preExecute()
	 */
	public boolean preExecute() {
		// note that the model adds the ports to the node in this call
		// pop up the new the wizard to create the new jsf file
		if (child instanceof PageflowPage) {
			// if the new page's file path is not empty, i.e., drag a file from
			// resource navigator view
			if (null != ((PageflowPage) child).getPath()) {
				if (PageflowValidation.getInstance().isExistedPage(parent,
						(PageflowPage) child)) {
					// Pageflow.PageflowEditor.Alert.DNDResourceTitle = Pageflow
					// Creation Error
					// Pageflow.PageflowEditor.Alert.ExistingPage = The web page
					// {0} is already existed in current PageFlow.
					EditorPlugin.getAlerts().error(
							"Pageflow.PageflowEditor.Alert.DNDResourceTitle", //$NON-NLS-1$
							"Pageflow.PageflowEditor.Alert.ExistingPage", //$NON-NLS-1$
							((PageflowPage) child).getPath());
					return false;
				}
			} else
			// create a new jsf page from scratch
			{
				// return createNewJSFPage();
				return selectJSPPage();
			}
		}
		return true;
	}
}

Back to the top