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