7c9570c6330b56cc86a0b747707201362a2fac04
[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 net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
22 //import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
23 //import net.sourceforge.phpdt.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 String fOldText;
33
34   private final IRegion fRegion;
35
36   private final String fBuiltinFunctionName;
37
38   /**
39    * Creates a template proposal with a template and its context.
40    * @param template  the template
41    * @param context   the context in which the template was requested.
42    * @param image     the icon of the proposal.
43    */
44   public BuiltInProposal(String functionName, PHPFunction function, TemplateContext context, IRegion region, ITextViewer viewer) {
45     super(viewer);
46     fBuiltinFunctionName = functionName;
47     fFunction = function;
48     fContext = context;
49     //    fViewer = viewer;
50     fRegion = region;
51   }
52
53   /*
54    * @see ICompletionProposal#apply(IDocument)
55    */
56   public void apply(IDocument document) {
57     try {
58       //                    if (fTemplateBuffer == null)
59       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
60
61       int start = fRegion.getOffset();
62       int end = fRegion.getOffset() + fRegion.getLength();
63
64       // insert template string
65       //  String templateString = fTemplate; // fTemplateBuffer.getString();    
66       document.replace(start, end - start, fBuiltinFunctionName + "()");
67
68       // translate positions
69       LinkedPositionManager manager = new LinkedPositionManager(document);
70       //                        TemplatePosition[] variables= fTemplateBuffer.getVariables();
71       //                        for (int i= 0; i != variables.length; i++) {
72       //                                TemplatePosition variable= variables[i];
73       //
74       //                                if (variable.isResolved())
75       //                                        continue;
76       //                                
77       //                                int[] offsets= variable.getOffsets();
78       //                                int length= variable.getLength();
79       //                                
80       //                                for (int j= 0; j != offsets.length; j++)
81       //                                        manager.addPosition(offsets[j] + start, length);
82       //                        }
83
84       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
85       editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start + 1);
86       //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
87       editor.enter();
88
89       fSelectedRegion = editor.getSelectedRegion();
90
91     } catch (BadLocationException e) {
92       PHPeclipsePlugin.log(e);
93       openErrorDialog(e);
94
95     }
96     //      catch (CoreException e) {
97     //                  handleException(e);
98     //      }       
99   }
100
101   /*
102    * @see ICompletionProposal#getAdditionalProposalInfo()
103    */
104   public String getAdditionalProposalInfo() {
105     return textToHTML(fFunction.getDescription()); 
106   }
107
108   /*
109    * @see ICompletionProposal#getContextInformation()
110    */
111   public IContextInformation getContextInformation() {
112     return null;
113   }
114
115   /*
116    * @see ICompletionProposal#getDisplayString()
117    */
118   public String getDisplayString() {
119     return fBuiltinFunctionName + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$ //$NON-NLS-1$
120   }
121
122   /*
123    * @see ICompletionProposal#getImage()
124    */
125   public Image getImage() {
126     return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
127   }
128
129   /*
130    * @see IJavaCompletionProposal#getRelevance()
131    */
132   public int getRelevance() {
133
134     if (fContext instanceof JavaContext) {
135       JavaContext context = (JavaContext) fContext;
136       switch (context.getCharacterBeforeStart()) {
137         // high relevance after whitespace
138         case ' ' :
139         case '\r' :
140         case '\n' :
141         case '\t' :
142           return 50;
143
144         default :
145           return 0;
146       }
147     } else {
148       return 50;
149     }
150   }
151
152 }