Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2006 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jst.jsp.ui.internal.wizard; |
12 |
|
13 |
import java.util.ArrayList; |
14 |
import java.util.Arrays; |
15 |
import java.util.List; |
16 |
|
17 |
import org.eclipse.core.resources.IProject; |
18 |
import org.eclipse.core.resources.IResource; |
19 |
import org.eclipse.core.resources.IWorkspace; |
20 |
import org.eclipse.core.resources.ResourcesPlugin; |
21 |
import org.eclipse.core.runtime.CoreException; |
22 |
import org.eclipse.core.runtime.IPath; |
23 |
import org.eclipse.core.runtime.IStatus; |
24 |
import org.eclipse.core.runtime.Platform; |
25 |
import org.eclipse.core.runtime.Preferences; |
26 |
import org.eclipse.core.runtime.content.IContentType; |
27 |
import org.eclipse.jdt.core.JavaCore; |
28 |
import org.eclipse.jface.viewers.IStructuredSelection; |
29 |
import org.eclipse.jst.jsp.core.internal.JSPCorePlugin; |
30 |
import org.eclipse.jst.jsp.core.internal.preferences.JSPCorePreferenceNames; |
31 |
import org.eclipse.jst.jsp.core.internal.provisional.contenttype.ContentTypeIdForJSP; |
32 |
import org.eclipse.jst.jsp.ui.internal.JSPUIMessages; |
33 |
import org.eclipse.jst.jsp.ui.internal.Logger; |
34 |
import org.eclipse.osgi.util.NLS; |
35 |
import org.eclipse.ui.dialogs.WizardNewFileCreationPage; |
36 |
import org.eclipse.wst.common.componentcore.ComponentCore; |
37 |
import org.eclipse.wst.common.componentcore.internal.util.IModuleConstants; |
38 |
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent; |
39 |
import org.eclipse.wst.common.project.facet.core.IFacetedProject; |
40 |
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager; |
41 |
|
42 |
class NewJSPFileWizardPage extends WizardNewFileCreationPage { |
43 |
|
44 |
private IContentType fContentType; |
45 |
private List fValidExtensions = null; |
46 |
|
47 |
public NewJSPFileWizardPage(String pageName, IStructuredSelection selection) { |
48 |
super(pageName, selection); |
49 |
} |
50 |
|
51 |
/** |
52 |
* This method is overriden to set the selected folder to web contents |
53 |
* folder if the current selection is outside the web contents folder. |
54 |
*/ |
55 |
protected void initialPopulateContainerNameField() { |
56 |
super.initialPopulateContainerNameField(); |
57 |
|
58 |
IPath fullPath = getContainerFullPath(); |
59 |
IProject project = getProjectFromPath(fullPath); |
60 |
IPath webContentPath = getWebContentPath(project); |
61 |
|
62 |
if (webContentPath != null && !webContentPath.isPrefixOf(fullPath)) { |
63 |
setContainerFullPath(webContentPath); |
64 |
} |
65 |
} |
66 |
|
67 |
/** |
68 |
* This method is overriden to set additional validation specific to jsp |
69 |
* files. |
70 |
*/ |
71 |
protected boolean validatePage() { |
72 |
setMessage(null); |
73 |
setErrorMessage(null); |
74 |
|
75 |
if (!super.validatePage()) { |
76 |
return false; |
77 |
} |
78 |
|
79 |
String fileName = getFileName(); |
80 |
IPath fullPath = getContainerFullPath(); |
81 |
if ((fullPath != null) && (fullPath.isEmpty() == false) && (fileName != null)) { |
82 |
// check that filename does not contain invalid extension |
83 |
if (!extensionValidForContentType(fileName)) { |
84 |
setErrorMessage(NLS.bind(JSPUIMessages._ERROR_FILENAME_MUST_END_JSP, getValidExtensions().toString())); |
85 |
return false; |
86 |
} |
87 |
// no file extension specified so check adding default |
88 |
// extension doesn't equal a file that already exists |
89 |
if (fileName.lastIndexOf('.') == -1) { |
90 |
String newFileName = addDefaultExtension(fileName); |
91 |
IPath resourcePath = fullPath.append(newFileName); |
92 |
|
93 |
IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
94 |
IStatus result = workspace.validatePath(resourcePath.toString(), IResource.FOLDER); |
95 |
if (!result.isOK()) { |
96 |
// path invalid |
97 |
setErrorMessage(result.getMessage()); |
98 |
return false; |
99 |
} |
100 |
|
101 |
if ((workspace.getRoot().getFolder(resourcePath).exists() || workspace.getRoot().getFile(resourcePath).exists())) { |
102 |
setErrorMessage(JSPUIMessages.ResourceGroup_nameExists); |
103 |
return false; |
104 |
} |
105 |
} |
106 |
|
107 |
// get the IProject for the selection path |
108 |
IProject project = getProjectFromPath(fullPath); |
109 |
if (project != null) { |
110 |
if (!isJavaProject(project)) { |
111 |
setMessage(JSPUIMessages._WARNING_FILE_MUST_BE_INSIDE_JAVA_PROJECT, WARNING); |
112 |
} |
113 |
// if inside web project, check if inside webContent folder |
114 |
if (isDynamicWebProject(project)) { |
115 |
// check that the path is inside the webContent folder |
116 |
IPath webContentPath = getWebContentPath(project); |
117 |
if (!webContentPath.isPrefixOf(fullPath)) { |
118 |
setMessage(JSPUIMessages._WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT, WARNING); |
119 |
} |
120 |
} |
121 |
} |
122 |
} |
123 |
|
124 |
return true; |
125 |
} |
126 |
|
127 |
/** |
128 |
* Adds default extension to the filename |
129 |
* |
130 |
* @param filename |
131 |
* @return |
132 |
*/ |
133 |
String addDefaultExtension(String filename) { |
134 |
StringBuffer newFileName = new StringBuffer(filename); |
135 |
|
136 |
Preferences preference = JSPCorePlugin.getDefault().getPluginPreferences(); |
137 |
String ext = preference.getString(JSPCorePreferenceNames.DEFAULT_EXTENSION); |
138 |
|
139 |
newFileName.append("."); //$NON-NLS-1$ |
140 |
newFileName.append(ext); |
141 |
|
142 |
return newFileName.toString(); |
143 |
} |
144 |
|
145 |
/** |
146 |
* Get content type associated with this new file wizard |
147 |
* |
148 |
* @return IContentType |
149 |
*/ |
150 |
private IContentType getContentType() { |
151 |
if (fContentType == null) |
152 |
fContentType = Platform.getContentTypeManager().getContentType(ContentTypeIdForJSP.ContentTypeID_JSP); |
153 |
return fContentType; |
154 |
} |
155 |
|
156 |
/** |
157 |
* Get list of valid extensions for JSP Content type |
158 |
* |
159 |
* @return |
160 |
*/ |
161 |
private List getValidExtensions() { |
162 |
if (fValidExtensions == null) { |
163 |
IContentType type = getContentType(); |
164 |
fValidExtensions = new ArrayList(Arrays.asList(type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC))); |
165 |
} |
166 |
return fValidExtensions; |
167 |
} |
168 |
|
169 |
/** |
170 |
* Verifies if fileName is valid name for content type. Takes base content |
171 |
* type into consideration. |
172 |
* |
173 |
* @param fileName |
174 |
* @return true if extension is valid for this content type |
175 |
*/ |
176 |
private boolean extensionValidForContentType(String fileName) { |
177 |
boolean valid = false; |
178 |
|
179 |
IContentType type = getContentType(); |
180 |
// there is currently an extension |
181 |
if (fileName.lastIndexOf('.') != -1) { |
182 |
// check what content types are associated with current extension |
183 |
IContentType[] types = Platform.getContentTypeManager().findContentTypesFor(fileName); |
184 |
int i = 0; |
185 |
while (i < types.length && !valid) { |
186 |
valid = types[i].isKindOf(type); |
187 |
++i; |
188 |
} |
189 |
} |
190 |
else |
191 |
valid = true; // no extension so valid |
192 |
return valid; |
193 |
} |
194 |
|
195 |
/** |
196 |
* Returns the project that contains the specified path |
197 |
* |
198 |
* @param path |
199 |
* the path which project is needed |
200 |
* @return IProject object. If path is <code>null</code> the return |
201 |
* value is also <code>null</code>. |
202 |
*/ |
203 |
private IProject getProjectFromPath(IPath path) { |
204 |
IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
205 |
IProject project = null; |
206 |
|
207 |
if (path != null) { |
208 |
if (workspace.validatePath(path.toString(), IResource.PROJECT).isOK()) { |
209 |
project = workspace.getRoot().getProject(path.toString()); |
210 |
} |
211 |
else { |
212 |
project = workspace.getRoot().getFile(path).getProject(); |
213 |
} |
214 |
} |
215 |
|
216 |
return project; |
217 |
} |
218 |
|
219 |
/** |
220 |
* Checks if the specified project is a web project. |
221 |
* |
222 |
* @param project |
223 |
* project to be checked |
224 |
* @return true if the project is web project, otherwise false |
225 |
*/ |
226 |
private boolean isDynamicWebProject(IProject project) { |
227 |
IFacetedProject faceted = null; |
228 |
try { |
229 |
faceted = ProjectFacetsManager.create(project); |
230 |
} |
231 |
catch (CoreException e) { |
232 |
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
233 |
} |
234 |
|
235 |
if (faceted != null && faceted.hasProjectFacet(ProjectFacetsManager.getProjectFacet(IModuleConstants.JST_WEB_MODULE))) { |
236 |
return true; |
237 |
} |
238 |
|
239 |
return false; |
240 |
} |
241 |
|
242 |
/** |
243 |
* Checks if the specified project is a type of java project. |
244 |
* |
245 |
* @param project |
246 |
* project to be checked (cannot be null) |
247 |
* @return true if the project is a type of java project, otherwise false |
248 |
*/ |
249 |
private boolean isJavaProject(IProject project) { |
250 |
boolean isJava = false; |
251 |
try { |
252 |
isJava = project.hasNature(JavaCore.NATURE_ID); |
253 |
} |
254 |
catch (CoreException e) { |
255 |
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
256 |
} |
257 |
|
258 |
return isJava; |
259 |
} |
260 |
|
261 |
/** |
262 |
* Returns the web contents folder of the specified project |
263 |
* |
264 |
* @param project |
265 |
* the project which web contents path is needed |
266 |
* @return IPath of the web contents folder |
267 |
*/ |
268 |
private IPath getWebContentPath(IProject project) { |
269 |
IPath path = null; |
270 |
|
271 |
if (project != null && isDynamicWebProject(project)) { |
272 |
IVirtualComponent component = ComponentCore.createComponent(project); |
273 |
path = component.getRootFolder().getWorkspaceRelativePath(); |
274 |
} |
275 |
|
276 |
return path; |
277 |
} |
278 |
} |