Syntax highlighting is changeable.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / BuiltInProposal.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text.template;
6 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
7 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
8 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
9 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
10 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
11 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
12 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
13
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.jface.text.contentassist.IContextInformation;
19 import org.eclipse.jface.text.templates.TemplateContext;
20 import org.eclipse.swt.graphics.Image;
21 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
22 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
23 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
24
25 /**
26  * A PHP identifier proposal.
27  */
28 public class BuiltInProposal extends AbstractProposal { // implements IPHPCompletionProposal {
29   private final TemplateContext fContext;
30   private final PHPFunction fFunction;
31
32   //private TemplateBuffer fTemplateBuffer;
33   private String fOldText;
34   //  private final Image fImage_builtin;
35
36   private final IRegion fRegion;
37   //  private IRegion fSelectedRegion; // initialized by apply()
38
39   private final String fBuiltinFunctionName;
40   //  private final ITextViewer fViewer;
41
42   /**
43    * Creates a template proposal with a template and its context.
44    * @param template  the template
45    * @param context   the context in which the template was requested.
46    * @param image     the icon of the proposal.
47    */
48   public BuiltInProposal(String functionName, PHPFunction function, TemplateContext context, IRegion region, ITextViewer viewer) {
49     super(viewer);
50     fBuiltinFunctionName = functionName;
51     fFunction = function;
52     fContext = context;
53     //    fViewer = viewer;
54     fRegion = region;
55   }
56
57   /*
58    * @see ICompletionProposal#apply(IDocument)
59    */
60   public void apply(IDocument document) {
61     try {
62       //                    if (fTemplateBuffer == null)
63       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
64
65       int start = fRegion.getOffset();
66       int end = fRegion.getOffset() + fRegion.getLength();
67
68       // insert template string
69       //  String templateString = fTemplate; // fTemplateBuffer.getString();    
70       document.replace(start, end - start, fBuiltinFunctionName + "()");
71
72       // translate positions
73       LinkedPositionManager manager = new LinkedPositionManager(document);
74       //                        TemplatePosition[] variables= fTemplateBuffer.getVariables();
75       //                        for (int i= 0; i != variables.length; i++) {
76       //                                TemplatePosition variable= variables[i];
77       //
78       //                                if (variable.isResolved())
79       //                                        continue;
80       //                                
81       //                                int[] offsets= variable.getOffsets();
82       //                                int length= variable.getLength();
83       //                                
84       //                                for (int j= 0; j != offsets.length; j++)
85       //                                        manager.addPosition(offsets[j] + start, length);
86       //                        }
87
88       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
89       editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start + 1);
90       //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
91       editor.enter();
92
93       fSelectedRegion = editor.getSelectedRegion();
94
95     } catch (BadLocationException e) {
96       PHPeclipsePlugin.log(e);
97       openErrorDialog(e);
98
99     }
100     //      catch (CoreException e) {
101     //                  handleException(e);
102     //      }       
103   }
104
105   /*
106    * @see ICompletionProposal#getAdditionalProposalInfo()
107    */
108   public String getAdditionalProposalInfo() {
109     return textToHTML(fFunction.getDescription()); // fTemplateBuffer.getString());
110   }
111
112   /*
113    * @see ICompletionProposal#getContextInformation()
114    */
115   public IContextInformation getContextInformation() {
116     return null;
117   }
118
119   /*
120    * @see ICompletionProposal#getDisplayString()
121    */
122   public String getDisplayString() {
123     return fBuiltinFunctionName + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$ //$NON-NLS-1$
124     //          return fTemplate.getName() + ObfuscatorMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
125   }
126
127   /*
128    * @see ICompletionProposal#getImage()
129    */
130   public Image getImage() {
131     return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
132   }
133
134   /*
135    * @see IJavaCompletionProposal#getRelevance()
136    */
137   public int getRelevance() {
138
139     if (fContext instanceof JavaContext) {
140       JavaContext context = (JavaContext) fContext;
141       switch (context.getCharacterBeforeStart()) {
142         // high relevance after whitespace
143         case ' ' :
144         case '\r' :
145         case '\n' :
146         case '\t' :
147           return 50;
148
149         default :
150           return 0;
151       }
152     } else {
153       return 50;
154     }
155   }
156
157 }