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 fTemplate;
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(
52 PHPIdentifierLocation location,
53 TemplateContext context,
63 // fImage_fun = image_fun;
64 // fImage_var = image_var;
69 * @see ICompletionProposal#apply(IDocument)
71 public void apply(IDocument document) {
73 // if (fTemplateBuffer == null)
74 // fTemplateBuffer= fContext.evaluate(fTemplate);
76 int start = fRegion.getOffset();
77 int end = fRegion.getOffset() + fRegion.getLength();
79 // insert template string
80 // String templateString = fTemplate; // fTemplateBuffer.getString();
81 document.replace(start, end - start, fTemplate);
83 // translate positions
84 LinkedPositionManager manager = new LinkedPositionManager(document);
85 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
86 // for (int i= 0; i != variables.length; i++) {
87 // TemplatePosition variable= variables[i];
89 // if (variable.isResolved())
92 // int[] offsets= variable.getOffsets();
93 // int length= variable.getLength();
95 // for (int j= 0; j != offsets.length; j++)
96 // manager.addPosition(offsets[j] + start, length);
99 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
100 editor.setFinalCaretOffset(fTemplate.length() + start);
101 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
104 fSelectedRegion = editor.getSelectedRegion();
106 } catch (BadLocationException e) {
107 PHPeclipsePlugin.log(e);
111 // catch (CoreException e) {
112 // handleException(e);
117 * @see ICompletionProposal#getAdditionalProposalInfo()
119 public String getAdditionalProposalInfo() {
120 StringBuffer hoverInfoBuffer = new StringBuffer();
121 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
122 String filename = workspaceLocation + fLocation.getFilename();
123 PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
124 return textToHTML(hoverInfoBuffer.toString());
128 * @see ICompletionProposal#getContextInformation()
130 public IContextInformation getContextInformation() {
135 * @see ICompletionProposal#getDisplayString()
137 public String getDisplayString() {
138 return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
139 // return fTemplate.getName() + ObfuscatorMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
143 * @see ICompletionProposal#getImage()
145 public Image getImage() {
146 switch (fLocation.getType()) {
147 case PHPIdentifierLocation.FUNCTION :
148 return PHPUiImages.get(PHPUiImages.IMG_FUN);
149 case PHPIdentifierLocation.CLASS :
150 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
151 case PHPIdentifierLocation.METHOD :
152 return PHPUiImages.get(PHPUiImages.IMG_FUN);
153 case PHPIdentifierLocation.DEFINE :
154 return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
155 case PHPIdentifierLocation.VARIABLE :
156 return PHPUiImages.get(PHPUiImages.IMG_VAR);
158 return PHPUiImages.get(PHPUiImages.IMG_FUN);
162 * @see IJavaCompletionProposal#getRelevance()
164 public int getRelevance() {
166 if (fContext instanceof PHPUnitContext) {
167 PHPUnitContext context = (PHPUnitContext) fContext;
168 switch (context.getCharacterBeforeStart()) {
169 // high relevance after whitespace