Author: dmaliarevich
Date: 2011-02-28 05:30:36 -0500 (Mon, 28 Feb 2011)
New Revision: 29380
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 , selection validation 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-28
09:02:34 UTC (rev 29379)
+++
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/jsf2/action/CreateJSF2CompositeAction.java 2011-02-28
10:30:36 UTC (rev 29380)
@@ -14,6 +14,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@@ -34,6 +35,7 @@
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
@@ -42,6 +44,8 @@
import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
+import org.eclipse.wst.xml.core.internal.document.NodeImpl;
+import org.eclipse.wst.xml.core.internal.document.TextImpl;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.jboss.tools.common.model.XModelObjectConstants;
@@ -62,7 +66,7 @@
public class CreateJSF2CompositeAction extends Action {
private final String IMPLEMENTATION = "composite:implementation";
//$NON-NLS-1$
-
+
private JSPMultiPageEditor editor;
public CreateJSF2CompositeAction() {
@@ -77,82 +81,110 @@
* Get the selected text
*/
ISelection selection = editor.getSelectionProvider().getSelection();
- if (selection instanceof TextSelection) {
+ if ((selection instanceof TextSelection)
+ && (selection instanceof IStructuredSelection)) {
TextSelection textSelection = (TextSelection) selection;
- String text = textSelection.getText();
+ IStructuredSelection ss = (IStructuredSelection) selection;
final IProject project = editor.getProject();
/*
- * Get composite template creator
+ * Correct marked text if some tags were partially selected.
*/
- 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;
+ int offset = 0;
+ int length = 0;
+ /*
+ * When there is only text node - use only selected text
+ */
+ if ((ss.size() == 1) && (ss.getFirstElement() instanceof TextImpl)) {
+ offset = textSelection.getOffset();
+ length = textSelection.getLength();
+ } else {
+ /*
+ * Otherwise selection will be expanded
+ * to the whole selected element with its children
+ */
+ Iterator it = ss.iterator();
+ boolean first = true;
+ while (it.hasNext()) {
+ Object ob = (Object) it.next();
+ if (ob instanceof NodeImpl) {
+ NodeImpl n = ((NodeImpl) ob);
+ if (first) {
+ offset = n.getStartOffset();
+ first = false;
}
- });
- if (dlg.open() == Window.OK) {
+ length += n.getEndOffset() - n.getStartOffset();
+ }
+ }
/*
- * Create all required files
+ * Get composite's name and namespace from the popup dialog
*/
- String componentName = dlg.getValue();
- String[] split = componentName.split(":", 2); //$NON-NLS-1$
- String path = ""; //$NON-NLS-1$
- path = componentName.replaceAll(":", "/") + ".xhtml";
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- if (project != null) {
- try {
- IFile createdFile = JSF2ResourceUtil
- .createCompositeComponentFile(project, new Path(path));
+ 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$
/*
- * Add selected text to the template
+ * Check the correct format.
+ * Matcher will accept only word characters with optional numbers.
*/
- IModelManager manager = StructuredModelManager.getModelManager();
- if (manager != null) {
- IStructuredModel model = null;
- try {
+ 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$
+ path = componentName.replaceAll(":", "/") + ".xhtml";
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ if (project != null) {
+ IStructuredModel model = null;
+ try {
+ IFile createdFile = JSF2ResourceUtil
+ .createCompositeComponentFile(project, new Path(path));
+ /*
+ * Add selected text to the template
+ */
+ IModelManager manager = StructuredModelManager.getModelManager();
+ if (manager != null) {
model = manager.getModelForEdit(createdFile);
if (model instanceof IDOMModel) {
IDOMModel domModel = (IDOMModel) model;
IDOMDocument document = domModel.getDocument();
NodeList list = document.getElementsByTagName(IMPLEMENTATION);
if (list.getLength() == 1) {
+ IDocument doc =
editor.getDocumentProvider().getDocument(editor.getEditorInput());
+ String replacedText = doc.get(offset, length);
String content = document.getStructuredDocument().getText();
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$
+ content = content.subSequence(0, index) + "\n" + replacedText +
content.subSequence(index, content.length()); //$NON-NLS-1$
domModel.reload(new ByteArrayInputStream(content.getBytes()));
model.save();
/*
* Register JSF 2 composite on the current page
*/
- IDocument doc =
editor.getDocumentProvider().getDocument(editor.getEditorInput());
- int offset = textSelection.getOffset();
- int length = textSelection.getLength();
String replacement = "<" + componentName + "> </"
+ componentName + ">"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
doc.replace(offset, length, replacement);
/*
@@ -194,7 +226,7 @@
* Parse selected fragment to find used taglibs
*/
Pattern p = Pattern.compile("<([a-zA-Z]+\\d*)+:"); //$NON-NLS-1$
- Matcher m = p.matcher(text);
+ Matcher m = p.matcher(replacedText);
while (m.find()) {
if (sourceTaglibs.keySet().contains(m.group(1))
&& !requiredTaglibs.keySet().contains(m.group(1))) {
@@ -224,24 +256,22 @@
*/
FileEditorInput input = new FileEditorInput(createdFile);
JSPMultiPageEditor part = (JSPMultiPageEditor) PlatformUI
- .getWorkbench().getActiveWorkbenchWindow().getActivePage()
- .openEditor(input,
"org.jboss.tools.jst.jsp.jspeditor.JSPTextEditor", true); //$NON-NLS-1$
+ .getWorkbench().getActiveWorkbenchWindow().getActivePage()
+ .openEditor(input, "org.jboss.tools.jst.jsp.jspeditor.JSPTextEditor",
true); //$NON-NLS-1$
}
}
- } catch (CoreException e) {
- JSFModelPlugin.getPluginLog().logError(e);
- } catch (IOException e) {
- JSFModelPlugin.getPluginLog().logError(e);
- } catch (BadLocationException e) {
- JSFModelPlugin.getPluginLog().logError(e);
- } finally {
- if (model != null) {
- model.releaseFromEdit();
- }
}
+ } catch (CoreException e) {
+ JSFModelPlugin.getPluginLog().logError(e);
+ } catch (IOException e) {
+ JSFModelPlugin.getPluginLog().logError(e);
+ } catch (BadLocationException e) {
+ JSFModelPlugin.getPluginLog().logError(e);
+ } finally {
+ if (model != null) {
+ model.releaseFromEdit();
+ }
}
- } catch (CoreException e) {
- e.printStackTrace();
}
}
}