1 20 21 package org.apache.directory.ldapstudio.ldifeditor.editor.reconciler; 22 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile; 31 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart; 32 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifCommentContainer; 33 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer; 34 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifNonEmptyLineBase; 35 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifSepLine; 36 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorConstants; 37 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorActivator; 38 import org.apache.directory.ldapstudio.ldifeditor.editor.ILdifEditor; 39 40 import org.eclipse.jface.text.BadLocationException; 41 import org.eclipse.jface.text.IDocument; 42 import org.eclipse.jface.text.Position; 43 import org.eclipse.jface.text.source.Annotation; 44 import org.eclipse.jface.text.source.ISourceViewer; 45 import org.eclipse.jface.text.source.projection.ProjectionAnnotation; 46 import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel; 47 import org.eclipse.jface.util.IPropertyChangeListener; 48 import org.eclipse.jface.util.PropertyChangeEvent; 49 50 51 public class LdifFoldingRegionUpdater implements IPropertyChangeListener 52 { 53 54 private ILdifEditor editor; 55 56 57 public LdifFoldingRegionUpdater( ILdifEditor editor ) 58 { 59 this.editor = editor; 60 61 LdifEditorActivator.getDefault().getPreferenceStore().addPropertyChangeListener( this ); 62 } 63 64 65 public void dispose() 66 { 67 LdifEditorActivator.getDefault().getPreferenceStore().removePropertyChangeListener( this ); 68 } 69 70 71 public void propertyChange( PropertyChangeEvent event ) 72 { 73 if ( LdifEditorConstants.PREFERENCE_LDIFEDITOR_FOLDING_ENABLE.equals( event.getProperty() ) 74 || LdifEditorConstants.PREFERENCE_LDIFEDITOR_FOLDING_INITIALLYFOLDCOMMENTS.equals( event.getProperty() ) 75 || LdifEditorConstants.PREFERENCE_LDIFEDITOR_FOLDING_INITIALLYFOLDRECORDS.equals( event.getProperty() ) 76 || LdifEditorConstants.PREFERENCE_LDIFEDITOR_FOLDING_INITIALLYFOLDWRAPPEDLINES.equals( event.getProperty() ) ) 77 { 78 this.updateFoldingRegions(); 79 } 80 } 81 82 83 public void updateFoldingRegions() 84 { 85 86 ISourceViewer viewer = ( ISourceViewer ) editor.getAdapter( ISourceViewer.class ); 87 if ( viewer == null ) 88 return; 89 90 IDocument document = viewer.getDocument(); 91 92 try 93 { 94 ProjectionAnnotationModel projectionAnnotationModel = ( ProjectionAnnotationModel ) editor 95 .getAdapter( ProjectionAnnotationModel.class ); 96 if ( projectionAnnotationModel == null ) 97 return; 98 99 Map positionToAnnotationMap = createFoldingRegions( editor.getLdifModel(), document ); 103 104 List annotationsToDeleteList = new ArrayList (); 106 Map annotationsToAddMap = new HashMap (); 107 this.computeDifferences( projectionAnnotationModel, positionToAnnotationMap, annotationsToDeleteList, 108 annotationsToAddMap ); 109 Annotation[] annotationsToDelete = ( Annotation[] ) annotationsToDeleteList 110 .toArray( new Annotation[annotationsToDeleteList.size()] ); 111 112 if ( !annotationsToDeleteList.isEmpty() || !annotationsToAddMap.isEmpty() ) 114 { 115 projectionAnnotationModel.modifyAnnotations( annotationsToDelete, annotationsToAddMap, 116 new Annotation[0] ); 117 } 118 119 } 120 catch ( BadLocationException e ) 121 { 122 e.printStackTrace(); 123 } 124 } 125 126 127 private void computeDifferences( ProjectionAnnotationModel model, Map positionToAnnotationMap, 128 List annotationsToDeleteList, Map annotationsToAddMap ) 129 { 130 131 for ( Iterator iter = model.getAnnotationIterator(); iter.hasNext(); ) 132 { 133 Object annotation = iter.next(); 134 if ( annotation instanceof ProjectionAnnotation ) 135 { 136 Position position = model.getPosition( ( Annotation ) annotation ); 137 if ( positionToAnnotationMap.containsKey( position ) ) 138 positionToAnnotationMap.remove( position ); 139 else 140 annotationsToDeleteList.add( annotation ); 141 } 142 } 143 144 for ( Iterator iter = positionToAnnotationMap.keySet().iterator(); iter.hasNext(); ) 145 { 146 Position position = ( Position ) iter.next(); 147 ProjectionAnnotation annotation = ( ProjectionAnnotation ) positionToAnnotationMap.get( position ); 148 annotationsToAddMap.put( annotation, position ); 149 } 150 } 151 152 153 162 private Map createFoldingRegions( LdifFile model, IDocument document ) throws BadLocationException 163 { 164 Map positionToAnnotationMap = new HashMap (); 165 LdifContainer[] containers = model.getContainers(); 166 167 boolean ENABLE_FOLDING = LdifEditorActivator.getDefault().getPreferenceStore().getBoolean( 168 LdifEditorConstants.PREFERENCE_LDIFEDITOR_FOLDING_ENABLE ); 169 boolean FOLD_COMMENTS = LdifEditorActivator.getDefault().getPreferenceStore().getBoolean( 170 LdifEditorConstants.PREFERENCE_LDIFEDITOR_FOLDING_INITIALLYFOLDCOMMENTS ); 171 boolean FOLD_RECORDS = LdifEditorActivator.getDefault().getPreferenceStore().getBoolean( 172 LdifEditorConstants.PREFERENCE_LDIFEDITOR_FOLDING_INITIALLYFOLDRECORDS ); 173 boolean FOLD_WRAPPEDLINES = LdifEditorActivator.getDefault().getPreferenceStore().getBoolean( 174 LdifEditorConstants.PREFERENCE_LDIFEDITOR_FOLDING_INITIALLYFOLDWRAPPEDLINES ); 175 176 if ( ENABLE_FOLDING ) 177 { 178 for ( int i = 0; i < containers.length; i++ ) 179 { 180 LdifContainer container = containers[i]; 181 int containerStartLine = document.getLineOfOffset( container.getOffset() ); 182 int containerEndLine = -1; 183 LdifPart[] parts = container.getParts(); 184 for ( int j = parts.length - 1; j >= 0; j-- ) 185 { 186 if ( containerEndLine == -1 187 && ( !( parts[j] instanceof LdifSepLine ) || ( container instanceof LdifCommentContainer && j < parts.length - 1 ) ) ) 188 { 189 containerEndLine = document.getLineOfOffset( parts[j].getOffset() + parts[j].getLength() - 1 ); 190 } 192 if ( parts[j] instanceof LdifNonEmptyLineBase ) 193 { 194 LdifNonEmptyLineBase line = ( LdifNonEmptyLineBase ) parts[j]; 195 if ( line.isFolded() ) 196 { 197 Position position = new Position( line.getOffset(), line.getLength() ); 198 ProjectionAnnotation annotation = new ProjectionAnnotation( FOLD_WRAPPEDLINES ); 201 positionToAnnotationMap.put( position, annotation ); 202 } 203 } 204 } 205 206 if ( containerStartLine < containerEndLine ) 207 { 208 int start = document.getLineOffset( containerStartLine ); 209 int end = document.getLineOffset( containerEndLine ) + document.getLineLength( containerEndLine ); 210 Position position = new Position( start, end - start ); 211 ProjectionAnnotation annotation = new ProjectionAnnotation( FOLD_RECORDS 215 || ( FOLD_COMMENTS && container instanceof LdifCommentContainer ) ); 216 positionToAnnotationMap.put( position, annotation ); 217 } 218 } 219 } 220 221 return positionToAnnotationMap; 222 } 223 224 } 225 | Popular Tags |