View | Details | Raw Unified | Return to bug 140752 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/wst/html/ui/internal/wizard/NewHTMLWizard.java (-122 / +4 lines)
Lines 13-46 Link Here
13
import java.io.ByteArrayInputStream;
13
import java.io.ByteArrayInputStream;
14
import java.io.ByteArrayOutputStream;
14
import java.io.ByteArrayOutputStream;
15
import java.io.OutputStreamWriter;
15
import java.io.OutputStreamWriter;
16
import java.util.ArrayList;
17
import java.util.Arrays;
18
import java.util.List;
19
16
20
import org.eclipse.core.resources.IFile;
17
import org.eclipse.core.resources.IFile;
21
import org.eclipse.core.resources.IResource;
22
import org.eclipse.core.resources.IWorkspace;
23
import org.eclipse.core.resources.ResourcesPlugin;
24
import org.eclipse.core.runtime.IPath;
25
import org.eclipse.core.runtime.IStatus;
26
import org.eclipse.core.runtime.Platform;
27
import org.eclipse.core.runtime.Preferences;
18
import org.eclipse.core.runtime.Preferences;
28
import org.eclipse.core.runtime.content.IContentType;
29
import org.eclipse.jface.resource.ImageDescriptor;
19
import org.eclipse.jface.resource.ImageDescriptor;
30
import org.eclipse.jface.viewers.IStructuredSelection;
20
import org.eclipse.jface.viewers.IStructuredSelection;
31
import org.eclipse.jface.viewers.StructuredSelection;
21
import org.eclipse.jface.viewers.StructuredSelection;
32
import org.eclipse.jface.wizard.Wizard;
22
import org.eclipse.jface.wizard.Wizard;
33
import org.eclipse.osgi.util.NLS;
34
import org.eclipse.ui.INewWizard;
23
import org.eclipse.ui.INewWizard;
35
import org.eclipse.ui.IWorkbench;
24
import org.eclipse.ui.IWorkbench;
36
import org.eclipse.ui.IWorkbenchPage;
25
import org.eclipse.ui.IWorkbenchPage;
37
import org.eclipse.ui.PartInitException;
26
import org.eclipse.ui.PartInitException;
38
import org.eclipse.ui.PlatformUI;
27
import org.eclipse.ui.PlatformUI;
39
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
40
import org.eclipse.ui.ide.IDE;
28
import org.eclipse.ui.ide.IDE;
41
import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
29
import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
42
import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
43
import org.eclipse.wst.html.core.internal.provisional.contenttype.ContentTypeIdForHTML;
44
import org.eclipse.wst.html.ui.internal.HTMLUIMessages;
30
import org.eclipse.wst.html.ui.internal.HTMLUIMessages;
45
import org.eclipse.wst.html.ui.internal.Logger;
31
import org.eclipse.wst.html.ui.internal.Logger;
46
import org.eclipse.wst.html.ui.internal.editor.HTMLEditorPluginImageHelper;
32
import org.eclipse.wst.html.ui.internal.editor.HTMLEditorPluginImageHelper;
Lines 48-166 Link Here
48
import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames;
34
import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames;
49
35
50
public class NewHTMLWizard extends Wizard implements INewWizard {
36
public class NewHTMLWizard extends Wizard implements INewWizard {
51
	private WizardNewFileCreationPage fNewFilePage;
37
	
38
	private NewHTMLFileWizardPage fNewFilePage;
52
	private NewHTMLTemplatesWizardPage fNewFileTemplatesPage;
39
	private NewHTMLTemplatesWizardPage fNewFileTemplatesPage;
53
	private IStructuredSelection fSelection;
40
	private IStructuredSelection fSelection;
54
	private IContentType fContentType;
55
	private List fValidExtensions = null;
56
57
	/**
58
	 * Adds default extension to the filename
59
	 * 
60
	 * @param filename
61
	 * @return
62
	 */
63
	String addDefaultExtension(String filename) {
64
		StringBuffer newFileName = new StringBuffer(filename);
65
66
		Preferences preference = HTMLCorePlugin.getDefault().getPluginPreferences();
67
		String ext = preference.getString(HTMLCorePreferenceNames.DEFAULT_EXTENSION);
68
69
		newFileName.append("."); //$NON-NLS-1$
70
		newFileName.append(ext);
71
72
		return newFileName.toString();
73
	}
74
75
	/**
76
	 * Get content type associated with this new file wizard
77
	 * 
78
	 * @return IContentType
79
	 */
80
	IContentType getContentType() {
81
		if (fContentType == null)
82
			fContentType = Platform.getContentTypeManager().getContentType(ContentTypeIdForHTML.ContentTypeID_HTML);
83
		return fContentType;
84
	}
85
86
	/**
87
	 * Get list of valid extensions for HTML Content type
88
	 * 
89
	 * @return
90
	 */
91
	List getValidExtensions() {
92
		if (fValidExtensions == null) {
93
			IContentType type = getContentType();
94
			fValidExtensions = new ArrayList(Arrays.asList(type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC)));
95
		}
96
		return fValidExtensions;
97
	}
98
	
99
	/**
100
	 * Verifies if fileName is valid name for content type. Takes base content
101
	 * type into consideration.
102
	 * 
103
	 * @param fileName
104
	 * @return true if extension is valid for this content type
105
	 */
106
	boolean extensionValidForContentType(String fileName) {
107
		boolean valid = false;
108
109
		IContentType type = getContentType();
110
		// there is currently an extension
111
		if (fileName.lastIndexOf('.') != -1) {
112
			// check what content types are associated with current extension
113
			IContentType[] types = Platform.getContentTypeManager().findContentTypesFor(fileName);
114
			int i = 0;
115
			while (i < types.length && !valid) {
116
				valid = types[i].isKindOf(type);
117
				++i;
118
			}
119
		}
120
		else
121
			valid = true; // no extension so valid
122
		return valid;
123
	}
124
41
125
	public void addPages() {
42
	public void addPages() {
126
		fNewFilePage = new WizardNewFileCreationPage("HTMLWizardNewFileCreationPage", new StructuredSelection(IDE.computeSelectedResources(fSelection))) { //$NON-NLS-1$
43
		fNewFilePage = new NewHTMLFileWizardPage("HTMLWizardNewFileCreationPage", new StructuredSelection(IDE.computeSelectedResources(fSelection))); //$NON-NLS-1$
127
			protected boolean validatePage() {
128
				String fileName = getFileName();
129
				IPath fullPath = getContainerFullPath();
130
				if ((fullPath != null) && (fullPath.isEmpty() == false) && (fileName != null)) {
131
					// check that filename does not contain invalid extension
132
					if (!extensionValidForContentType(fileName)) {
133
						setErrorMessage(NLS.bind(HTMLUIMessages._ERROR_FILENAME_MUST_END_HTML, getValidExtensions().toString()));
134
						return false;
135
					}
136
					// no file extension specified so check adding default
137
					// extension doesn't equal a file that already exists
138
					if (fileName.lastIndexOf('.') == -1) {
139
						String newFileName = addDefaultExtension(fileName);
140
						IPath resourcePath = fullPath.append(newFileName);
141
142
						IWorkspace workspace = ResourcesPlugin.getWorkspace();
143
						IStatus result = workspace.validatePath(resourcePath.toString(), IResource.FOLDER);
144
						if (!result.isOK()) {
145
							// path invalid
146
							setErrorMessage(result.getMessage());
147
							return false;
148
						}
149
150
						if ((workspace.getRoot().getFolder(resourcePath).exists() || workspace.getRoot().getFile(resourcePath).exists())) {
151
							setErrorMessage(HTMLUIMessages.ResourceGroup_nameExists);
152
							return false;
153
						}
154
					}
155
				}
156
157
				setErrorMessage(null);
158
				return super.validatePage();
159
			}
160
		};
161
		fNewFilePage.setTitle(HTMLUIMessages._UI_WIZARD_NEW_HEADING);
44
		fNewFilePage.setTitle(HTMLUIMessages._UI_WIZARD_NEW_HEADING);
162
		fNewFilePage.setDescription(HTMLUIMessages._UI_WIZARD_NEW_DESCRIPTION);
45
		fNewFilePage.setDescription(HTMLUIMessages._UI_WIZARD_NEW_DESCRIPTION);
163
164
		addPage(fNewFilePage);
46
		addPage(fNewFilePage);
165
47
166
		fNewFileTemplatesPage = new NewHTMLTemplatesWizardPage();
48
		fNewFileTemplatesPage = new NewHTMLTemplatesWizardPage();
Lines 199-205 Link Here
199
		// no file extension specified so add default extension
81
		// no file extension specified so add default extension
200
		String fileName = fNewFilePage.getFileName();
82
		String fileName = fNewFilePage.getFileName();
201
		if (fileName.lastIndexOf('.') == -1) {
83
		if (fileName.lastIndexOf('.') == -1) {
202
			String newFileName = addDefaultExtension(fileName);
84
			String newFileName = fNewFilePage.addDefaultExtension(fileName);
203
			fNewFilePage.setFileName(newFileName);
85
			fNewFilePage.setFileName(newFileName);
204
		}
86
		}
205
87
(-)plugin.xml (-14 / +10 lines)
Lines 322-341 Link Here
322
        wizardId="org.eclipse.wst.html.ui.internal.wizard.NewHTMLWizard">
322
        wizardId="org.eclipse.wst.html.ui.internal.wizard.NewHTMLWizard">
323
			<enablement>
323
			<enablement>
324
				<or>
324
				<or>
325
					<adapt type="org.eclipse.core.resources.IProject">
325
					<adapt type="org.eclipse.core.resources.IResource">
326
		           	  <or>
326
						<or>
327
			              <test
327
							<test
328
			                    property="org.eclipse.wst.common.project.facet.core.projectFacet"
328
								property="org.eclipse.wst.common.project.facet.core.projectFacet"
329
			                    value="wst.web"/>
329
								value="wst.web"/>
330
			              <test
330
							<test
331
			                    property="org.eclipse.wst.common.project.facet.core.projectFacet"
331
								property="org.eclipse.wst.common.project.facet.core.projectFacet"
332
			                    value="jst.web"/>
332
								value="jst.web"/>
333
			          </or>
333
						</or>
334
		           </adapt>
334
					</adapt>
335
					<instanceof
336
						value="org.eclipse.core.resources.IFolder" />
337
					<instanceof
338
						value="org.eclipse.core.resources.IFile" /> 
339
				</or>
335
				</or>
340
			</enablement>
336
			</enablement>
341
		</commonWizard>
337
		</commonWizard>
(-)src/org/eclipse/wst/html/ui/internal/HTMLUIPluginResources.properties (+1 lines)
Lines 19-24 Link Here
19
_UI_WIZARD_NEW_HEADING = HTML Page
19
_UI_WIZARD_NEW_HEADING = HTML Page
20
_UI_WIZARD_NEW_DESCRIPTION = Create a new HTML Page.
20
_UI_WIZARD_NEW_DESCRIPTION = Create a new HTML Page.
21
_ERROR_FILENAME_MUST_END_HTML = The file name must end in one of the following extensions {0}.
21
_ERROR_FILENAME_MUST_END_HTML = The file name must end in one of the following extensions {0}.
22
_WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT = Files created outside of the Web Content folder will not be included in your deployed Web application. 
22
ResourceGroup_nameExists = The same name already exists.
23
ResourceGroup_nameExists = The same name already exists.
23
NewHTMLTemplatesWizardPage_0=Select HTML Template
24
NewHTMLTemplatesWizardPage_0=Select HTML Template
24
NewHTMLTemplatesWizardPage_1=Select a template as initial content in the HTML page.
25
NewHTMLTemplatesWizardPage_1=Select a template as initial content in the HTML page.
(-)src/org/eclipse/wst/html/ui/internal/HTMLUIMessages.java (+1 lines)
Lines 49-54 Link Here
49
	public static String _UI_WIZARD_NEW_HEADING;
49
	public static String _UI_WIZARD_NEW_HEADING;
50
	public static String _UI_WIZARD_NEW_DESCRIPTION;
50
	public static String _UI_WIZARD_NEW_DESCRIPTION;
51
	public static String _ERROR_FILENAME_MUST_END_HTML;
51
	public static String _ERROR_FILENAME_MUST_END_HTML;
52
	public static String _WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT;
52
	public static String ResourceGroup_nameExists;
53
	public static String ResourceGroup_nameExists;
53
	public static String NewHTMLTemplatesWizardPage_0;
54
	public static String NewHTMLTemplatesWizardPage_0;
54
	public static String NewHTMLTemplatesWizardPage_1;
55
	public static String NewHTMLTemplatesWizardPage_1;
(-)META-INF/MANIFEST.MF (+2 lines)
Lines 46-50 Link Here
46
 org.eclipse.wst.common.uriresolver,
46
 org.eclipse.wst.common.uriresolver,
47
 org.eclipse.wst.validation,
47
 org.eclipse.wst.validation,
48
 org.eclipse.wst.javascript.ui,
48
 org.eclipse.wst.javascript.ui,
49
 org.eclipse.wst.common.project.facet.core,
50
 org.eclipse.wst.common.modulecore,
49
 com.ibm.icu
51
 com.ibm.icu
50
Eclipse-LazyStart: true
52
Eclipse-LazyStart: true
(-)src/org/eclipse/wst/html/ui/internal/wizard/NewHTMLFileWizardPage.java (+251 lines)
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
 *******************************************************************************/
12
package org.eclipse.wst.html.ui.internal.wizard;
13
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.List;
17
18
import org.eclipse.core.resources.IProject;
19
import org.eclipse.core.resources.IResource;
20
import org.eclipse.core.resources.IWorkspace;
21
import org.eclipse.core.resources.ResourcesPlugin;
22
import org.eclipse.core.runtime.CoreException;
23
import org.eclipse.core.runtime.IPath;
24
import org.eclipse.core.runtime.IStatus;
25
import org.eclipse.core.runtime.Platform;
26
import org.eclipse.core.runtime.Preferences;
27
import org.eclipse.core.runtime.content.IContentType;
28
import org.eclipse.jface.viewers.IStructuredSelection;
29
import org.eclipse.osgi.util.NLS;
30
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
31
import org.eclipse.wst.common.componentcore.ComponentCore;
32
import org.eclipse.wst.common.componentcore.internal.util.IModuleConstants;
33
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
34
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
35
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
36
import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
37
import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
38
import org.eclipse.wst.html.core.internal.provisional.contenttype.ContentTypeIdForHTML;
39
import org.eclipse.wst.html.ui.internal.HTMLUIMessages;
40
import org.eclipse.wst.html.ui.internal.Logger;
41
42
class NewHTMLFileWizardPage extends WizardNewFileCreationPage {
43
44
	private IContentType fContentType;
45
	private List fValidExtensions = null;
46
	
47
	public NewHTMLFileWizardPage(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 
69
	 * html 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(HTMLUIMessages._ERROR_FILENAME_MUST_END_HTML, 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(HTMLUIMessages.ResourceGroup_nameExists);
103
					return false;
104
				}
105
			}
106
			
107
			// get the IProject for the selection path
108
			IProject project = getProjectFromPath(fullPath);
109
			// if inside web project, check if inside webContent folder
110
			if (project != null && isWebProject(project)) {
111
				// check that the path is inside the webContent folder
112
				IPath webContentPath = getWebContentPath(project);
113
				if (!webContentPath.isPrefixOf(fullPath)) {
114
					setMessage(HTMLUIMessages._WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT, WARNING);
115
				}
116
			}
117
		}
118
119
		return true;
120
	}
121
	
122
	/**
123
	 * Get content type associated with this new file wizard
124
	 * 
125
	 * @return IContentType
126
	 */
127
	private IContentType getContentType() {
128
		if (fContentType == null)
129
			fContentType = Platform.getContentTypeManager().getContentType(ContentTypeIdForHTML.ContentTypeID_HTML);
130
		return fContentType;
131
	}
132
133
	/**
134
	 * Get list of valid extensions for HTML Content type
135
	 * 
136
	 * @return
137
	 */
138
	private List getValidExtensions() {
139
		if (fValidExtensions == null) {
140
			IContentType type = getContentType();
141
			fValidExtensions = new ArrayList(Arrays.asList(type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC)));
142
		}
143
		return fValidExtensions;
144
	}
145
	
146
	/**
147
	 * Verifies if fileName is valid name for content type. Takes base content
148
	 * type into consideration.
149
	 * 
150
	 * @param fileName
151
	 * @return true if extension is valid for this content type
152
	 */
153
	private boolean extensionValidForContentType(String fileName) {
154
		boolean valid = false;
155
156
		IContentType type = getContentType();
157
		// there is currently an extension
158
		if (fileName.lastIndexOf('.') != -1) {
159
			// check what content types are associated with current extension
160
			IContentType[] types = Platform.getContentTypeManager().findContentTypesFor(fileName);
161
			int i = 0;
162
			while (i < types.length && !valid) {
163
				valid = types[i].isKindOf(type);
164
				++i;
165
			}
166
		}
167
		else
168
			valid = true; // no extension so valid
169
		return valid;
170
	}
171
172
	/**
173
	 * Adds default extension to the filename
174
	 * 
175
	 * @param filename
176
	 * @return
177
	 */
178
	String addDefaultExtension(String filename) {
179
		StringBuffer newFileName = new StringBuffer(filename);
180
181
		Preferences preference = HTMLCorePlugin.getDefault().getPluginPreferences();
182
		String ext = preference.getString(HTMLCorePreferenceNames.DEFAULT_EXTENSION);
183
184
		newFileName.append("."); //$NON-NLS-1$
185
		newFileName.append(ext);
186
187
		return newFileName.toString();
188
	}
189
	
190
	/**
191
	 * Returns the project that contains the specified path
192
	 * 
193
	 * @param path the path which project is needed
194
	 * @return IProject object. If path is <code>null</code> the return value 
195
	 * 		   is also <code>null</code>. 
196
	 */
197
	private IProject getProjectFromPath(IPath path) {
198
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
199
		IProject project = null;
200
		
201
		if (path != null) {
202
			if (workspace.validatePath(path.toString(), IResource.PROJECT).isOK()) {
203
				project = workspace.getRoot().getProject(path.toString());
204
			} else {
205
				project = workspace.getRoot().getFile(path).getProject();
206
			}
207
		}
208
		
209
		return project;
210
	}
211
	
212
	/**
213
	 * Checks if the specified project is a web project. 
214
	 * 
215
	 * @param project project to be checked
216
	 * @return true if the project is web project, otherwise false
217
	 */
218
	private boolean isWebProject(IProject project) {
219
		IFacetedProject faceted = null;
220
		try {
221
			faceted = ProjectFacetsManager.create(project);
222
		} catch (CoreException e) {
223
			Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
224
		}
225
		
226
		if (faceted != null && 
227
			(faceted.hasProjectFacet(ProjectFacetsManager.getProjectFacet(IModuleConstants.WST_WEB_MODULE)) || 
228
			 faceted.hasProjectFacet(ProjectFacetsManager.getProjectFacet(IModuleConstants.JST_WEB_MODULE)))) {
229
			return true;
230
		}
231
		
232
		return false;
233
	}
234
	
235
	/**
236
	 * Returns the web contents folder of the specified project
237
	 * 
238
	 * @param project the project which web contents path is needed
239
	 * @return IPath of the web contents folder
240
	 */
241
	private IPath getWebContentPath(IProject project) {
242
		IPath path = null;
243
		
244
		if (project != null && isWebProject(project)) {			
245
			IVirtualComponent component = ComponentCore.createComponent(project);
246
			path = component.getRootFolder().getWorkspaceRelativePath();
247
		}
248
		
249
		return path;
250
	}
251
}
(-)src/org/eclipse/wst/css/ui/internal/CSSUIMessages.java (+1 lines)
Lines 69-74 Link Here
69
	public static String _UI_WIZARD_NEW_HEADING;
69
	public static String _UI_WIZARD_NEW_HEADING;
70
	public static String _UI_WIZARD_NEW_DESCRIPTION;
70
	public static String _UI_WIZARD_NEW_DESCRIPTION;
71
	public static String _ERROR_FILENAME_MUST_END_CSS;
71
	public static String _ERROR_FILENAME_MUST_END_CSS;
72
	public static String _WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT;
72
	public static String Title_InvalidValue;
73
	public static String Title_InvalidValue;
73
	public static String Message_InvalidValue;
74
	public static String Message_InvalidValue;
74
	public static String FormatMenu_label;
75
	public static String FormatMenu_label;
(-)src/org/eclipse/wst/css/ui/internal/CSSUIPluginResources.properties (+1 lines)
Lines 56-61 Link Here
56
_UI_WIZARD_NEW_HEADING = Cascading Style Sheet
56
_UI_WIZARD_NEW_HEADING = Cascading Style Sheet
57
_UI_WIZARD_NEW_DESCRIPTION = Create a new CSS file.
57
_UI_WIZARD_NEW_DESCRIPTION = Create a new CSS file.
58
_ERROR_FILENAME_MUST_END_CSS = The file name must end in one of the following extensions {0}.
58
_ERROR_FILENAME_MUST_END_CSS = The file name must end in one of the following extensions {0}.
59
_WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT = Files created outside of the Web Content folder will not be included in your deployed Web application. 
59
#
60
#
60
Title_InvalidValue=Invalid Value
61
Title_InvalidValue=Invalid Value
61
Message_InvalidValue=Invalid property value.
62
Message_InvalidValue=Invalid property value.
(-)META-INF/MANIFEST.MF (+2 lines)
Lines 43-47 Link Here
43
 org.eclipse.core.resources,
43
 org.eclipse.core.resources,
44
 org.eclipse.core.runtime,
44
 org.eclipse.core.runtime,
45
 org.eclipse.ui.ide,
45
 org.eclipse.ui.ide,
46
 org.eclipse.wst.common.project.facet.core,
47
 org.eclipse.wst.common.modulecore,
46
 com.ibm.icu
48
 com.ibm.icu
47
Eclipse-LazyStart: true
49
Eclipse-LazyStart: true
(-)src/org/eclipse/wst/css/ui/internal/wizard/NewCSSWizard.java (-121 / +3 lines)
Lines 14-47 Link Here
14
import java.io.ByteArrayInputStream;
14
import java.io.ByteArrayInputStream;
15
import java.io.ByteArrayOutputStream;
15
import java.io.ByteArrayOutputStream;
16
import java.io.OutputStreamWriter;
16
import java.io.OutputStreamWriter;
17
import java.util.ArrayList;
18
import java.util.Arrays;
19
import java.util.List;
20
17
21
import org.eclipse.core.resources.IFile;
18
import org.eclipse.core.resources.IFile;
22
import org.eclipse.core.resources.IResource;
23
import org.eclipse.core.resources.IWorkspace;
24
import org.eclipse.core.resources.ResourcesPlugin;
25
import org.eclipse.core.runtime.IPath;
26
import org.eclipse.core.runtime.IStatus;
27
import org.eclipse.core.runtime.Platform;
28
import org.eclipse.core.runtime.Preferences;
19
import org.eclipse.core.runtime.Preferences;
29
import org.eclipse.core.runtime.content.IContentType;
30
import org.eclipse.jface.resource.ImageDescriptor;
20
import org.eclipse.jface.resource.ImageDescriptor;
31
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.jface.viewers.IStructuredSelection;
32
import org.eclipse.jface.viewers.StructuredSelection;
22
import org.eclipse.jface.viewers.StructuredSelection;
33
import org.eclipse.jface.wizard.Wizard;
23
import org.eclipse.jface.wizard.Wizard;
34
import org.eclipse.osgi.util.NLS;
35
import org.eclipse.ui.INewWizard;
24
import org.eclipse.ui.INewWizard;
36
import org.eclipse.ui.IWorkbench;
25
import org.eclipse.ui.IWorkbench;
37
import org.eclipse.ui.IWorkbenchPage;
26
import org.eclipse.ui.IWorkbenchPage;
38
import org.eclipse.ui.PartInitException;
27
import org.eclipse.ui.PartInitException;
39
import org.eclipse.ui.PlatformUI;
28
import org.eclipse.ui.PlatformUI;
40
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
41
import org.eclipse.ui.ide.IDE;
29
import org.eclipse.ui.ide.IDE;
42
import org.eclipse.wst.css.core.internal.CSSCorePlugin;
30
import org.eclipse.wst.css.core.internal.CSSCorePlugin;
43
import org.eclipse.wst.css.core.internal.preferences.CSSCorePreferenceNames;
44
import org.eclipse.wst.css.core.internal.provisional.contenttype.ContentTypeIdForCSS;
45
import org.eclipse.wst.css.ui.internal.CSSUIMessages;
31
import org.eclipse.wst.css.ui.internal.CSSUIMessages;
46
import org.eclipse.wst.css.ui.internal.Logger;
32
import org.eclipse.wst.css.ui.internal.Logger;
47
import org.eclipse.wst.css.ui.internal.editor.CSSEditorPluginImages;
33
import org.eclipse.wst.css.ui.internal.editor.CSSEditorPluginImages;
Lines 49-166 Link Here
49
import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames;
35
import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames;
50
36
51
public class NewCSSWizard extends Wizard implements INewWizard {
37
public class NewCSSWizard extends Wizard implements INewWizard {
52
	private WizardNewFileCreationPage fNewFilePage;
38
	private NewCSSFileWizardPage fNewFilePage;
53
	private NewCSSTemplatesWizardPage fNewFileTemplatesPage;
39
	private NewCSSTemplatesWizardPage fNewFileTemplatesPage;
54
	private IStructuredSelection fSelection;
40
	private IStructuredSelection fSelection;
55
	private IContentType fContentType;
56
	private List fValidExtensions = null;
57
58
	/**
59
	 * Adds default extension to the filename
60
	 * 
61
	 * @param filename
62
	 * @return
63
	 */
64
	String addDefaultExtension(String filename) {
65
		StringBuffer newFileName = new StringBuffer(filename);
66
67
		Preferences preference = CSSCorePlugin.getDefault().getPluginPreferences();
68
		String ext = preference.getString(CSSCorePreferenceNames.DEFAULT_EXTENSION);
69
70
		newFileName.append("."); //$NON-NLS-1$
71
		newFileName.append(ext);
72
73
		return newFileName.toString();
74
	}
75
76
	/**
77
	 * Get content type associated with this new file wizard
78
	 * 
79
	 * @return IContentType
80
	 */
81
	IContentType getContentType() {
82
		if (fContentType == null)
83
			fContentType = Platform.getContentTypeManager().getContentType(ContentTypeIdForCSS.ContentTypeID_CSS);
84
		return fContentType;
85
	}
86
87
	/**
88
	 * Get list of valid extensions for CSS Content type
89
	 * 
90
	 * @return
91
	 */
92
	List getValidExtensions() {
93
		if (fValidExtensions == null) {
94
			IContentType type = getContentType();
95
			fValidExtensions = new ArrayList(Arrays.asList(type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC)));
96
		}
97
		return fValidExtensions;
98
	}
99
	
100
	/**
101
	 * Verifies if fileName is valid name for content type. Takes base content
102
	 * type into consideration.
103
	 * 
104
	 * @param fileName
105
	 * @return true if extension is valid for this content type
106
	 */
107
	boolean extensionValidForContentType(String fileName) {
108
		boolean valid = false;
109
110
		IContentType type = getContentType();
111
		// there is currently an extension
112
		if (fileName.lastIndexOf('.') != -1) {
113
			// check what content types are associated with current extension
114
			IContentType[] types = Platform.getContentTypeManager().findContentTypesFor(fileName);
115
			int i = 0;
116
			while (i < types.length && !valid) {
117
				valid = types[i].isKindOf(type);
118
				++i;
119
			}
120
		}
121
		else
122
			valid = true; // no extension so valid
123
		return valid;
124
	}
125
41
126
	public void addPages() {
42
	public void addPages() {
127
		fNewFilePage = new WizardNewFileCreationPage("CSSWizardNewFileCreationPage", new StructuredSelection(IDE.computeSelectedResources(fSelection))) { //$NON-NLS-1$
43
		fNewFilePage = new NewCSSFileWizardPage("CSSWizardNewFileCreationPage", new StructuredSelection(IDE.computeSelectedResources(fSelection))); //$NON-NLS-1$
128
			protected boolean validatePage() {
129
				String fileName = getFileName();
130
				IPath fullPath = getContainerFullPath();
131
				if ((fullPath != null) && (fullPath.isEmpty() == false) && (fileName != null)) {
132
					// check that filename does not contain invalid extension
133
					if (!extensionValidForContentType(fileName)) {
134
						setErrorMessage(NLS.bind(CSSUIMessages._ERROR_FILENAME_MUST_END_CSS, getValidExtensions().toString()));
135
						return false;
136
					}
137
					// no file extension specified so check adding default
138
					// extension doesn't equal a file that already exists
139
					if (fileName.lastIndexOf('.') == -1) {
140
						String newFileName = addDefaultExtension(fileName);
141
						IPath resourcePath = fullPath.append(newFileName);
142
143
						IWorkspace workspace = ResourcesPlugin.getWorkspace();
144
						IStatus result = workspace.validatePath(resourcePath.toString(), IResource.FOLDER);
145
						if (!result.isOK()) {
146
							// path invalid
147
							setErrorMessage(result.getMessage());
148
							return false;
149
						}
150
151
						if ((workspace.getRoot().getFolder(resourcePath).exists() || workspace.getRoot().getFile(resourcePath).exists())) {
152
							setErrorMessage(CSSUIMessages.ResourceGroup_nameExists);
153
							return false;
154
						}
155
					}
156
				}
157
				setErrorMessage(null);
158
				return super.validatePage();
159
			}
160
		};
161
		fNewFilePage.setTitle(CSSUIMessages._UI_WIZARD_NEW_HEADING); //$NON-NLS-1$
44
		fNewFilePage.setTitle(CSSUIMessages._UI_WIZARD_NEW_HEADING); //$NON-NLS-1$
162
		fNewFilePage.setDescription(CSSUIMessages._UI_WIZARD_NEW_DESCRIPTION); //$NON-NLS-1$
45
		fNewFilePage.setDescription(CSSUIMessages._UI_WIZARD_NEW_DESCRIPTION); //$NON-NLS-1$
163
164
		addPage(fNewFilePage);
46
		addPage(fNewFilePage);
165
47
166
		fNewFileTemplatesPage = new NewCSSTemplatesWizardPage();
48
		fNewFileTemplatesPage = new NewCSSTemplatesWizardPage();
Lines 199-205 Link Here
199
		// no file extension specified so add default extension
81
		// no file extension specified so add default extension
200
		String fileName = fNewFilePage.getFileName();
82
		String fileName = fNewFilePage.getFileName();
201
		if (fileName.lastIndexOf('.') == -1) {
83
		if (fileName.lastIndexOf('.') == -1) {
202
			String newFileName = addDefaultExtension(fileName);
84
			String newFileName = fNewFilePage.addDefaultExtension(fileName);
203
			fNewFilePage.setFileName(newFileName);
85
			fNewFilePage.setFileName(newFileName);
204
		}
86
		}
205
87
(-)src/org/eclipse/wst/css/ui/internal/wizard/NewCSSFileWizardPage.java (+252 lines)
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
 *******************************************************************************/
12
package org.eclipse.wst.css.ui.internal.wizard;
13
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.List;
17
18
import org.eclipse.core.resources.IProject;
19
import org.eclipse.core.resources.IResource;
20
import org.eclipse.core.resources.IWorkspace;
21
import org.eclipse.core.resources.ResourcesPlugin;
22
import org.eclipse.core.runtime.CoreException;
23
import org.eclipse.core.runtime.IPath;
24
import org.eclipse.core.runtime.IStatus;
25
import org.eclipse.core.runtime.Platform;
26
import org.eclipse.core.runtime.Preferences;
27
import org.eclipse.core.runtime.content.IContentType;
28
import org.eclipse.jface.viewers.IStructuredSelection;
29
import org.eclipse.osgi.util.NLS;
30
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
31
import org.eclipse.wst.common.componentcore.ComponentCore;
32
import org.eclipse.wst.common.componentcore.internal.util.IModuleConstants;
33
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
34
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
35
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
36
import org.eclipse.wst.css.core.internal.CSSCorePlugin;
37
import org.eclipse.wst.css.core.internal.preferences.CSSCorePreferenceNames;
38
import org.eclipse.wst.css.core.internal.provisional.contenttype.ContentTypeIdForCSS;
39
import org.eclipse.wst.css.ui.internal.CSSUIMessages;
40
import org.eclipse.wst.css.ui.internal.Logger;
41
42
class NewCSSFileWizardPage extends WizardNewFileCreationPage {
43
44
	private IContentType fContentType;
45
	private List fValidExtensions = null;
46
	
47
	public NewCSSFileWizardPage(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 
69
	 * css 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(CSSUIMessages._ERROR_FILENAME_MUST_END_CSS, 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(CSSUIMessages.ResourceGroup_nameExists);
103
					return false;
104
				}
105
			}
106
			
107
			// get the IProject for the selection path
108
			IProject project = getProjectFromPath(fullPath);
109
			// if inside web project, check if inside webContent folder
110
			if (project != null && isWebProject(project)) {
111
				// check that the path is inside the webContent folder
112
				IPath webContentPath = getWebContentPath(project);
113
				if (!webContentPath.isPrefixOf(fullPath)) {
114
					setMessage(CSSUIMessages._WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT, WARNING);
115
				}
116
			}
117
		}
118
119
		return true;
120
	}
121
	
122
	/**
123
	 * Get content type associated with this new file wizard
124
	 * 
125
	 * @return IContentType
126
	 */
127
	private IContentType getContentType() {
128
		if (fContentType == null)
129
			fContentType = Platform.getContentTypeManager().getContentType(ContentTypeIdForCSS.ContentTypeID_CSS);
130
		return fContentType;
131
	}
132
133
	/**
134
	 * Get list of valid extensions for CSS Content type
135
	 * 
136
	 * @return
137
	 */
138
	private List getValidExtensions() {
139
		if (fValidExtensions == null) {
140
			IContentType type = getContentType();
141
			fValidExtensions = new ArrayList(Arrays.asList(type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC)));
142
		}
143
		return fValidExtensions;
144
	}
145
	
146
	/**
147
	 * Verifies if fileName is valid name for content type. Takes base content
148
	 * type into consideration.
149
	 * 
150
	 * @param fileName
151
	 * @return true if extension is valid for this content type
152
	 */
153
	private boolean extensionValidForContentType(String fileName) {
154
		boolean valid = false;
155
156
		IContentType type = getContentType();
157
		// there is currently an extension
158
		if (fileName.lastIndexOf('.') != -1) {
159
			// check what content types are associated with current extension
160
			IContentType[] types = Platform.getContentTypeManager().findContentTypesFor(fileName);
161
			int i = 0;
162
			while (i < types.length && !valid) {
163
				valid = types[i].isKindOf(type);
164
				++i;
165
			}
166
		}
167
		else
168
			valid = true; // no extension so valid
169
		return valid;
170
	}
171
172
	/**
173
	 * Adds default extension to the filename
174
	 * 
175
	 * @param filename
176
	 * @return
177
	 */
178
	String addDefaultExtension(String filename) {
179
		StringBuffer newFileName = new StringBuffer(filename);
180
181
		Preferences preference = CSSCorePlugin.getDefault().getPluginPreferences();
182
		String ext = preference.getString(CSSCorePreferenceNames.DEFAULT_EXTENSION);
183
184
		newFileName.append("."); //$NON-NLS-1$
185
		newFileName.append(ext);
186
187
		return newFileName.toString();
188
	}
189
	
190
	/**
191
	 * Returns the project that contains the specified path
192
	 * 
193
	 * @param path the path which project is needed
194
	 * @return IProject object. If path is <code>null</code> the return value 
195
	 * 		   is also <code>null</code>. 
196
	 */
197
	private IProject getProjectFromPath(IPath path) {
198
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
199
		IProject project = null;
200
		
201
		if (path != null) {
202
			if (workspace.validatePath(path.toString(), IResource.PROJECT).isOK()) {
203
				project = workspace.getRoot().getProject(path.toString());
204
			} else {
205
				project = workspace.getRoot().getFile(path).getProject();
206
			}
207
		}
208
		
209
		return project;
210
	}
211
	
212
	/**
213
	 * Checks if the specified project is a web project. 
214
	 * 
215
	 * @param project project to be checked
216
	 * @return true if the project is web project, otherwise false
217
	 */
218
	private boolean isWebProject(IProject project) {
219
		IFacetedProject faceted = null;
220
		try {
221
			faceted = ProjectFacetsManager.create(project);
222
		} catch (CoreException e) {
223
			Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
224
		}
225
		
226
		if (faceted != null && 
227
			(faceted.hasProjectFacet(ProjectFacetsManager.getProjectFacet(IModuleConstants.WST_WEB_MODULE)) || 
228
			 faceted.hasProjectFacet(ProjectFacetsManager.getProjectFacet(IModuleConstants.JST_WEB_MODULE)))) {
229
			return true;
230
		}
231
		
232
		return false;
233
	}
234
	
235
	/**
236
	 * Returns the web contents folder of the specified project
237
	 * 
238
	 * @param project the project which web contents path is needed
239
	 * @return IPath of the web contents folder
240
	 */
241
	private IPath getWebContentPath(IProject project) {
242
		IPath path = null;
243
		
244
		if (project != null && isWebProject(project)) {			
245
			IVirtualComponent component = ComponentCore.createComponent(project);
246
			path = component.getRootFolder().getWorkspaceRelativePath();
247
		}
248
		
249
		return path;
250
	}
251
252
}
(-)src/org/eclipse/jst/jsp/ui/internal/JSPUIPluginResources.properties (-1 / +3 lines)
Lines 36-42 Link Here
36
_UI_WIZARD_NEW_TITLE = New JavaServer Page
36
_UI_WIZARD_NEW_TITLE = New JavaServer Page
37
_UI_WIZARD_NEW_HEADING = JavaServer Page
37
_UI_WIZARD_NEW_HEADING = JavaServer Page
38
_UI_WIZARD_NEW_DESCRIPTION = Create a new JavaServer Page.
38
_UI_WIZARD_NEW_DESCRIPTION = Create a new JavaServer Page.
39
_ERROR_FILENAME_MUST_END_JSP = The file name must end in one of the following extensions {0}.
39
_ERROR_FILENAME_MUST_END_JSP = The file name must end in one of the following extensions {0}. 
40
_WARNING_FILE_MUST_BE_INSIDE_JAVA_PROJECT = JavaServer Pages created in projects that do not support Java might not work as expected. 
41
_WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT = Files created outside of the Web Content folder will not be included in your deployed Web application. 
40
ResourceGroup_nameExists = The same name already exists.
42
ResourceGroup_nameExists = The same name already exists.
41
NewJSPTemplatesWizardPage_0=Select JSP Template
43
NewJSPTemplatesWizardPage_0=Select JSP Template
42
NewJSPTemplatesWizardPage_1=Select a template as initial content in the JSP page.
44
NewJSPTemplatesWizardPage_1=Select a template as initial content in the JSP page.
(-)src/org/eclipse/jst/jsp/ui/internal/JSPUIMessages.java (+2 lines)
Lines 61-66 Link Here
61
	public static String _UI_WIZARD_NEW_HEADING;
61
	public static String _UI_WIZARD_NEW_HEADING;
62
	public static String _UI_WIZARD_NEW_DESCRIPTION;
62
	public static String _UI_WIZARD_NEW_DESCRIPTION;
63
	public static String _ERROR_FILENAME_MUST_END_JSP;
63
	public static String _ERROR_FILENAME_MUST_END_JSP;
64
	public static String _WARNING_FILE_MUST_BE_INSIDE_JAVA_PROJECT;
65
	public static String _WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT;
64
	public static String ResourceGroup_nameExists;
66
	public static String ResourceGroup_nameExists;
65
	public static String NewJSPTemplatesWizardPage_0;
67
	public static String NewJSPTemplatesWizardPage_0;
66
	public static String NewJSPTemplatesWizardPage_1;
68
	public static String NewJSPTemplatesWizardPage_1;
(-)META-INF/MANIFEST.MF (+2 lines)
Lines 58-62 Link Here
58
 org.eclipse.ui.ide,
58
 org.eclipse.ui.ide,
59
 org.eclipse.wst.javascript.ui,
59
 org.eclipse.wst.javascript.ui,
60
 org.eclipse.ui.views,
60
 org.eclipse.ui.views,
61
 org.eclipse.wst.common.project.facet.core,
62
 org.eclipse.wst.common.modulecore,
61
 com.ibm.icu
63
 com.ibm.icu
62
Eclipse-LazyStart: true
64
Eclipse-LazyStart: true
(-)src/org/eclipse/jst/jsp/ui/internal/wizard/NewJSPWizard.java (-121 / +3 lines)
Lines 13-165 Link Here
13
import java.io.ByteArrayInputStream;
13
import java.io.ByteArrayInputStream;
14
import java.io.ByteArrayOutputStream;
14
import java.io.ByteArrayOutputStream;
15
import java.io.OutputStreamWriter;
15
import java.io.OutputStreamWriter;
16
import java.util.ArrayList;
17
import java.util.Arrays;
18
import java.util.List;
19
16
20
import org.eclipse.core.resources.IFile;
17
import org.eclipse.core.resources.IFile;
21
import org.eclipse.core.resources.IResource;
22
import org.eclipse.core.resources.IWorkspace;
23
import org.eclipse.core.resources.ResourcesPlugin;
24
import org.eclipse.core.runtime.IPath;
25
import org.eclipse.core.runtime.IStatus;
26
import org.eclipse.core.runtime.Platform;
27
import org.eclipse.core.runtime.Preferences;
18
import org.eclipse.core.runtime.Preferences;
28
import org.eclipse.core.runtime.content.IContentType;
29
import org.eclipse.jface.resource.ImageDescriptor;
19
import org.eclipse.jface.resource.ImageDescriptor;
30
import org.eclipse.jface.viewers.IStructuredSelection;
20
import org.eclipse.jface.viewers.IStructuredSelection;
31
import org.eclipse.jface.viewers.StructuredSelection;
21
import org.eclipse.jface.viewers.StructuredSelection;
32
import org.eclipse.jface.wizard.Wizard;
22
import org.eclipse.jface.wizard.Wizard;
33
import org.eclipse.jst.jsp.core.internal.JSPCorePlugin;
23
import org.eclipse.jst.jsp.core.internal.JSPCorePlugin;
34
import org.eclipse.jst.jsp.core.internal.preferences.JSPCorePreferenceNames;
35
import org.eclipse.jst.jsp.core.internal.provisional.contenttype.ContentTypeIdForJSP;
36
import org.eclipse.jst.jsp.ui.internal.JSPUIMessages;
24
import org.eclipse.jst.jsp.ui.internal.JSPUIMessages;
37
import org.eclipse.jst.jsp.ui.internal.Logger;
25
import org.eclipse.jst.jsp.ui.internal.Logger;
38
import org.eclipse.jst.jsp.ui.internal.editor.JSPEditorPluginImageHelper;
26
import org.eclipse.jst.jsp.ui.internal.editor.JSPEditorPluginImageHelper;
39
import org.eclipse.jst.jsp.ui.internal.editor.JSPEditorPluginImages;
27
import org.eclipse.jst.jsp.ui.internal.editor.JSPEditorPluginImages;
40
import org.eclipse.osgi.util.NLS;
41
import org.eclipse.ui.INewWizard;
28
import org.eclipse.ui.INewWizard;
42
import org.eclipse.ui.IWorkbench;
29
import org.eclipse.ui.IWorkbench;
43
import org.eclipse.ui.IWorkbenchPage;
30
import org.eclipse.ui.IWorkbenchPage;
44
import org.eclipse.ui.PartInitException;
31
import org.eclipse.ui.PartInitException;
45
import org.eclipse.ui.PlatformUI;
32
import org.eclipse.ui.PlatformUI;
46
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
47
import org.eclipse.ui.ide.IDE;
33
import org.eclipse.ui.ide.IDE;
48
import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames;
34
import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames;
49
35
50
public class NewJSPWizard extends Wizard implements INewWizard {
36
public class NewJSPWizard extends Wizard implements INewWizard {
51
	private WizardNewFileCreationPage fNewFilePage;
37
	private NewJSPFileWizardPage fNewFilePage;
52
	private NewJSPTemplatesWizardPage fNewFileTemplatesPage;
38
	private NewJSPTemplatesWizardPage fNewFileTemplatesPage;
53
	private IStructuredSelection fSelection;
39
	private IStructuredSelection fSelection;
54
	private IContentType fContentType;
55
	private List fValidExtensions = null;
56
57
	/**
58
	 * Adds default extension to the filename
59
	 * 
60
	 * @param filename
61
	 * @return
62
	 */
63
	String addDefaultExtension(String filename) {
64
		StringBuffer newFileName = new StringBuffer(filename);
65
66
		Preferences preference = JSPCorePlugin.getDefault().getPluginPreferences();
67
		String ext = preference.getString(JSPCorePreferenceNames.DEFAULT_EXTENSION);
68
69
		newFileName.append("."); //$NON-NLS-1$
70
		newFileName.append(ext);
71
72
		return newFileName.toString();
73
	}
74
75
	/**
76
	 * Get content type associated with this new file wizard
77
	 * 
78
	 * @return IContentType
79
	 */
80
	IContentType getContentType() {
81
		if (fContentType == null)
82
			fContentType = Platform.getContentTypeManager().getContentType(ContentTypeIdForJSP.ContentTypeID_JSP);
83
		return fContentType;
84
	}
85
86
	/**
87
	 * Get list of valid extensions for JSP Content type
88
	 * 
89
	 * @return
90
	 */
91
	List getValidExtensions() {
92
		if (fValidExtensions == null) {
93
			IContentType type = getContentType();
94
			fValidExtensions = new ArrayList(Arrays.asList(type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC)));
95
		}
96
		return fValidExtensions;
97
	}
98
99
	/**
100
	 * Verifies if fileName is valid name for content type. Takes base content
101
	 * type into consideration.
102
	 * 
103
	 * @param fileName
104
	 * @return true if extension is valid for this content type
105
	 */
106
	boolean extensionValidForContentType(String fileName) {
107
		boolean valid = false;
108
109
		IContentType type = getContentType();
110
		// there is currently an extension
111
		if (fileName.lastIndexOf('.') != -1) {
112
			// check what content types are associated with current extension
113
			IContentType[] types = Platform.getContentTypeManager().findContentTypesFor(fileName);
114
			int i = 0;
115
			while (i < types.length && !valid) {
116
				valid = types[i].isKindOf(type);
117
				++i;
118
			}
119
		}
120
		else
121
			valid = true; // no extension so valid
122
		return valid;
123
	}
124
40
125
	public void addPages() {
41
	public void addPages() {
126
		fNewFilePage = new WizardNewFileCreationPage("JSPWizardNewFileCreationPage", new StructuredSelection(IDE.computeSelectedResources(fSelection))) { //$NON-NLS-1$
42
		fNewFilePage = new NewJSPFileWizardPage("JSPWizardNewFileCreationPage", new StructuredSelection(IDE.computeSelectedResources(fSelection))); //$NON-NLS-1$ 
127
			protected boolean validatePage() {
128
				String fileName = getFileName();
129
				IPath fullPath = getContainerFullPath();
130
				if ((fullPath != null) && (fullPath.isEmpty() == false) && (fileName != null)) {
131
					// check that filename does not contain invalid extension
132
					if (!extensionValidForContentType(fileName)) {
133
						setErrorMessage(NLS.bind(JSPUIMessages._ERROR_FILENAME_MUST_END_JSP, getValidExtensions().toString()));
134
						return false;
135
					}
136
					// no file extension specified so check adding default
137
					// extension doesn't equal a file that already exists
138
					if (fileName.lastIndexOf('.') == -1) {
139
						String newFileName = addDefaultExtension(fileName);
140
						IPath resourcePath = fullPath.append(newFileName);
141
142
						IWorkspace workspace = ResourcesPlugin.getWorkspace();
143
						IStatus result = workspace.validatePath(resourcePath.toString(), IResource.FOLDER);
144
						if (!result.isOK()) {
145
							// path invalid
146
							setErrorMessage(result.getMessage());
147
							return false;
148
						}
149
150
						if ((workspace.getRoot().getFolder(resourcePath).exists() || workspace.getRoot().getFile(resourcePath).exists())) {
151
							setErrorMessage(JSPUIMessages.ResourceGroup_nameExists);
152
							return false;
153
						}
154
					}
155
				}
156
				setErrorMessage(null);
157
				return super.validatePage();
158
			}
159
		};
160
		fNewFilePage.setTitle(JSPUIMessages._UI_WIZARD_NEW_HEADING);
43
		fNewFilePage.setTitle(JSPUIMessages._UI_WIZARD_NEW_HEADING);
161
		fNewFilePage.setDescription(JSPUIMessages._UI_WIZARD_NEW_DESCRIPTION);
44
		fNewFilePage.setDescription(JSPUIMessages._UI_WIZARD_NEW_DESCRIPTION);
162
163
		addPage(fNewFilePage);
45
		addPage(fNewFilePage);
164
46
165
		fNewFileTemplatesPage = new NewJSPTemplatesWizardPage();
47
		fNewFileTemplatesPage = new NewJSPTemplatesWizardPage();
Lines 199-205 Link Here
199
		// no file extension specified so add default extension
81
		// no file extension specified so add default extension
200
		String fileName = fNewFilePage.getFileName();
82
		String fileName = fNewFilePage.getFileName();
201
		if (fileName.lastIndexOf('.') == -1) {
83
		if (fileName.lastIndexOf('.') == -1) {
202
			String newFileName = addDefaultExtension(fileName);
84
			String newFileName = fNewFilePage.addDefaultExtension(fileName);
203
			fNewFilePage.setFileName(newFileName);
85
			fNewFilePage.setFileName(newFileName);
204
		}
86
		}
205
87
(-)plugin.xml (-18 / +5 lines)
Lines 420-443 Link Here
420
        wizardId="org.eclipse.jst.jsp.ui.internal.wizard.NewJSPWizard">
420
        wizardId="org.eclipse.jst.jsp.ui.internal.wizard.NewJSPWizard">
421
			<enablement>
421
			<enablement>
422
				<or>
422
				<or>
423
					 
423
					<adapt type="org.eclipse.core.resources.IResource">
424
		           <adapt type="org.eclipse.core.resources.IProject">
424
						<test
425
		           	  <or>
425
							property="org.eclipse.wst.common.project.facet.core.projectFacet"
426
			              <test
426
							value="jst.web"/>
427
			                    property="org.eclipse.wst.common.project.facet.core.projectFacet"
427
					</adapt>
428
			                    value="wst.web"/>
429
			              <test
430
			                    property="org.eclipse.wst.common.project.facet.core.projectFacet"
431
			                    value="jst.web"/>
432
			          </or>
433
		           </adapt>
434
435
					<instanceof
436
						value="org.eclipse.core.resources.IFolder" />
437
438
					<instanceof
439
						value="org.eclipse.core.resources.IFile" /> 				
440
441
				</or>
428
				</or>
442
			</enablement>
429
			</enablement>
443
		</commonWizard>
430
		</commonWizard>
(-)src/org/eclipse/jst/jsp/ui/internal/wizard/NewJSPFileWizardPage.java (+278 lines)
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
}
(-)src/org/eclipse/wst/javascript/ui/internal/editor/JavaScriptUIMessages.java (+1 lines)
Lines 41-46 Link Here
41
	public static String _UI_WIZARD_NEW_HEADING;
41
	public static String _UI_WIZARD_NEW_HEADING;
42
	public static String _UI_WIZARD_NEW_DESCRIPTION;
42
	public static String _UI_WIZARD_NEW_DESCRIPTION;
43
	public static String _ERROR_FILENAME_MUST_END_JS;
43
	public static String _ERROR_FILENAME_MUST_END_JS;
44
	public static String _WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT;
44
	public static String ResourceGroup_nameExists;
45
	public static String ResourceGroup_nameExists;
45
	public static String Editor_Cut_label; // Resource bundle
46
	public static String Editor_Cut_label; // Resource bundle
46
	public static String Editor_Cut_tooltip; // Resource bundle
47
	public static String Editor_Cut_tooltip; // Resource bundle
(-)src/org/eclipse/wst/javascript/ui/internal/editor/JSUIPluginResources.properties (+1 lines)
Lines 30-35 Link Here
30
_UI_WIZARD_NEW_HEADING = JavaScript
30
_UI_WIZARD_NEW_HEADING = JavaScript
31
_UI_WIZARD_NEW_DESCRIPTION = Create a new JavaScript file.
31
_UI_WIZARD_NEW_DESCRIPTION = Create a new JavaScript file.
32
_ERROR_FILENAME_MUST_END_JS = The file name must end in one of the following extensions {0}.
32
_ERROR_FILENAME_MUST_END_JS = The file name must end in one of the following extensions {0}.
33
_WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT = Files created outside of the Web Content folder will not be included in your deployed Web application. 
33
ResourceGroup_nameExists = The same name already exists.
34
ResourceGroup_nameExists = The same name already exists.
34
# these are copied from sse ui
35
# these are copied from sse ui
35
Editor_Cut_label=Cu&t@Ctrl+X
36
Editor_Cut_label=Cu&t@Ctrl+X
(-)META-INF/MANIFEST.MF (-1 / +3 lines)
Lines 34-38 Link Here
34
 org.eclipse.core.runtime,
34
 org.eclipse.core.runtime,
35
 org.eclipse.ui.ide,
35
 org.eclipse.ui.ide,
36
 org.eclipse.wst.xml.core,
36
 org.eclipse.wst.xml.core,
37
 com.ibm.icu
37
 com.ibm.icu,
38
 org.eclipse.wst.common.project.facet.core,
39
 org.eclipse.wst.common.modulecore
38
Eclipse-LazyStart: true
40
Eclipse-LazyStart: true
(-)src/org/eclipse/wst/javascript/ui/internal/wizard/NewJSWizard.java (-124 / +14 lines)
Lines 1-150 Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 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
 *******************************************************************************/
1
package org.eclipse.wst.javascript.ui.internal.wizard;
12
package org.eclipse.wst.javascript.ui.internal.wizard;
2
13
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.List;
6
7
import org.eclipse.core.resources.IFile;
14
import org.eclipse.core.resources.IFile;
8
import org.eclipse.core.resources.IResource;
9
import org.eclipse.core.resources.IWorkspace;
10
import org.eclipse.core.resources.ResourcesPlugin;
11
import org.eclipse.core.runtime.IPath;
12
import org.eclipse.core.runtime.IStatus;
13
import org.eclipse.core.runtime.Platform;
14
import org.eclipse.core.runtime.Preferences;
15
import org.eclipse.core.runtime.content.IContentType;
16
import org.eclipse.jface.resource.ImageDescriptor;
15
import org.eclipse.jface.resource.ImageDescriptor;
17
import org.eclipse.jface.viewers.IStructuredSelection;
16
import org.eclipse.jface.viewers.IStructuredSelection;
18
import org.eclipse.jface.viewers.StructuredSelection;
17
import org.eclipse.jface.viewers.StructuredSelection;
19
import org.eclipse.jface.wizard.Wizard;
18
import org.eclipse.jface.wizard.Wizard;
20
import org.eclipse.osgi.util.NLS;
21
import org.eclipse.ui.INewWizard;
19
import org.eclipse.ui.INewWizard;
22
import org.eclipse.ui.IWorkbench;
20
import org.eclipse.ui.IWorkbench;
23
import org.eclipse.ui.IWorkbenchPage;
21
import org.eclipse.ui.IWorkbenchPage;
24
import org.eclipse.ui.PartInitException;
22
import org.eclipse.ui.PartInitException;
25
import org.eclipse.ui.PlatformUI;
23
import org.eclipse.ui.PlatformUI;
26
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
27
import org.eclipse.ui.ide.IDE;
24
import org.eclipse.ui.ide.IDE;
28
import org.eclipse.wst.javascript.core.internal.JavaScriptCorePlugin;
29
import org.eclipse.wst.javascript.core.internal.contenttype.ContentTypeIdForJavaScript;
30
import org.eclipse.wst.javascript.core.internal.preferences.JavaScriptCorePreferenceNames;
31
import org.eclipse.wst.javascript.ui.internal.editor.JSEditorPlugin;
25
import org.eclipse.wst.javascript.ui.internal.editor.JSEditorPlugin;
32
import org.eclipse.wst.javascript.ui.internal.editor.JavaScriptUIMessages;
26
import org.eclipse.wst.javascript.ui.internal.editor.JavaScriptUIMessages;
33
import org.eclipse.wst.javascript.ui.internal.editor.Logger;
27
import org.eclipse.wst.javascript.ui.internal.editor.Logger;
34
28
35
public class NewJSWizard extends Wizard implements INewWizard {
29
public class NewJSWizard extends Wizard implements INewWizard {
36
	static String PAGE_IMAGE = "/icons/full/wizban/newjscriptfile_wiz.gif"; //$NON-NLS-1$
30
	static String PAGE_IMAGE = "/icons/full/wizban/newjscriptfile_wiz.gif"; //$NON-NLS-1$
37
	private WizardNewFileCreationPage fNewFilePage;
31
	private NewJSFileWizardPage fNewFilePage;
38
	private IStructuredSelection fSelection;
32
	private IStructuredSelection fSelection;
39
	private IContentType fContentType;
40
	private List fValidExtensions = null;
41
42
	/**
43
	 * Adds default extension to the filename
44
	 * 
45
	 * @param filename
46
	 * @return
47
	 */
48
	String addDefaultExtension(String filename) {
49
		StringBuffer newFileName = new StringBuffer(filename);
50
51
		Preferences preference = JavaScriptCorePlugin.getDefault().getPluginPreferences();
52
		String ext = preference.getString(JavaScriptCorePreferenceNames.DEFAULT_EXTENSION);
53
54
		newFileName.append("."); //$NON-NLS-1$
55
		newFileName.append(ext);
56
57
		return newFileName.toString();
58
	}
59
60
	/**
61
	 * Get content type associated with this new file wizard
62
	 * 
63
	 * @return IContentType
64
	 */
65
	IContentType getContentType() {
66
		if (fContentType == null)
67
			fContentType = Platform.getContentTypeManager().getContentType(ContentTypeIdForJavaScript.ContentTypeID_JAVASCRIPT);
68
		return fContentType;
69
	}
70
71
	/**
72
	 * Get list of valid extensions for JavaScript Content type
73
	 * 
74
	 * @return
75
	 */
76
	List getValidExtensions() {
77
		if (fValidExtensions == null) {
78
			IContentType type = getContentType();
79
			fValidExtensions = new ArrayList(Arrays.asList(type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC)));
80
		}
81
		return fValidExtensions;
82
	}
83
	
84
	/**
85
	 * Verifies if fileName is valid name for content type. Takes base content
86
	 * type into consideration.
87
	 * 
88
	 * @param fileName
89
	 * @return true if extension is valid for this content type
90
	 */
91
	boolean extensionValidForContentType(String fileName) {
92
		boolean valid = false;
93
94
		IContentType type = getContentType();
95
		// there is currently an extension
96
		if (fileName.lastIndexOf('.') != -1) {
97
			// check what content types are associated with current extension
98
			IContentType[] types = Platform.getContentTypeManager().findContentTypesFor(fileName);
99
			int i = 0;
100
			while (i < types.length && !valid) {
101
				valid = types[i].isKindOf(type);
102
				++i;
103
			}
104
		}
105
		else
106
			valid = true; // no extension so valid
107
		return valid;
108
	}
109
33
110
	public void addPages() {
34
	public void addPages() {
111
		fNewFilePage = new WizardNewFileCreationPage("JSWizardNewFileCreationPage", new StructuredSelection(IDE.computeSelectedResources(fSelection))) { //$NON-NLS-1$
35
		fNewFilePage = new NewJSFileWizardPage("JSWizardNewFileCreationPage", new StructuredSelection(IDE.computeSelectedResources(fSelection))); //$NON-NLS-1$
112
			protected boolean validatePage() {
113
				String fileName = getFileName();
114
				IPath fullPath = getContainerFullPath();
115
				if ((fullPath != null) && (fullPath.isEmpty() == false) && (fileName != null)) {
116
					// check that filename does not contain invalid extension
117
					if (!extensionValidForContentType(fileName)) {
118
						setErrorMessage(NLS.bind(JavaScriptUIMessages._ERROR_FILENAME_MUST_END_JS, getValidExtensions().toString()));
119
						return false;
120
					}
121
					// no file extension specified so check adding default
122
					// extension doesn't equal a file that already exists
123
					if (fileName.lastIndexOf('.') == -1) {
124
						String newFileName = addDefaultExtension(fileName);
125
						IPath resourcePath = fullPath.append(newFileName);
126
127
						IWorkspace workspace = ResourcesPlugin.getWorkspace();
128
						IStatus result = workspace.validatePath(resourcePath.toString(), IResource.FOLDER);
129
						if (!result.isOK()) {
130
							// path invalid
131
							setErrorMessage(result.getMessage());
132
							return false;
133
						}
134
135
						if ((workspace.getRoot().getFolder(resourcePath).exists() || workspace.getRoot().getFile(resourcePath).exists())) {
136
							setErrorMessage(JavaScriptUIMessages.ResourceGroup_nameExists);
137
							return false;
138
						}
139
					}
140
				}
141
				setErrorMessage(null);
142
				return super.validatePage();
143
			}
144
		};
145
		fNewFilePage.setTitle(JavaScriptUIMessages._UI_WIZARD_NEW_HEADING); //$NON-NLS-1$
36
		fNewFilePage.setTitle(JavaScriptUIMessages._UI_WIZARD_NEW_HEADING); //$NON-NLS-1$
146
		fNewFilePage.setDescription(JavaScriptUIMessages._UI_WIZARD_NEW_DESCRIPTION); //$NON-NLS-1$
37
		fNewFilePage.setDescription(JavaScriptUIMessages._UI_WIZARD_NEW_DESCRIPTION); //$NON-NLS-1$
147
148
		addPage(fNewFilePage);
38
		addPage(fNewFilePage);
149
	}
39
	}
150
40
Lines 183-189 Link Here
183
		// no file extension specified so add default extension
73
		// no file extension specified so add default extension
184
		String fileName = fNewFilePage.getFileName();
74
		String fileName = fNewFilePage.getFileName();
185
		if (fileName.lastIndexOf('.') == -1) {
75
		if (fileName.lastIndexOf('.') == -1) {
186
			String newFileName = addDefaultExtension(fileName);
76
			String newFileName = fNewFilePage.addDefaultExtension(fileName);
187
			fNewFilePage.setFileName(newFileName);
77
			fNewFilePage.setFileName(newFileName);
188
		}
78
		}
189
79
(-)src/org/eclipse/wst/javascript/ui/internal/wizard/NewJSFileWizardPage.java (+251 lines)
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
 *******************************************************************************/
12
package org.eclipse.wst.javascript.ui.internal.wizard;
13
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.List;
17
18
import org.eclipse.core.resources.IProject;
19
import org.eclipse.core.resources.IResource;
20
import org.eclipse.core.resources.IWorkspace;
21
import org.eclipse.core.resources.ResourcesPlugin;
22
import org.eclipse.core.runtime.CoreException;
23
import org.eclipse.core.runtime.IPath;
24
import org.eclipse.core.runtime.IStatus;
25
import org.eclipse.core.runtime.Platform;
26
import org.eclipse.core.runtime.Preferences;
27
import org.eclipse.core.runtime.content.IContentType;
28
import org.eclipse.jface.viewers.IStructuredSelection;
29
import org.eclipse.osgi.util.NLS;
30
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
31
import org.eclipse.wst.common.componentcore.ComponentCore;
32
import org.eclipse.wst.common.componentcore.internal.util.IModuleConstants;
33
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
34
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
35
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
36
import org.eclipse.wst.javascript.core.internal.JavaScriptCorePlugin;
37
import org.eclipse.wst.javascript.core.internal.contenttype.ContentTypeIdForJavaScript;
38
import org.eclipse.wst.javascript.core.internal.preferences.JavaScriptCorePreferenceNames;
39
import org.eclipse.wst.javascript.ui.internal.editor.JavaScriptUIMessages;
40
import org.eclipse.wst.javascript.ui.internal.editor.Logger;
41
42
class NewJSFileWizardPage extends WizardNewFileCreationPage {
43
	
44
	private IContentType fContentType;
45
	private List fValidExtensions = null;
46
	
47
	public NewJSFileWizardPage(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 
69
	 * javascript 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(JavaScriptUIMessages._ERROR_FILENAME_MUST_END_JS, 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(JavaScriptUIMessages.ResourceGroup_nameExists);
103
					return false;
104
				}
105
			}
106
			
107
			// get the IProject for the selection path
108
			IProject project = getProjectFromPath(fullPath);
109
			// if inside web project, check if inside webContent folder
110
			if (project != null && isWebProject(project)) {
111
				// check that the path is inside the webContent folder
112
				IPath webContentPath = getWebContentPath(project);
113
				if (!webContentPath.isPrefixOf(fullPath)) {
114
					setMessage(JavaScriptUIMessages._WARNING_FOLDER_MUST_BE_INSIDE_WEB_CONTENT, WARNING);
115
				}
116
			}
117
		}
118
119
		return true;
120
	}
121
	
122
	/**
123
	 * Get content type associated with this new file wizard
124
	 * 
125
	 * @return IContentType
126
	 */
127
	private IContentType getContentType() {
128
		if (fContentType == null)
129
			fContentType = Platform.getContentTypeManager().getContentType(ContentTypeIdForJavaScript.ContentTypeID_JAVASCRIPT);
130
		return fContentType;
131
	}
132
133
	/**
134
	 * Get list of valid extensions for JavaScript Content type
135
	 * 
136
	 * @return
137
	 */
138
	private List getValidExtensions() {
139
		if (fValidExtensions == null) {
140
			IContentType type = getContentType();
141
			fValidExtensions = new ArrayList(Arrays.asList(type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC)));
142
		}
143
		return fValidExtensions;
144
	}
145
	
146
	/**
147
	 * Verifies if fileName is valid name for content type. Takes base content
148
	 * type into consideration.
149
	 * 
150
	 * @param fileName
151
	 * @return true if extension is valid for this content type
152
	 */
153
	private boolean extensionValidForContentType(String fileName) {
154
		boolean valid = false;
155
156
		IContentType type = getContentType();
157
		// there is currently an extension
158
		if (fileName.lastIndexOf('.') != -1) {
159
			// check what content types are associated with current extension
160
			IContentType[] types = Platform.getContentTypeManager().findContentTypesFor(fileName);
161
			int i = 0;
162
			while (i < types.length && !valid) {
163
				valid = types[i].isKindOf(type);
164
				++i;
165
			}
166
		}
167
		else
168
			valid = true; // no extension so valid
169
		return valid;
170
	}
171
172
	/**
173
	 * Adds default extension to the filename
174
	 * 
175
	 * @param filename
176
	 * @return
177
	 */
178
	String addDefaultExtension(String filename) {
179
		StringBuffer newFileName = new StringBuffer(filename);
180
181
		Preferences preference = JavaScriptCorePlugin.getDefault().getPluginPreferences();
182
		String ext = preference.getString(JavaScriptCorePreferenceNames.DEFAULT_EXTENSION);
183
184
		newFileName.append("."); //$NON-NLS-1$
185
		newFileName.append(ext);
186
187
		return newFileName.toString();
188
	}
189
	
190
	/**
191
	 * Returns the project that contains the specified path
192
	 * 
193
	 * @param path the path which project is needed
194
	 * @return IProject object. If path is <code>null</code> the return value 
195
	 * 		   is also <code>null</code>. 
196
	 */
197
	private IProject getProjectFromPath(IPath path) {
198
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
199
		IProject project = null;
200
		
201
		if (path != null) {
202
			if (workspace.validatePath(path.toString(), IResource.PROJECT).isOK()) {
203
				project = workspace.getRoot().getProject(path.toString());
204
			} else {
205
				project = workspace.getRoot().getFile(path).getProject();
206
			}
207
		}
208
		
209
		return project;
210
	}
211
	
212
	/**
213
	 * Checks if the specified project is a web project. 
214
	 * 
215
	 * @param project project to be checked
216
	 * @return true if the project is web project, otherwise false
217
	 */
218
	private boolean isWebProject(IProject project) {
219
		IFacetedProject faceted = null;
220
		try {
221
			faceted = ProjectFacetsManager.create(project);
222
		} catch (CoreException e) {
223
			Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
224
		}
225
		
226
		if (faceted != null && 
227
			(faceted.hasProjectFacet(ProjectFacetsManager.getProjectFacet(IModuleConstants.WST_WEB_MODULE)) || 
228
			 faceted.hasProjectFacet(ProjectFacetsManager.getProjectFacet(IModuleConstants.JST_WEB_MODULE)))) {
229
			return true;
230
		}
231
		
232
		return false;
233
	}
234
	
235
	/**
236
	 * Returns the web contents folder of the specified project
237
	 * 
238
	 * @param project the project which web contents path is needed
239
	 * @return IPath of the web contents folder
240
	 */
241
	private IPath getWebContentPath(IProject project) {
242
		IPath path = null;
243
		
244
		if (project != null && isWebProject(project)) {			
245
			IVirtualComponent component = ComponentCore.createComponent(project);
246
			path = component.getRootFolder().getWorkspaceRelativePath();
247
		}
248
		
249
		return path;
250
	}
251
}

Return to bug 140752