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