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("NewJSPTemplatesWizardPage", |
127 |
protected boolean validatePage() { |
43 |
new StructuredSelection(IDE.computeSelectedResources(fSelection))); |
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); |
44 |
fNewFilePage.setTitle(JSPUIMessages._UI_WIZARD_NEW_HEADING); |
161 |
fNewFilePage.setDescription(JSPUIMessages._UI_WIZARD_NEW_DESCRIPTION); |
45 |
fNewFilePage.setDescription(JSPUIMessages._UI_WIZARD_NEW_DESCRIPTION); |
162 |
|
|
|
163 |
addPage(fNewFilePage); |
46 |
addPage(fNewFilePage); |
164 |
|
47 |
|
165 |
fNewFileTemplatesPage = new NewJSPTemplatesWizardPage(); |
48 |
fNewFileTemplatesPage = new NewJSPTemplatesWizardPage(); |
Lines 199-205
Link Here
|
199 |
// no file extension specified so add default extension |
82 |
// no file extension specified so add default extension |
200 |
String fileName = fNewFilePage.getFileName(); |
83 |
String fileName = fNewFilePage.getFileName(); |
201 |
if (fileName.lastIndexOf('.') == -1) { |
84 |
if (fileName.lastIndexOf('.') == -1) { |
202 |
String newFileName = addDefaultExtension(fileName); |
85 |
String newFileName = fNewFilePage.addDefaultExtension(fileName); |
203 |
fNewFilePage.setFileName(newFileName); |
86 |
fNewFilePage.setFileName(newFileName); |
204 |
} |
87 |
} |
205 |
|
88 |
|