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