KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > editor > text > LdifDamagerRepairer


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.text;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifEOFPart;
30 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile;
31 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifInvalidPart;
32 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart;
33 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeAddRecord;
34 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeDeleteRecord;
35 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeModDnRecord;
36 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeModifyRecord;
37 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer;
38 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifModSpec;
39 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine;
40 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifChangeTypeLine;
41 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifCommentLine;
42 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifControlLine;
43 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifDeloldrdnLine;
44 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifDnLine;
45 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifLineBase;
46 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifModSpecSepLine;
47 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifModSpecTypeLine;
48 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifNewrdnLine;
49 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifNewsuperiorLine;
50 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifVersionLine;
51 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorConstants;
52 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorActivator;
53 import org.apache.directory.ldapstudio.ldifeditor.editor.ILdifEditor;
54
55 import org.eclipse.jface.preference.IPreferenceStore;
56 import org.eclipse.jface.preference.PreferenceConverter;
57 import org.eclipse.jface.text.DocumentEvent;
58 import org.eclipse.jface.text.IDocument;
59 import org.eclipse.jface.text.IRegion;
60 import org.eclipse.jface.text.ITypedRegion;
61 import org.eclipse.jface.text.Region;
62 import org.eclipse.jface.text.TextAttribute;
63 import org.eclipse.jface.text.TextPresentation;
64 import org.eclipse.jface.text.TextUtilities;
65 import org.eclipse.jface.text.presentation.IPresentationDamager;
66 import org.eclipse.jface.text.presentation.IPresentationRepairer;
67 import org.eclipse.swt.SWT;
68 import org.eclipse.swt.custom.StyleRange;
69 import org.eclipse.swt.graphics.Color;
70 import org.eclipse.swt.graphics.RGB;
71
72
73 public class LdifDamagerRepairer implements IPresentationDamager, IPresentationRepairer
74 {
75
76     private ILdifEditor editor;
77
78
79     // private IDocument document;
80

81     public LdifDamagerRepairer( ILdifEditor editor )
82     {
83         super();
84         this.editor = editor;
85         // this.document = null;
86
}
87
88
89     public void setDocument( IDocument document )
90     {
91         // this.document = document;
92
}
93
94
95     public IRegion getDamageRegion( ITypedRegion partition, DocumentEvent event, boolean documentPartitioningChanged )
96     {
97         return partition;
98     }
99
100
101     public void createPresentation( TextPresentation presentation, ITypedRegion damage )
102     {
103
104         LdifFile ldifModel = this.editor.getLdifModel();
105         LdifContainer[] allContainers = ldifModel.getContainers();
106         List JavaDoc containerList = new ArrayList JavaDoc();
107         for ( int i = 0; i < allContainers.length; i++ )
108         {
109             LdifContainer container = allContainers[i];
110             Region containerRegion = new Region( container.getOffset(), container.getLength() );
111             if ( TextUtilities.overlaps( containerRegion, damage ) )
112             {
113                 containerList.add( container );
114             }
115         }
116         LdifContainer[] containers = ( LdifContainer[] ) containerList
117             .toArray( new LdifContainer[containerList.size()] );
118         this.highlight( containers, presentation, damage );
119
120         // LdifFile ldifModel = this.editor.getLdifModel();
121
// System.out.println(ldifModel.toRawString());
122
// LdifContainer[] allContainers = ldifModel.getContainers();
123
// this.highlight(allContainers, presentation, null);
124

125     }
126
127     private Map JavaDoc textAttributeKeyToValueMap;
128
129
130     private TextAttribute geTextAttribute( String JavaDoc key )
131     {
132         IPreferenceStore store = LdifEditorActivator.getDefault().getPreferenceStore();
133
134         RGB rgb = PreferenceConverter
135             .getColor( store, key + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_RGB_SUFFIX );
136         int style = store.getInt( key + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_STYLE_SUFFIX );
137
138         if ( textAttributeKeyToValueMap != null )
139         {
140             if ( textAttributeKeyToValueMap.containsKey( key
141                 + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_RGB_SUFFIX ) )
142             {
143                 rgb = ( RGB ) textAttributeKeyToValueMap.get( key
144                     + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_RGB_SUFFIX );
145             }
146             if ( textAttributeKeyToValueMap.containsKey( key
147                 + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_STYLE_SUFFIX ) )
148             {
149                 style = ( ( Integer JavaDoc ) textAttributeKeyToValueMap.get( key
150                     + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_STYLE_SUFFIX ) ).intValue();
151             }
152         }
153
154         Color color = LdifEditorActivator.getDefault().getColor( rgb );
155         TextAttribute textAttribute = new TextAttribute( color, null, style );
156         return textAttribute;
157     }
158
159
160     /**
161      * Overwrites the style set in preference store
162      *
163      * @param key
164      * the key
165      * LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_xxx +
166      * LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_RGB_SUFFIX
167      * ore
168      * LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_STYLE_SUFFIX
169      * @param newValue
170      * RGB object or Integer object
171      */

172     public void setTextAttribute( String JavaDoc key, RGB rgb, int style )
173     {
174         if ( textAttributeKeyToValueMap == null )
175         {
176             textAttributeKeyToValueMap = new HashMap JavaDoc();
177         }
178         textAttributeKeyToValueMap.put( key + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_RGB_SUFFIX, rgb );
179         textAttributeKeyToValueMap.put( key + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_STYLE_SUFFIX,
180             new Integer JavaDoc( style ) );
181     }
182
183
184     private void highlight( LdifContainer[] containers, TextPresentation presentation, ITypedRegion damage )
185     {
186
187         // TextAttribute DEFAULT_TEXT_ATTRIBUTE = new
188
// TextAttribute(Activator.getDefault().getColor(new RGB(0, 0,
189
// 0)));
190

191         TextAttribute COMMENT_TEXT_ATTRIBUTE = geTextAttribute( LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_COMMENT );
192         TextAttribute KEYWORD_TEXT_ATTRIBUTE = geTextAttribute( LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_KEYWORD );
193         TextAttribute DN_TEXT_ATTRIBUTE = geTextAttribute( LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_DN );
194         TextAttribute ATTRIBUTE_TEXT_ATTRIBUTE = geTextAttribute( LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_ATTRIBUTE );
195         TextAttribute VALUETYPE_TEXT_ATTRIBUTE = geTextAttribute( LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_VALUETYPE );
196         TextAttribute VALUE_TEXT_ATTRIBUTE = geTextAttribute( LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_VALUE );
197         TextAttribute ADD_TEXT_ATTRIBUTE = geTextAttribute( LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_CHANGETYPEADD );
198         TextAttribute MODIFY_TEXT_ATTRIBUTE = geTextAttribute( LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_CHANGETYPEMODIFY );
199         TextAttribute DELETE_TEXT_ATTRIBUTE = geTextAttribute( LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_CHANGETYPEDELETE );
200         TextAttribute MODDN_TEXT_ATTRIBUTE = geTextAttribute( LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_CHANGETYPEMODDN );
201
202         for ( int z = 0; z < containers.length; z++ )
203         {
204
205             LdifContainer container = containers[z];
206
207             LdifPart[] parts = container.getParts();
208
209             for ( int i = 0; i < parts.length; i++ )
210             {
211
212                 // int offset = damage.getOffset() + parts[i].getOffset();
213
int offset = parts[i].getOffset();
214
215                 if ( parts[i] instanceof LdifLineBase )
216                 {
217                     LdifLineBase line = ( LdifLineBase ) parts[i];
218
219                     // String debug = line.getClass().getName() +
220
// "("+line.getOffset()+","+line.getLength()+"):
221
// "+line.toString();
222
// debug = debug.replaceAll("\n", "\\\\n");
223
// debug = debug.replaceAll("\r", "\\\\r");
224
// System.out.println(debug);
225

226                     if ( line instanceof LdifVersionLine )
227                     {
228                         this.addStyleRange( presentation, offset, line.getLength(), KEYWORD_TEXT_ATTRIBUTE );
229                     }
230                     else if ( line instanceof LdifCommentLine )
231                     {
232                         this.addStyleRange( presentation, offset, line.getLength(), COMMENT_TEXT_ATTRIBUTE );
233                     }
234                     else if ( line instanceof LdifDnLine )
235                     {
236                         LdifDnLine dnLine = ( LdifDnLine ) line;
237                         int dnSpecLength = dnLine.getRawDnSpec().length();
238                         int valueTypeLength = dnLine.getRawValueType().length();
239                         int dnLength = dnLine.getRawDn().length();
240                         this.addStyleRange( presentation, offset, dnSpecLength, DN_TEXT_ATTRIBUTE );
241                         this.addStyleRange( presentation, offset + dnSpecLength, valueTypeLength,
242                             VALUETYPE_TEXT_ATTRIBUTE );
243                         this.addStyleRange( presentation, offset + dnSpecLength + valueTypeLength, dnLength,
244                             DN_TEXT_ATTRIBUTE );
245                     }
246                     else if ( line instanceof LdifAttrValLine )
247                     {
248                         LdifAttrValLine attrValLine = ( LdifAttrValLine ) line;
249                         int attributeNameLength = attrValLine.getRawAttributeDescription().length();
250                         int valueTypeLength = attrValLine.getRawValueType().length();
251                         int valueLength = attrValLine.getRawValue().length();
252                         this.addStyleRange( presentation, offset, attributeNameLength, ATTRIBUTE_TEXT_ATTRIBUTE );
253                         this.addStyleRange( presentation, offset + attributeNameLength, valueTypeLength,
254                             VALUETYPE_TEXT_ATTRIBUTE );
255                         this.addStyleRange( presentation, offset + attributeNameLength + valueTypeLength, valueLength,
256                             VALUE_TEXT_ATTRIBUTE );
257                     }
258                     else if ( line instanceof LdifChangeTypeLine )
259                     {
260                         LdifChangeTypeLine changeTypeLine = ( LdifChangeTypeLine ) line;
261                         int changeTypeSpecLength = changeTypeLine.getRawChangeTypeSpec().length();
262                         int valueTypeLength = changeTypeLine.getRawValueType().length();
263                         int changeTypeLength = changeTypeLine.getRawChangeType().length();
264                         this.addStyleRange( presentation, offset, changeTypeSpecLength, KEYWORD_TEXT_ATTRIBUTE );
265                         this.addStyleRange( presentation, offset + changeTypeSpecLength, valueTypeLength,
266                             VALUETYPE_TEXT_ATTRIBUTE );
267
268                         if ( container instanceof LdifChangeAddRecord )
269                         {
270                             this.addStyleRange( presentation, offset + changeTypeSpecLength + valueTypeLength,
271                                 changeTypeLength, ADD_TEXT_ATTRIBUTE );
272                         }
273                         else if ( container instanceof LdifChangeModifyRecord )
274                         {
275                             this.addStyleRange( presentation, offset + changeTypeSpecLength + valueTypeLength,
276                                 changeTypeLength, MODIFY_TEXT_ATTRIBUTE );
277                         }
278                         else if ( container instanceof LdifChangeModDnRecord )
279                         {
280                             this.addStyleRange( presentation, offset + changeTypeSpecLength + valueTypeLength,
281                                 changeTypeLength, MODDN_TEXT_ATTRIBUTE );
282                         }
283                         else if ( container instanceof LdifChangeDeleteRecord )
284                         {
285                             this.addStyleRange( presentation, offset + changeTypeSpecLength + valueTypeLength,
286                                 changeTypeLength, DELETE_TEXT_ATTRIBUTE );
287                         }
288                     }
289                     else if ( line instanceof LdifNewrdnLine )
290                     {
291                         LdifNewrdnLine newrdnLine = ( LdifNewrdnLine ) line;
292                         int newrdnSpecLength = newrdnLine.getRawNewrdnSpec().length();
293                         int valueTypeLength = newrdnLine.getRawValueType().length();
294                         int newrdnLength = newrdnLine.getRawNewrdn().length();
295                         this.addStyleRange( presentation, offset, newrdnSpecLength, KEYWORD_TEXT_ATTRIBUTE );
296                         this.addStyleRange( presentation, offset + newrdnSpecLength, valueTypeLength,
297                             VALUETYPE_TEXT_ATTRIBUTE );
298                         this.addStyleRange( presentation, offset + newrdnSpecLength + valueTypeLength, newrdnLength,
299                             VALUE_TEXT_ATTRIBUTE );
300                     }
301                     else if ( line instanceof LdifDeloldrdnLine )
302                     {
303                         LdifDeloldrdnLine deleteoldrdnLine = ( LdifDeloldrdnLine ) line;
304                         int deleteoldrdnSpecLength = deleteoldrdnLine.getRawDeleteOldrdnSpec().length();
305                         int valueTypeLength = deleteoldrdnLine.getRawValueType().length();
306                         int deleteoldrdnLength = deleteoldrdnLine.getRawDeleteOldrdn().length();
307                         this.addStyleRange( presentation, offset, deleteoldrdnSpecLength, KEYWORD_TEXT_ATTRIBUTE );
308                         this.addStyleRange( presentation, offset + deleteoldrdnSpecLength, valueTypeLength,
309                             VALUETYPE_TEXT_ATTRIBUTE );
310                         this.addStyleRange( presentation, offset + deleteoldrdnSpecLength + valueTypeLength,
311                             deleteoldrdnLength, VALUE_TEXT_ATTRIBUTE );
312                     }
313                     else if ( line instanceof LdifNewsuperiorLine )
314                     {
315                         LdifNewsuperiorLine newsuperiorLine = ( LdifNewsuperiorLine ) line;
316                         int newsuperiorSpecLength = newsuperiorLine.getRawNewSuperiorSpec().length();
317                         int valueTypeLength = newsuperiorLine.getRawValueType().length();
318                         int newsuperiorLength = newsuperiorLine.getRawNewSuperiorDn().length();
319                         this.addStyleRange( presentation, offset, newsuperiorSpecLength, KEYWORD_TEXT_ATTRIBUTE );
320                         this.addStyleRange( presentation, offset + newsuperiorSpecLength, valueTypeLength,
321                             VALUETYPE_TEXT_ATTRIBUTE );
322                         this.addStyleRange( presentation, offset + newsuperiorSpecLength + valueTypeLength,
323                             newsuperiorLength, VALUE_TEXT_ATTRIBUTE );
324                     }
325                     // else if(line instanceof LdifDeloldrdnLine) {
326
// this.addStyleRange(presentation, offset,
327
// line.getLength(), MODTYPE_TEXT_ATTRIBUTE);
328
// }
329
// else if(line instanceof LdifNewsuperiorLine) {
330
// this.addStyleRange(presentation, offset,
331
// line.getLength(), MODTYPE_TEXT_ATTRIBUTE);
332
// }
333
else if ( line instanceof LdifModSpecTypeLine )
334                     {
335                         LdifModSpecTypeLine modSpecTypeLine = ( LdifModSpecTypeLine ) line;
336                         int modTypeLength = modSpecTypeLine.getRawModType().length();
337                         int valueTypeLength = modSpecTypeLine.getRawValueType().length();
338                         int attributeDescriptionLength = modSpecTypeLine.getRawAttributeDescription().length();
339                         this.addStyleRange( presentation, offset, modTypeLength, KEYWORD_TEXT_ATTRIBUTE );
340                         this.addStyleRange( presentation, offset + modTypeLength, valueTypeLength,
341                             VALUETYPE_TEXT_ATTRIBUTE );
342                         this.addStyleRange( presentation, offset + modTypeLength + valueTypeLength,
343                             attributeDescriptionLength, ATTRIBUTE_TEXT_ATTRIBUTE );
344                     }
345                     else if ( line instanceof LdifModSpecSepLine )
346                     {
347                         this.addStyleRange( presentation, offset, line.getLength(), VALUETYPE_TEXT_ATTRIBUTE );
348                     }
349                     else if ( line instanceof LdifControlLine )
350                     {
351                         LdifControlLine controlLine = ( LdifControlLine ) line;
352                         int controlSpecLength = controlLine.getRawControlSpec().length();
353                         int controlTypeLength = controlLine.getRawControlType().length();
354                         int oidLength = controlLine.getRawOid().length();
355                         int critLength = controlLine.getRawCriticality().length();
356                         int valueTypeLength = controlLine.getRawControlValueType().length();
357                         int valueLength = controlLine.getRawControlValue().length();
358                         this.addStyleRange( presentation, offset, controlSpecLength, KEYWORD_TEXT_ATTRIBUTE );
359                         this.addStyleRange( presentation, offset + controlSpecLength, controlTypeLength,
360                             VALUETYPE_TEXT_ATTRIBUTE );
361                         this.addStyleRange( presentation, offset + controlSpecLength + controlTypeLength, oidLength,
362                             ATTRIBUTE_TEXT_ATTRIBUTE );
363                         this.addStyleRange( presentation, offset + controlSpecLength + controlTypeLength + oidLength,
364                             critLength, KEYWORD_TEXT_ATTRIBUTE );
365                         this.addStyleRange( presentation, offset + controlSpecLength + controlTypeLength + oidLength
366                             + critLength, valueTypeLength, VALUETYPE_TEXT_ATTRIBUTE );
367                         this.addStyleRange( presentation, offset + controlSpecLength + controlTypeLength + oidLength
368                             + critLength + valueTypeLength, valueLength, VALUE_TEXT_ATTRIBUTE );
369                     }
370                     else
371                     {
372                         // this.addStyleRange(presentation, offset,
373
// line.getLength(), DEFAULT_TEXT_ATTRIBUTE);
374
}
375                 }
376                 else if ( parts[i] instanceof LdifModSpec )
377                 {
378                     LdifModSpec modSpec = ( LdifModSpec ) parts[i];
379                     this.highlight( new LdifContainer[]
380                         { modSpec }, presentation, damage );
381
382                 }
383                 else if ( parts[i] instanceof LdifInvalidPart )
384                 {
385                     // LdifUnknownPart unknownPart =
386
// (LdifUnknownPart)parts[i];
387
// this.addStyleRange(presentation, offset,
388
// unknownPart.getLength(), UNKNOWN_TEXT_ATTRIBUTE);
389
// this.addStyleRange(presentation, offset,
390
// parts[i].getLength(), DEFAULT_TEXT_ATTRIBUTE);
391
}
392                 else if ( parts[i] instanceof LdifEOFPart )
393                 {
394                     // ignore
395
}
396                 else
397                 {
398                     // TODO
399
System.out.println( "LdifDamagerRepairer: Unspecified Token: " + parts[i].getClass() );
400                 }
401
402             }
403         }
404
405     }
406
407
408     private void addStyleRange( TextPresentation presentation, int offset, int length, TextAttribute textAttribute )
409     {
410         if ( offset >= 0 && length > 0 )
411         {
412             StyleRange range = new StyleRange( offset, length, textAttribute.getForeground(), textAttribute
413                 .getBackground(), textAttribute.getStyle() & ( SWT.BOLD | SWT.ITALIC ) );
414             range.underline = ( textAttribute.getStyle() & TextAttribute.UNDERLINE ) != 0;
415             range.strikeout = ( textAttribute.getStyle() & TextAttribute.STRIKETHROUGH ) != 0;
416             presentation.addStyleRange( range );
417         }
418     }
419
420 }
421
Popular Tags