KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > template > preferences > TemplateVariableProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.template.preferences;
12
13 import org.eclipse.swt.graphics.Image;
14 import org.eclipse.swt.graphics.Point;
15 import org.eclipse.swt.widgets.Shell;
16
17 import org.eclipse.jface.dialogs.MessageDialog;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.contentassist.ICompletionProposal;
23 import org.eclipse.jface.text.contentassist.IContextInformation;
24 import org.eclipse.jface.text.templates.TemplateVariableResolver;
25
26 import org.eclipse.jdt.internal.ui.JavaPlugin;
27
28 /**
29  * A proposal for insertion of template variables.
30  */

31 public class TemplateVariableProposal implements ICompletionProposal {
32
33     private TemplateVariableResolver fResolver;
34     private int fOffset;
35     private int fLength;
36     private ITextViewer fViewer;
37
38     private Point fSelection;
39     private final boolean fIncludeBrace;
40
41     /**
42      * Creates a template variable proposal.
43      *
44      * @param variable the template variable
45      * @param offset the offset to replace
46      * @param length the length to replace
47      * @param viewer the viewer
48      * @param includeBrace whether to also replace the ${
49      */

50     public TemplateVariableProposal(TemplateVariableResolver variable, int offset, int length, ITextViewer viewer, boolean includeBrace) {
51         fResolver= variable;
52         fOffset= offset;
53         fLength= length;
54         fViewer= viewer;
55         fIncludeBrace= includeBrace;
56     }
57
58     /*
59      * @see ICompletionProposal#apply(IDocument)
60      */

61     public void apply(IDocument document) {
62
63         try {
64             String JavaDoc variable;
65             String JavaDoc type= fResolver.getType();
66             if (type.equals("dollar")) //$NON-NLS-1$
67
variable= "$$"; //$NON-NLS-1$
68
else if (fIncludeBrace)
69                 variable= "${" + type + '}'; //$NON-NLS-1$
70
else
71                 variable= type;
72             document.replace(fOffset, fLength, variable);
73             fSelection= new Point(fOffset + variable.length(), 0);
74
75         } catch (BadLocationException e) {
76             JavaPlugin.log(e);
77
78             Shell shell= fViewer.getTextWidget().getShell();
79             MessageDialog.openError(shell, TemplatePreferencesMessages.TemplateVariableProposal_error_title, e.getMessage());
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 JavaDoc getAdditionalProposalInfo() {
94         return fResolver.getDescription();
95     }
96
97     /*
98      * @see ICompletionProposal#getDisplayString()
99      */

100     public String JavaDoc getDisplayString() {
101         return fResolver.getType();
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 }
118
Popular Tags