KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > editor > reconciler > LdifFoldingRegionUpdater


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.ldifeditor.editor.reconciler;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
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             // create folding regions of current LDIF model; mark comments
100
// and
101
// folded lines as collapsed
102
Map JavaDoc positionToAnnotationMap = createFoldingRegions( editor.getLdifModel(), document );
103
104             // compare with current annotation model (--> toAdd, toDelete)
105
List JavaDoc annotationsToDeleteList = new ArrayList JavaDoc();
106             Map JavaDoc annotationsToAddMap = new HashMap JavaDoc();
107             this.computeDifferences( projectionAnnotationModel, positionToAnnotationMap, annotationsToDeleteList,
108                 annotationsToAddMap );
109             Annotation[] annotationsToDelete = ( Annotation[] ) annotationsToDeleteList
110                 .toArray( new Annotation[annotationsToDeleteList.size()] );
111
112             // update annotation model
113
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 JavaDoc positionToAnnotationMap,
128         List JavaDoc annotationsToDeleteList, Map JavaDoc annotationsToAddMap )
129     {
130
131         for ( Iterator JavaDoc iter = model.getAnnotationIterator(); iter.hasNext(); )
132         {
133             Object JavaDoc 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 JavaDoc 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     /**
154      * Creates all folding region of the given LDIF model.
155      * LdifCommentContainers and wrapped lines are marked as collapsed.
156      *
157      * @param model
158      * @param document
159      * @return a map with positions as keys to annotations as values
160      * @throws BadLocationException
161      */

162     private Map JavaDoc createFoldingRegions( LdifFile model, IDocument document ) throws BadLocationException
163     {
164         Map JavaDoc positionToAnnotationMap = new HashMap JavaDoc();
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                         // break;
191
}
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
199
// ProjectionAnnotation(true);
200
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
212
// ProjectionAnnotation(container instanceof
213
// LdifCommentContainer);
214
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