83b325355afb09ea87435b4e076d37265d72b5aa
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / DeclarationProposal.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text.template;
6
7 import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocUtil;
8 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
9 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
10 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
11 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
12 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
13 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.contentassist.IContextInformation;
22 import org.eclipse.swt.graphics.Image;
23 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
24 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
25 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
26
27 /**
28  * A PHP identifier proposal.
29  */
30 public class DeclarationProposal extends AbstractProposal { //implements IPHPCompletionProposal {
31   private final TemplateContext fContext;
32   private final PHPIdentifierLocation fLocation;
33
34   //private TemplateBuffer fTemplateBuffer;
35   private String fOldText;
36   //  private final Image fImage_fun;
37   //  private final Image fImage_var;
38   private final IRegion fRegion;
39   //  private IRegion fSelectedRegion; // initialized by apply()
40
41   private final String fIdentifierName;
42   //  private final ITextViewer fViewer;
43
44   /**
45    * Creates a template proposal with a template and its context.
46    * @param template  the template
47    * @param context   the context in which the template was requested.
48    * @param image     the icon of the proposal.
49    */
50   public DeclarationProposal(
51     String identifierName,
52     PHPIdentifierLocation location,
53     TemplateContext context,
54     IRegion region,
55     ITextViewer viewer) {
56     super(viewer);
57     //    Image image_fun,
58     //    Image image_var) {
59     fIdentifierName = identifierName;
60     fLocation = location;
61     fContext = context;
62     fRegion = region;
63   }
64
65   /*
66    * @see ICompletionProposal#apply(IDocument)
67    */
68   public void apply(IDocument document) {
69     try {
70       //                    if (fTemplateBuffer == null)
71       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
72
73       int start = fRegion.getOffset();
74       int end = fRegion.getOffset() + fRegion.getLength();
75
76       switch (fLocation.getType()) {
77         case PHPIdentifierLocation.FUNCTION :
78           document.replace(start, end - start, fIdentifierName + "()");
79           break;
80
81         case PHPIdentifierLocation.METHOD :
82           document.replace(start, end - start, fIdentifierName + "()");
83           break;
84
85         default :
86           document.replace(start, end - start, fIdentifierName);
87       }
88
89       // translate positions
90       LinkedPositionManager manager = new LinkedPositionManager(document);
91       //                        TemplatePosition[] variables= fTemplateBuffer.getVariables();
92       //                        for (int i= 0; i != variables.length; i++) {
93       //                                TemplatePosition variable= variables[i];
94       //
95       //                                if (variable.isResolved())
96       //                                        continue;
97       //                                
98       //                                int[] offsets= variable.getOffsets();
99       //                                int length= variable.getLength();
100       //                                
101       //                                for (int j= 0; j != offsets.length; j++)
102       //                                        manager.addPosition(offsets[j] + start, length);
103       //                        }
104
105       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
106       switch (fLocation.getType()) {
107         case PHPIdentifierLocation.FUNCTION :
108           editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
109           break;
110
111         case PHPIdentifierLocation.METHOD :
112           editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
113           break;
114
115         default :
116           editor.setFinalCaretOffset(fIdentifierName.length() + start);
117       }
118       editor.enter();
119
120       fSelectedRegion = editor.getSelectedRegion();
121
122     } catch (BadLocationException e) {
123       PHPeclipsePlugin.log(e);
124       openErrorDialog(e);
125
126     }
127     //      catch (CoreException e) {
128     //                  handleException(e);
129     //      }       
130   }
131
132   /*
133    * @see ICompletionProposal#getAdditionalProposalInfo()
134    */
135   public String getAdditionalProposalInfo() {
136     StringBuffer hoverInfoBuffer = new StringBuffer();
137     String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
138     String filename = workspaceLocation + fLocation.getFilename();
139     PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
140     return textToHTML(hoverInfoBuffer.toString());
141   }
142
143   /*
144    * @see ICompletionProposal#getContextInformation()
145    */
146   public IContextInformation getContextInformation() {
147     return null;
148   }
149
150   /*
151    * @see ICompletionProposal#getDisplayString()
152    */
153   public String getDisplayString() {
154     return fIdentifierName + TemplateMessages.getString("TemplateProposal.delimiter") + fIdentifierName; // $NON-NLS-1$ //$NON-NLS-1$
155     //          return fTemplate.getName() + ObfuscatorMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
156   }
157
158   /*
159    * @see ICompletionProposal#getImage()
160    */
161   public Image getImage() {
162     switch (fLocation.getType()) {
163       case PHPIdentifierLocation.FUNCTION :
164         return PHPUiImages.get(PHPUiImages.IMG_FUN);
165       case PHPIdentifierLocation.CLASS :
166         return PHPUiImages.get(PHPUiImages.IMG_CLASS);
167       case PHPIdentifierLocation.METHOD :
168         return PHPUiImages.get(PHPUiImages.IMG_FUN);
169       case PHPIdentifierLocation.DEFINE :
170         return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
171       case PHPIdentifierLocation.VARIABLE :
172         return PHPUiImages.get(PHPUiImages.IMG_VAR);
173     }
174     return PHPUiImages.get(PHPUiImages.IMG_FUN);
175   }
176
177   /*
178    * @see IJavaCompletionProposal#getRelevance()
179    */
180   public int getRelevance() {
181
182     if (fContext instanceof PHPUnitContext) {
183       PHPUnitContext context = (PHPUnitContext) fContext;
184       switch (context.getCharacterBeforeStart()) {
185         // high relevance after whitespace
186         case ' ' :
187         case '\r' :
188         case '\n' :
189         case '\t' :
190           return 90;
191
192         default :
193           return 0;
194       }
195     } else {
196       return 90;
197     }
198   }
199
200 }