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;
21 private final IRegion fRegion;
22 private int fRelevance;
25 * Creates a template proposal with a template and its context.
29 * the icon of the proposal.
31 public LocalVariableProposal(String identifierName, IRegion region, ITextViewer viewer) {
33 fIdentifierName = identifierName;
39 * @see ICompletionProposal#apply(IDocument)
41 public void apply(IDocument document) {
43 // if (fTemplateBuffer == null)
44 // fTemplateBuffer= fContext.evaluate(fTemplate);
46 int start = fRegion.getOffset();
47 int end = fRegion.getOffset() + fRegion.getLength();
49 document.replace(start, end - start, fIdentifierName);
51 // translate positions
52 LinkedPositionManager manager = new LinkedPositionManager(document);
54 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
55 editor.setFinalCaretOffset(fIdentifierName.length() + start);
58 fSelectedRegion = editor.getSelectedRegion();
60 } catch (BadLocationException e) {
61 PHPeclipsePlugin.log(e);
65 // catch (CoreException e) {
66 // handleException(e);
73 * @see java.lang.Object#equals(java.lang.Object)
75 public boolean equals(Object obj) {
76 if (obj instanceof LocalVariableProposal) {
77 return fIdentifierName.equals(((LocalVariableProposal) obj).fIdentifierName);
83 * @see ICompletionProposal#getAdditionalProposalInfo()
85 public String getAdditionalProposalInfo() {
86 StringBuffer hoverInfoBuffer = new StringBuffer();
87 // String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
88 // String workspaceLocation;
89 // if (fProject != null) {
90 // workspaceLocation = fProject.getLocation().toString() + '/';
92 // // should never happen?
93 // workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
95 hoverInfoBuffer.append("local variable -");
96 hoverInfoBuffer.append(fIdentifierName);
97 return hoverInfoBuffer.toString();
101 * @see ICompletionProposal#getContextInformation()
103 public IContextInformation getContextInformation() {
108 * @see ICompletionProposal#getDisplayString()
110 public String getDisplayString() {
111 return fIdentifierName; // $NON-NLS-1$ //$NON-NLS-1$
115 * @see ICompletionProposal#getImage()
117 public Image getImage() {
118 return PHPUiImages.get(PHPUiImages.IMG_VAR);
122 * @see IJavaCompletionProposal#getRelevance()
124 public int getRelevance() {
131 * @see java.lang.Object#hashCode()
133 public int hashCode() {
134 return fIdentifierName.hashCode();
137 * @param relevance The relevance to set.
139 public void setRelevance(int relevance) {
140 fRelevance = relevance;