1 11 package org.eclipse.jface.text.projection; 12 13 14 import java.util.ArrayList ; 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.jface.text.DocumentEvent; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.IDocumentInformationMapping; 23 import org.eclipse.jface.text.IDocumentListener; 24 import org.eclipse.jface.text.ISlaveDocumentManager; 25 import org.eclipse.jface.text.ISlaveDocumentManagerExtension; 26 27 28 47 public class ProjectionDocumentManager implements IDocumentListener, ISlaveDocumentManager, ISlaveDocumentManagerExtension { 48 49 50 private Map fProjectionRegistry= new HashMap (); 51 52 58 private void add(IDocument master, ProjectionDocument projection) { 59 List list= (List ) fProjectionRegistry.get(master); 60 if (list == null) { 61 list= new ArrayList (1); 62 fProjectionRegistry.put(master, list); 63 } 64 list.add(projection); 65 } 66 67 73 private void remove(IDocument master, ProjectionDocument projection) { 74 List list= (List ) fProjectionRegistry.get(master); 75 if (list != null) { 76 list.remove(projection); 77 if (list.size() == 0) 78 fProjectionRegistry.remove(master); 79 } 80 } 81 82 88 private boolean hasProjection(IDocument master) { 89 return (fProjectionRegistry.get(master) instanceof List ); 90 } 91 92 99 private Iterator getProjectionsIterator(IDocument master) { 100 List list= (List ) fProjectionRegistry.get(master); 101 if (list != null) 102 return list.iterator(); 103 return null; 104 } 105 106 112 protected void fireDocumentEvent(boolean about, DocumentEvent masterEvent) { 113 IDocument master= masterEvent.getDocument(); 114 Iterator e= getProjectionsIterator(master); 115 if (e == null) 116 return; 117 118 while (e.hasNext()) { 119 ProjectionDocument document= (ProjectionDocument) e.next(); 120 if (about) 121 document.masterDocumentAboutToBeChanged(masterEvent); 122 else 123 document.masterDocumentChanged(masterEvent); 124 } 125 } 126 127 130 public void documentChanged(DocumentEvent event) { 131 fireDocumentEvent(false, event); 132 } 133 134 137 public void documentAboutToBeChanged(DocumentEvent event) { 138 fireDocumentEvent(true, event); 139 } 140 141 144 public IDocumentInformationMapping createMasterSlaveMapping(IDocument slave) { 145 if (slave instanceof ProjectionDocument) { 146 ProjectionDocument projectionDocument= (ProjectionDocument) slave; 147 return projectionDocument.getProjectionMapping(); 148 } 149 return null; 150 } 151 152 155 public IDocument createSlaveDocument(IDocument master) { 156 if (!hasProjection(master)) 157 master.addDocumentListener(this); 158 ProjectionDocument slave= createProjectionDocument(master); 159 add(master, slave); 160 return slave; 161 } 162 163 169 protected ProjectionDocument createProjectionDocument(IDocument master) { 170 return new ProjectionDocument(master); 171 } 172 173 176 public void freeSlaveDocument(IDocument slave) { 177 if (slave instanceof ProjectionDocument) { 178 ProjectionDocument projectionDocument= (ProjectionDocument) slave; 179 IDocument master= projectionDocument.getMasterDocument(); 180 remove(master, projectionDocument); 181 projectionDocument.dispose(); 182 if (!hasProjection(master)) 183 master.removeDocumentListener(this); 184 } 185 } 186 187 190 public IDocument getMasterDocument(IDocument slave) { 191 if (slave instanceof ProjectionDocument) 192 return ((ProjectionDocument) slave).getMasterDocument(); 193 return null; 194 } 195 196 199 public boolean isSlaveDocument(IDocument document) { 200 return (document instanceof ProjectionDocument); 201 } 202 203 206 public void setAutoExpandMode(IDocument slave, boolean autoExpanding) { 207 if (slave instanceof ProjectionDocument) 208 ((ProjectionDocument) slave).setAutoExpandMode(autoExpanding); 209 } 210 211 214 public IDocument[] getSlaveDocuments(IDocument master) { 215 List list= (List ) fProjectionRegistry.get(master); 216 if (list != null) { 217 IDocument[] result= new IDocument[list.size()]; 218 list.toArray(result); 219 return result; 220 } 221 return null; 222 } 223 } 224 | Popular Tags |