improved PHP parser
[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, ITextViewer viewer) {
35     this(identifierName, region, viewer, 99);
36   }
37
38   public LocalVariableProposal(String identifierName, IRegion region, ITextViewer viewer, int relevance) {
39     super(viewer);
40     fIdentifierName = identifierName;
41     fRegion = region;
42     fRelevance = relevance;
43   }
44
45   /*
46    * @see ICompletionProposal#apply(IDocument)
47    */
48   public void apply(IDocument document) {
49     try {
50       //                    if (fTemplateBuffer == null)
51       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
52
53       int start = fRegion.getOffset();
54       int end = fRegion.getOffset() + fRegion.getLength();
55
56       document.replace(start, end - start, fIdentifierName);
57
58       // translate positions
59       LinkedPositionManager manager = new LinkedPositionManager(document);
60
61       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
62       editor.setFinalCaretOffset(fIdentifierName.length() + start);
63       editor.enter();
64
65       fSelectedRegion = editor.getSelectedRegion();
66
67     } catch (BadLocationException e) {
68       PHPeclipsePlugin.log(e);
69       openErrorDialog(e);
70
71     }
72     //      catch (CoreException e) {
73     //                  handleException(e);
74     //      }
75   }
76
77   /*
78    * (non-Javadoc)
79    * 
80    * @see java.lang.Object#equals(java.lang.Object)
81    */
82   public boolean equals(Object obj) {
83     if (obj instanceof LocalVariableProposal) {
84       return fIdentifierName.equals(((LocalVariableProposal) obj).fIdentifierName);
85     }
86     return false;
87   }
88
89   /*
90    * @see ICompletionProposal#getAdditionalProposalInfo()
91    */
92   public String getAdditionalProposalInfo() {
93     StringBuffer hoverInfoBuffer = new StringBuffer();
94     if (fRelevance>95) {
95       hoverInfoBuffer.append("function source variable -");
96     } else {
97       hoverInfoBuffer.append("editor source variable -");
98     }
99     hoverInfoBuffer.append(fIdentifierName);
100     return hoverInfoBuffer.toString();
101   }
102
103   /*
104    * @see ICompletionProposal#getContextInformation()
105    */
106   public IContextInformation getContextInformation() {
107     return null;
108   }
109
110   /*
111    * @see ICompletionProposal#getDisplayString()
112    */
113   public String getDisplayString() {
114     return fIdentifierName; // $NON-NLS-1$ //$NON-NLS-1$
115   }
116
117   /*
118    * @see ICompletionProposal#getImage()
119    */
120   public Image getImage() {
121     return PHPUiImages.get(PHPUiImages.IMG_VAR);
122   }
123
124   /*
125    * @see IJavaCompletionProposal#getRelevance()
126    */
127   public int getRelevance() {
128     return fRelevance;
129   }
130
131   /*
132    * (non-Javadoc)
133    * 
134    * @see java.lang.Object#hashCode()
135    */
136   public int hashCode() {
137     return fIdentifierName.hashCode();
138   }
139
140 }