KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile;
28 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart;
29 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer;
30 import org.apache.directory.ldapstudio.ldifeditor.editor.ILdifEditor;
31
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.Position;
34 import org.eclipse.jface.text.source.Annotation;
35 import org.eclipse.jface.text.source.IAnnotationModel;
36 import org.eclipse.jface.text.source.IAnnotationModelExtension;
37 import org.eclipse.jface.text.source.ISourceViewer;
38
39
40 class LdifAnnotationUpdater
41 {
42
43     private static final String JavaDoc ERROR_ANNOTATION_TYPE = "org.eclipse.ui.workbench.texteditor.error";
44
45     private ILdifEditor editor;
46
47
48     public LdifAnnotationUpdater( ILdifEditor editor )
49     {
50         this.editor = editor;
51     }
52
53
54     public void dispose()
55     {
56     }
57
58
59     public void updateAnnotations( LdifContainer[] containers )
60     {
61
62     }
63
64
65     public void updateAnnotations()
66     {
67
68         LdifFile model = editor.getLdifModel();
69         ISourceViewer viewer = ( ISourceViewer ) editor.getAdapter( ISourceViewer.class );
70         if ( viewer == null )
71             return;
72
73         IDocument document = viewer.getDocument();
74         IAnnotationModel annotationModel = viewer.getAnnotationModel();
75         if ( document == null || annotationModel == null || model == null )
76             return;
77
78         if ( annotationModel instanceof IAnnotationModelExtension )
79         {
80             ( ( IAnnotationModelExtension ) annotationModel ).removeAllAnnotations();
81
82             List JavaDoc positionList = new ArrayList JavaDoc();
83
84             LdifContainer[] containers = model.getContainers();
85             for ( int i = 0; i < containers.length; i++ )
86             {
87                 LdifContainer container = containers[i];
88
89                 // LdifPart errorPart = null;
90
int errorOffset = -1;
91                 int errorLength = -1;
92                 StringBuffer JavaDoc errorText = null;
93
94                 LdifPart[] parts = container.getParts();
95                 for ( int k = 0; k < parts.length; k++ )
96                 {
97                     LdifPart part = parts[k];
98                     if ( !part.isValid() )
99                     {
100                         if ( errorOffset == -1 )
101                         {
102                             // errorPart = part;
103
errorOffset = part.getOffset();
104                             errorLength = part.getLength();
105                             errorText = new StringBuffer JavaDoc();
106                             errorText.append( part.toRawString() );
107                         }
108                         else
109                         {
110                             errorLength += part.getLength();
111                             errorText.append( part.toRawString() );
112                         }
113                     }
114                 }
115
116                 if ( errorOffset == -1 && !container.isValid() )
117                 {
118                     errorOffset = container.getOffset();
119                     errorLength = container.getLength();
120                     errorText = new StringBuffer JavaDoc();
121                     errorText.append( container.toRawString() );
122                 }
123
124                 if ( errorOffset > -1 )
125                 {
126                     // Annotation annotation = new Annotation("DEFAULT",
127
// true,
128
// invalidFilters[i].toString());
129
// if(errorPart instanceof LdifUnknownPart) {
130
// errorOffset = container.getOffset();
131
// errorLength = container.getLength();
132
// errorText = new StringBuffer(container.toString());
133
// }
134
Annotation annotation = new Annotation( ERROR_ANNOTATION_TYPE, true, errorText.toString() );
135                     Position position = new Position( errorOffset, errorLength );
136                     positionList.add( position );
137                     viewer.getAnnotationModel().addAnnotation( annotation, position );
138                 }
139
140             }
141         }
142     }
143
144 }
Popular Tags