diff options
Diffstat (limited to 'org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/InsertExpressionProposal.java')
-rw-r--r-- | org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/InsertExpressionProposal.java | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/InsertExpressionProposal.java b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/InsertExpressionProposal.java new file mode 100644 index 00000000..1424e7b6 --- /dev/null +++ b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/InsertExpressionProposal.java @@ -0,0 +1,110 @@ +package org.eclipse.m2e.editor.xml; + +import org.apache.maven.model.InputLocation; +import org.apache.maven.model.InputSource; +import org.apache.maven.model.Model; +import org.apache.maven.project.MavenProject; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.Region; +import org.eclipse.jface.text.contentassist.ICompletionProposal; +import org.eclipse.jface.text.contentassist.ICompletionProposalExtension5; +import org.eclipse.jface.text.contentassist.IContextInformation; +import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.osgi.util.NLS; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.Point; + +import org.eclipse.m2e.core.project.IMavenProjectFacade; +import org.eclipse.m2e.editor.xml.internal.Messages; +/** + * insertion proposal for ${ expressions + * @author mkleint + * + */ +public class InsertExpressionProposal implements ICompletionProposal, ICompletionProposalExtension5 { + + private IMavenProjectFacade project; + private String key; + private Region region; + private ISourceViewer sourceViewer; + private int len = 0; + + public InsertExpressionProposal(ISourceViewer sourceViewer, Region region, String key, IMavenProjectFacade mvnproject) { + assert project != null; + this.sourceViewer = sourceViewer; + this.region = region; + this.key = key; + this.project = mvnproject; + } + + public Object getAdditionalProposalInfo(IProgressMonitor monitor) { + String value = PomTemplateContext.simpleInterpolate(project.getProject(), "${" + key + "}"); //$NON-NLS-1$ //$NON-NLS-2$ + MavenProject mavprj = project.getMavenProject(); + String loc = null; + if (mavprj != null) { + Model mdl = mavprj.getModel(); + if (mdl.getProperties() != null && mdl.getProperties().containsKey(key)) { + if (mdl.getLocation("properties") != null) { + InputLocation location = mdl.getLocation("properties").getLocation(key); //$NON-NLS-1$ + if (location != null) { + //MNGECLIPSE-2539 apparently you can have an InputLocation with null input source. + // check! + InputSource source = location.getSource(); + if (source != null) { + loc = source.getModelId(); + } + } + } + } + } + StringBuffer buff = new StringBuffer(); + buff.append("<html>"); //$NON-NLS-1$ + if (value != null) { + buff.append(NLS.bind(Messages.InsertExpressionProposal_hint1, value)); + } + if (loc != null) { + buff.append(NLS.bind(Messages.InsertExpressionProposal_hint2, loc)); + } + buff.append("</html>"); //$NON-NLS-1$ + return buff.toString(); + } + + public void apply(IDocument document) { + int offset = region.getOffset(); + String replace = "${" + key + "}"; //$NON-NLS-1$ //$NON-NLS-2$ + try { + document.replace(offset, region.getLength(), replace); + len = replace.length(); + } catch(BadLocationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + public Point getSelection(IDocument document) { + return new Point(region.getOffset() + len, 0); + } + + public String getAdditionalProposalInfo() { + //not used anymore + return null; + } + + public String getDisplayString() { + return "${" + key + "}"; //$NON-NLS-1$ //$NON-NLS-2$ + } + + public Image getImage() { + // TODO what kind of icon to use? + return null; + } + + public IContextInformation getContextInformation() { + return null; + } + +} |