32769cb421f64a1e24606983b024ae76f4355089
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / IdentifierProposal.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.TemplateContext;
7 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
8 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
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 org.eclipse.jface.text.BadLocationException;
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.IRegion;
15 import org.eclipse.jface.text.ITextViewer;
16 import org.eclipse.jface.text.contentassist.IContextInformation;
17 import org.eclipse.swt.graphics.Image;
18 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
19 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
20 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
21 /**
22  * A PHP identifier proposal.
23  */
24 public class IdentifierProposal extends AbstractProposal { //implements
25                                                            // IPHPCompletionProposal
26                                                            // {
27   private final TemplateContext fContext;
28   private final Image fImage_fun;
29   private final Image fImage_var;
30   //private TemplateBuffer fTemplateBuffer;
31   private String fOldText;
32   private final IRegion fRegion;
33   //  private IRegion fSelectedRegion; // initialized by apply()
34   private final String fTemplate;
35   //  private final ITextViewer fViewer;
36   /**
37    * Creates a template proposal with a template and its context.
38    * 
39    * @param template
40    *            the template
41    * @param context
42    *            the context in which the template was requested.
43    * @param image
44    *            the icon of the proposal.
45    */
46   public IdentifierProposal(String template, TemplateContext context,
47       IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
48     super(viewer);
49     fTemplate = template;
50     fContext = context;
51     //    fViewer = viewer;
52     fImage_fun = image_fun;
53     fImage_var = image_var;
54     fRegion = region;
55   }
56   /*
57    * @see ICompletionProposal#apply(IDocument)
58    */
59   public void apply(IDocument document) {
60     try {
61       //                    if (fTemplateBuffer == null)
62       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
63       int start = fRegion.getOffset();
64       int end = fRegion.getOffset() + fRegion.getLength();
65       // insert template string
66       //  String templateString = fTemplate; // fTemplateBuffer.getString();
67       document.replace(start, end - start, fTemplate);
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       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
84       editor.setFinalCaretOffset(fTemplate.length() + start);
85       //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
86       editor.enter();
87       fSelectedRegion = editor.getSelectedRegion();
88     } catch (BadLocationException e) {
89       PHPeclipsePlugin.log(e);
90       openErrorDialog(e);
91     }
92     //      catch (CoreException e) {
93     //                  handleException(e);
94     //      }
95   }
96   /*
97    * @see ICompletionProposal#getAdditionalProposalInfo()
98    */
99   public String getAdditionalProposalInfo() {
100     //      try {
101     //                  if (fTemplateBuffer == null)
102     //                          fTemplateBuffer= fContext.evaluate(fTemplate);
103     return textToHTML(fTemplate); // fTemplateBuffer.getString());
104     //      } catch (CoreException e) {
105     //                  handleException(e);
106     //                  return null;
107     //      }
108   }
109   /*
110    * @see ICompletionProposal#getContextInformation()
111    */
112   public IContextInformation getContextInformation() {
113     return null;
114   }
115   /*
116    * @see ICompletionProposal#getDisplayString()
117    */
118   public String getDisplayString() {
119     return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ 
120   }
121   /*
122    * @see ICompletionProposal#getImage()
123    */
124   public Image getImage() {
125     if (fTemplate.charAt(0) == '$') {
126       return fImage_var;
127     }
128     return fImage_fun;
129   }
130   /*
131    * @see IJavaCompletionProposal#getRelevance()
132    */
133   public int getRelevance() {
134     if (fContext instanceof PHPUnitContext) {
135       PHPUnitContext context = (PHPUnitContext) 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         default :
144           return 0;
145       }
146     } else {
147       return 50;
148     }
149   }
150 }