blob: f670de6d058993c711af9c009919418f71a1260b [file] [log] [blame]
amywufe0f3612006-01-25 03:48:57 +00001/*******************************************************************************
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 *******************************************************************************/
nitind2d5bc542005-03-10 23:04:05 +000011package org.eclipse.jst.jsp.ui.internal.wizard;
12
david_williams45d38072005-04-15 23:39:31 +000013import java.io.ByteArrayInputStream;
14import java.io.ByteArrayOutputStream;
15import java.io.OutputStreamWriter;
nitind2d5bc542005-03-10 23:04:05 +000016import java.util.ArrayList;
17import java.util.Arrays;
18import java.util.List;
19
20import org.eclipse.core.resources.IFile;
amywufe0f3612006-01-25 03:48:57 +000021import org.eclipse.core.resources.IResource;
22import org.eclipse.core.resources.IWorkspace;
23import org.eclipse.core.resources.ResourcesPlugin;
nitind2d5bc542005-03-10 23:04:05 +000024import org.eclipse.core.runtime.IPath;
amywufe0f3612006-01-25 03:48:57 +000025import org.eclipse.core.runtime.IStatus;
david_williams45d38072005-04-15 23:39:31 +000026import org.eclipse.core.runtime.Platform;
27import org.eclipse.core.runtime.Preferences;
28import org.eclipse.core.runtime.content.IContentType;
nitind2d5bc542005-03-10 23:04:05 +000029import org.eclipse.jface.resource.ImageDescriptor;
30import org.eclipse.jface.viewers.IStructuredSelection;
31import org.eclipse.jface.viewers.StructuredSelection;
32import org.eclipse.jface.wizard.Wizard;
david_williams45d38072005-04-15 23:39:31 +000033import org.eclipse.jst.jsp.core.internal.JSPCorePlugin;
amywufe0f3612006-01-25 03:48:57 +000034import org.eclipse.jst.jsp.core.internal.preferences.JSPCorePreferenceNames;
david_williams757ccfe2005-05-01 08:03:21 +000035import org.eclipse.jst.jsp.core.internal.provisional.contenttype.ContentTypeIdForJSP;
david_williams48d88a62005-04-08 19:00:20 +000036import org.eclipse.jst.jsp.ui.internal.JSPUIMessages;
nitind2d5bc542005-03-10 23:04:05 +000037import org.eclipse.jst.jsp.ui.internal.Logger;
david_williams385fc5c2005-06-07 22:34:13 +000038import org.eclipse.jst.jsp.ui.internal.editor.JSPEditorPluginImageHelper;
39import org.eclipse.jst.jsp.ui.internal.editor.JSPEditorPluginImages;
david_williams45d38072005-04-15 23:39:31 +000040import org.eclipse.osgi.util.NLS;
nitind2d5bc542005-03-10 23:04:05 +000041import org.eclipse.ui.INewWizard;
42import org.eclipse.ui.IWorkbench;
43import org.eclipse.ui.IWorkbenchPage;
44import org.eclipse.ui.PartInitException;
45import org.eclipse.ui.PlatformUI;
46import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
47import org.eclipse.ui.ide.IDE;
david_williams45d38072005-04-15 23:39:31 +000048import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames;
nitind2d5bc542005-03-10 23:04:05 +000049
50public class NewJSPWizard extends Wizard implements INewWizard {
nitind2d5bc542005-03-10 23:04:05 +000051 private WizardNewFileCreationPage fNewFilePage;
david_williams45d38072005-04-15 23:39:31 +000052 private NewJSPTemplatesWizardPage fNewFileTemplatesPage;
nitind2d5bc542005-03-10 23:04:05 +000053 private IStructuredSelection fSelection;
amywufe0f3612006-01-25 03:48:57 +000054 private IContentType fContentType;
david_williams45d38072005-04-15 23:39:31 +000055 private List fValidExtensions = null;
56
57 /**
amywufe0f3612006-01-25 03:48:57 +000058 * 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 /**
david_williams45d38072005-04-15 23:39:31 +000087 * Get list of valid extensions for JSP Content type
88 *
89 * @return
90 */
91 List getValidExtensions() {
92 if (fValidExtensions == null) {
amywufe0f3612006-01-25 03:48:57 +000093 IContentType type = getContentType();
david_williams45d38072005-04-15 23:39:31 +000094 fValidExtensions = new ArrayList(Arrays.asList(type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC)));
95 }
96 return fValidExtensions;
97 }
nitind2d5bc542005-03-10 23:04:05 +000098
amywua81d7ee2006-03-07 21:01:32 +000099 /**
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
nitind2d5bc542005-03-10 23:04:05 +0000125 public void addPages() {
david_williams48d88a62005-04-08 19:00:20 +0000126 fNewFilePage = new WizardNewFileCreationPage("JSPWizardNewFileCreationPage", new StructuredSelection(IDE.computeSelectedResources(fSelection))) { //$NON-NLS-1$
nitind2d5bc542005-03-10 23:04:05 +0000127 protected boolean validatePage() {
amywufe0f3612006-01-25 03:48:57 +0000128 String fileName = getFileName();
amywudf228a82006-02-03 15:54:24 +0000129 IPath fullPath = getContainerFullPath();
130 if ((fullPath != null) && (fullPath.isEmpty() == false) && (fileName != null)) {
131 // check that filename does not contain invalid extension
amywua81d7ee2006-03-07 21:01:32 +0000132 if (!extensionValidForContentType(fileName)) {
amywudf228a82006-02-03 15:54:24 +0000133 setErrorMessage(NLS.bind(JSPUIMessages._ERROR_FILENAME_MUST_END_JSP, getValidExtensions().toString()));
amywufe0f3612006-01-25 03:48:57 +0000134 return false;
135 }
amywudf228a82006-02-03 15:54:24 +0000136 // 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);
amywufe0f3612006-01-25 03:48:57 +0000141
amywudf228a82006-02-03 15:54:24 +0000142 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 }
amywufe0f3612006-01-25 03:48:57 +0000154 }
155 }
nitind2d5bc542005-03-10 23:04:05 +0000156 setErrorMessage(null);
157 return super.validatePage();
158 }
159 };
david_williams48d88a62005-04-08 19:00:20 +0000160 fNewFilePage.setTitle(JSPUIMessages._UI_WIZARD_NEW_HEADING);
161 fNewFilePage.setDescription(JSPUIMessages._UI_WIZARD_NEW_DESCRIPTION);
nitind2d5bc542005-03-10 23:04:05 +0000162
163 addPage(fNewFilePage);
david_williams45d38072005-04-15 23:39:31 +0000164
165 fNewFileTemplatesPage = new NewJSPTemplatesWizardPage();
166 addPage(fNewFileTemplatesPage);
nitind2d5bc542005-03-10 23:04:05 +0000167 }
168
169 public void init(IWorkbench aWorkbench, IStructuredSelection aSelection) {
170 fSelection = aSelection;
david_williams48d88a62005-04-08 19:00:20 +0000171 setWindowTitle(JSPUIMessages._UI_WIZARD_NEW_TITLE);
amywufe0f3612006-01-25 03:48:57 +0000172
david_williams385fc5c2005-06-07 22:34:13 +0000173 ImageDescriptor descriptor = JSPEditorPluginImageHelper.getInstance().getImageDescriptor(JSPEditorPluginImages.IMG_OBJ_WIZBAN_NEWJSPFILE);
174 setDefaultPageImageDescriptor(descriptor);
nitind2d5bc542005-03-10 23:04:05 +0000175 }
176
177 private void openEditor(final IFile file) {
178 if (file != null) {
179 getShell().getDisplay().asyncExec(new Runnable() {
180 public void run() {
181 try {
182 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
183 IDE.openEditor(page, file, true);
184 }
185 catch (PartInitException e) {
186 Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
187 }
188 }
189 });
190 }
191 }
192
193 public boolean performFinish() {
amywufe0f3612006-01-25 03:48:57 +0000194 boolean performedOK = false;
195
david_williams45d38072005-04-15 23:39:31 +0000196 // save user options for next use
197 fNewFileTemplatesPage.saveLastSavedPreferences();
198
amywufe0f3612006-01-25 03:48:57 +0000199 // no file extension specified so add default extension
200 String fileName = fNewFilePage.getFileName();
201 if (fileName.lastIndexOf('.') == -1) {
202 String newFileName = addDefaultExtension(fileName);
203 fNewFilePage.setFileName(newFileName);
204 }
205
david_williams45d38072005-04-15 23:39:31 +0000206 // create a new empty file
nitind2d5bc542005-03-10 23:04:05 +0000207 IFile file = fNewFilePage.createNewFile();
david_williams45d38072005-04-15 23:39:31 +0000208
amywufe0f3612006-01-25 03:48:57 +0000209 // if there was problem with creating file, it will be null, so make
210 // sure to check
211 if (file != null) {
212 // put template contents into file
213 String templateString = fNewFileTemplatesPage.getTemplateString();
214 if (templateString != null) {
215 // determine the encoding for the new file
216 Preferences preference = JSPCorePlugin.getDefault().getPluginPreferences();
217 String charSet = preference.getString(CommonEncodingPreferenceNames.OUTPUT_CODESET);
david_williams45d38072005-04-15 23:39:31 +0000218
amywufe0f3612006-01-25 03:48:57 +0000219 try {
220 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
221 OutputStreamWriter outputStreamWriter = null;
222 if (charSet == null || charSet.trim().equals("")) { //$NON-NLS-1$
223 // just use default encoding
224 outputStreamWriter = new OutputStreamWriter(outputStream);
225 }
226 else {
227 outputStreamWriter = new OutputStreamWriter(outputStream, charSet);
228 }
229 outputStreamWriter.write(templateString);
230 outputStreamWriter.flush();
231 outputStreamWriter.close();
232 ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
233 file.setContents(inputStream, true, false, null);
234 inputStream.close();
david_williams45d38072005-04-15 23:39:31 +0000235 }
amywufe0f3612006-01-25 03:48:57 +0000236 catch (Exception e) {
237 Logger.log(Logger.WARNING_DEBUG, "Could not create contents for new JSP file", e); //$NON-NLS-1$
238 }
david_williams45d38072005-04-15 23:39:31 +0000239 }
david_williams45d38072005-04-15 23:39:31 +0000240
amywufe0f3612006-01-25 03:48:57 +0000241 // open the file in editor
242 openEditor(file);
243
244 // everything's fine
245 performedOK = true;
246 }
247 return performedOK;
nitind2d5bc542005-03-10 23:04:05 +0000248 }
249
250}