Author: vrubezhny
Date: 2009-06-03 12:43:27 -0400 (Wed, 03 Jun 2009)
New Revision: 15686
Modified:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/AbstractXMLContentAssistProcessor.java
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/JspContentAssistProcessor.java
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/XmlContentAssistProcessor.java
Log:
JBIDE-2808: Improve/refactor org.jboss.tools.common.kb plugin.
The skeleton to prompt for the tag attribute names is added
The skeleton to prompt for the tag attribute values is added
Modified:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/AbstractXMLContentAssistProcessor.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/AbstractXMLContentAssistProcessor.java 2009-06-03
16:33:44 UTC (rev 15685)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/AbstractXMLContentAssistProcessor.java 2009-06-03
16:43:27 UTC (rev 15686)
@@ -144,18 +144,19 @@
- /*
+ /**
* Calculates and adds the attribute name proposals to the Content Assist Request
object
*/
protected void addAttributeNameProposals(ContentAssistRequest contentAssistRequest) {
System.out.println("AbstractXMLContentAssistProcessor: addAttributeNameProposals()
invoked");
}
- /*
+ /**
* Calculates and adds the attribute value proposals to the Content Assist Request
object
*/
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest) {
-
+ System.out.println("AbstractXMLContentAssistProcessor:
addAttributeValueProposals() invoked");
+/*
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
// Find the attribute region and name for which this position should
@@ -203,6 +204,7 @@
else {
setErrorMessage(UNKNOWN_CONTEXT);
}
+ */
}
/*
@@ -320,10 +322,6 @@
* @return
*/
- void a() {}
- ///
-
-
/**
* Returns the <code>org.jboss.tools.jst.web.kb.KbQuery</code> instance
*
@@ -450,28 +448,6 @@
}
/**
- * For internal use only!
- * Returns the parent Element for the Attribute or TEXT nodes
- *
- * @param xmlDocument
- * @return
- */
- protected Node _getParentElement(Document xmlDocument) {
- Node n = findNodeForOffset(xmlDocument, getOffset());
- if (n == null)
- return null;
-
- if (n instanceof Element)
- return n;
-
- if (n instanceof Attr) {
- return ((Attr) n).getOwnerElement();
- } else {
- return n.getParentNode();
- }
- }
-
- /**
* Returns name of the parent attribute/tag name
*
* @return
Modified:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/JspContentAssistProcessor.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/JspContentAssistProcessor.java 2009-06-03
16:33:44 UTC (rev 15685)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/JspContentAssistProcessor.java 2009-06-03
16:43:27 UTC (rev 15686)
@@ -39,11 +39,14 @@
import org.jboss.tools.jst.web.kb.taglib.INameSpace;
import org.jboss.tools.jst.web.kb.taglib.ITagLibrary;
import org.jboss.tools.jst.web.kb.taglib.TagLibriryManager;
+import org.w3c.dom.Attr;
import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
public class JspContentAssistProcessor extends XmlContentAssistProcessor {
- /*
+ /**
* (non-Javadoc)
* @see org.jboss.tools.jst.jsp.contentassist.XmlContentAssistProcessor#createContext()
*/
@@ -65,6 +68,11 @@
return context;
}
+ /**
+ * Collects the namespaces over the JSP-page and sets them up to the context specified.
+ *
+ * @param context
+ */
protected void setNameSpaces(JspContextImpl context) {
IStructuredModel sModel = StructuredModelManager
.getModelManager()
@@ -114,9 +122,14 @@
}
private static final ITagLibrary[] EMPTY_LIBRARIES = new ITagLibrary[0];
+
/**
- */
-
+ * Returns the Tag Libraries for the namespaces collected in the context.
+ * Important: The context must be created using createContext() method before using this
method.
+ *
+ * @param context The context object instance
+ * @return
+ */
protected ITagLibrary[] getTagLibraries(IPageContext context) {
Map<String, INameSpace> nameSpaces = context.getNameSpaces(getOffset());
if (nameSpaces == null || nameSpaces.isEmpty())
@@ -135,60 +148,233 @@
(ITagLibrary[])tagLibraries.toArray(new ITagLibrary[tagLibraries.size()]));
}
+ /**
+ * Returns the resource bundles
+ *
+ * @return
+ */
protected IResourceBundle[] getResourceBundles() {
// TODO
return null;
}
+ /**
+ * Returns the
<code>org.jboss.tools.common.el.core.resolver.ELContext</code> instance
+ *
+ * @return
+ */
@Override
protected IPageContext getContext() {
return (IPageContext)super.getContext();
}
+ /**
+ * Returns URI string for the prefix specified using the namespaces collected for
+ * the {@link IPageContext} context.
+ * Important: The context must be created using createContext() method before using this
method.
+ *
+ * @param prefix
+ * @return
+ */
@Override
+ protected String getUri(String prefix) {
+ if (prefix == null || prefix.length() == 0)
+ return null;
+
+ Map<String, INameSpace> nameSpaces = getContext().getNameSpaces(getOffset());
+ if (nameSpaces == null || nameSpaces.isEmpty())
+ return null;
+
+ for (INameSpace nameSpace : nameSpaces.values()) {
+ if (prefix.equals(nameSpace.getPrefix())) {
+ return nameSpace.getURI();
+ }
+ }
+ return null;
+ }
+
+ /*
+ * Checks if the specified attribute exists
+ *
+ * @param attrName Name of attribute to check
+ */
+ private boolean isExistingAttribute(String attrName) {
+ IStructuredModel sModel = StructuredModelManager.getModelManager()
+ .getExistingModelForRead(getDocument());
+ try {
+ if (sModel == null)
+ return false;
+
+ Document xmlDocument = (sModel instanceof IDOMModel) ? ((IDOMModel) sModel)
+ .getDocument()
+ : null;
+
+ if (xmlDocument == null)
+ return false;
+
+ Node n = findNodeForOffset(xmlDocument, getOffset());
+ if (n == null)
+ return false;
+
+ // Find the first parent tag
+ if (!(n instanceof Element)) {
+ if (n instanceof Attr) {
+ n = ((Attr) n).getOwnerElement();
+ } else {
+ return false;
+ }
+ }
+
+ if (n == null)
+ return false;
+
+ return (((Element)n).getAttribute(attrName) != null);
+ } finally {
+ if (sModel != null) {
+ sModel.releaseFromRead();
+ }
+ }
+
+ }
+
+ /**
+ * Calculates and adds the attribute name proposals to the Content Assist Request
object
+ *
+ * @param contentAssistRequest Content Assist Request object
+ * @param childPosition the
+ */
+
+ @Override
protected void addTagNameProposals(
ContentAssistRequest contentAssistRequest, int childPosition) {
// TODO Auto-generated method stub
System.out.println("JspContentAssistProcessor: addTagNameProposals()
invoked");
- String matchString = contentAssistRequest.getMatchString();
- String query = matchString;
- if (query == null)
- query = "";
- if (query.indexOf(KbQuery.PREFIX_SEPARATOR) > -1) {
- query = matchString.substring(query.indexOf(KbQuery.PREFIX_SEPARATOR) + 1);
+ try {
+ String matchString = contentAssistRequest.getMatchString();
+ String query = matchString;
+ if (query == null)
+ query = "";
+ String stringQuery = "<" + matchString;
+
+ KbQuery kbQuery = createKbQuery(Type.TAG_NAME, query, stringQuery);
+ TextProposal[] proposals = PageProcessor.getInstance().getProposals(kbQuery,
getContext());
+
+ for (int i = 0; proposals != null && i < proposals.length; i++) {
+ TextProposal textProposal = proposals[i];
+
+ System.out.println("Tag Name proposal [" + (i + 1) + "/" +
proposals.length + "]: " + textProposal.getReplacementString());
+
+ String replacementString = textProposal.getReplacementString() + ">";
+ if (!replacementString.endsWith("/>")) {
+ replacementString += "</" + textProposal.getLabel() +
">";
+ }
+
+ int replacementOffset = contentAssistRequest.getReplacementBeginPosition();
+ int replacementLength = contentAssistRequest.getReplacementLength();
+ int cursorPosition = getCursorPositionForProposedText(replacementString);
+ Image image = textProposal.getImage();
+ String displayString = textProposal.getLabel();
+ IContextInformation contextInformation = null;
+ String additionalProposalInfo = textProposal.getContextInfo();
+ int relevance = textProposal.getRelevance() + 10000;
+
+
+ CustomCompletionProposal proposal = new CustomCompletionProposal(replacementString,
replacementOffset, replacementLength, cursorPosition, image, displayString,
contextInformation, additionalProposalInfo, relevance);
+ contentAssistRequest.addProposal(proposal);
+ }
+ } finally {
+ System.out.println("JspContentAssistProcessor: addTagNameProposals()
exited");
}
- String stringQuery = "<" + matchString;
+ return;
+ }
+
+ /**
+ * Calculates and adds the attribute name proposals to the Content Assist Request
object
+ *
+ * @param contentAssistRequest Content Assist Request object
+ * @param childPosition the
+ */
+ protected void addAttributeNameProposals(ContentAssistRequest contentAssistRequest) {
+ System.out.println("JspContentAssistProcessor: addAttributeNameProposals()
invoked");
+ try {
+ String matchString = contentAssistRequest.getMatchString();
+ String query = matchString;
+ if (query == null)
+ query = "";
+ String stringQuery = matchString;
+
+ KbQuery kbQuery = createKbQuery(Type.ATTRIBUTE_NAME, query, stringQuery);
+ TextProposal[] proposals = PageProcessor.getInstance().getProposals(kbQuery,
getContext());
+
+ for (int i = 0; proposals != null && i < proposals.length; i++) {
+ TextProposal textProposal = proposals[i];
- KbQuery kbQuery = createKbQuery(Type.TAG_NAME, query, stringQuery);
- TextProposal[] proposals = PageProcessor.getInstance().getProposals(kbQuery,
getContext());
-
- for (int i = 0; proposals != null && i < proposals.length; i++) {
- TextProposal textProposal = proposals[i];
- System.out.println("Tag Name proposal [" + (i + 1) + "/" +
proposals.length + "]: " + textProposal.getReplacementString());
+ System.out.println("Tag Attribute proposal [" + (i + 1) + "/" +
proposals.length + "]: " + textProposal.getReplacementString());
+
+ if (isExistingAttribute(textProposal.getLabel()))
+ continue;
+
+ String replacementString = textProposal.getReplacementString() +
"=\"\"";
+
+ int replacementOffset = contentAssistRequest.getReplacementBeginPosition();
+ int replacementLength = contentAssistRequest.getReplacementLength();
+ int cursorPosition = getCursorPositionForProposedText(replacementString);
+ Image image = textProposal.getImage();
+ String displayString = textProposal.getLabel();
+ IContextInformation contextInformation = null;
+ String additionalProposalInfo = textProposal.getContextInfo();
+ int relevance = textProposal.getRelevance() + 10000;
+
+
+ CustomCompletionProposal proposal = new CustomCompletionProposal(replacementString,
replacementOffset, replacementLength, cursorPosition, image, displayString,
contextInformation, additionalProposalInfo, relevance);
+ contentAssistRequest.addProposal(proposal);
+ }
+ } finally {
+ System.out.println("JspContentAssistProcessor: addAttributeNameProposals()
exited");
+ }
+ }
+
+ /**
+ * Calculates and adds the attribute value proposals to the Content Assist Request
object
+ */
+ protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest) {
+ System.out.println("JspContentAssistProcessor: addAttributeValueProposals()
invoked");
+ try {
+ String matchString = contentAssistRequest.getMatchString();
+ String query = matchString;
+ if (query == null)
+ query = "";
+ String stringQuery = matchString;
+
+ KbQuery kbQuery = createKbQuery(Type.ATTRIBUTE_VALUE, query, stringQuery);
+ TextProposal[] proposals = PageProcessor.getInstance().getProposals(kbQuery,
getContext());
- String replacementString = textProposal.getReplacementString() + ">";
-
- int replacementOffset = contentAssistRequest.getReplacementBeginPosition();
- int replacementLength = contentAssistRequest.getReplacementLength();
- int cursorPosition = getCursorPositionForProposedText(replacementString);
- Image image = textProposal.getImage();
- String displayString = textProposal.getLabel();
- IContextInformation contextInformation = null;
- String additionalProposalInfo = textProposal.getContextInfo();
- int relevance = textProposal.getRelevance() + 10000;
-
-
-// cursorAdjustment = proposedText.length() +
- // 1;
- // proposedText += "></" +
- // getRequiredName(parent, elementDecl) + ">";
- // //$NON-NLS-2$//$NON-NLS-1$
-
-
- CustomCompletionProposal proposal = new CustomCompletionProposal(replacementString,
replacementOffset, replacementLength, cursorPosition, image, displayString,
contextInformation, additionalProposalInfo, relevance);
- contentAssistRequest.addProposal(proposal);
+ for (int i = 0; proposals != null && i < proposals.length; i++) {
+ TextProposal textProposal = proposals[i];
+
+ System.out.println("Tag Attribute Value proposal [" + (i + 1) +
"/" + proposals.length + "]: " +
textProposal.getReplacementString());
+
+ if (isExistingAttribute(textProposal.getLabel()))
+ continue;
+
+ String replacementString = textProposal.getReplacementString() +
"=\"\"";
+
+ int replacementOffset = contentAssistRequest.getReplacementBeginPosition();
+ int replacementLength = contentAssistRequest.getReplacementLength();
+ int cursorPosition = getCursorPositionForProposedText(replacementString);
+ Image image = textProposal.getImage();
+ String displayString = textProposal.getLabel();
+ IContextInformation contextInformation = null;
+ String additionalProposalInfo = textProposal.getContextInfo();
+ int relevance = textProposal.getRelevance() + 10000;
+
+
+ CustomCompletionProposal proposal = new CustomCompletionProposal(replacementString,
replacementOffset, replacementLength, cursorPosition, image, displayString,
contextInformation, additionalProposalInfo, relevance);
+ contentAssistRequest.addProposal(proposal);
+ }
+ } finally {
+ System.out.println("JspContentAssistProcessor: addAttributeValueProposals()
exited");
}
- return;
}
}
\ No newline at end of file
Modified:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/XmlContentAssistProcessor.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/XmlContentAssistProcessor.java 2009-06-03
16:33:44 UTC (rev 15685)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/XmlContentAssistProcessor.java 2009-06-03
16:43:27 UTC (rev 15686)
@@ -16,8 +16,10 @@
import org.jboss.tools.common.el.core.resolver.ELContextImpl;
import org.jboss.tools.common.el.core.resolver.ELResolver;
import org.jboss.tools.jst.web.kb.IFaceletPageContext;
+import org.jboss.tools.jst.web.kb.IPageContext;
import org.jboss.tools.jst.web.kb.KbQuery;
import org.jboss.tools.jst.web.kb.KbQuery.Type;
+import org.jboss.tools.jst.web.kb.internal.JspContextImpl;
public class XmlContentAssistProcessor extends AbstractXMLContentAssistProcessor {
@@ -66,7 +68,12 @@
return kbQuery;
}
- @Override
+ /**
+ * Returns URI string for the prefix specified using the namespaces collected for
+ * the {@link IPageContext} context.
+ *
+ * @Override
org.jboss.tools.jst.jsp.contentassist.AbstractXMLContentAssistProcessor#getUri(String)
+ */
protected String getUri(String prefix) {
return null;
}