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