Syntax highlighting is changeable.
[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.TemplateMessages;
9 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
10 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
11 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
12 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
13 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
14 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
15
16 import org.eclipse.core.resources.IProject;
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.jface.text.templates.TemplateContext;
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;
27
28 /**
29  * A PHP identifier proposal.
30  */
31 public class DeclarationProposal extends AbstractProposal { //implements IPHPCompletionProposal {
32   private IProject fProject;
33   private final TemplateContext fContext;
34   private final PHPIdentifierLocation fLocation;
35
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()
42
43   private final String fIdentifierName;
44   //  private final ITextViewer fViewer;
45
46   /**
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.
51    */
52   public DeclarationProposal(
53       IProject project,
54     String identifierName,
55     PHPIdentifierLocation location,
56     TemplateContext context,
57     IRegion region,
58     ITextViewer viewer) {
59     super(viewer);
60     //    Image image_fun,
61     //    Image image_var) {
62     fProject = project;
63     fIdentifierName = identifierName;
64     fLocation = location;
65     fContext = context;
66     fRegion = region;
67   }
68
69   /*
70    * @see ICompletionProposal#apply(IDocument)
71    */
72   public void apply(IDocument document) {
73     try {
74       //                    if (fTemplateBuffer == null)
75       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
76
77       int start = fRegion.getOffset();
78       int end = fRegion.getOffset() + fRegion.getLength();
79
80       switch (fLocation.getType()) {
81         case PHPIdentifierLocation.FUNCTION :
82           document.replace(start, end - start, fIdentifierName + "()");
83           break;
84         case PHPIdentifierLocation.CONSTRUCTOR :
85           document.replace(start, end - start, fIdentifierName + "()");
86           break;
87         case PHPIdentifierLocation.METHOD :
88           document.replace(start, end - start, fIdentifierName + "()");
89           break;
90
91         default :
92           document.replace(start, end - start, fIdentifierName);
93       }
94
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];
100       //
101       //                                if (variable.isResolved())
102       //                                        continue;
103       //                                
104       //                                int[] offsets= variable.getOffsets();
105       //                                int length= variable.getLength();
106       //                                
107       //                                for (int j= 0; j != offsets.length; j++)
108       //                                        manager.addPosition(offsets[j] + start, length);
109       //                        }
110
111       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
112       switch (fLocation.getType()) {
113         case PHPIdentifierLocation.FUNCTION :
114           editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
115           break;
116         case PHPIdentifierLocation.CONSTRUCTOR :
117           editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
118           break;
119         case PHPIdentifierLocation.METHOD :
120           editor.setFinalCaretOffset(fIdentifierName.length() + start + 1);
121           break;
122
123         default :
124           editor.setFinalCaretOffset(fIdentifierName.length() + start);
125       }
126       editor.enter();
127
128       fSelectedRegion = editor.getSelectedRegion();
129
130     } catch (BadLocationException e) {
131       PHPeclipsePlugin.log(e);
132       openErrorDialog(e);
133
134     }
135     //      catch (CoreException e) {
136     //                  handleException(e);
137     //      }       
138   }
139
140   /*
141    * @see ICompletionProposal#getAdditionalProposalInfo()
142    */
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()+'/';
149     } else {
150       // should never happen?
151       workspaceLocation = PHPeclipsePlugin.getWorkspace()
152       .getRoot().getLocation().toString();
153     }
154     String filename = workspaceLocation + fLocation.getFilename();
155     PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
156     return hoverInfoBuffer.toString();
157   }
158
159   /*
160    * @see ICompletionProposal#getContextInformation()
161    */
162   public IContextInformation getContextInformation() {
163     return null;
164   }
165
166   /*
167    * @see ICompletionProposal#getDisplayString()
168    */
169   public String getDisplayString() {
170 //    String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
171     String workspaceLocation;
172     if (fProject!=null) {
173       workspaceLocation = fProject.getLocation().toString()+'/';
174     } else {
175       // should never happen?
176       workspaceLocation = PHPeclipsePlugin.getWorkspace()
177       .getRoot().getLocation().toString();
178     }
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$
181   }
182
183   /*
184    * @see ICompletionProposal#getImage()
185    */
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);
202     }
203     return PHPUiImages.get(PHPUiImages.IMG_FUN);
204   }
205
206   /*
207    * @see IJavaCompletionProposal#getRelevance()
208    */
209   public int getRelevance() {
210
211     if (fContext instanceof JavaContext) {
212       JavaContext context = (JavaContext) fContext;
213       switch (context.getCharacterBeforeStart()) {
214         // high relevance after whitespace
215         case ' ' :
216         case '\r' :
217         case '\n' :
218         case '\t' :
219           return 90;
220         case '>' : // ->
221         case ':' : // ::
222           return 95;
223         default :
224           return 0;
225       }
226     } else {
227       return 90;
228     }
229   }
230
231 }