3f27aaddcdb2cd41396c5f30908cdb9c18de2356
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / template / IdentifierProposal.java
1 package net.sourceforge.phpdt.internal.ui.text.template;
2
3 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
4 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
5 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
6 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
7 import net.sourceforge.phpeclipse.ui.WebUI;
8 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
9
10 import org.eclipse.jface.text.BadLocationException;
11 import org.eclipse.jface.text.IDocument;
12 import org.eclipse.jface.text.IRegion;
13 import org.eclipse.jface.text.ITextViewer;
14 import org.eclipse.jface.text.templates.TemplateContext;
15 import org.eclipse.swt.graphics.Image;
16
17 /**
18  * A PHP identifier proposal.
19  */
20 public class IdentifierProposal extends AbstractProposal {
21         private final TemplateContext fContext;
22
23         private final Image fImage_fun;
24
25         private final Image fImage_var;
26
27         private final IRegion fRegion;
28
29         private final String fTemplate;
30
31         public IdentifierProposal(String template, TemplateContext context,
32                         IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
33                 super(viewer);
34                 fTemplate = template;
35                 fContext = context;
36
37                 fImage_fun = image_fun;
38                 fImage_var = image_var;
39                 fRegion = region;
40         }
41
42         public void apply(IDocument document) {
43                 try {
44                         int start = fRegion.getOffset();
45                         int end = fRegion.getOffset() + fRegion.getLength();
46                         document.replace(start, end - start, fTemplate);
47                         // translate positions
48                         LinkedPositionManager manager = new LinkedPositionManager(document);
49
50                         LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
51                         editor.setFinalCaretOffset(fTemplate.length() + start);
52                         editor.enter();
53                         fSelectedRegion = editor.getSelectedRegion();
54                 } catch (BadLocationException e) {
55                         WebUI.log(e);
56                         openErrorDialog(e);
57                 }
58         }
59
60         public String getAdditionalProposalInfo() {
61                 return textToHTML(fTemplate); 
62         }
63
64         public String getDisplayString() {
65                 return fTemplate
66                                 + TemplateMessages.getString("TemplateProposal.delimiter")
67                                 + fTemplate; // $NON-NLS-1$
68         }
69
70         public Image getImage() {
71                 if (fTemplate.charAt(0) == '$') {
72                         return fImage_var;
73                 }
74                 return fImage_fun;
75         }
76
77         public int getRelevance() {
78                 if (fContext instanceof JavaContext) {
79                         JavaContext context = (JavaContext) fContext;
80                         switch (context.getCharacterBeforeStart()) {
81                         // high relevance after whitespace
82                         case ' ':
83                         case '\r':
84                         case '\n':
85                         case '\t':
86                                 return 50;
87                         default:
88                                 return 0;
89                         }
90                 } else {
91                         return 50;
92                 }
93         }
94 }