KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > templates > TemplateVariableProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.texteditor.templates;
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 /**
27  * A proposal for insertion of template variables.
28  * <p>
29  * This class should not be used by clients and may become package visible in
30  * the future.
31  * </p>
32  *
33  * @since 3.0
34  */

35 final class TemplateVariableProposal implements ICompletionProposal {
36
37     private TemplateVariableResolver fVariable;
38     private int fOffset;
39     private int fLength;
40     private ITextViewer fViewer;
41
42     private Point fSelection;
43
44     /**
45      * Creates a template variable proposal.
46      *
47      * @param variable the template variable
48      * @param offset the offset to replace
49      * @param length the length to replace
50      * @param viewer the viewer
51      */

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

62     public void apply(IDocument document) {
63
64         try {
65             String JavaDoc variable= fVariable.getType().equals("dollar") ? "$$" : "${" + fVariable.getType() + '}'; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
66
document.replace(fOffset, fLength, variable);
67             fSelection= new Point(fOffset + variable.length(), 0);
68
69         } catch (BadLocationException e) {
70             Shell shell= fViewer.getTextWidget().getShell();
71             MessageDialog.openError(shell, TextEditorTemplateMessages.TemplateVariableProposal_error_title, e.getMessage());
72         }
73     }
74
75     /*
76      * @see ICompletionProposal#getSelection(IDocument)
77      */

78     public Point getSelection(IDocument document) {
79         return fSelection;
80     }
81
82     /*
83      * @see ICompletionProposal#getAdditionalProposalInfo()
84      */

85     public String JavaDoc getAdditionalProposalInfo() {
86         return null;
87     }
88
89     /*
90      * @see ICompletionProposal#getDisplayString()
91      */

92     public String JavaDoc getDisplayString() {
93         return fVariable.getType() + " - " + fVariable.getDescription(); //$NON-NLS-1$
94
}
95
96     /*
97      * @see ICompletionProposal#getImage()
98      */

99     public Image getImage() {
100         return null;
101     }
102
103     /*
104      * @see ICompletionProposal#getContextInformation()
105      */

106     public IContextInformation getContextInformation() {
107         return null;
108     }
109 }
110
Popular Tags