001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor.templates;
011:
012: import org.eclipse.swt.graphics.Image;
013: import org.eclipse.swt.graphics.Point;
014: import org.eclipse.swt.widgets.Shell;
015:
016: import org.eclipse.jface.dialogs.MessageDialog;
017:
018: import org.eclipse.jface.text.BadLocationException;
019: import org.eclipse.jface.text.IDocument;
020: import org.eclipse.jface.text.ITextViewer;
021: import org.eclipse.jface.text.contentassist.ICompletionProposal;
022: import org.eclipse.jface.text.contentassist.IContextInformation;
023: import org.eclipse.jface.text.templates.TemplateVariableResolver;
024:
025: /**
026: * A proposal for insertion of template variables.
027: * <p>
028: * This class should not be used by clients and may become package visible in
029: * the future.
030: * </p>
031: *
032: * @since 3.0
033: */
034: final class TemplateVariableProposal implements ICompletionProposal {
035:
036: private TemplateVariableResolver fVariable;
037: private int fOffset;
038: private int fLength;
039: private ITextViewer fViewer;
040:
041: private Point fSelection;
042:
043: /**
044: * Creates a template variable proposal.
045: *
046: * @param variable the template variable
047: * @param offset the offset to replace
048: * @param length the length to replace
049: * @param viewer the viewer
050: */
051: public TemplateVariableProposal(TemplateVariableResolver variable,
052: int offset, int length, ITextViewer viewer) {
053: fVariable = variable;
054: fOffset = offset;
055: fLength = length;
056: fViewer = viewer;
057: }
058:
059: /*
060: * @see ICompletionProposal#apply(IDocument)
061: */
062: public void apply(IDocument document) {
063:
064: try {
065: String variable = fVariable.getType().equals("dollar") ? "$$" : "${" + fVariable.getType() + '}'; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
066: document.replace(fOffset, fLength, variable);
067: fSelection = new Point(fOffset + variable.length(), 0);
068:
069: } catch (BadLocationException e) {
070: Shell shell = fViewer.getTextWidget().getShell();
071: MessageDialog
072: .openError(
073: shell,
074: TextEditorTemplateMessages.TemplateVariableProposal_error_title,
075: e.getMessage());
076: }
077: }
078:
079: /*
080: * @see ICompletionProposal#getSelection(IDocument)
081: */
082: public Point getSelection(IDocument document) {
083: return fSelection;
084: }
085:
086: /*
087: * @see ICompletionProposal#getAdditionalProposalInfo()
088: */
089: public String getAdditionalProposalInfo() {
090: return null;
091: }
092:
093: /*
094: * @see ICompletionProposal#getDisplayString()
095: */
096: public String getDisplayString() {
097: return fVariable.getType() + " - " + fVariable.getDescription(); //$NON-NLS-1$
098: }
099:
100: /*
101: * @see ICompletionProposal#getImage()
102: */
103: public Image getImage() {
104: return null;
105: }
106:
107: /*
108: * @see ICompletionProposal#getContextInformation()
109: */
110: public IContextInformation getContextInformation() {
111: return null;
112: }
113: }
|