Author: dmaliarevich
Date: 2011-02-17 07:41:37 -0500 (Thu, 17 Feb 2011)
New Revision: 29193
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/jsf2/action/CreateJSF2CompositeAction.java
Log:
https://issues.jboss.org/browse/JBIDE-6267 , validator for component name was added.
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/jsf2/action/CreateJSF2CompositeAction.java
===================================================================
---
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/jsf2/action/CreateJSF2CompositeAction.java 2011-02-17
12:20:33 UTC (rev 29192)
+++
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/jsf2/action/CreateJSF2CompositeAction.java 2011-02-17
12:41:37 UTC (rev 29193)
@@ -14,12 +14,16 @@
import java.io.IOException;
import java.util.List;
import java.util.Properties;
+import java.util.regex.Pattern;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.Action;
+import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
@@ -70,31 +74,51 @@
if (selection instanceof TextSelection) {
TextSelection textSelection = (TextSelection) selection;
String text = textSelection.getText();
+ final IProject project = editor.getProject();
/*
* Get composite template creator
*/
InputDialog dlg = new InputDialog(Display.getCurrent().getActiveShell(),
"Creating composite component", //$NON-NLS-1$
"Enter component's namespace and name:", "namespace:name",
//$NON-NLS-1$ //$NON-NLS-2$
- null);
+ new IInputValidator() {
+ public String isValid(String newText) {
+ String trim = newText.trim();
+ String result = null;
+ String[] split = trim.split(":", 2); //$NON-NLS-1$
+ Pattern p = Pattern.compile("([a-zA-Z]+\\d*)+"); //$NON-NLS-1$
+ /*
+ * Check the correct format.
+ * Matcher will accept only word characters with optional numbers.
+ */
+ if ((split.length != 2) || trim.startsWith(":") ||
trim.endsWith(":") //$NON-NLS-1$ //$NON-NLS-2$
+ || (split[0].length() == 0) || (split[1].length() == 0)) {
+ result = "Component's name should fit in the pattern
\"namespace:name\""; //$NON-NLS-1$
+ } else if(!p.matcher(split[0]).matches()) {
+ result = "Namespace '"+split[0]+"' has wrong spelling,
please correct"; //$NON-NLS-1$ //$NON-NLS-2$
+ } else if(!p.matcher(split[1]).matches()) {
+ result = "Name '"+split[1]+"' has wrong spelling, please
correct"; //$NON-NLS-1$ //$NON-NLS-2$
+ } else {
+ String nameSpaceURI = JSF2ResourceUtil.JSF2_URI_PREFIX + "/" +
split[0]; //$NON-NLS-1$
+ Object fld = JSF2ResourceUtil.findResourcesFolderContainerByNameSpace(project,
nameSpaceURI);
+ if (fld instanceof IFolder) {
+ IResource res = ((IFolder) fld).findMember(split[1]+ ".xhtml");
//$NON-NLS-1$
+ if ((res instanceof IFile) && ((IFile)res).exists() ) {
+ result = "Component with the same name already exists";
//$NON-NLS-1$
+ }
+ }
+ }
+ return result;
+ }
+ });
if (dlg.open() == Window.OK) {
/*
* Create all required files
*/
String componentName = dlg.getValue();
+ String[] split = componentName.split(":", 2); //$NON-NLS-1$
String path = ""; //$NON-NLS-1$
- String name = ""; //$NON-NLS-1$
- String namespace = ""; //$NON-NLS-1$
- int index = componentName.indexOf(":"); //$NON-NLS-1$
- if (index != -1) {
- namespace = componentName.substring(0, index);
- name = componentName.substring(index+1, componentName.length());
- }
- path = componentName.replaceAll(":", "/"); //$NON-NLS-1$
//$NON-NLS-2$
- if (!path.endsWith(".xhtml")) { //$NON-NLS-1$
- path = path + ".xhtml"; //$NON-NLS-1$
- }
- IProject project = editor.getProject();
+ path = componentName.replaceAll(":", "/") + ".xhtml";
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
if (project != null) {
try {
IFile createdFile = JSF2ResourceUtil
@@ -113,7 +137,7 @@
NodeList list = document.getElementsByTagName(IMPLEMENTATION);
if (list.getLength() == 1) {
String content = document.getStructuredDocument().getText();
- index = content.indexOf("<"+IMPLEMENTATION+">") +
("<"+IMPLEMENTATION+">").length(); //$NON-NLS-1$ //$NON-NLS-2$
//$NON-NLS-3$ //$NON-NLS-4$
+ int index = content.indexOf("<"+IMPLEMENTATION+">") +
("<"+IMPLEMENTATION+">").length(); //$NON-NLS-1$ //$NON-NLS-2$
//$NON-NLS-3$ //$NON-NLS-4$
content = content.subSequence(0, index) + "\n" + text +
content.subSequence(index, content.length()); //$NON-NLS-1$
domModel.reload(new ByteArrayInputStream(content.getBytes()));
model.save();
@@ -130,17 +154,17 @@
*/
StructuredTextEditor ed = ((JSPMultiPageEditor) editor).getSourceEditor();
if (ed instanceof JSPTextEditor) {
- String libraryUri = JSF2ResourceUtil.JSF2_URI_PREFIX + "/" +
namespace; //$NON-NLS-1$
+ String libraryUri = JSF2ResourceUtil.JSF2_URI_PREFIX + "/" +
split[0]; //$NON-NLS-1$
PaletteTaglibInserter PaletteTaglibInserter = new PaletteTaglibInserter();
Properties p = new Properties();
p.put("selectionProvider", editor.getSelectionProvider());
//$NON-NLS-1$
p.setProperty(URIConstants.LIBRARY_URI, libraryUri);
p.setProperty(URIConstants.LIBRARY_VERSION, ""); //$NON-NLS-1$
- p.setProperty(URIConstants.DEFAULT_PREFIX, namespace);
+ p.setProperty(URIConstants.DEFAULT_PREFIX, split[0]);
p.setProperty(JSPPaletteInsertHelper.PROPOPERTY_ADD_TAGLIB, "true");
//$NON-NLS-1$
p.setProperty(XModelObjectConstants.REFORMAT, "yes"); //$NON-NLS-1$
p.setProperty(XModelObjectConstants.START_TEXT,
- "<%@ taglib uri=\""+libraryUri+"\"
prefix=\"" +namespace+ "\" %>\\n"); //$NON-NLS-1$
//$NON-NLS-2$ //$NON-NLS-3$
+ "<%@ taglib uri=\""+libraryUri+"\"
prefix=\"" +split[0]+ "\" %>\\n"); //$NON-NLS-1$ //$NON-NLS-2$
//$NON-NLS-3$
PaletteTaglibInserter.inserTaglib(ed.getTextViewer().getDocument(), p);
}
/*