1 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 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 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 61 public void apply(IDocument document) { 62 63 try { 64 String variable; 65 String type= fResolver.getType(); 66 if (type.equals("dollar")) variable= "$$"; else if (fIncludeBrace) 69 variable= "${" + type + '}'; 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 86 public Point getSelection(IDocument document) { 87 return fSelection; 88 } 89 90 93 public String getAdditionalProposalInfo() { 94 return fResolver.getDescription(); 95 } 96 97 100 public String getDisplayString() { 101 return fResolver.getType(); 102 } 103 104 107 public Image getImage() { 108 return null; 109 } 110 111 114 public IContextInformation getContextInformation() { 115 return null; 116 } 117 } 118 | Popular Tags |