testdev/ite/src/com.nokia.testfw.stf.scripteditor/src/com/nokia/testfw/stf/scripteditor/editors/ScriptScanner.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.scripteditor.editors;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 
       
    23 import org.eclipse.jface.text.TextAttribute;
       
    24 import org.eclipse.jface.text.rules.EndOfLineRule;
       
    25 import org.eclipse.jface.text.rules.MultiLineRule;
       
    26 import org.eclipse.jface.text.rules.IWhitespaceDetector;
       
    27 import org.eclipse.jface.text.rules.RuleBasedScanner;
       
    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 import org.eclipse.jface.text.rules.IRule;
       
    35 
       
    36 
       
    37 import com.nokia.testfw.stf.scripteditor.editors.scripter.ScripterWordsProvider;
       
    38 import com.nokia.testfw.stf.scripteditor.utils.KeywordDetector;
       
    39 
       
    40 /**
       
    41  * Config source scaner. Defines syntax highlighting rules.
       
    42  *
       
    43  */
       
    44 public class ScriptScanner extends RuleBasedScanner {
       
    45 
       
    46     /**
       
    47      * Creates config source scaner
       
    48      */
       
    49 	public ScriptScanner(/*EditorMode mode*/){
       
    50 		
       
    51 		sectionWordRule = new WordRule(new SectionDetector());
       
    52 		keywordRule = new WordRule(new KeywordDetector(), Token.WHITESPACE);
       
    53 		
       
    54 		wsRule = new WhitespaceRule(new IWhitespaceDetector() {
       
    55             public boolean isWhitespace(char c) {
       
    56                return Character.isWhitespace(c);
       
    57             }
       
    58          });
       
    59 		
       
    60 		Color COMMENT_COLOR = Display.getCurrent().getSystemColor( SWT.COLOR_DARK_GRAY );
       
    61 		Token comment = new Token(new TextAttribute( COMMENT_COLOR ));
       
    62 		hashCommentRule = new EndOfLineRule("#", comment);
       
    63 		slashCommentRule = new EndOfLineRule("//", comment);
       
    64 		blkCommentRule = new MultiLineRule("/*", "*/", comment);
       
    65 
       
    66 		String[] sectionWords = null;
       
    67 		String[] keywords = null;
       
    68 
       
    69 		sectionWords = ScripterWordsProvider.provideSectionWords();
       
    70 		keywords = ScripterWordsProvider.provideKeywords();
       
    71 		for (int i = 0; i < sectionWords.length; i++) {
       
    72 			sectionWordRule.addWord(sectionWords[i], sectionWordToken);
       
    73 		}
       
    74 		for(int i = 0; i < keywords.length; i++){
       
    75 			keywordRule.addWord(keywords[i], keywordToken);
       
    76 		}
       
    77  		setRules(new IRule[] {sectionWordRule, keywordRule, hashCommentRule, slashCommentRule, wsRule, blkCommentRule});
       
    78 	}
       
    79 	
       
    80 	 /**
       
    81      * Changes set of keywords between TestScripter and TestCombiner
       
    82      */
       
    83 	
       
    84 	public void changeSetOfKeywords(ArrayList<String> subSectionContent/*EditorMode mode*/){
       
    85 		sectionWordRule = new WordRule(new SectionDetector());
       
    86 		keywordRule = new WordRule(new KeywordDetector(), Token.WHITESPACE);
       
    87 		
       
    88 		String[] sectionWords = null;
       
    89 		String[] keywords = null;
       
    90 
       
    91 		sectionWords = ScripterWordsProvider.provideSectionWords();
       
    92 		keywords = ScripterWordsProvider.provideKeywords();
       
    93 		
       
    94 		for(int i = 0; i < sectionWords.length; i++){
       
    95 			sectionWordRule.addWord(sectionWords[i], sectionWordToken);
       
    96 		}
       
    97 
       
    98 		if(subSectionContent!=null && subSectionContent.size()>0){
       
    99 			if(subSectionContent.get(0).equals("[Sub")){
       
   100 				sectionWordRule.addWord(subSectionContent.get(0), sectionWordToken);
       
   101 			}
       
   102 			if(subSectionContent.size()>1){
       
   103 				for(int i=1;i<subSectionContent.size();i++){
       
   104 					if(subSectionContent.get(i).matches("[a-z]+\\]")){
       
   105 						sectionWordRule.addWord(subSectionContent.get(i), sectionWordToken);
       
   106 					}
       
   107 				}
       
   108 			}
       
   109 		}
       
   110 		
       
   111 		for(int i = 0; i < keywords.length; i++){
       
   112 			keywordRule.addWord(keywords[i], keywordToken);
       
   113 		}
       
   114 		
       
   115 		setRules(new IRule[] {sectionWordRule, keywordRule, hashCommentRule, slashCommentRule, wsRule, blkCommentRule});
       
   116 	}
       
   117 	
       
   118 	private Color KEYWORD_COLOR = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
       
   119 	private Color SECTION_WORD_COLOR = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN);
       
   120 	
       
   121 	private WordRule sectionWordRule;
       
   122 	private WordRule keywordRule;
       
   123 	private WhitespaceRule wsRule;
       
   124 	private EndOfLineRule hashCommentRule;
       
   125 	private EndOfLineRule slashCommentRule;
       
   126 	private MultiLineRule blkCommentRule;
       
   127 	
       
   128 	private Token sectionWordToken = new Token(new TextAttribute(SECTION_WORD_COLOR, null, SWT.BOLD));
       
   129 	private Token keywordToken = new Token(new TextAttribute(KEYWORD_COLOR, null, SWT.BOLD));
       
   130 }