Author: scabanovich
Date: 2009-07-13 12:33:13 -0400 (Mon, 13 Jul 2009)
New Revision: 16548
Added:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/JSPDialogContentProposalProvider.java
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/outline/JSPDialogCellEditor.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-1826
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-07-13
15:09:33 UTC (rev 16547)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/AbstractXMLContentAssistProcessor.java 2009-07-13
16:33:13 UTC (rev 16548)
@@ -726,7 +726,7 @@
}
}
- protected static class TextRegion {
+ public static class TextRegion {
private int startOffset;
private int offset;
private int length;
Added:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/JSPDialogContentProposalProvider.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/JSPDialogContentProposalProvider.java
(rev 0)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/JSPDialogContentProposalProvider.java 2009-07-13
16:33:13 UTC (rev 16548)
@@ -0,0 +1,236 @@
+/*******************************************************************************
+ * Copyright (c) 2009 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.jst.jsp.contentassist;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.jface.fieldassist.IContentProposal;
+import org.eclipse.jface.fieldassist.IContentProposalProvider;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
+import org.jboss.tools.common.el.core.model.ELInstance;
+import org.jboss.tools.common.el.core.model.ELInvocationExpression;
+import org.jboss.tools.common.el.core.model.ELModel;
+import org.jboss.tools.common.el.core.model.ELUtil;
+import org.jboss.tools.common.el.core.parser.ELParser;
+import org.jboss.tools.common.el.core.parser.ELParserUtil;
+import org.jboss.tools.common.el.core.resolver.ELResolver;
+import org.jboss.tools.common.el.core.resolver.ELResolverFactoryManager;
+import org.jboss.tools.common.model.ui.ModelUIPlugin;
+import org.jboss.tools.common.text.TextProposal;
+import
org.jboss.tools.jst.jsp.contentassist.AbstractXMLContentAssistProcessor.TextRegion;
+import org.jboss.tools.jst.jsp.jspeditor.JSPMultiPageEditor;
+import org.jboss.tools.jst.jsp.outline.cssdialog.common.Constants;
+import org.jboss.tools.jst.web.kb.IPageContext;
+import org.jboss.tools.jst.web.kb.KbQuery;
+import org.jboss.tools.jst.web.kb.PageProcessor;
+import org.jboss.tools.jst.web.kb.KbQuery.Type;
+import org.w3c.dom.Node;
+
+/**
+ *
+ * @author Viacheslav Kabanovich
+ *
+ */
+public class JSPDialogContentProposalProvider implements IContentProposalProvider {
+ Properties context;
+ String attributeName;
+ String nodeName;
+ int offset = 0;
+ JSPDialogContentAssistProcessor processor;
+ IPageContext pageContext = null;
+
+ public JSPDialogContentProposalProvider() {}
+
+ public void setContext(Properties context) {
+ this.context = context;
+ attributeName = Constants.EMPTY +
context.getProperty("attributeName");
+ nodeName = Constants.EMPTY + context.getProperty("nodeName");
+ Node node = (Node)context.get("node");
+ if (node instanceof IDOMElement) {
+ offset = ((IDOMElement)node).getStartEndOffset(); //approximation, attribute may
be not defined
+ }
+ processor = new JSPDialogContentAssistProcessor();
+ processor.computeCompletionProposals(getTextViewer(), offset);
+ pageContext = processor.getContext();
+ }
+
+ public IContentProposal[] getProposals(String contents, int position) {
+ List<IContentProposal> result = new ArrayList<IContentProposal>();
+ TextRegion prefix = getELPrefix(contents, position);
+ if (prefix == null || !prefix.isELStarted()) {
+ IContentProposal proposal = new ContentProposal("#{}", 0, "#{}",
"New EL Expression");
+ result.add(proposal);
+ return result.toArray(new IContentProposal[0]);
+ }
+ String matchString = "#{" + prefix.getText();
+ String query = matchString;
+ if (query == null)
+ query = "";
+ String stringQuery = matchString;
+
+ int beginChangeOffset = prefix.getStartOffset() + prefix.getOffset();
+
+ KbQuery kbQuery = createKbQuery(Type.ATTRIBUTE_VALUE, query, stringQuery, position);
+ TextProposal[] proposals = PageProcessor.getInstance().getProposals(kbQuery,
pageContext);
+
+ if(proposals != null) for (TextProposal textProposal: proposals) {
+ int replacementOffset = beginChangeOffset;
+ int replacementLength = prefix.getLength();
+ String displayString = prefix.getText().substring(0, replacementLength) +
textProposal.getReplacementString();
+ int cursorPosition = /*replacementOffset + */
textProposal.getReplacementString().length();
+
+ if(!prefix.isELClosed()) {
+ textProposal.setReplacementString(textProposal.getReplacementString() +
"}");
+ }
+
+ Image image = textProposal.getImage();
+
+// IContextInformation contextInformation = null;
+// String additionalProposalInfo = textProposal.getContextInfo();
+// int relevance = textProposal.getRelevance() + 10000;
+
+ IContentProposal proposal = //new ContentProposal(replacementString,
replacementOffset, replacementLength, cursorPosition, image, displayString,
contextInformation, additionalProposalInfo, relevance);
+ new ContentProposal(textProposal.getReplacementString(), cursorPosition,
displayString, displayString);
+ result.add(proposal);
+ }
+
+ if (prefix.isELStarted() && !prefix.isELClosed()) {
+ IContentProposal proposal = new ContentProposal("}", 0, "}",
"Close EL Expression");
+ result.add(proposal);
+ }
+
+ return result.toArray(new IContentProposal[0]);
+ }
+
+ class ContentProposal implements IContentProposal {
+ String content;
+ int pos;
+ String description = "";
+ String label;
+
+ public ContentProposal(String content, int pos, String label, String description) {
+ this.content = content;
+ this.pos = pos;
+ this.label = label;
+ this.description = description;
+ }
+
+ public String getContent() {
+ return content;
+ }
+
+ public int getCursorPosition() {
+ return pos;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public String getLabel() {
+ return label;
+ }
+
+ }
+
+ protected TextRegion getELPrefix(String text, int pos) {
+ int inValueOffset = pos;
+ if (text.length() < inValueOffset) { // probably, the attribute value ends before
the document position
+ return null;
+ }
+ if (inValueOffset<0) {
+ return null;
+ }
+
+ String matchString = text.substring(0, inValueOffset);
+
+ ELParser p = ELParserUtil.getJbossFactory().createParser();
+ ELModel model = p.parse(text);
+
+ ELInstance is = ELUtil.findInstance(model, inValueOffset);// ELInstance
+ ELInvocationExpression ie = ELUtil.findExpression(model, inValueOffset);//
ELExpression
+
+ boolean isELStarted = (model != null && is != null &&
(model.toString().startsWith("#{") ||
+ model.toString().startsWith("${")));
+ boolean isELClosed = (model != null && is != null &&
model.toString().endsWith("}"));
+
+// boolean insideEL = startOffset + model.toString().length()
+ TextRegion tr = new TextRegion(0, ie == null ? inValueOffset : ie.getStartPosition(),
ie == null ? 0 : inValueOffset - ie.getStartPosition(), ie == null ? "" :
ie.getText(), isELStarted, isELClosed);
+
+ return tr;
+ }
+
+ protected ELResolver[] getELResolvers(IResource resource) {
+ ELResolverFactoryManager elrfm = ELResolverFactoryManager.getInstance();
+ return elrfm.getResolvers(resource);
+ }
+
+ protected ITextViewer getTextViewer() {
+ IEditorPart editor =
ModelUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
+ if(editor == null) return null;
+ if (editor instanceof JSPMultiPageEditor) {
+ JSPMultiPageEditor jsp = (JSPMultiPageEditor)editor;
+ return jsp.getSourceEditor().getTextViewer();
+ }
+ return null;
+ }
+
+ protected KbQuery createKbQuery(Type type, String query, String stringQuery, int pos) {
+ KbQuery kbQuery = new KbQuery();
+
+ String prefix = processor.getTagPrefix();
+ String uri = processor.getTagUri();
+ String[] parentTags = processor.getParentTags(attributeName);
+ String parent = attributeName;
+ String queryValue = query;
+ String queryStringValue = stringQuery;
+
+ kbQuery.setPrefix(prefix);
+ kbQuery.setUri(uri);
+ kbQuery.setParentTags(parentTags);
+ kbQuery.setParent(parent);
+ kbQuery.setMask(true);
+ kbQuery.setType(type);
+ kbQuery.setOffset(pos);
+ kbQuery.setValue(queryValue);
+ kbQuery.setStringQuery(queryStringValue);
+
+ return kbQuery;
+ }
+
+ protected int getOffset() {
+ return offset;
+ }
+
+ static class JSPDialogContentAssistProcessor extends JspContentAssistProcessor {
+ public String getTagPrefix() {
+ return super.getTagPrefix();
+ }
+ public String getTagUri() {
+ return super.getTagUri();
+ }
+ protected String[] getParentTags(String attr) {
+ String[] result = super.getParentTags(true);
+ String[] result1 = new String[result.length + 1];
+ System.arraycopy(result, 0, result1, 0, result.length);
+ result1[result.length] = attr;
+ return result1;
+ }
+
+ }
+
+}
Property changes on:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/contentassist/JSPDialogContentProposalProvider.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/JSPDialogCellEditor.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/JSPDialogCellEditor.java 2009-07-13
15:09:33 UTC (rev 16547)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/JSPDialogCellEditor.java 2009-07-13
16:33:13 UTC (rev 16548)
@@ -12,20 +12,28 @@
import java.util.Properties;
-import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
+import org.eclipse.jface.fieldassist.ContentProposalAdapter;
+import org.eclipse.jface.fieldassist.ControlDecoration;
+import org.eclipse.jface.fieldassist.FieldDecoration;
+import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
+import org.eclipse.jface.fieldassist.IContentProposal;
+import org.eclipse.jface.fieldassist.IControlContentAdapter;
+import org.eclipse.jface.fieldassist.TextContentAdapter;
+import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.window.Window;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
-import org.eclipse.ui.contentassist.ContentAssistHandler;
import org.jboss.tools.common.meta.key.WizardKeys;
+import
org.jboss.tools.common.model.ui.attribute.AttributeContentProposalProviderFactory;
import org.jboss.tools.common.model.ui.attribute.editor.DialogCellEditorEx;
import org.jboss.tools.common.model.ui.objecteditor.AttributeWrapper;
import org.jboss.tools.common.model.ui.objecteditor.ExtendedCellEditorProvider;
import org.jboss.tools.jst.jsp.contentassist.FaceletsHtmlContentAssistProcessor;
-import org.jboss.tools.jst.jsp.contentassist.JSPDialogCellEditorContentAssistProcessor;
-import org.jboss.tools.jst.jsp.drop.treeviewer.model.RootElement;
+import org.jboss.tools.jst.jsp.contentassist.JSPDialogContentProposalProvider;
import org.jboss.tools.jst.jsp.outline.cssdialog.CSSStyleDialog;
import org.jboss.tools.jst.jsp.outline.cssdialog.common.CSSConstants;
import org.jboss.tools.jst.jsp.outline.cssdialog.common.Constants;
@@ -38,8 +46,7 @@
Properties context;
// ValueHelper valueHelper;
- JSPDialogCellEditorContentAssistProcessor contentAssistentProcessor;
- ContentAssistHandler handler = null;
+// ContentAssistHandler handler = null;
boolean hasProposals = false;
/**
@@ -51,12 +58,7 @@
super(parent);
this.context = context;
- contentAssistentProcessor = new JSPDialogCellEditorContentAssistProcessor();
-
- contentAssistentProcessor.setContext(context);
-
- handler = ContentAssistHandler.createHandlerForText(getTextField(),
-
ControlContentAssistHelper.createJavaContentAssistant(contentAssistentProcessor));
+ addContentAssist(getTextField());
}
public void activate() {
@@ -71,34 +73,25 @@
if (context == null) {
return;
}
-
- // valueHelper = (ValueHelper)context.get("valueHelper");
- // if(valueHelper == null) return;
- ValueHelper valueHelper = new ValueHelper();
+
String attributeName = Constants.EMPTY +
context.getProperty("attributeName");
- String nodeName = Constants.EMPTY + context.getProperty("nodeName");
- String query = Constants.SLASH;
-
- if (valueHelper.isFacetets() && (nodeName.indexOf(':') < 0))
{
- query += FaceletsHtmlContentAssistProcessor.faceletHtmlPrefixStart;
+ if(attributeName.equalsIgnoreCase(CSSConstants.STYLE)
+ || attributeName.equalsIgnoreCase(CSSConstants.CLASS)) {
+ hasProposals = true;
+ return;
}
- query += (nodeName + "@" + attributeName);
-
- RootElement root = (RootElement) valueHelper.getInitalInput(query);
- hasProposals = ((root != null) && (root.getChildren().length > 0))
- || attributeName.equalsIgnoreCase(CSSConstants.STYLE)
- || attributeName.equalsIgnoreCase(CSSConstants.CLASS);
+ JSPDialogContentProposalProvider cpp = new JSPDialogContentProposalProvider();
+ cpp.setContext(context);
+ IContentProposal[] ps = cpp.getProposals("#{}", 2);
+ System.out.println(ps.length);
+ hasProposals = ((ps != null) && (ps.length > 0));
}
private void checkButtonEnablement() {
if (context == null) {
return;
}
-
-// valueHelper = (ValueHelper)context.get("valueHelper");
-// if(valueHelper == null) return;
-// ValueHelper valueHelper = new ValueHelper();
Button button = getButtonControl();
if ((button == null) || button.isDisposed()) {
@@ -106,7 +99,6 @@
}
button.setVisible(hasProposals);
- handler.setEnabled(hasProposals);
}
protected Object openDialogBox(Control cellEditorWindow) {
@@ -179,4 +171,47 @@
protected Text getTextField() {
return text;
}
+
+ protected Control createContents(Composite cell) {
+ super.createContents(cell);
+
+ return getTextControl();
+ }
+
+ protected void addContentAssist(Text text) {
+ IControlContentAdapter controlAdapter = new TextContentAdapter();
+ JSPDialogContentProposalProvider cpp = new JSPDialogContentProposalProvider();
+ cpp.setContext(context);
+
+ ContentProposalAdapter adapter = new ContentProposalAdapter(
+ text,
+ controlAdapter,
+ cpp,
+ AttributeContentProposalProviderFactory.getCtrlSpaceKeyStroke(),
+ null);
+ adapter.setPropagateKeys(true);
+ adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_INSERT);
+ if(popup != null) {
+ adapter.addContentProposalListener(popup);
+ }
+
+ int bits = SWT.TOP | SWT.LEFT;
+ ControlDecoration controlDecoration = new ControlDecoration(getTextControl(), bits) {
+ public Image getImage() {
+ return super.getImage();
+ }
+ };
+ // Configure text widget decoration
+ // No margin
+ controlDecoration.setMarginWidth(0);
+ // Custom hover tip text
+ controlDecoration.setDescriptionText("code assist"
/*PDEUIMessages.PDEJavaHelper_msgContentAssistAvailable*/);
+ // Custom hover properties
+ controlDecoration.setShowHover(true);
+ controlDecoration.setShowOnlyOnFocus(true);
+ // Hover image to use
+ FieldDecoration contentProposalImage =
FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
+ controlDecoration.setImage(contentProposalImage.getImage());
+ }
+
}