2cd8210eea0d5d578de6d7d386d929f77fed6b14
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / LocalVariableProposal.java
1 package net.sourceforge.phpdt.internal.ui.text.template;
2
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;
7
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;
14
15 /**
16  * A PHP local identifier proposal.
17  */
18 public class LocalVariableProposal extends AbstractProposal {
19
20         private final String fIdentifierName;
21
22         private final IRegion fRegion;
23
24         private final int fRelevance;
25
26         /**
27          * Creates a template proposal with a template and its context.
28          * 
29          * @param template
30          *            the template
31          * @param image
32          *            the icon of the proposal.
33          */
34         public LocalVariableProposal(String identifierName, IRegion region,
35                         ITextViewer viewer) {
36                 this(identifierName, region, viewer, 99);
37         }
38
39         public LocalVariableProposal(String identifierName, IRegion region,
40                         ITextViewer viewer, int relevance) {
41                 super(viewer);
42                 fIdentifierName = identifierName;
43                 fRegion = region;
44                 fRelevance = relevance;
45         }
46
47         /*
48          * @see ICompletionProposal#apply(IDocument)
49          */
50         public void apply(IDocument document) {
51                 try {
52                         // if (fTemplateBuffer == null)
53                         // fTemplateBuffer= fContext.evaluate(fTemplate);
54
55                         int start = fRegion.getOffset();
56                         int end = fRegion.getOffset() + fRegion.getLength();
57
58                         document.replace(start, end - start, fIdentifierName);
59
60                         // translate positions
61                         LinkedPositionManager manager = new LinkedPositionManager(document);
62
63                         LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
64                         editor.setFinalCaretOffset(fIdentifierName.length() + start);
65                         editor.enter();
66
67                         fSelectedRegion = editor.getSelectedRegion();
68
69                 } catch (BadLocationException e) {
70                         PHPeclipsePlugin.log(e);
71                         openErrorDialog(e);
72
73                 }
74                 // catch (CoreException e) {
75                 // handleException(e);
76                 // }
77         }
78
79         /*
80          * (non-Javadoc)
81          * 
82          * @see java.lang.Object#equals(java.lang.Object)
83          */
84         public boolean equals(Object obj) {
85                 if (obj instanceof LocalVariableProposal) {
86                         return fIdentifierName
87                                         .equals(((LocalVariableProposal) obj).fIdentifierName);
88                 }
89                 return false;
90         }
91
92         /*
93          * @see ICompletionProposal#getAdditionalProposalInfo()
94          */
95         public String getAdditionalProposalInfo() {
96                 StringBuffer hoverInfoBuffer = new StringBuffer();
97                 if (fRelevance > 95) {
98                         hoverInfoBuffer.append("function source variable -");
99                 } else {
100                         hoverInfoBuffer.append("editor source variable -");
101                 }
102                 hoverInfoBuffer.append(fIdentifierName);
103                 return hoverInfoBuffer.toString();
104         }
105
106         /*
107          * @see ICompletionProposal#getContextInformation()
108          */
109         public IContextInformation getContextInformation() {
110                 return null;
111         }
112
113         /*
114          * @see ICompletionProposal#getDisplayString()
115          */
116         public String getDisplayString() {
117                 return fIdentifierName; // $NON-NLS-1$ //$NON-NLS-1$
118         }
119
120         /*
121          * @see ICompletionProposal#getImage()
122          */
123         public Image getImage() {
124                 return PHPUiImages.get(PHPUiImages.IMG_VAR);
125         }
126
127         /*
128          * @see IJavaCompletionProposal#getRelevance()
129          */
130         public int getRelevance() {
131                 return fRelevance;
132         }
133
134         /*
135          * (non-Javadoc)
136          * 
137          * @see java.lang.Object#hashCode()
138          */
139         public int hashCode() {
140                 return fIdentifierName.hashCode();
141         }
142
143 }