1 package net.sourceforge.phpdt.internal.ui.text.template;
3 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
4 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
5 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
6 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
8 import org.eclipse.jface.text.BadLocationException;
9 import org.eclipse.jface.text.IDocument;
10 import org.eclipse.jface.text.IRegion;
11 import org.eclipse.jface.text.ITextViewer;
12 import org.eclipse.jface.text.contentassist.IContextInformation;
13 import org.eclipse.swt.graphics.Image;
16 * A PHP local identifier proposal.
18 public class LocalVariableProposal extends AbstractProposal {
20 private final String fIdentifierName;
22 private final IRegion fRegion;
24 private final int fRelevance;
27 * Creates a template proposal with a template and its context.
32 * the icon of the proposal.
34 public LocalVariableProposal(String identifierName, IRegion region, ITextViewer viewer) {
35 this(identifierName, region, viewer, 99);
38 public LocalVariableProposal(String identifierName, IRegion region, ITextViewer viewer, int relevance) {
40 fIdentifierName = identifierName;
42 fRelevance = relevance;
46 * @see ICompletionProposal#apply(IDocument)
48 public void apply(IDocument document) {
50 // if (fTemplateBuffer == null)
51 // fTemplateBuffer= fContext.evaluate(fTemplate);
53 int start = fRegion.getOffset();
54 int end = fRegion.getOffset() + fRegion.getLength();
56 document.replace(start, end - start, fIdentifierName);
58 // translate positions
59 LinkedPositionManager manager = new LinkedPositionManager(document);
61 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
62 editor.setFinalCaretOffset(fIdentifierName.length() + start);
65 fSelectedRegion = editor.getSelectedRegion();
67 } catch (BadLocationException e) {
68 PHPeclipsePlugin.log(e);
72 // catch (CoreException e) {
73 // handleException(e);
80 * @see java.lang.Object#equals(java.lang.Object)
82 public boolean equals(Object obj) {
83 if (obj instanceof LocalVariableProposal) {
84 return fIdentifierName.equals(((LocalVariableProposal) obj).fIdentifierName);
90 * @see ICompletionProposal#getAdditionalProposalInfo()
92 public String getAdditionalProposalInfo() {
93 StringBuffer hoverInfoBuffer = new StringBuffer();
95 hoverInfoBuffer.append("function source variable -");
97 hoverInfoBuffer.append("editor source variable -");
99 hoverInfoBuffer.append(fIdentifierName);
100 return hoverInfoBuffer.toString();
104 * @see ICompletionProposal#getContextInformation()
106 public IContextInformation getContextInformation() {
111 * @see ICompletionProposal#getDisplayString()
113 public String getDisplayString() {
114 return fIdentifierName; // $NON-NLS-1$ //$NON-NLS-1$
118 * @see ICompletionProposal#getImage()
120 public Image getImage() {
121 return PHPUiImages.get(PHPUiImages.IMG_VAR);
125 * @see IJavaCompletionProposal#getRelevance()
127 public int getRelevance() {
134 * @see java.lang.Object#hashCode()
136 public int hashCode() {
137 return fIdentifierName.hashCode();