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.core.resources.IProject;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.contentassist.IContextInformation;
23 import org.eclipse.swt.graphics.Image;
24 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
25 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
26 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
29 * A PHP identifier proposal.
31 public class DeclarationProposal extends AbstractProposal { //implements IPHPCompletionProposal {
32 private IProject fProject;
33 private final TemplateContext fContext;
34 private final PHPIdentifierLocation fLocation;
36 //private TemplateBuffer fTemplateBuffer;
37 private String fOldText;
38 // private final Image fImage_fun;
39 // private final Image fImage_var;
40 private final IRegion fRegion;
41 // private IRegion fSelectedRegion; // initialized by apply()
43 private final String fIdentifierName;
44 // private final ITextViewer fViewer;
47 * Creates a template proposal with a template and its context.
48 * @param template the template
49 * @param context the context in which the template was requested.
50 * @param image the icon of the proposal.
52 public DeclarationProposal(
54 String identifierName,
55 PHPIdentifierLocation location,
56 TemplateContext context,
63 fIdentifierName = identifierName;
70 * @see ICompletionProposal#apply(IDocument)
72 public void apply(IDocument document) {
74 // if (fTemplateBuffer == null)
75 // fTemplateBuffer= fContext.evaluate(fTemplate);
77 int start = fRegion.getOffset();
78 int end = fRegion.getOffset() + fRegion.getLength();
80 switch (fLocation.getType()) {
81 case PHPIdentifierLocation.FUNCTION :
82 document.replace(start, end - start, fIdentifierName + "()");
84 case PHPIdentifierLocation.CONSTRUCTOR :
85 document.replace(start, end - start, fIdentifierName + "()");
87 case PHPIdentifierLocation.METHOD :
88 document.replace(start, end - start, fIdentifierName + "()");
92 document.replace(start, end - start, fIdentifierName);
95 // translate positions
96 LinkedPositionManager manager = new LinkedPositionManager(document);
97 // TemplatePosition[] variables= fTemplateBuffer.getVariables();
98 // for (int i= 0; i != variables.length; i++) {
99 // TemplatePosition variable= variables[i];
101 // if (variable.isResolved())
104 // int[] offsets= variable.getOffsets();
105 // int length= variable.getLength();
107 // for (int j= 0; j != offsets.length; j++)
108 // manager.addPosition(offsets[j] + start, length);
111 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
112 switch (fLocation.getType()) {
113 case PHPIdentifierLocation.FUNCTION :
114 editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
116 case PHPIdentifierLocation.CONSTRUCTOR :
117 editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
119 case PHPIdentifierLocation.METHOD :
120 editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
124 editor.setFinalCaretOffset(fIdentifierName.length() + start);
128 fSelectedRegion = editor.getSelectedRegion();
130 } catch (BadLocationException e) {
131 PHPeclipsePlugin.log(e);
135 // catch (CoreException e) {
136 // handleException(e);
141 * @see ICompletionProposal#getAdditionalProposalInfo()
143 public String getAdditionalProposalInfo() {
144 StringBuffer hoverInfoBuffer = new StringBuffer();
145 // String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
146 String workspaceLocation;
147 if (fProject!=null) {
148 workspaceLocation = fProject.getLocation().toString()+'/';
150 // should never happen?
151 workspaceLocation = PHPeclipsePlugin.getWorkspace()
152 .getRoot().getLocation().toString();
154 String filename = workspaceLocation + fLocation.getFilename();
155 PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
156 return hoverInfoBuffer.toString();
160 * @see ICompletionProposal#getContextInformation()
162 public IContextInformation getContextInformation() {
167 * @see ICompletionProposal#getDisplayString()
169 public String getDisplayString() {
170 // String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
171 String workspaceLocation;
172 if (fProject!=null) {
173 workspaceLocation = fProject.getLocation().toString()+'/';
175 // should never happen?
176 workspaceLocation = PHPeclipsePlugin.getWorkspace()
177 .getRoot().getLocation().toString();
179 String filename = workspaceLocation + fLocation.getFilename();
180 return fIdentifierName + TemplateMessages.getString("TemplateProposal.delimiter") + PHPDocUtil.getUsage(filename, fLocation) + TemplateMessages.getString("TemplateProposal.delimiter") + filename; // $NON-NLS-1$ //$NON-NLS-1$
184 * @see ICompletionProposal#getImage()
186 public Image getImage() {
187 switch (fLocation.getType()) {
188 case PHPIdentifierLocation.FUNCTION :
189 return PHPUiImages.get(PHPUiImages.IMG_FUN);
190 case PHPIdentifierLocation.CLASS :
191 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
192 case PHPIdentifierLocation.CONSTRUCTOR :
193 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
194 case PHPIdentifierLocation.METHOD :
195 return PHPUiImages.get(PHPUiImages.IMG_FUN);
196 case PHPIdentifierLocation.DEFINE :
197 return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
198 case PHPIdentifierLocation.VARIABLE :
199 return PHPUiImages.get(PHPUiImages.IMG_VAR);
200 case PHPIdentifierLocation.GLOBAL_VARIABLE :
201 return PHPUiImages.get(PHPUiImages.IMG_VAR);
203 return PHPUiImages.get(PHPUiImages.IMG_FUN);
207 * @see IJavaCompletionProposal#getRelevance()
209 public int getRelevance() {
211 if (fContext instanceof PHPUnitContext) {
212 PHPUnitContext context = (PHPUnitContext) fContext;
213 switch (context.getCharacterBeforeStart()) {
214 // high relevance after whitespace