2  * (c) Copyright IBM Corp. 2000, 2001.
 
   5 package net.sourceforge.phpdt.internal.ui.text.template;
 
   7 import net.sourceforge.phpdt.internal.corext.phpdoc.PHPDocUtil;
 
   8 import net.sourceforge.phpdt.internal.corext.template.TemplateContext;
 
   9 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
 
  10 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
 
  11 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
 
  12 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
 
  13 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
 
  14 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
 
  15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  16 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
 
  18 import org.eclipse.core.runtime.CoreException;
 
  19 import org.eclipse.jface.dialogs.MessageDialog;
 
  20 import org.eclipse.jface.text.BadLocationException;
 
  21 import org.eclipse.jface.text.IDocument;
 
  22 import org.eclipse.jface.text.IRegion;
 
  23 import org.eclipse.jface.text.ITextViewer;
 
  24 import org.eclipse.jface.text.contentassist.IContextInformation;
 
  25 import org.eclipse.swt.graphics.Image;
 
  26 import org.eclipse.swt.graphics.Point;
 
  27 import org.eclipse.swt.widgets.Shell;
 
  28 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
 
  29 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
 
  30 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
 
  33  * A PHP identifier proposal.
 
  35 public class DeclarationProposal implements IPHPCompletionProposal {
 
  37   private final String fTemplate;
 
  38   private final PHPIdentifierLocation fLocation;
 
  39   private final TemplateContext fContext;
 
  40   private final ITextViewer fViewer;
 
  41   //  private final Image fImage_fun;
 
  42   //  private final Image fImage_var;
 
  43   private final IRegion fRegion;
 
  45   //private TemplateBuffer fTemplateBuffer;
 
  46   private String fOldText;
 
  47   private IRegion fSelectedRegion; // initialized by apply()
 
  50    * Creates a template proposal with a template and its context.
 
  51    * @param template  the template
 
  52    * @param context   the context in which the template was requested.
 
  53    * @param image     the icon of the proposal.
 
  55   public DeclarationProposal(
 
  57     PHPIdentifierLocation location,
 
  58     TemplateContext context,
 
  67     //    fImage_fun = image_fun;
 
  68     //    fImage_var = image_var;
 
  73    * @see ICompletionProposal#apply(IDocument)
 
  75   public void apply(IDocument document) {
 
  77       //                    if (fTemplateBuffer == null)
 
  78       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
 
  80       int start = fRegion.getOffset();
 
  81       int end = fRegion.getOffset() + fRegion.getLength();
 
  83       // insert template string
 
  84       //  String templateString = fTemplate; // fTemplateBuffer.getString();    
 
  85       document.replace(start, end - start, fTemplate);
 
  87       // translate positions
 
  88       LinkedPositionManager manager = new LinkedPositionManager(document);
 
  89       //                        TemplatePosition[] variables= fTemplateBuffer.getVariables();
 
  90       //                        for (int i= 0; i != variables.length; i++) {
 
  91       //                                TemplatePosition variable= variables[i];
 
  93       //                                if (variable.isResolved())
 
  96       //                                int[] offsets= variable.getOffsets();
 
  97       //                                int length= variable.getLength();
 
  99       //                                for (int j= 0; j != offsets.length; j++)
 
 100       //                                        manager.addPosition(offsets[j] + start, length);
 
 103       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
 
 104       editor.setFinalCaretOffset(fTemplate.length() + start);
 
 105       //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
 
 108       fSelectedRegion = editor.getSelectedRegion();
 
 110     } catch (BadLocationException e) {
 
 111       PHPeclipsePlugin.log(e);
 
 115     //      catch (CoreException e) {
 
 116     //                  handleException(e);
 
 120   //  private static int getCaretOffset(TemplateBuffer buffer) {
 
 121   //    TemplatePosition[] variables = buffer.getVariables();
 
 122   //    for (int i = 0; i != variables.length; i++) {
 
 123   //      TemplatePosition variable = variables[i];
 
 125   //      if (variable.getName().equals(JavaTemplateMessages.getString("GlobalVariables.variable.name.cursor"))) //$NON-NLS-1$
 
 126   //        return variable.getOffsets()[0];
 
 129   //    return buffer.getString().length();
 
 133    * @see ICompletionProposal#getSelection(IDocument)
 
 135   public Point getSelection(IDocument document) {
 
 136     return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
 
 141    * @see ICompletionProposal#getAdditionalProposalInfo()
 
 143   public String getAdditionalProposalInfo() {
 
 144     StringBuffer hoverInfoBuffer = new StringBuffer();
 
 145     String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
 
 146     String filename = workspaceLocation + fLocation.getFilename();
 
 147     PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
 
 148     return textToHTML(hoverInfoBuffer.toString());
 
 152    * @see ICompletionProposal#getDisplayString()
 
 154   public String getDisplayString() {
 
 155     return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
 
 156     //          return fTemplate.getName() + ObfuscatorMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
 
 160    * @see ICompletionProposal#getImage()
 
 162   public Image getImage() {
 
 163     switch (fLocation.getType()) {
 
 164       case PHPIdentifierLocation.FUNCTION :
 
 165         return PHPUiImages.get(PHPUiImages.IMG_FUN);
 
 166                         case PHPIdentifierLocation.CLASS :
 
 167                                 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
 
 168                         case PHPIdentifierLocation.METHOD :
 
 169                                 return PHPUiImages.get(PHPUiImages.IMG_FUN);
 
 170                         case PHPIdentifierLocation.DEFINE :
 
 171                                 return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
 
 172                         case PHPIdentifierLocation.VARIABLE :
 
 173                                 return PHPUiImages.get(PHPUiImages.IMG_VAR);
 
 175     return PHPUiImages.get(PHPUiImages.IMG_FUN);
 
 179    * @see ICompletionProposal#getContextInformation()
 
 181   public IContextInformation getContextInformation() {
 
 185   private static String textToHTML(String string) {
 
 186     StringBuffer buffer = new StringBuffer(string.length());
 
 187     buffer.append("<pre>"); //$NON-NLS-1$
 
 189     for (int i = 0; i != string.length(); i++) {
 
 190       char ch = string.charAt(i);
 
 194           buffer.append("&"); //$NON-NLS-1$
 
 198           buffer.append("<"); //$NON-NLS-1$
 
 202           buffer.append(">"); //$NON-NLS-1$
 
 206           buffer.append("    "); //$NON-NLS-1$
 
 210           buffer.append("<br>"); //$NON-NLS-1$
 
 219     buffer.append("</pre>"); //$NON-NLS-1$
 
 220     return buffer.toString();
 
 223   private void openErrorDialog(BadLocationException e) {
 
 224     Shell shell = fViewer.getTextWidget().getShell();
 
 225     MessageDialog.openError(shell, TemplateMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
 
 228   private void handleException(CoreException e) {
 
 229     Shell shell = fViewer.getTextWidget().getShell();
 
 230     PHPeclipsePlugin.log(e);
 
 231     //          ExceptionHandler.handle(e, shell, ObfuscatorMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
 
 235    * @see IJavaCompletionProposal#getRelevance()
 
 237   public int getRelevance() {
 
 239     if (fContext instanceof PHPUnitContext) {
 
 240       PHPUnitContext context = (PHPUnitContext) fContext;
 
 241       switch (context.getCharacterBeforeStart()) {
 
 242         // high relevance after whitespace