Syntax highlighting is changeable.
[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         private int fOffset;
33         private int fLength;    
34         private ITextViewer fViewer;
35         
36         private Point fSelection;
37
38         /**
39          * Creates a template variable proposal.
40          * 
41          * @param variable the template variable
42          * @param offset the offset to replace
43          * @param length the length to replace
44          * @param viewer the viewer
45          */
46         public TemplateVariableProposal(TemplateVariableResolver variable, int offset, int length, ITextViewer viewer) {
47                 fVariable= variable;
48                 fOffset= offset;
49                 fLength= length;
50                 fViewer= viewer;
51         }
52         
53         /*
54          * @see ICompletionProposal#apply(IDocument)
55          */
56         public void apply(IDocument document) {
57
58                 try {
59                         String variable= fVariable.getType().equals("dollar") ? "$$" : "${" + fVariable.getType() + '}'; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
60                         document.replace(fOffset, fLength, variable);
61                         fSelection= new Point(fOffset + variable.length(), 0);
62
63                 } catch (BadLocationException e) {
64                         PHPeclipsePlugin.log(e);
65
66                         Shell shell= fViewer.getTextWidget().getShell();
67                         MessageDialog.openError(shell, TemplatePreferencesMessages.getString("TemplateVariableProposal.error.title"), e.getMessage()); //$NON-NLS-1$
68                 }
69         }
70
71         /*
72          * @see ICompletionProposal#getSelection(IDocument)
73          */
74         public Point getSelection(IDocument document) {
75                 return fSelection;
76         }
77
78         /*
79          * @see ICompletionProposal#getAdditionalProposalInfo()
80          */
81         public String getAdditionalProposalInfo() {
82                 return null;
83         }
84
85         /*
86          * @see ICompletionProposal#getDisplayString()
87          */
88         public String getDisplayString() {
89                 return fVariable.getType() + " - " + fVariable.getDescription(); //$NON-NLS-1$
90         }
91
92         /*
93          * @see ICompletionProposal#getImage()
94          */
95         public Image getImage() {
96                 return null;
97         }
98
99         /*
100          * @see ICompletionProposal#getContextInformation()
101          */
102         public IContextInformation getContextInformation() {
103                 return null;
104         }
105 }