1 11 12 package org.eclipse.jdt.internal.ui.text.template.contentassist; 13 14 15 import org.eclipse.core.runtime.Assert; 16 17 import org.eclipse.swt.graphics.Image; 18 import org.eclipse.swt.graphics.Point; 19 20 import org.eclipse.jface.text.BadLocationException; 21 import org.eclipse.jface.text.DocumentEvent; 22 import org.eclipse.jface.text.IDocument; 23 import org.eclipse.jface.text.ITextViewer; 24 import org.eclipse.jface.text.Position; 25 import org.eclipse.jface.text.contentassist.ICompletionProposal; 26 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension; 27 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2; 28 import org.eclipse.jface.text.contentassist.IContextInformation; 29 30 31 35 public class PositionBasedCompletionProposal implements ICompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2 { 36 37 38 private String fDisplayString; 39 40 private String fReplacementString; 41 42 private Position fReplacementPosition; 43 44 private int fCursorPosition; 45 46 private Image fImage; 47 48 private IContextInformation fContextInformation; 49 50 private String fAdditionalProposalInfo; 51 52 60 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition) { 61 this(replacementString, replacementPosition, cursorPosition, null, null, null, null); 62 } 63 64 75 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo) { 76 Assert.isNotNull(replacementString); 77 Assert.isTrue(replacementPosition != null); 78 79 fReplacementString= replacementString; 80 fReplacementPosition= replacementPosition; 81 fCursorPosition= cursorPosition; 82 fImage= image; 83 fDisplayString= displayString; 84 fContextInformation= contextInformation; 85 fAdditionalProposalInfo= additionalProposalInfo; 86 } 87 88 91 public void apply(IDocument document) { 92 try { 93 document.replace(fReplacementPosition.getOffset(), fReplacementPosition.getLength(), fReplacementString); 94 } catch (BadLocationException x) { 95 } 97 } 98 99 102 public Point getSelection(IDocument document) { 103 return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0); 104 } 105 106 109 public IContextInformation getContextInformation() { 110 return fContextInformation; 111 } 112 113 116 public Image getImage() { 117 return fImage; 118 } 119 120 123 public String getDisplayString() { 124 if (fDisplayString != null) 125 return fDisplayString; 126 return fReplacementString; 127 } 128 129 132 public String getAdditionalProposalInfo() { 133 return fAdditionalProposalInfo; 134 } 135 136 139 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { 140 apply(viewer.getDocument()); 141 } 142 143 146 public void selected(ITextViewer viewer, boolean smartToggle) { 147 } 148 149 152 public void unselected(ITextViewer viewer) { 153 } 154 155 158 public boolean validate(IDocument document, int offset, DocumentEvent event) { 159 try { 160 String content= document.get(fReplacementPosition.getOffset(), offset - fReplacementPosition.getOffset()); 161 if (fReplacementString.startsWith(content)) 162 return true; 163 } catch (BadLocationException e) { 164 } 166 return false; 167 } 168 169 172 public void apply(IDocument document, char trigger, int offset) { 173 } 175 176 179 public boolean isValidFor(IDocument document, int offset) { 180 return false; 182 } 183 184 187 public char[] getTriggerCharacters() { 188 return null; 189 } 190 191 194 public int getContextInformationPosition() { 195 return fReplacementPosition.getOffset(); 196 } 197 198 } 199 200 | Popular Tags |