54ec6b3021e42af75ba5e596c7ff67a66abce19e
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / template / preferences / TemplateVariableProposal.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.template.preferences;
12
13 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
14
15 import net.sourceforge.phpeclipse.ui.WebUI;
16
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.contentassist.ICompletionProposal;
22 import org.eclipse.jface.text.contentassist.IContextInformation;
23 import org.eclipse.jface.text.templates.TemplateVariableResolver;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.widgets.Shell;
27
28 /**
29  * A proposal for insertion of template variables.
30  */
31 public class TemplateVariableProposal implements ICompletionProposal {
32
33         private TemplateVariableResolver fVariable;
34
35         private int fOffset;
36
37         private int fLength;
38
39         private ITextViewer fViewer;
40
41         private Point fSelection;
42
43         /**
44          * Creates a template variable proposal.
45          * 
46          * @param variable
47          *            the template variable
48          * @param offset
49          *            the offset to replace
50          * @param length
51          *            the length to replace
52          * @param viewer
53          *            the viewer
54          */
55         public TemplateVariableProposal(TemplateVariableResolver variable,
56                         int offset, int length, ITextViewer viewer) {
57                 fVariable = variable;
58                 fOffset = offset;
59                 fLength = length;
60                 fViewer = viewer;
61         }
62
63         /*
64          * @see ICompletionProposal#apply(IDocument)
65          */
66         public void apply(IDocument document) {
67
68                 try {
69                         String variable = fVariable.getType().equals("dollar") ? "$$" : "${" + fVariable.getType() + '}'; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
70                         document.replace(fOffset, fLength, variable);
71                         fSelection = new Point(fOffset + variable.length(), 0);
72
73                 } catch (BadLocationException e) {
74                         WebUI.log(e);
75
76                         Shell shell = fViewer.getTextWidget().getShell();
77                         MessageDialog
78                                         .openError(
79                                                         shell,
80                                                         TemplatePreferencesMessages
81                                                                         .getString("TemplateVariableProposal.error.title"), e.getMessage()); //$NON-NLS-1$
82                 }
83         }
84
85         /*
86          * @see ICompletionProposal#getSelection(IDocument)
87          */
88         public Point getSelection(IDocument document) {
89                 return fSelection;
90         }
91
92         /*
93          * @see ICompletionProposal#getAdditionalProposalInfo()
94          */
95         public String getAdditionalProposalInfo() {
96                 return null;
97         }
98
99         /*
100          * @see ICompletionProposal#getDisplayString()
101          */
102         public String getDisplayString() {
103                 return fVariable.getType() + " - " + fVariable.getDescription(); //$NON-NLS-1$
104         }
105
106         /*
107          * @see ICompletionProposal#getImage()
108          */
109         public Image getImage() {
110                 return null;
111         }
112
113         /*
114          * @see ICompletionProposal#getContextInformation()
115          */
116         public IContextInformation getContextInformation() {
117                 return null;
118         }
119 }