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