794209d1ce1edca7764d3b078678402fd71214d6
[phpeclipse.git] /
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.TemplateMessages;
8 import net.sourceforge.phpdt.internal.corext.template.TemplateVariable;
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
10 import org.eclipse.jface.dialogs.MessageDialog;
11 import org.eclipse.jface.text.BadLocationException;
12 import org.eclipse.jface.text.IDocument;
13 import org.eclipse.jface.text.ITextViewer;
14 import org.eclipse.jface.text.contentassist.ICompletionProposal;
15 import org.eclipse.jface.text.contentassist.IContextInformation;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.widgets.Shell;
19 //import org.eclipse.jdt.internal.ui.JavaPlugin;
20
21 /**
22  * A proposal for insertion of template variables.
23  */
24 public class TemplateVariableProposal implements ICompletionProposal {
25
26         private TemplateVariable fVariable;
27         private int fOffset;
28         private int fLength;    
29         private ITextViewer fViewer;
30         
31         private Point fSelection;
32
33         /**
34          * Creates a template variable proposal.
35          * 
36          * @param variable the template variable
37          * @param offset the offset to replace
38          * @param length the length to replace
39          * @param viewer the viewer
40          */
41         public TemplateVariableProposal(TemplateVariable variable, int offset, int length, ITextViewer viewer) {
42                 fVariable= variable;
43                 fOffset= offset;
44                 fLength= length;
45                 fViewer= viewer;
46         }
47         
48         /*
49          * @see ICompletionProposal#apply(IDocument)
50          */
51         public void apply(IDocument document) {
52
53                 try {
54                         String variable= fVariable.getName().equals("dollar") ? "$$" : "${" + fVariable.getName() + '}'; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
55                         document.replace(fOffset, fLength, variable);
56                         fSelection= new Point(fOffset + variable.length(), 0);
57
58                 } catch (BadLocationException e) {
59                         PHPeclipsePlugin.log(e);
60
61                         Shell shell= fViewer.getTextWidget().getShell();
62                         MessageDialog.openError(shell, TemplateMessages.getString("TemplateVariableProposal.error.title"), e.getMessage()); //$NON-NLS-1$
63                 }
64         }
65
66         /*
67          * @see ICompletionProposal#getSelection(IDocument)
68          */
69         public Point getSelection(IDocument document) {
70                 return fSelection;
71         }
72
73         /*
74          * @see ICompletionProposal#getAdditionalProposalInfo()
75          */
76         public String getAdditionalProposalInfo() {
77                 return null;
78         }
79
80         /*
81          * @see ICompletionProposal#getDisplayString()
82          */
83         public String getDisplayString() {
84                 return fVariable.getName() + " - " + fVariable.getDescription(); //$NON-NLS-1$
85         }
86
87         /*
88          * @see ICompletionProposal#getImage()
89          */
90         public Image getImage() {
91                 return null;
92         }
93
94         /*
95          * @see ICompletionProposal#getContextInformation()
96          */
97         public IContextInformation getContextInformation() {
98                 return null;
99         }
100 }