Deleted PHPFunctionNames use only phpsyntax.xml instead !
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / DeclarationProposal.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.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.link.LinkedPositionManager;
13 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.contentassist.IContextInformation;
22 import org.eclipse.swt.graphics.Image;
23 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
24 //import org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI;
25 //import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
26
27 /**
28  * A PHP identifier proposal.
29  */
30 public class DeclarationProposal extends AbstractProposal {//implements IPHPCompletionProposal {
31   private final TemplateContext fContext;
32   private final PHPIdentifierLocation fLocation;
33
34   //private TemplateBuffer fTemplateBuffer;
35   private String fOldText;
36   //  private final Image fImage_fun;
37   //  private final Image fImage_var;
38   private final IRegion fRegion;
39 //  private IRegion fSelectedRegion; // initialized by apply()
40
41   private final String fTemplate;
42 //  private final ITextViewer fViewer;
43
44   /**
45    * Creates a template proposal with a template and its context.
46    * @param template  the template
47    * @param context   the context in which the template was requested.
48    * @param image     the icon of the proposal.
49    */
50   public DeclarationProposal(
51     String template,
52     PHPIdentifierLocation location,
53     TemplateContext context,
54     IRegion region,
55     ITextViewer viewer) {
56         super(viewer);
57     //    Image image_fun,
58     //    Image image_var) {
59     fTemplate = template;
60     fLocation = location;
61     fContext = context;
62 //    fViewer = viewer;
63     //    fImage_fun = image_fun;
64     //    fImage_var = image_var;
65     fRegion = region;
66   }
67
68   /*
69    * @see ICompletionProposal#apply(IDocument)
70    */
71   public void apply(IDocument document) {
72     try {
73       //                    if (fTemplateBuffer == null)
74       //                                fTemplateBuffer= fContext.evaluate(fTemplate);
75
76       int start = fRegion.getOffset();
77       int end = fRegion.getOffset() + fRegion.getLength();
78
79       // insert template string
80       //  String templateString = fTemplate; // fTemplateBuffer.getString();    
81       document.replace(start, end - start, fTemplate);
82
83       // translate positions
84       LinkedPositionManager manager = new LinkedPositionManager(document);
85       //                        TemplatePosition[] variables= fTemplateBuffer.getVariables();
86       //                        for (int i= 0; i != variables.length; i++) {
87       //                                TemplatePosition variable= variables[i];
88       //
89       //                                if (variable.isResolved())
90       //                                        continue;
91       //                                
92       //                                int[] offsets= variable.getOffsets();
93       //                                int length= variable.getLength();
94       //                                
95       //                                for (int j= 0; j != offsets.length; j++)
96       //                                        manager.addPosition(offsets[j] + start, length);
97       //                        }
98
99       LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
100       editor.setFinalCaretOffset(fTemplate.length() + start);
101       //   editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
102       editor.enter();
103
104       fSelectedRegion = editor.getSelectedRegion();
105
106     } catch (BadLocationException e) {
107       PHPeclipsePlugin.log(e);
108       openErrorDialog(e);
109
110     }
111     //      catch (CoreException e) {
112     //                  handleException(e);
113     //      }       
114   }
115
116   /*
117    * @see ICompletionProposal#getAdditionalProposalInfo()
118    */
119   public String getAdditionalProposalInfo() {
120     StringBuffer hoverInfoBuffer = new StringBuffer();
121     String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
122     String filename = workspaceLocation + fLocation.getFilename();
123     PHPDocUtil.appendPHPDoc(hoverInfoBuffer, filename, fLocation);
124     return textToHTML(hoverInfoBuffer.toString());
125   }
126
127   /*
128    * @see ICompletionProposal#getContextInformation()
129    */
130   public IContextInformation getContextInformation() {
131     return null;
132   }
133
134   /*
135    * @see ICompletionProposal#getDisplayString()
136    */
137   public String getDisplayString() {
138     return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$ //$NON-NLS-1$
139     //          return fTemplate.getName() + ObfuscatorMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); // $NON-NLS-1$ //$NON-NLS-1$
140   }
141
142   /*
143    * @see ICompletionProposal#getImage()
144    */
145   public Image getImage() {
146     switch (fLocation.getType()) {
147       case PHPIdentifierLocation.FUNCTION :
148         return PHPUiImages.get(PHPUiImages.IMG_FUN);
149                         case PHPIdentifierLocation.CLASS :
150                                 return PHPUiImages.get(PHPUiImages.IMG_CLASS);
151                         case PHPIdentifierLocation.METHOD :
152                                 return PHPUiImages.get(PHPUiImages.IMG_FUN);
153                         case PHPIdentifierLocation.DEFINE :
154                                 return PHPUiImages.get(PHPUiImages.IMG_DEFINE);
155                         case PHPIdentifierLocation.VARIABLE :
156                                 return PHPUiImages.get(PHPUiImages.IMG_VAR);
157     }
158     return PHPUiImages.get(PHPUiImages.IMG_FUN);
159   }
160
161   /*
162    * @see IJavaCompletionProposal#getRelevance()
163    */
164   public int getRelevance() {
165
166     if (fContext instanceof PHPUnitContext) {
167       PHPUnitContext context = (PHPUnitContext) fContext;
168       switch (context.getCharacterBeforeStart()) {
169         // high relevance after whitespace
170         case ' ' :
171         case '\r' :
172         case '\n' :
173         case '\t' :
174           return 90;
175
176         default :
177           return 0;
178       }
179     } else {
180       return 90;
181     }
182   }
183
184
185 }