testdev/ite/src/com.nokia.testfw.stf.configeditor/src/com/nokia/testfw/stf/configeditor/editors/ConfigSourceScaner.java
changeset 1 96906a986c3b
equal deleted inserted replaced
0:f1112f777ce9 1:96906a986c3b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.testfw.stf.configeditor.editors;
       
    20 
       
    21 import org.eclipse.jface.text.TextAttribute;
       
    22 import org.eclipse.jface.text.rules.EndOfLineRule;
       
    23 import org.eclipse.jface.text.rules.IRule;
       
    24 import org.eclipse.jface.text.rules.IWhitespaceDetector;
       
    25 import org.eclipse.jface.text.rules.IWordDetector;
       
    26 import org.eclipse.jface.text.rules.RuleBasedScanner;
       
    27 import org.eclipse.jface.text.rules.SingleLineRule;
       
    28 import org.eclipse.jface.text.rules.Token;
       
    29 import org.eclipse.jface.text.rules.WhitespaceRule;
       
    30 import org.eclipse.jface.text.rules.WordRule;
       
    31 import org.eclipse.swt.SWT;
       
    32 import org.eclipse.swt.graphics.Color;
       
    33 import org.eclipse.swt.widgets.Display;
       
    34 
       
    35 import com.nokia.testfw.stf.configmanager.ConfigUtil;
       
    36 import com.nokia.testfw.stf.configmanager.SectionElementType;
       
    37 
       
    38 /**
       
    39  * Config source scaner. Defines syntax highlighting rules used by {@link ConfigSourceEditor}.
       
    40  *
       
    41  */
       
    42 public class ConfigSourceScaner extends RuleBasedScanner {
       
    43 	private Color KEYWORD_COLOR = null;
       
    44 	private Color COMMENT_COLOR = null;
       
    45 	private Color SECTION_COLOR = null;
       
    46 	
       
    47     /**
       
    48      * Creates config source scaner
       
    49      */
       
    50     public ConfigSourceScaner() {
       
    51         WordRule rule = new WordRule(new IWordDetector() {
       
    52         	public boolean isWordStart(char c) { 
       
    53         		return Character.isJavaIdentifierStart(c); 
       
    54         	}
       
    55         	public boolean isWordPart(char c) {  
       
    56         		if ( c == '=' ) {
       
    57         			return true;
       
    58         		}
       
    59         		return Character.isJavaIdentifierPart(c); 
       
    60         	}
       
    61         });
       
    62         
       
    63         SECTION_COLOR = Display.getCurrent().getSystemColor( SWT.COLOR_BLUE );
       
    64         KEYWORD_COLOR = Display.getCurrent().getSystemColor( SWT.COLOR_BLACK );
       
    65         COMMENT_COLOR = Display.getCurrent().getSystemColor( SWT.COLOR_DARK_GRAY );        
       
    66         
       
    67         Token section = new Token(new TextAttribute( SECTION_COLOR, null, SWT.BOLD));
       
    68         Token keyword = new Token(new TextAttribute( KEYWORD_COLOR, null, SWT.BOLD));
       
    69         Token comment = new Token(new TextAttribute( COMMENT_COLOR ));
       
    70 
       
    71         //add tokens for each reserved word
       
    72         SectionElementType[] engineDefaultsElements = ConfigUtil.getAllowedEngineDefaultsSectionElements();
       
    73         for ( int i = 0; i < engineDefaultsElements.length; i++) {
       
    74         	rule.addWord( ConfigUtil.getEngineDefaultsSectionElementTag(engineDefaultsElements[ i ]), 
       
    75             		keyword );
       
    76         }
       
    77 
       
    78         SectionElementType[] loggerDefaultsElements = ConfigUtil.getAllowedLoggerDefaultsSectionElements();
       
    79         for ( int i = 0; i < loggerDefaultsElements.length; i++) {
       
    80         	rule.addWord( ConfigUtil.getLoggerDefaultsSectionElementTag(loggerDefaultsElements[ i ]), 
       
    81         			keyword );
       
    82         }
       
    83 
       
    84         SectionElementType[] moduleElements = ConfigUtil.getAllowedModuleSectionElements();
       
    85         for ( int i = 0; i < moduleElements.length; i++) {
       
    86         	rule.addWord( ConfigUtil.getModuleSectionElementTag(moduleElements[ i ]), keyword );
       
    87         }
       
    88         
       
    89         setRules(new IRule[] {
       
    90            rule,
       
    91            new SingleLineRule("[", "]", section ),
       
    92            new EndOfLineRule("#", comment ),           
       
    93            new EndOfLineRule("\\\\", comment ),           
       
    94            new WhitespaceRule(new IWhitespaceDetector() {
       
    95               public boolean isWhitespace(char c) {
       
    96                  return Character.isWhitespace(c);
       
    97               }
       
    98            }),
       
    99         });
       
   100      }
       
   101 }