1 11 12 package org.eclipse.jface.text.templates; 13 14 15 import org.eclipse.swt.graphics.Image; 16 import org.eclipse.swt.graphics.Point; 17 18 import org.eclipse.core.runtime.Assert; 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.ICompletionProposalExtension2; 27 import org.eclipse.jface.text.contentassist.IContextInformation; 28 29 30 35 final class PositionBasedCompletionProposal implements ICompletionProposal, 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 } 170 | Popular Tags |