1 11 package org.eclipse.jface.text.link; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.Assert; 21 22 import org.eclipse.jface.text.BadLocationException; 23 import org.eclipse.jface.text.IDocument; 24 import org.eclipse.jface.text.Position; 25 import org.eclipse.jface.text.link.LinkedModeModel; 26 import org.eclipse.jface.text.link.LinkedPosition; 27 import org.eclipse.jface.text.link.LinkedPositionGroup; 28 import org.eclipse.jface.text.source.Annotation; 29 import org.eclipse.jface.text.source.AnnotationModel; 30 31 36 final class LinkedPositionAnnotations extends AnnotationModel { 37 38 39 private static final String TARGET_ANNOTATION_TYPE= "org.eclipse.ui.internal.workbench.texteditor.link.target"; private static final String SLAVE_ANNOTATION_TYPE= "org.eclipse.ui.internal.workbench.texteditor.link.slave"; private static final String FOCUS_ANNOTATION_TYPE= "org.eclipse.ui.internal.workbench.texteditor.link.master"; private static final String EXIT_ANNOTATION_TYPE= "org.eclipse.ui.internal.workbench.texteditor.link.exit"; 44 45 private boolean fMarkTargets= true; 46 private boolean fMarkSlaves= true; 47 private boolean fMarkFocus= true; 48 private boolean fMarkExitTarget= true; 49 50 private Annotation fFocusAnnotation= null; 51 private Annotation fExitAnnotation= null; 52 private final Map fGroupAnnotations= new HashMap (); 53 private final Map fTargetAnnotations= new HashMap (); 54 private Position[] fTargets= new Position[0]; 55 private LinkedPosition fExitPosition= null; 56 57 65 private void setFocusPosition(Position position) throws BadLocationException { 66 if (fMarkFocus && getPosition(fFocusAnnotation) != position) { 67 removeAnnotation(fFocusAnnotation, false); 68 if (position != null) { 69 fFocusAnnotation= new Annotation(FOCUS_ANNOTATION_TYPE, false, ""); addAnnotation(fFocusAnnotation, position, false); 71 } else 72 fFocusAnnotation= null; 73 } 74 } 75 76 84 private void setExitPosition(Position position) throws BadLocationException { 85 if (fMarkExitTarget && getPosition(fExitAnnotation) != position) { 86 removeAnnotation(fExitAnnotation, false); 87 if (position != null) { 88 fExitAnnotation= new Annotation(EXIT_ANNOTATION_TYPE, false, ""); addAnnotation(fExitAnnotation, position, false); 90 } else 91 fExitAnnotation= null; 92 } 93 } 94 95 102 private void setGroupPositions(List positions) throws BadLocationException { 103 if (!fMarkSlaves) 104 return; 105 106 List toRemove= new ArrayList (fGroupAnnotations.values()); 111 Map toAdd= new HashMap (); 112 if (positions != null) { 113 for (Iterator iter= positions.iterator(); iter.hasNext();) { 114 Position p= (Position) iter.next(); 115 if (fGroupAnnotations.containsKey(p)) { 116 toRemove.remove(fGroupAnnotations.get(p)); 117 } else { 118 Annotation a= new Annotation(SLAVE_ANNOTATION_TYPE, false, ""); toAdd.put(a, p); 120 fGroupAnnotations.put(p, a); 121 } 122 } 123 } 124 fGroupAnnotations.values().removeAll(toRemove); 125 126 replaceAnnotations((Annotation[]) toRemove.toArray(new Annotation[0]), toAdd, false); 127 } 128 129 136 private void setTargetPositions(List positions) throws BadLocationException { 137 if (!fMarkTargets) 138 return; 139 140 List toRemove= new ArrayList (fTargetAnnotations.values()); 145 Map toAdd= new HashMap (); 146 if (positions != null) { 147 for (Iterator iter= positions.iterator(); iter.hasNext();) { 148 Position p= (Position) iter.next(); 149 if (fTargetAnnotations.containsKey(p)) { 150 toRemove.remove(fTargetAnnotations.get(p)); 151 } else { 152 Annotation a= new Annotation(TARGET_ANNOTATION_TYPE, false, ""); toAdd.put(a, p); 154 fTargetAnnotations.put(p, a); 155 } 156 } 157 } 158 fTargetAnnotations.values().removeAll(toRemove); 159 160 replaceAnnotations((Annotation[]) toRemove.toArray(new Annotation[0]), toAdd, false); 161 } 162 163 172 public void switchToPosition(LinkedModeModel env, LinkedPosition position) { 173 if (fDocument == null || 174 (position != null && getPosition(fFocusAnnotation) == position) || 175 (position == null && fFocusAnnotation == null)) 176 return; 177 178 LinkedPositionGroup linkedGroup= null; 179 if (position != null) 180 linkedGroup= env.getGroupForPosition(position); 181 182 List targets= new ArrayList (); 183 targets.addAll(Arrays.asList(fTargets)); 184 185 List group; 186 if (linkedGroup != null) 187 group= new ArrayList (Arrays.asList(linkedGroup.getPositions())); 188 else 189 group= new ArrayList (); 190 191 if (position == null || !fDocument.equals(position.getDocument())) 192 position= null; 194 195 LinkedPosition exit= fExitPosition; 196 if (exit == null || !fDocument.equals(exit.getDocument())) 197 exit= null; 199 200 201 if (exit != null) { 202 group.remove(exit); 203 targets.remove(exit); 204 } 205 206 group.removeAll(targets); 207 targets.remove(position); 208 group.remove(position); 209 prune(targets); 210 prune(group); 211 212 try { 213 setFocusPosition(position); 214 setExitPosition(exit); 215 setGroupPositions(group); 216 setTargetPositions(targets); 217 } catch (BadLocationException e) { 218 Assert.isTrue(false); 221 } 222 fireModelChanged(); 223 224 } 225 226 232 private void prune(List list) { 233 for (Iterator iter= list.iterator(); iter.hasNext();) { 234 LinkedPosition pos= (LinkedPosition) iter.next(); 235 if (!pos.getDocument().equals(fDocument)) 236 iter.remove(); 237 } 238 } 239 240 244 public void setTargets(Position[] positions) { 245 fTargets= positions; 246 } 247 248 253 public void setExitTarget(LinkedPosition position) { 254 fExitPosition = position; 255 } 256 257 260 protected void addPosition(IDocument document, Position position) { 261 } 264 265 268 protected void removePosition(IDocument document, Position pos) { 269 } 272 273 276 public void fireModelChanged() { 277 super.fireModelChanged(); 278 } 279 280 285 public void markExitTarget(boolean markExitTargets) { 286 fMarkExitTarget= markExitTargets; 287 } 288 289 294 public void markFocus(boolean markFocus) { 295 fMarkFocus= markFocus; 296 } 297 298 303 public void markSlaves(boolean markSlaves) { 304 fMarkSlaves= markSlaves; 305 } 306 307 312 public void markTargets(boolean markTargets) { 313 fMarkTargets= markTargets; 314 } 315 } 316 | Popular Tags |