Fixed ?
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / TemplateProposal.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.Template;
8 import net.sourceforge.phpdt.internal.corext.template.TemplateBuffer;
9 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
10 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
11 import net.sourceforge.phpdt.internal.corext.template.TemplatePosition;
12 import net.sourceforge.phpdt.internal.corext.template.php.PHPTemplateMessages;
13 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
14 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
15 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.text.ITextViewer;
23 import org.eclipse.jface.text.contentassist.IContextInformation;
24 import org.eclipse.swt.graphics.Image;
25 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
26 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
27 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
28
29 /**
30  * A template proposal.
31  */
32 public class TemplateProposal extends AbstractProposal {
33   private final TemplateContext fContext;
34   private final Image fImage;
35   private String fOldText;
36   private final IRegion fRegion;
37
38   private final Template fTemplate;
39
40   private TemplateBuffer fTemplateBuffer;
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 TemplateProposal(Template template, TemplateContext context, IRegion region, ITextViewer viewer, Image image) {
49     super(viewer);
50
51     fTemplate = template;
52     fContext = context;
53     //    fViewer = viewer;
54     fImage = image;
55     fRegion = region;
56   }
57
58   private static int getCaretOffset(TemplateBuffer buffer) {
59     TemplatePosition[] variables = buffer.getVariables();
60     for (int i = 0; i != variables.length; i++) {
61       TemplatePosition variable = variables[i];
62
63       if (variable.getName().equals(PHPTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$
64         return variable.getOffsets()[0];
65     }
66
67     return buffer.getString().length();
68   }
69
70   /*
71    * @see ICompletionProposal#apply(IDocument)
72    */
73   public void apply(IDocument document) {
74     try {
75       if (fTemplateBuffer == null)
76         fTemplateBuffer = fContext.evaluate(fTemplate);
77
78       int start = fRegion.getOffset();
79       int end = fRegion.getOffset() + fRegion.getLength();
80
81       // insert template string
82       String templateString = fTemplateBuffer.getString();
83       document.replace(start, end - start, templateString);
84
85       // translate positions
86       LinkedPositionManager manager = new LinkedPositionManager(document);
87       TemplatePosition[] variables = fTemplateBuffer.getVariables();
88       for (int i = 0; i != variables.length; i++) {
89         TemplatePosition variable = variables[i];
90
91         if (variable.isResolved())
92           continue;
93
94         int[] offsets = variable.getOffsets();
95         int length = variable.getLength();
96
97         for (int j = 0; j != offsets.length; j++)
98           manager.addPosition(offsets[j] + start, length);
99       }
100
101       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
102       editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
103       editor.enter();
104
105       fSelectedRegion = editor.getSelectedRegion();
106
107     } catch (BadLocationException e) {
108       PHPeclipsePlugin.log(e);
109       openErrorDialog(e);
110
111     } catch (CoreException e) {
112       handleException(e);
113     }
114   }
115
116   /*
117    * @see ICompletionProposal#getAdditionalProposalInfo()
118    */
119   public String getAdditionalProposalInfo() {
120     try {
121       if (fTemplateBuffer == null)
122         fTemplateBuffer = fContext.evaluate(fTemplate);
123
124       return textToHTML(fTemplateBuffer.getString());
125
126     } catch (CoreException e) {
127       handleException(e);
128       return null;
129     }
130   }
131
132   /*
133    * @see ICompletionProposal#getContextInformation()
134    */
135   public IContextInformation getContextInformation() {
136     return null;
137   }
138
139   /*
140    * @see ICompletionProposal#getDisplayString()
141    */
142   public String getDisplayString() {
143     return fTemplate.getName() + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
144   }
145
146   /*
147    * @see ICompletionProposal#getImage()
148    */
149   public Image getImage() {
150     return fImage;
151   }
152
153   /*
154    * @see IJavaCompletionProposal#getRelevance()
155    */
156   public int getRelevance() {
157
158     if (fContext instanceof PHPUnitContext) {
159       PHPUnitContext context = (PHPUnitContext) fContext;
160       switch (context.getCharacterBeforeStart()) {
161         // high relevance after whitespace
162         case ' ' :
163         case '\r' :
164         case '\n' :
165         case '\t' :
166           return 85;
167
168         default :
169           return 0;
170       }
171     } else {
172       return 90;
173     }
174   }
175
176 }