2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.template;
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;
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;
28 * A PHP identifier proposal.
30 public class DeclarationProposal extends AbstractProposal { //implements IPHPCompletionProposal {
31 private final TemplateContext fContext;
32 private final PHPIdentifierLocation fLocation;
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()
41 private final String fIdentifierName;
42 // private final ITextViewer fViewer;
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.
50 public DeclarationProposal(
51 String identifierName,
52 PHPIdentifierLocation location,
53 TemplateContext context,
59 fIdentifierName = identifierName;
66 * @see ICompletionProposal#apply(IDocument)
68 public void apply(IDocument document) {
70 // if (fTemplateBuffer == null)
71 // fTemplateBuffer= fContext.evaluate(fTemplate);
73 int start = fRegion.getOffset();
74 int end = fRegion.getOffset() + fRegion.getLength();
76 switch (fLocation.getType()) {
77 case PHPIdentifierLocation.FUNCTION :
78 document.replace(start, end - start, fIdentifierName + "()");
81 case PHPIdentifierLocation.METHOD :
82 document.replace(start, end - start, fIdentifierName + "()");
86 document.replace(start, end - start, fIdentifierName);
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];
95 // if (variable.isResolved())
98 // int[] offsets= variable.getOffsets();
99 // int length= variable.getLength();
101 // for (int j= 0; j != offsets.length; j++)
102 // manager.addPosition(offsets[j] + start, length);
105 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
106 switch (fLocation.getType()) {
107 case PHPIdentifierLocation.FUNCTION :
108 editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
111 case PHPIdentifierLocation.METHOD :
112 editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
116 editor.setFinalCaretOffset(fIdentifierName.length() + start);
120 fSelectedRegion = editor.getSelectedRegion();
122 } catch (BadLocationException e) {
123 PHPeclipsePlugin.log(e);
127 // catch (CoreException e) {
128 // handleException(e);
133 * @see ICompletionProposal#getAdditionalProposalInfo()
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());
144 * @see ICompletionProposal#getContextInformation()
146 public IContextInformation getContextInformation() {
151 * @see ICompletionProposal#getDisplayString()
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$
159 * @see ICompletionProposal#getImage()
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);
174 return PHPUiImages.get(PHPUiImages.IMG_FUN);
178 * @see IJavaCompletionProposal#getRelevance()
180 public int getRelevance() {
182 if (fContext instanceof PHPUnitContext) {
183 PHPUnitContext context = (PHPUnitContext) fContext;
184 switch (context.getCharacterBeforeStart()) {
185 // high relevance after whitespace