KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterParser;
26 import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterToken;
27
28 import org.eclipse.jface.text.DocumentEvent;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.IRegion;
31 import org.eclipse.jface.text.ITypedRegion;
32 import org.eclipse.jface.text.TextAttribute;
33 import org.eclipse.jface.text.TextPresentation;
34 import org.eclipse.jface.text.presentation.IPresentationDamager;
35 import org.eclipse.jface.text.presentation.IPresentationRepairer;
36 import org.eclipse.jface.text.source.SourceViewer;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.custom.StyleRange;
39 import org.eclipse.swt.graphics.RGB;
40
41
42 public class FilterDamagerRepairer implements IPresentationDamager, IPresentationRepairer
43 {
44
45     private static final TextAttribute DEFAULT_TEXT_ATTRIBUTE = new TextAttribute( BrowserCommonActivator.getDefault()
46         .getColor( new RGB( 0, 0, 0 ) ) );
47
48     private static final TextAttribute AND_OR_NOT_TEXT_ATTRIBUTE = new TextAttribute( BrowserCommonActivator.getDefault()
49         .getColor( new RGB( 0, 127, 0 ) ), null, SWT.BOLD );
50
51     private static final TextAttribute ATTRIBUTE_TEXT_ATTRIBUTE = new TextAttribute( BrowserCommonActivator.getDefault()
52         .getColor( new RGB( 127, 0, 85 ) ) );
53
54     private static final TextAttribute FILTER_TYPE_TEXT_ATTRIBUTE = new TextAttribute( BrowserCommonActivator.getDefault()
55         .getColor( new RGB( 255, 0, 0 ) ), null, SWT.BOLD );
56
57     private static final TextAttribute VALUE_TEXT_ATTRIBUTE = new TextAttribute( BrowserCommonActivator.getDefault().getColor(
58         new RGB( 0, 0, 127 ) ) );
59
60     private static final TextAttribute PARENTHESIS_TEXT_ATTRIBUTE = new TextAttribute( BrowserCommonActivator.getDefault()
61         .getColor( new RGB( 0, 0, 0 ) ), null, SWT.BOLD );
62
63     private SourceViewer sourceViewer;
64
65     private LdapFilterParser parser;
66
67     private IDocument document;
68
69
70     public FilterDamagerRepairer( SourceViewer sourceViewer, LdapFilterParser parser )
71     {
72         super();
73         this.sourceViewer = sourceViewer;
74         this.parser = parser;
75         this.document = null;
76     }
77
78
79     public void setDocument( IDocument document )
80     {
81         this.document = document;
82     }
83
84
85     public IRegion getDamageRegion( ITypedRegion partition, DocumentEvent event, boolean documentPartitioningChanged )
86     {
87         return partition;
88     }
89
90
91     public void createPresentation( TextPresentation presentation, ITypedRegion damage )
92     {
93
94         // parse the filter
95
this.parser.parse( this.document.get() );
96
97         // get tokens
98
LdapFilterToken[] tokens = this.parser.getModel().getTokens();
99
100         // syntax highlighting
101
for ( int i = 0; i < tokens.length; i++ )
102         {
103             switch ( tokens[i].getType() )
104             {
105                 case LdapFilterToken.LPAR:
106                 case LdapFilterToken.RPAR:
107                     this.addStyleRange( presentation, tokens[i], PARENTHESIS_TEXT_ATTRIBUTE );
108                     break;
109                 case LdapFilterToken.AND:
110                 case LdapFilterToken.OR:
111                 case LdapFilterToken.NOT:
112                     this.addStyleRange( presentation, tokens[i], AND_OR_NOT_TEXT_ATTRIBUTE );
113                     break;
114                 case LdapFilterToken.EQUAL:
115                 case LdapFilterToken.GREATER:
116                 case LdapFilterToken.LESS:
117                 case LdapFilterToken.APROX:
118                 case LdapFilterToken.PRESENT:
119                     this.addStyleRange( presentation, tokens[i], FILTER_TYPE_TEXT_ATTRIBUTE );
120                     break;
121                 case LdapFilterToken.ATTRIBUTE:
122                     this.addStyleRange( presentation, tokens[i], ATTRIBUTE_TEXT_ATTRIBUTE );
123                     break;
124                 case LdapFilterToken.VALUE:
125                     this.addStyleRange( presentation, tokens[i], VALUE_TEXT_ATTRIBUTE );
126                     break;
127                 default:
128                     this.addStyleRange( presentation, tokens[i], DEFAULT_TEXT_ATTRIBUTE );
129             }
130         }
131
132     }
133
134
135     private void addStyleRange( TextPresentation presentation, LdapFilterToken token, TextAttribute textAttribute )
136     {
137         StyleRange range = new StyleRange( token.getOffset(), token.getLength(), textAttribute.getForeground(),
138             textAttribute.getBackground(), textAttribute.getStyle() );
139         presentation.addStyleRange( range );
140     }
141
142 }
143
Popular Tags