KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > filtereditor > FilterReconcilingStrategy


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.browser.common.filtereditor;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
28 import org.apache.directory.ldapstudio.browser.core.model.filter.LdapFilter;
29 import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterParser;
30 import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterToken;
31
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.IRegion;
34 import org.eclipse.jface.text.PaintManager;
35 import org.eclipse.jface.text.Position;
36 import org.eclipse.jface.text.reconciler.DirtyRegion;
37 import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
38 import org.eclipse.jface.text.source.Annotation;
39 import org.eclipse.jface.text.source.AnnotationModel;
40 import org.eclipse.jface.text.source.AnnotationPainter;
41 import org.eclipse.jface.text.source.IAnnotationModel;
42 import org.eclipse.jface.text.source.IAnnotationModelExtension;
43 import org.eclipse.jface.text.source.MatchingCharacterPainter;
44 import org.eclipse.jface.text.source.SourceViewer;
45 import org.eclipse.swt.graphics.RGB;
46
47
48 public class FilterReconcilingStrategy implements IReconcilingStrategy
49 {
50
51     private SourceViewer sourceViewer;
52
53     private LdapFilterParser parser;
54
55     private IDocument document;
56
57     private PaintManager paintManager;
58
59
60     public FilterReconcilingStrategy( SourceViewer sourceViewer, LdapFilterParser parser )
61     {
62         super();
63         this.sourceViewer = sourceViewer;
64         this.parser = parser;
65         this.document = null;
66         this.paintManager = null;
67     }
68
69
70     public void setDocument( IDocument document )
71     {
72         this.document = document;
73
74         if ( this.sourceViewer.getAnnotationModel() == null )
75         {
76             IAnnotationModel model = new AnnotationModel();
77             this.sourceViewer.setDocument( this.sourceViewer.getDocument(), model );
78         }
79
80         // add annotation painter
81
if ( this.paintManager == null && this.sourceViewer.getAnnotationModel() instanceof IAnnotationModelExtension )
82         {
83             AnnotationPainter ap = new AnnotationPainter( this.sourceViewer, null );
84             ap.addAnnotationType( "DEFAULT" );
85             ap.setAnnotationTypeColor( "DEFAULT", BrowserCommonActivator.getDefault().getColor( new RGB( 255, 0, 0 ) ) );
86             this.sourceViewer.getAnnotationModel().addAnnotationModelListener( ap );
87
88             FilterCharacterPairMatcher cpm = new FilterCharacterPairMatcher( this.sourceViewer, this.parser );
89             MatchingCharacterPainter mcp = new MatchingCharacterPainter( this.sourceViewer, cpm );
90             mcp.setColor( BrowserCommonActivator.getDefault().getColor( new RGB( 159, 159, 159 ) ) );
91
92             this.paintManager = new PaintManager( this.sourceViewer );
93             this.paintManager.addPainter( ap );
94             this.paintManager.addPainter( mcp );
95         }
96
97     }
98
99
100     public void reconcile( DirtyRegion dirtyRegion, IRegion subRegion )
101     {
102         this.reconcile( dirtyRegion );
103     }
104
105
106     public void reconcile( IRegion partition )
107     {
108
109         /*
110          * Display.getDefault().syncExec(new Runnable(){ public void run() {
111          * if(sourceViewer.canDoOperation(SourceViewer.FORMAT)) {
112          * sourceViewer.doOperation(SourceViewer.FORMAT); } } });
113          */

114
115         LdapFilterToken[] tokens = this.parser.getModel().getTokens();
116
117         // annotations
118
if ( this.sourceViewer.getAnnotationModel() instanceof IAnnotationModelExtension )
119         {
120             ( ( IAnnotationModelExtension ) this.sourceViewer.getAnnotationModel() ).removeAllAnnotations();
121
122             List JavaDoc positionList = new ArrayList JavaDoc();
123
124             LdapFilter[] invalidFilters = this.parser.getModel().getInvalidFilters();
125             for ( int i = 0; i < invalidFilters.length; i++ )
126             {
127                 if ( invalidFilters[i].getStartToken() != null )
128                 {
129                     int start = invalidFilters[i].getStartToken().getOffset();
130                     int stop = invalidFilters[i].getStopToken() != null ? invalidFilters[i].getStopToken().getOffset()
131                         + invalidFilters[i].getStopToken().getLength() : start
132                         + invalidFilters[i].getStartToken().getLength();
133
134                     Annotation annotation = new Annotation( "DEFAULT", true, invalidFilters[i].toString() );
135                     Position position = new Position( start, stop - start );
136                     positionList.add( position );
137                     this.sourceViewer.getAnnotationModel().addAnnotation( annotation, position );
138                 }
139             }
140
141             for ( int i = 0; i < tokens.length; i++ )
142             {
143                 if ( tokens[i].getType() == LdapFilterToken.ERROR )
144                 {
145
146                     boolean overlaps = false;
147                     for ( int k = 0; k < positionList.size(); k++ )
148                     {
149                         Position pos = ( Position ) positionList.get( k );
150                         if ( pos.overlapsWith( tokens[i].getOffset(), tokens[i].getLength() ) )
151                         {
152                             overlaps = true;
153                             break;
154                         }
155                     }
156                     if ( !overlaps )
157                     {
158                         Annotation annotation = new Annotation( "DEFAULT", true, tokens[i].getValue() );
159                         Position position = new Position( tokens[i].getOffset(), tokens[i].getLength() );
160                         this.sourceViewer.getAnnotationModel().addAnnotation( annotation, position );
161                     }
162                 }
163             }
164         }
165
166     }
167
168 }
169
Popular Tags