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