1 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 this.parser.parse( this.document.get() ); 96 97 LdapFilterToken[] tokens = this.parser.getModel().getTokens(); 99 100 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 |