1 11 12 package org.eclipse.ant.internal.ui.editor; 13 14 import org.eclipse.jface.text.BadLocationException; 15 import org.eclipse.jface.text.DocumentEvent; 16 import org.eclipse.jface.text.IDocument; 17 import org.eclipse.jface.text.ITextViewer; 18 import org.eclipse.jface.text.contentassist.ICompletionProposal; 19 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2; 20 import org.eclipse.jface.text.contentassist.IContextInformation; 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.swt.graphics.Point; 23 24 public class AntCompletionProposal implements ICompletionProposal, ICompletionProposalExtension2 { 25 26 public static final int TAG_CLOSING_PROPOSAL= 0; 27 public static final int TASK_PROPOSAL= 1; 28 public static final int PROPERTY_PROPOSAL= 2; 29 30 31 private String fDisplayString; 32 33 private String fReplacementString; 34 35 private int fReplacementOffset; 36 37 private int fReplacementLength; 38 39 private int fCursorPosition; 40 41 private Image fImage; 42 43 private String fAdditionalProposalInfo; 44 45 private int fType; 46 47 59 public AntCompletionProposal(String replacementString, int replacementOffset, int replacementLength, int cursorPosition, Image image, String displayString, String additionalProposalInfo, int type) { 60 fReplacementString= replacementString; 61 fReplacementOffset= replacementOffset; 62 fReplacementLength= replacementLength; 63 fCursorPosition= cursorPosition; 64 fImage= image; 65 fDisplayString= displayString; 66 fAdditionalProposalInfo= additionalProposalInfo; 67 fType= type; 68 } 69 70 71 74 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { 75 apply(viewer.getDocument()); 76 77 } 78 79 82 public void selected(ITextViewer viewer, boolean smartToggle) { 83 } 84 85 88 public void unselected(ITextViewer viewer) { 89 } 90 91 94 public boolean validate(IDocument document, int offset, DocumentEvent event) { 95 String enteredText= ""; try { 97 enteredText = document.get(fReplacementOffset, offset-fReplacementOffset); 98 } catch (BadLocationException e) { 99 } 100 int enteredLength= enteredText.length(); 101 if (fType == TASK_PROPOSAL && enteredText.startsWith("<")) { enteredText= enteredText.substring(1); 103 } else if (fType == PROPERTY_PROPOSAL) { 104 if(enteredText.startsWith("${")) { enteredText= enteredText.substring(2); 106 } 107 if(enteredText.startsWith("$")) { enteredText= enteredText.substring(1); 109 } 110 } else if (fType == TAG_CLOSING_PROPOSAL) { 111 if (enteredText.startsWith("</")) { enteredText= enteredText.substring(2); 113 } else if (enteredText.startsWith("/")) { try { 115 if (document.getChar(fReplacementOffset - 1) == '<') { 116 enteredText= enteredText.substring(1); 117 } 118 } catch (BadLocationException e) { 119 } 120 } else if (enteredText.startsWith("<")) { enteredText= enteredText.substring(1); 122 } 123 } 124 boolean valid= fDisplayString.toLowerCase().startsWith(enteredText.toLowerCase()); 125 if (valid) { 126 fReplacementLength= enteredLength; 127 } 128 return valid; 129 } 130 131 134 public void apply(IDocument document) { 135 try { 136 document.replace(fReplacementOffset, fReplacementLength, fReplacementString); 137 } catch (BadLocationException x) { 138 } 140 } 141 142 145 public Point getSelection(IDocument document) { 146 return new Point(fReplacementOffset + fCursorPosition, 0); 147 } 148 149 152 public String getAdditionalProposalInfo() { 153 return fAdditionalProposalInfo; 154 } 155 156 159 public String getDisplayString() { 160 if (fDisplayString != null){ 161 return fDisplayString; 162 } 163 return fReplacementString; 164 } 165 166 169 public Image getImage() { 170 return fImage; 171 } 172 173 176 public IContextInformation getContextInformation() { 177 return null; 178 } 179 180 183 public String toString() { 184 return getDisplayString(); 185 } 186 187 190 public int getType() { 191 return fType; 192 } 193 } 194 | Popular Tags |