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