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;
37 private ITextViewer fViewer;
39 private Point fSelection;
42 * Creates a template variable proposal.
45 * the template variable
47 * the offset to replace
49 * the length to replace
53 public TemplateVariableProposal(TemplateVariableResolver variable,
54 int offset, int length, ITextViewer viewer) {
62 * @see ICompletionProposal#apply(IDocument)
64 public void apply(IDocument document) {
67 String variable = fVariable.getType().equals("dollar") ? "$$" : "${" + fVariable.getType() + '}'; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
68 document.replace(fOffset, fLength, variable);
69 fSelection = new Point(fOffset + variable.length(), 0);
71 } catch (BadLocationException e) {
72 PHPeclipsePlugin.log(e);
74 Shell shell = fViewer.getTextWidget().getShell();
78 TemplatePreferencesMessages
79 .getString("TemplateVariableProposal.error.title"), e.getMessage()); //$NON-NLS-1$
84 * @see ICompletionProposal#getSelection(IDocument)
86 public Point getSelection(IDocument document) {
91 * @see ICompletionProposal#getAdditionalProposalInfo()
93 public String getAdditionalProposalInfo() {
98 * @see ICompletionProposal#getDisplayString()
100 public String getDisplayString() {
101 return fVariable.getType() + " - " + fVariable.getDescription(); //$NON-NLS-1$
105 * @see ICompletionProposal#getImage()
107 public Image getImage() {
112 * @see ICompletionProposal#getContextInformation()
114 public IContextInformation getContextInformation() {