1 11 package org.eclipse.jdt.internal.ui.text.template.contentassist; 12 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 import org.eclipse.core.runtime.Assert; 20 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.swt.graphics.Point; 23 24 import org.eclipse.jface.text.BadLocationException; 25 import org.eclipse.jface.text.DocumentEvent; 26 import org.eclipse.jface.text.IDocument; 27 import org.eclipse.jface.text.ITextViewer; 28 import org.eclipse.jface.text.contentassist.ICompletionProposal; 29 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2; 30 import org.eclipse.jface.text.contentassist.IContextInformation; 31 32 33 39 public class MultiVariableGuess { 40 41 44 private static class Proposal implements ICompletionProposal, ICompletionProposalExtension2 { 45 46 47 private String fDisplayString; 48 49 String fReplacementString; 50 51 private int fReplacementOffset; 52 53 private int fReplacementLength; 54 55 private int fCursorPosition; 56 57 private Image fImage; 58 59 private IContextInformation fContextInformation; 60 61 private String fAdditionalProposalInfo; 62 63 72 public Proposal(String replacementString, int replacementOffset, int replacementLength, int cursorPosition) { 73 this(replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null); 74 } 75 76 88 public Proposal(String replacementString, int replacementOffset, int replacementLength, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo) { 89 Assert.isNotNull(replacementString); 90 Assert.isTrue(replacementOffset >= 0); 91 Assert.isTrue(replacementLength >= 0); 92 Assert.isTrue(cursorPosition >= 0); 93 94 fReplacementString= replacementString; 95 fReplacementOffset= replacementOffset; 96 fReplacementLength= replacementLength; 97 fCursorPosition= cursorPosition; 98 fImage= image; 99 fDisplayString= displayString; 100 fContextInformation= contextInformation; 101 fAdditionalProposalInfo= additionalProposalInfo; 102 } 103 104 107 public void apply(IDocument document) { 108 try { 109 document.replace(fReplacementOffset, fReplacementLength, fReplacementString); 110 } catch (BadLocationException x) { 111 } 113 } 114 115 118 public Point getSelection(IDocument document) { 119 return new Point(fReplacementOffset + fCursorPosition, 0); 120 } 121 122 125 public IContextInformation getContextInformation() { 126 return fContextInformation; 127 } 128 129 132 public Image getImage() { 133 return fImage; 134 } 135 136 139 public String getDisplayString() { 140 if (fDisplayString != null) 141 return fDisplayString; 142 return fReplacementString; 143 } 144 145 148 public String getAdditionalProposalInfo() { 149 return fAdditionalProposalInfo; 150 } 151 152 155 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { 156 apply(viewer.getDocument()); 157 } 158 159 162 public void selected(ITextViewer viewer, boolean smartToggle) { 163 } 164 165 168 public void unselected(ITextViewer viewer) { 169 } 170 171 174 public boolean validate(IDocument document, int offset, DocumentEvent event) { 175 try { 176 String content= document.get(fReplacementOffset, fReplacementLength); 177 if (content.startsWith(fReplacementString)) 178 return true; 179 } catch (BadLocationException e) { 180 } 182 return false; 183 } 184 } 185 186 private final Map fDependencies= new HashMap (); 187 private final Map fBackwardDeps= new HashMap (); 188 private final Map fPositions= new HashMap (); 189 190 public MultiVariableGuess() { 191 } 192 193 public ICompletionProposal[] getProposals(final MultiVariable variable, int offset, int length) { 194 MultiVariable master= (MultiVariable) fBackwardDeps.get(variable); 195 Object [] choices; 196 if (master == null) 197 choices= variable.getChoices(); 198 else 199 choices= variable.getChoices(master.getCurrentChoice()); 200 201 if (choices == null) 202 return null; 203 204 if (fDependencies.containsKey(variable)) { 205 ICompletionProposal[] ret= new ICompletionProposal[choices.length]; 206 for (int i= 0; i < ret.length; i++) { 207 final Object choice= choices[i]; 208 ret[i]= new Proposal(variable.toString(choice), offset, length, offset + length) { 209 public void apply(IDocument document) { 210 super.apply(document); 211 Object oldChoice= variable.getCurrentChoice(); 212 variable.setCurrentChoice(choice); 213 updateSlaves(variable, document, oldChoice); 214 } 215 }; 216 } 217 218 return ret; 219 220 } else { 221 if (choices.length < 2) 222 return null; 223 224 ICompletionProposal[] ret= new ICompletionProposal[choices.length]; 225 for (int i= 0; i < ret.length; i++) 226 ret[i]= new Proposal(variable.toString(choices[i]), offset, length, offset + length); 227 228 return ret; 229 } 230 } 231 232 private void updateSlaves(MultiVariable variable, IDocument document, Object oldChoice) { 233 Object choice= variable.getCurrentChoice(); 234 if (!oldChoice.equals(choice)) { 235 Set slaves= (Set ) fDependencies.get(variable); 236 for (Iterator it= slaves.iterator(); it.hasNext();) { 237 MultiVariable slave= (MultiVariable) it.next(); 238 VariablePosition pos= (VariablePosition) fPositions.get(slave); 239 240 Object slavesOldChoice= slave.getCurrentChoice(); 241 slave.setKey(choice); try { 243 document.replace(pos.getOffset(), pos.getLength(), slave.getDefaultValue()); 244 } catch (BadLocationException x) { 245 } 247 if (fDependencies.containsKey(slave)) 249 updateSlaves(slave, document, slavesOldChoice); 250 } 251 } 252 } 253 254 257 public void addSlave(VariablePosition position) { 258 fPositions.put(position.getVariable(), position); 259 } 260 261 266 public void addDependency(MultiVariable master, MultiVariable slave) { 267 if (fBackwardDeps.containsKey(slave)) 269 throw new IllegalArgumentException ("slave can only serve one master"); Object parent= master; 271 while (parent != null) { 272 parent= fBackwardDeps.get(parent); 273 if (parent == slave) 274 throw new IllegalArgumentException ("cycle detected"); } 276 277 Set slaves= (Set ) fDependencies.get(master); 278 if (slaves == null) { 279 slaves= new HashSet (); 280 fDependencies.put(master, slaves); 281 } 282 fBackwardDeps.put(slave, master); 283 slaves.add(slave); 284 } 285 } 286 | Popular Tags |