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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.template.preferences;
13 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
27 * A proposal for insertion of template variables.
29 public class TemplateVariableProposal implements ICompletionProposal {
31 private TemplateVariableResolver fVariable;
34 private ITextViewer fViewer;
36 private Point fSelection;
39 * Creates a template variable proposal.
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
46 public TemplateVariableProposal(TemplateVariableResolver variable, int offset, int length, ITextViewer viewer) {
54 * @see ICompletionProposal#apply(IDocument)
56 public void apply(IDocument document) {
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);
63 } catch (BadLocationException e) {
64 PHPeclipsePlugin.log(e);
66 Shell shell= fViewer.getTextWidget().getShell();
67 MessageDialog.openError(shell, TemplatePreferencesMessages.getString("TemplateVariableProposal.error.title"), e.getMessage()); //$NON-NLS-1$
72 * @see ICompletionProposal#getSelection(IDocument)
74 public Point getSelection(IDocument document) {
79 * @see ICompletionProposal#getAdditionalProposalInfo()
81 public String getAdditionalProposalInfo() {
86 * @see ICompletionProposal#getDisplayString()
88 public String getDisplayString() {
89 return fVariable.getType() + " - " + fVariable.getDescription(); //$NON-NLS-1$
93 * @see ICompletionProposal#getImage()
95 public Image getImage() {
100 * @see ICompletionProposal#getContextInformation()
102 public IContextInformation getContextInformation() {