Author: dmaliarevich
Date: 2012-07-27 08:51:04 -0400 (Fri, 27 Jul 2012)
New Revision: 42759
Added:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/commands/NameInputValidator.java
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/commands/CreateJSF2CompositeCommandHandler.java
Log:
https://issues.jboss.org/browse/JBIDE-6267 - updating the command for creating new JSF2
composition component.
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/commands/CreateJSF2CompositeCommandHandler.java
===================================================================
---
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/commands/CreateJSF2CompositeCommandHandler.java 2012-07-27
12:38:34 UTC (rev 42758)
+++
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/commands/CreateJSF2CompositeCommandHandler.java 2012-07-27
12:51:04 UTC (rev 42759)
@@ -75,7 +75,9 @@
public class CreateJSF2CompositeCommandHandler extends AbstractHandler {
+ private static final Pattern USED_TAGLIBS_PATTERN =
Pattern.compile("<([a-zA-Z]+\\d*)+:"); //$NON-NLS-1$
private final String IMPLEMENTATION = "composite:implementation";
//$NON-NLS-1$
+ private NameInputValidator nameInputValidator;
private JSPMultiPageEditor editor;
@Override
@@ -148,39 +150,15 @@
/*
* Get composite's name and namespace from the popup dialog
*/
+ if (nameInputValidator == null) {
+ nameInputValidator = new NameInputValidator(project);
+ } else {
+ nameInputValidator.setProject(project);
+ }
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$
- 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;
- }
- });
+ nameInputValidator);
if (dlg.open() == Window.OK) {
/*
* Create all required files
@@ -224,16 +202,16 @@
if (ed instanceof JSPTextEditor) {
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, 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,
+ Properties properties = new Properties();
+ properties.put("selectionProvider", editor.getSelectionProvider());
//$NON-NLS-1$
+ properties.setProperty(URIConstants.LIBRARY_URI, libraryUri);
+ properties.setProperty(URIConstants.LIBRARY_VERSION, "");
//$NON-NLS-1$
+ properties.setProperty(URIConstants.DEFAULT_PREFIX, split[0]);
+ properties.setProperty(JSPPaletteInsertHelper.PROPOPERTY_ADD_TAGLIB,
"true"); //$NON-NLS-1$
+ properties.setProperty(XModelObjectConstants.REFORMAT, "yes");
//$NON-NLS-1$
+ properties.setProperty(XModelObjectConstants.START_TEXT,
"<%@ taglib uri=\""+libraryUri+"\"
prefix=\"" +split[0]+ "\" %>\\n"); //$NON-NLS-1$ //$NON-NLS-2$
//$NON-NLS-3$
- PaletteTaglibInserter.inserTaglib(ed.getTextViewer().getDocument(), p);
+ PaletteTaglibInserter.inserTaglib(ed.getTextViewer().getDocument(),
properties);
}
/*
* Add required taglibs to the composite file
@@ -255,8 +233,7 @@
/*
* Parse selected fragment to find used taglibs
*/
- Pattern p = Pattern.compile("<([a-zA-Z]+\\d*)+:"); //$NON-NLS-1$
- Matcher m = p.matcher(replacedText);
+ Matcher m = USED_TAGLIBS_PATTERN.matcher(replacedText);
while (m.find()) {
if (sourceTaglibs.keySet().contains(m.group(1))
&& !requiredTaglibs.keySet().contains(m.group(1))) {
@@ -274,7 +251,7 @@
compositeTaglibs.add(map.item(i).getNodeName());
}
for (String key : requiredTaglibs.keySet()) {
- String xmlns = "xmlns:"+key; //$NON-NLS-1$
+ String xmlns = "xmlns:" + key; //$NON-NLS-1$
if (!compositeTaglibs.contains(xmlns)) {
html.setAttribute(xmlns, requiredTaglibs.get(key));
}
@@ -292,6 +269,7 @@
JSPMultiPageEditor part = (JSPMultiPageEditor) PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.openEditor(input,
"org.jboss.tools.jst.jsp.jspeditor.JSPTextEditor", true); //$NON-NLS-1$
+ part.setFocus();
}
}
}
@@ -313,5 +291,5 @@
}
return null;
}
-
+
}
Added:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/commands/NameInputValidator.java
===================================================================
---
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/commands/NameInputValidator.java
(rev 0)
+++
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/commands/NameInputValidator.java 2012-07-27
12:51:04 UTC (rev 42759)
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2012 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.jsf.web.commands;
+
+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.jface.dialogs.IInputValidator;
+import org.jboss.tools.jsf.jsf2.util.JSF2ResourceUtil;
+
+public class NameInputValidator implements IInputValidator {
+
+ private static final Pattern NAME_PATTERN =
Pattern.compile("([a-zA-Z]+\\d*)+"); //$NON-NLS-1$
+ private IProject project;
+
+ public NameInputValidator(IProject project) {
+ super();
+ this.project = project;
+ }
+
+ public void setProject(IProject project) {
+ this.project = project;
+ }
+
+ @Override
+ public String isValid(String newText) {
+ String trim = newText.trim();
+ String result = null;
+ String[] split = trim.split(":", 2); //$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(!NAME_PATTERN.matcher(split[0]).matches()) {
+ result = "Namespace '"+split[0]+"' has wrong spelling, please
correct"; //$NON-NLS-1$ //$NON-NLS-2$
+ } else if(!NAME_PATTERN.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;
+ }
+}