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 + "()");
80 case PHPIdentifierLocation.CONSTRUCTOR :
81 document.replace(start, end - start, fIdentifierName + "()");
83 case PHPIdentifierLocation.METHOD :
84 document.replace(start, end - start, fIdentifierName + "()");
88 document.replace(start, end - start, fIdentifierName);
91 // translate positions
92 LinkedPositionManager manager = new LinkedPositionManager(document);
93 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
94 // for (int i= 0; i != variables.length; i++) {
95 // TemplatePosition variable= variables[i];
97 // if (variable.isResolved())
100 // int[] offsets= variable.getOffsets();
101 // int length= variable.getLength();
103 // for (int j= 0; j != offsets.length; j++)
104 // manager.addPosition(offsets[j] + start, length);
107 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
108 switch (fLocation.getType()) {
109 case PHPIdentifierLocation.FUNCTION :
110 editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
112 case PHPIdentifierLocation.CONSTRUCTOR :
113 editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
115 case PHPIdentifierLocation.METHOD :
116 editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
120 editor.setFinalCaretOffset(fIdentifierName.length() + start);
124 fSelectedRegion = editor.getSelectedRegion();
126 } catch (BadLocationException e) {
127 PHPeclipsePlugin.log(e);
131 // catch (CoreException e) {
132 // handleException(e);
137 * @see ICompletionProposal#getAdditionalProposalInfo()
139 public String getAdditionalProposalInfo() {
140 StringBuffer hoverInfoBuffer = new StringBuffer();
141 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
142 String filename = workspaceLocation + fLocation.getFilename();
143 PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
144 return textToHTML(hoverInfoBuffer.toString());
148 * @see ICompletionProposal#getContextInformation()
150 public IContextInformation getContextInformation() {
155 * @see ICompletionProposal#getDisplayString()
157 public String getDisplayString() {
158 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
159 String filename = workspaceLocation + fLocation.getFilename();
160 return fIdentifierName + TemplateMessages.getString("TemplateProposal.delimiter") + PHPDocUtil.getUsage(filename, fLocation) + TemplateMessages.getString("TemplateProposal.delimiter") + filename; // $NON-NLS-1$ //$NON-NLS-1$
164 * @see ICompletionProposal#getImage()
166 public Image getImage() {
167 switch (fLocation.getType()) {
168 case PHPIdentifierLocation.FUNCTION :
169 return PHPUiImages.get(PHPUiImages.IMG_FUN);
170 case PHPIdentifierLocation.CLASS :
171 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
172 case PHPIdentifierLocation.CONSTRUCTOR :
173 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
174 case PHPIdentifierLocation.METHOD :
175 return PHPUiImages.get(PHPUiImages.IMG_FUN);
176 case PHPIdentifierLocation.DEFINE :
177 return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
178 case PHPIdentifierLocation.VARIABLE :
179 return PHPUiImages.get(PHPUiImages.IMG_VAR);
180 case PHPIdentifierLocation.GLOBAL_VARIABLE :
181 return PHPUiImages.get(PHPUiImages.IMG_VAR);
183 return PHPUiImages.get(PHPUiImages.IMG_FUN);
187 * @see IJavaCompletionProposal#getRelevance()
189 public int getRelevance() {
191 if (fContext instanceof PHPUnitContext) {
192 PHPUnitContext context = (PHPUnitContext) fContext;
193 switch (context.getCharacterBeforeStart()) {
194 // high relevance after whitespace